Backstory: While making mean reversion its pretty usual to exit when close crosses under/over the mean. Sometimes close crosses over/under and the trade is pretty much perfect, other times price just keeps on running and you miss out on alot of profit.
I would love a kind of trailing stop loss that moves the stop to the price of the Moving average when the crossover happens. As you see in the picture:
1. I enter short trade when price is high, hoping price will revert back to its mean.
2. When in the short trade, and price crosses under the MA, the stop loss will move to that spesific price of the moving average, when that crossover happens. So if the moving average = 12 123, then thats my new stop loss.
It’s just abs(TRADEPRICE – CLOSE) of the candle when the crossing occurs.
Divide it by PIPSIZE if you need pips instead of a price difference.
If you need the price of the average, you decide which one and replace CLOSE with it.
not sure i understand what u mean Roberto, sorry.
Im not sure how i can keep the stop loss at the same level regardless of how long the trade continues?
You just need to save the value of your SL and, once set, never change it again. You’ll then place a pending STOP order each candlestick while On Market.
When Not OnMarket clear your SL value resetting it to zero.
Im stupid, cant get this to work, sorry.
Im someone wanna help me write the code id be grateful.
Try this:
IF Not OnMarket THEN
MyStop = 0
ENDIF
Sma20 = average[20,0[(close)
IF close CROSSES UNDER Sma20 AND MyStop = 0 THEN
MyStop = Sma20
ENDIF
IF MyStop AND ShortOnMarket THEN
EXITSHORT AT MyStop STOP
ENDIF
I have not tested it yet Roberto, but this was my intial thought as well. The problem (and please correct me if im wrong) is that i dont want “a MA 20” to be my new stop level. i want “the MA 20” level where price crossed under the MA 20.
I dont think the code u posted does this? it just has “if price crosses over MA20 then sell” basicly?
My head is buzzing trying to work out if the code is doing what I think it is supposed to! 🙂
More coffee then look at it again, but attached is what I am looking at.
The last orange bar was where it exit the Short trade.
Line 6 does the job. The level where CLOSE crosses SMA20 is… SMA20!
In case you need the moment when close FIRST crosses under SMA20, then you’ll have to resort to MTF so that you can tell exactly that very moment, but it’s not granted that by the closure of the bar the price won’t retrace so, indeed, no crossing could have occurred at all, despite your strategy may have assumed it!
Surely the exit was on the next bar / bar not shown (at the stop level exit) and you can see that the exit price was the same price as MyStop shown on the previous bar?