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)
ไลบรารีไพน์

ด้วยเจตนารมณ์หลักของ TradingView ผู้เขียนได้เผยแพร่ Pine code นี้เป็นโอเพนซอร์ซไลบรารีเพื่อให้ Pine โปรแกรมเมอร์คนอื่นในชุมชนของเราสามารถนำไปใช้ซ้ำได้ ต้องขอบคุณผู้เขียน! คุณสามารถใช้ไลบรารีนี้ในแบบส่วนตัวหรือในการเผยแพร่แบบโอเพนซอร์สอื่น ๆ แต่การนำโค้ดนี้ไปใช้ในการเผยแพร่ซ้ำจะต้องอยู่ภายใต้ กฎระเบียบการใช้งาน

คำจำกัดสิทธิ์ความรับผิดชอบ

ข้อมูลและบทความไม่ได้มีวัตถุประสงค์เพื่อก่อให้เกิดกิจกรรมทางการเงิน, การลงทุน, การซื้อขาย, ข้อเสนอแนะ หรือคำแนะนำประเภทอื่น ๆ ที่ให้หรือรับรองโดย TradingView อ่านเพิ่มเติมที่ เงื่อนไขการใช้บริการ

ต้องการใช้ไลบรารีนี้หรือไม่?

คัดลอกข้อความไปยังคลิปบอร์ดแล้ววางลงในสคริปต์ของคุณ