Thank you. I amended my code and things looks better now, but still i find the long trail stop sometimes move LOWER which is not allowed. here is my code, really hope someone could help me out
...
Halyconguy && Joshua sort of answered the question already:
As the current bar changes this could change the lowest value found.
It will have to be allowed to lower or terminate at some point, or else you end up in a situation like where it only works at all time highs....Its situational though, depending on how you enter, and where you initial stop is.
I present Two pictures (since I'm a little hungry, I chose Chipotle stock):
Chipotle, your original code:
Chipotle, revised code:
my assumption: you want to use the price action of recent lows or highs as your trailing stop, and you choose whether or not to enter the trade based on your state.long/state.short code
If my assumptions OK, then you can try the rest of this post. If my assumptions are not OK ..
hello , Sorry you will have to reword your question before I can understand it.
If my assumption is OK, then I think I can provide what you're looking for, using a couple small revisions to your existing code:
1. In your else, use state.init instead of state[1]
2. Change your BuyStop and SellStop to use one state criteria, and leverage maHigh and maLow INSTEAD OF bp and sp.
[Based on my analysis of your code, the bp and sp were fantastic criteria for getting a long or short state, but were less than ideal for the trailing stop, as they'd sometimes appear inside candles.]
These are the steps I followed to revise your code [You can jump to the bottom for the completed code.]
1. Comment out the BuyStop and SellStop
Code:
#plot BuyStop = if state == state.short or state != state[1] then bp else Double.NaN;
#plot SellStop = if state == state.long or state != state[1] then sp else Double.NaN;
#BuyStop.SetDefaultColor(GetColor(0));
#SellStop.SetDefaultColor(GetColor(1));
2. Leverage plots (and the Data Box) to check maHigh, maLow, bp, sp, state.short, and state.Long:
Code:
plot maHighVal = maHigh;
plot maLowVal = maLow;
plot bpVal = bp;
plot spVal = sp;
maHighVal.SetStyle(CURVE.SHORT_DASH);
maLowVal.SetStyle(CURVE.SHORT_DASH);
plot stateL = state == state.long;
plot stateS = state == state.short;
3. I generally liked your state.long and state.short as they appeared in the plot, so I only slightly tweaked the "else" condition, to not let them carry over a value from the prior candle, as I think this is what caused them to stay in trades too long and adjust the stop in the wrong direction when the trade should have been stopped out:
Code:
def state = {default init, short, long};
if (price > bp) {
state = state.long;
} else if (price < sp) {
state = state.short;
} else {
state = state.init;
}
4. Update the code for BuyStop and SellStop to only consider one state, and leverage maHigh and maLow:
Code:
plot BuyStop = if state == state.short && maHigh <= maHigh[1] then maHigh else Double.NaN;
plot SellStop = if state == state.long && maLow >= maLow[1] then maLow else Double.NaN;
BuyStop.SetDefaultColor(GetColor(0));
SellStop.SetDefaultColor(GetColor(1));
5. remove the temporary maHighVal, maLowVal, bpVal, and spVal
Code:
#plot maHighVal = maHigh;
#plot maLowVal = maLow;
#plot bpVal = bp;
#plot spVal = sp;
#maHighVal.SetStyle(CURVE.SHORT_DASH);
#maLowVal.SetStyle(CURVE.SHORT_DASH);
#plot stateL = state == state.long;
#plot stateS = state == state.short;
6. Here's the complete revised code:
https://usethinkscript.com/threads/trailing-stop-based-on-highs-or-lows-for-thinkorswim.10979/