DinGrogu

Margin/Leverage Calculation

Margin
This library calculates margin liquidation prices and quantities for long and short positions in your strategies.

Usage example
// ############################################################
// # INVESTMENT SETTINGS / INPUT
// ############################################################

// Get the investment capital from the properties tab of the strategy settings.
investment_capital = strategy.initial_capital

// Get the leverage from the properties tab of the strategy settings.
// The leverage is calculated from the order size for example: (300% = x3 leverage)
investment_leverage = margin.leverage()

// The maintainance rate and amount.
investment_leverage_maintenance_rate = input.float(title='Maintanance Rate (%)', defval=default_investment_leverage_maintenance_rate, minval=0, maxval=100, step=0.1, tooltip=tt_investment_leverage_maintenance_rate, group='MARGIN') / 100
investment_leverage_maintenance_amount = input.float(title='Maintanance Amount (%)', defval=default_investment_leverage_maintenance_amount, minval=0, maxval=100, step=0.1, tooltip=tt_investment_leverage_maintenance_amount, group='MARGIN')

// ############################################################
// # LIQUIDATION PRICES
// ############################################################

leverage_liquidation_price_long = 0.0
leverage_liquidation_price_long := na(leverage_liquidation_price_long[1]) ? na : leverage_liquidation_price_long[1]

leverage_liquidation_price_short = 0.0
leverage_liquidation_price_short := na(leverage_liquidation_price_short[1]) ? na : leverage_liquidation_price_short[1]

leverage_liquidation_price_long := margin.liquidation_price_long(investment_capital, strategy.position_avg_price, investment_leverage, investment_leverage_maintenance_rate, investment_leverage_maintenance_amount)
leverage_liquidation_price_short := margin.liquidation_price_short(investment_capital, strategy.position_avg_price, investment_leverage, investment_leverage_maintenance_rate, investment_leverage_maintenance_amount)

Get the qty for margin long or short position.
margin.qty_long(investment_capital, strategy.position_avg_price, investment_leverage, investment_leverage_maintenance_rate, investment_leverage_maintenance_amount)
margin.qty_short(investment_capital, strategy.position_avg_price, investment_leverage, investment_leverage_maintenance_rate, investment_leverage_maintenance_amount)

Get the price and qty for margin long or short position.
[price_long, qty_long] = margin.qty_long(investment_capital, strategy.position_avg_price, investment_leverage, investment_leverage_maintenance_rate, investment_leverage_maintenance_amount)
[price_short, qty_short] = margin.qty_short(investment_capital, strategy.position_avg_price, investment_leverage, investment_leverage_maintenance_rate, investment_leverage_maintenance_amount)
Pine腳本庫

本著真正的TradingView精神,作者將此Pine代碼以開源腳本庫發布,以便我們社群的其他Pine程式設計師可以重用它。向作者致敬!您可以私下或在其他開源出版物中使用此庫,但在出版物中重用此代碼受網站規則約束。

免責聲明

這些資訊和出版物並不意味著也不構成TradingView提供或認可的金融、投資、交易或其他類型的意見或建議。請在使用條款閱讀更多資訊。

想使用這個腳本庫嗎?

複製以下行並將其黏貼到您的腳本中。