Hi,
I’d like to code a simple SL function:
when the conditions for buy order are triggered, I want the stop loss to be put below the lowest of the previous x bars.
Same for sell orders, above the highest of the previous bars.
Thanks for any help
IF Not OnMarket and (your conditions) THEN
BUY at Market
SL = Lowest[100](Low)
SELL AT SL STOP
ENDIF
IF OnMarket THEN
SELL at SL STOP
ENDIF
This should work if I have understood right what you want. The stoploss price is set and an order set at the time of entry and an order placed every bar after that at that same stoploss price while you still on the market because the STOP orders need to be renewed at every new bar. Obviously change the [100] to whatever period you need for your lookback.
Code the reverse with Highest[x](High and SellShort and ExitShort for short positions.