May 26, 2021
Strategy Leverage

We’ve improved our backtesting engine and made it even more realistic.

Previously, our strategies could open positions many times larger than the available account size, which could lead to unrealistic profits.

Today we are adding the Leverage mechanism to strategies, which will help solve this problem.

How does it work?

When you open any position, the margin required to maintain the position will be calculated. If there are not enough funds, then a margin call occurs, which is the forced closure of part or all of the positions by a market order so that there are enough funds to maintain the positions. More detailed information on how the margin is calculated can be found in our User Manual.

You can customize the margin for short and long positions yourself. For this, the parameters margin_long and margin_short are added to the strategy function, which take non-negative values. You can also change these values ​​in the strategy settings.

By default, margin_long and margin_short are 100% for your new strategies. For strategies that were written before adding the margin functionality, the default values for margin_long and margin_short are 0.

You can also check the number of Margin Calls in the Strategy Tester. To do this, open the Performance Summary tab and find the Margin Calls field.

As an example, let’s use the RSI strategy for the BITSTAMP:BTCUSD with a 1-hour timeframe.

If the margin_short parameter is 0, then with a sharp rise in price on April 2, the short trade opened on March 27 will only be closed on April 11.

However, if we set the margin_short parameter to 20%, then when the price rises on April 2, a Margin Call will occur and the position will be partially closed.

Please note that Leverage in strategies will only work in Pine Script v4.

If you’d like to be aware of Pine Script updates as soon as they appear, keep an eye on the Release notes section in our User Manual. The PineCoders account also broadcasts the updates on its Squawk Box Telegram channel, Twitter and the Pine Script public chat on TradingView.

We hope you find these improvements useful. Please continue to share your opinion with us. We’re building TradingView for our users, and we love hearing what you think about our innovations.

The fastest way to follow markets

Launch Chart