2

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.

Casper Nybroe
  • 1,179
  • 3
  • 23
  • 47

1 Answers1

2

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.

Casper Nybroe
  • 1,179
  • 3
  • 23
  • 47
  • Is it possible to apply non-repainting trailing stop during backtesting on the trading-veiw? Does `intra-bar data` does this? – alper May 24 '21 at 11:56
  • @alper - yes, as far as I understand you can do this with the Premium subscription which gives you the `intra-bar data`. But I haven't tried this out myself. – Casper Nybroe Jun 01 '21 at 11:32
  • I have registered into highest premium subscription but I wasn't able to find `intra-bar data` or any related guide for it. By saying `intra-bar data` do you refer to 1 second (as in very short term) bar? – alper Jun 01 '21 at 12:28
  • I just checked the pricing charts again and I think there might be a confusion. There is no explicit area mentioning `intraBAR data`. There is something for `intraDAY data` but that only means timeframes lower than `Daily` timeframe. I have done some more research and I am unable to find anywhere Tradingview offers `intraBAR data`... I wrote the support but they are incredible slow.. `Bar Replay` doesn't seem to have all data for the single bar either. So I am not sure if it even is possible to get the entire data for each bar.. – Casper Nybroe Jun 02 '21 at 09:05
  • 1
    I have edited this answer with my new findings. – Casper Nybroe Jun 02 '21 at 09:14
  • Thanks I feel like we shouldn't trust tradinvview's backtesting results, they could be misguiding – alper Jun 02 '21 at 13:47
  • And you shouldn't - you should keep track of your results manually, let it run over some time to collect your own backtesting data. It is time consuming and annoying but the most trustworthy. I use Tradingview to get an indication of what might work - and when I feel comfortable enough I start testing on live data. – Casper Nybroe Jun 03 '21 at 12:08