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)
Biblioteka Pine

Działając zgodnie z prawdziwym duchem TradingView, autor opublikował ten kod Pine jako bibliotekę o otwartym kodzie źródłowym, aby inni programiści Pine z naszej społeczności mogli go ponownie wykorzystać. Brawa dla niego! Możesz korzystać z tej biblioteki prywatnie lub w innych publikacjach typu open source, ale ponowne wykorzystanie tego kodu w publikacji podlega Regulaminowi.

Wyłączenie odpowiedzialności

Informacje i publikacje przygotowane przez TradingView lub jego użytkowników, prezentowane na tej stronie, nie stanowią rekomendacji ani porad handlowych, inwestycyjnych i finansowych i nie powinny być w ten sposób traktowane ani wykorzystywane. Więcej informacji na ten temat znajdziesz w naszym Regulaminie.

Chcesz skorzystać z tej biblioteki?

Skopiuj poniższy wiersz i wklej go w swoim skrypcie.