Correct way to script a stop loss in signal tester

What is the correct way to script a 10% trailing stop in the signal and trade tester?

Whatever I try, the trade test never adheres to the stop.

Many thanks

Hi John,

I believe that the STOP() function doesn’t work with the signal testster. You can do a fairly good approximation by

//Your entry script / conditions
entry = SCRIPT(SCRIPTNAME=entry);

//stoploss percentage
v = 0.10;
//highest close since last entry (can change to whatever measure you want
stl = HIGHESTSINCE(Close(), entry);
//calculate close
Trail = stl* ( 1 - v);

//exit on cloe below trailing loss,
exit1 = close() CrossesBelow Trail;
// switch so we only do it once after an entry
inTrade = switch(entry, exit1) ;
inTrade ChangeTo 0

1 Like

Sorry- I have just been working on this, and the above is actually incorrect.
The below works as intended.
Change the Low() to close() or high() or whatever you need it to be. But basically need to create a custom RATCHET() function that gets reset every time you post an entry.

entry = SCRIPT(Scriptname = Entry);
//stop level
sl = 0.075;

//create a ratcheting highest low
hl = IF((entry) or (LOW() > hl[1]), LOW(), hl[1]);

//Offest by your %
sl1 = hl * (1- sl);

//Check for cross
LOW() CrossesBelow  sl1

You may need to add the switch back in so that you don’t end up with 100s of exits messing with your results.

Hi

But what if position is already taken and signal reappears for entry.

Regards
Deepak

Hi Deepak,

You could guard it with the switch statetment from my first reply, and only ‘reset’ if you weren’t in a trade the bar before the entry was triggered.

Just be aware that those types of self referencing feedback loops can be difficult to get right. Make sure you test them with a plot and/or show bar before you start signal testing.