Pinescript, trailing stop loss repaints

3k views Asked by At

Why does a simple Trailing Stop Loss in pinescript repaint this hugely?

I am doing some backtesting on ETH/USD, 1H on Tradingview using pinescript version 4. The complete script is shown below:

//@version=4
strategy(title="Simple SL script", shorttitle="Simple SL script", overlay = true, initial_capital=1000, currency="USD", commission_type=strategy.commission.percent, commission_value=0.1, slippage = 5, pyramiding=1, calc_on_every_tick=false)

risk = input(title='Risk %', defval=100.0, step=1.0)/100

//secScaler = secType == "Forex" ? 100000 : secType == "Metal Spot" ? 100 : secType == "Cryptocurrency" ? 10000 : secType == "Custom" ? contracts : 0
fixedSL = input(title="SL Points", defval=1000)*10000
fixedTP = input(title="TP Points", defval=10)*10000

//##############################################################################
//Trade Logic
//##############################################################################

balance = strategy.initial_capital + strategy.netprofit
if (balance > 0)
    lots = (risk * balance)/close
    strategy.entry("BUY", strategy.long, qty=lots)
    strategy.exit("B.Exit", "BUY", qty_percent = 100, loss=fixedSL, trail_offset=20, trail_points=fixedTP)

Using normal backtesting or Replay I get the following results:

enter image description here

Which results in a plus $20.

Before the backtesting I let it run through realtime data and I got the following results:

enter image description here

Which resultet in a negative $6.

So a difference of $26.

I am using no security or other questionable solutions for the trailing stop loss - how can the results vary this much over a short timeframe of 6 bars (6 hours)?

I did try to set the calc_on_every_tick to true but as expected I got a huge amount of buy and sell orders within the same candle.

1

There are 1 answers

7
Casper Nybroe On BEST ANSWER

I found the answer in several posts on various Tradingview ideas and scripts. The issue is that each bar while backtesting (BT) only holds the value of Open, Close, High and Low. Also, Tradingview favors the direction of your order when it comes to only having the above data fields and when the trade is executed intra-bar.

This means that if I am creating a Long positioned order and the script says to sell within the same bar - it will always close on High - which the first picture in the question clearly shows aswell.

During live data testing, Tradingview knows how to collect and temporarily store all the data for the bars it has been livetesting on. This means that it now holds data for each tick insted of just the Open, Close, High and Low.

Notice: The data is only stored in your browser session - if you refresh the page or navigate away from the script, your data is gone. You also need to have Recalulate on every tick set to true before starting the live test.

What to do then?

The best way to verify your script is to manually do some live data testing as mentioned above. Just let your computer run with your window with Tradingview and your script open and you will get the live data results. This is really annoying when testing long term scripts, like a swing strategy.

EDIT

Tradingview does NOT seem to offer intra-bar data. I was convinced that they had this with the Premium subscription but it doesn't seem to be available. They offer something called intraday data but that is just data for timeframes lower than daily. I also tested it with Bar Replay but that doesn't seem to have intrabar data either. The best way to be certain that your script works as expected is to do what I mention above in "What to do then?" and compare the results with normal backtesting.