Leon Wilson and others use the ADX or other indicators to control the level of the trailing stop to ensure we get out the market at better times.
I have tried to code something along the lines of once the ADX > 30 then the stop is 3.5x ATR away whilst when it is below its > 7.5x ATR awa.
The problem I have is the stop should stay at 3.5x once the ADX > 30 and not go back 7.5x. The time when the price peaks the stop temporarily moves up but then goes back to 7.5x
Because that is a value, you can not use it in the IF statement. That first item in the if must be a true false result (or at least 0 or 1)
I added “ad1” as a separate variable so it can be used in the ValueWhen and the If.
//Get the value when ADX CA 30
ad1 = adx() > 30;
$v=VALUEWHEN(ad1);
//Calculate trailing below the low on the date of the ADX move
v1 = RATCHET((LOW() – 3.5*atr()), START=$v);
v2 = st(MULTI=7.50, BARS=20);
if(ad1,v1,v2)
Thanks - unfortunately I have plotted this script and it doesn’t work. I also tried varying the script to make it 2x ATR. The dotted line is this 2x ATR where the script should have taken me out of the market.
My response was just to show you that you needed a boolean value for the IF condition. I’m not trying to give you a script that works.
My advice is to break out each individual value and make sure that it is doing what you expect.
If you want me to write the full script for you, please go to optuma.com/consults and book an enterprise consult. One of the other guys will do it for a normal scripting consult.