Editors' picks
OPEN-SOURCE SCRIPT

How to use Leverage and Margin in PineScript

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:


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:


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:


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!
educationForexleveragemarginmargintradingStochastic Oscillator

Open-source script

In true TradingView spirit, the author of this script has published it open-source, so traders can understand and verify it. Cheers to the author! You may use it for free, but reuse of this code in publication is governed by House rules. You can favorite it to use it on a chart.

Want to use this script on a chart?


I auto-execute TradingView Alerts into MT4/MT5 using this: tradingconnector.com 1-second delivery. Ping me if you need support with installation.
Also on:

Disclaimer