TradingView
Peter_O
2021년 6월 23일 오후 1시 33분

How to use Leverage and Margin in PineScript 

Euro Fx/U.S. DollarFXCM

설명

En route to being absolutely the best and most complete trading platform out there, TradingView has just closed 2 gaps in their PineScript language.

  • It is now possible to create and backtest a strategy for trading with leverage.
  • Backtester now produces Margin Calls - so recognizes mid-trade drawdown and if it is too big for the broker to maintain your trade, some part of if will be instantly closed.

New additions were announced in official blogpost, but it lacked code examples, so I have decided to publish this script. Having said that - this is purely educational stuff.

█ LEVERAGE

Let's start with the Leverage. I will discuss this assuming we are always entering trades with some percentage of our equity balance (default_qty_type = strategy.percent_of_equity), not fixed order quantity.

If you want to trade with 1:1 leverage (so no leverage) and enter a trade with all money in your trading account, then first line of your strategy script must include this parameter:
default_qty_value = 100 // which stands for 100%


Now, if you want to trade with 30:1 leverage, you need to multipy the quantity by 30x, so you'd get 30 x 100 = 3000:
default_qty_value = 3000 // which stands for 3000%


And you can play around with this value as you wish, so if you want to enter each trade with 10% equity on 15:1 leverage you'd get default_qty_value = 150.
That's easy. Of course you can modify this quantity value not only in the script, but also afterwards in Script Settings popup, "Properties" tab.

█ MARGIN

Second newly released feature is Margin calculation together with Margin Calls. If the market goes against your trades and your trading account cannot maintain mid-trade drawdown - those trades will be closed in full or partly. Also, if your trading account cannot afford to open more trades (pyramiding those trades), Margin mechanism will prevent them from being entered.

I will not go into details about how Margin calculation works, it was all explainged in above mentioned blogpost and documentation.

All you need to do is to add two parameters to the opening line of your script:
margin_long = 1./30*50, margin_short = 1./30*50


Whereas "30" is a leverage scale as in 30:1, and "50" stands for 50% of Margin required by your broker. Personally the Required Margin number I've met most often is 50%, so I'm using value 50 here, but there are literally 1000+ brokers in this world and this is individual decision by each of them, so you'd better ask yourself.

--------------------

Please note, that if you ever encounter a strategy which triggers Margin Call at least once, then it is probably a very bad strategy. Margin Call is a last resort, last security measure - all the risks should be calculated by the strategy algorithm before it is ever hit. So if you see a Margin Call being triggred, then something is wrong with risk management of the strategy. Therefore - don't use it!
코멘트
praveenbm5
I am a little confused with how this new Leverage and Margin work
I am currenty testing 5x leverage. So I set default_qty_type = strategy.percent_of_equity, default_qty_value = 500 and margin_short = 20
And then I sold a security at 100 which then went to 108. Approximately 8% loss. But I still got a margin call . If I understand right I should not get a margin call untill the loss is >= 20%
Peter_O
@praveenbm5, Maybe you should include leverage multiplier in margin_short as well? I'm calculating like this: margin_short=1./30*50. Check in the script description what "50" and "30" stand for.

Thanks for coins!
praveenbm5
@Peter_O, Thanks for the prompt reply.

"Whereas "30" is a leverage scale as in 30:1, and "50" stands for 50% of Margin required by your broker. "

I understand the 30x leverage. Which means you only 3.33% margin to open a position and at this leverage you will lose all money if the price move adversely by 3.33%.

What is this 50% margin required by broker. As mentioned above the broken needs 3.33% of MVS as margin to open a 30x leveraged position.
Peter_O
@praveenbm5, Every broker has this value pre-defined. If the margin on your open trades falls below "required margin", broker is not able to maintain your trades anymore and starts closing them (or liquidating). This is market with Margin Call on the chart.
mtahreemalam
@Peter_O, same question as @praveenbm5 . The leverage is usually around 5% or less.
I am highly confused as to which broker demands 50% margin, or am i doing something wrong.
Peter_O
@mtahreemalam, There are brokers who offer 1:500 leverage and more.
PineCoders
adolgov
👍
Peter_O
@adolgov, I should be thanking you, not the other way round :)
cliveloseby
Apologies for a rookie question but is there a guide that will show me how to actually trade with leverage (in Paper View)?
더보기