Margin/Leverage Calculation
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © DinGrogu

//@version=5

library('margin')

// ############################################################
// # FUNCTIONS
// ############################################################

f_liquidation(float _investment_capital, float _price, int _leverage, float _maintenance_rate, float _maintenance_amount, string _direction) =>
    contract_qty = (_investment_capital / _price)
    leveraged_balance = math.abs(contract_qty) * _price / (_leverage + 1)

    maintainance_rate = _direction == 'LONG' ? (_maintenance_rate - 1) : (_maintenance_rate + 1)
    total_balance = (leveraged_balance + _maintenance_amount)
    total_quantity = (contract_qty * _price)
    total_price = _direction == 'LONG' ? (total_balance - total_quantity) : (total_balance + total_quantity)

    liquidation_price = math.round_to_mintick(total_price) / (math.abs(contract_qty) * maintainance_rate)
    qty = liquidation_price * math.abs(contract_qty)

    [liquidation_price, qty]

f_strategy_order_size() =>
    if (strategy.closedtrades > 0)
        order_size = strategy.initial_capital / strategy.closedtrades.entry_price(0)
        math.round(strategy.closedtrades.size(0) / order_size, 2) * 100
    else
        order_size = 100

f_strategy_leverage() =>
    response = int(f_strategy_order_size() / 100)

// ############################################################
// # EXPORT FUNCTIONS
// ############################################################

// function - Calculate the liquidation price.
// @param _investment_capital <float> - The source input.
// @param _price <float> - The price to calculate the liquidation level.
// @param _leverage <int> - The leverage used on the investment.
// @param _maintenance_rate <int> - The Maintenance Rate.
// @param _maintenance_amount <int> - The Maintenance Amount.
// @param _direction <string> - The direction (long or short).
export liquidation(float _investment_capital, float _price, int _leverage, float _maintenance_rate, float _maintenance_amount, string _direction) =>
    f_liquidation(_investment_capital, _price, _leverage, _maintenance_rate, _maintenance_amount, _direction)

// function - Shortcut for the long liquidation price and qty based on leverage.
export liquidation_long(float _investment_capital, float _price, int _leverage, float _maintenance_rate, float _maintenance_amount) =>
    f_liquidation(_investment_capital, _price, _leverage, _maintenance_rate, _maintenance_amount, 'LONG')

// function - Shortcut for the short liquidation price and qty based on leverage.
export liquidation_short(float _investment_capital, float _price, int _leverage, float _maintenance_rate, float _maintenance_amount) =>
    f_liquidation(_investment_capital, _price, _leverage, _maintenance_rate, _maintenance_amount, 'SHORT')

// function - Shortcut for the long liquidation price based on leverage.
export liquidation_price_long(float _investment_capital, float _price, int _leverage, float _maintenance_rate, float _maintenance_amount) =>
    [price, qty] = f_liquidation(_investment_capital, _price, _leverage, _maintenance_rate, _maintenance_amount, 'LONG')
    response = price

// function - Shortcut for the short liquidation price based on leverage.
export liquidation_price_short(float _investment_capital, float _price, int _leverage, float _maintenance_rate, float _maintenance_amount) =>
    [price, qty] = f_liquidation(_investment_capital, _price, _leverage, _maintenance_rate, _maintenance_amount, 'SHORT')
    response = price

// function - Shortcut for the long liquidation qty based on leverage.
export qty_long(float _investment_capital, float _price, int _leverage, float _maintenance_rate, float _maintenance_amount) =>
    [price, qty] = f_liquidation(_investment_capital, _price, _leverage, _maintenance_rate, _maintenance_amount, 'LONG')
    response = qty

// function - Shortcut for the short liquidation qty based on leverage.
export qty_short(float _investment_capital, float _price, int _leverage, float _maintenance_rate, float _maintenance_amount) =>
    [price, qty] = f_liquidation(_investment_capital, _price, _leverage, _maintenance_rate, _maintenance_amount, 'SHORT')
    response = qty

// Function to get the order size from the properties tab.
export order_size() =>
    response = f_strategy_order_size()

// Function to calculate the leverage from the order size (200% = 2x leverage)
export leverage() =>
    response = f_strategy_leverage()
