Hello,
I’m working with stop orders in order to open new positions in my automated strategies. I have an issue with my SL however. My idea is to:
- set a first stop once the position is open, just below the previous candle low/high
- For the next bars, I would like to use another stop, a bit further, which is then fixed through the trade
The issue I have is that when used separately the code for point 1 and 2 works fine, but when combined only point 1 is working. So in the code the instruction “set stop loss” seems to prevail over “sell/existshort at stop”.
Does someone know why?
If long=1 then
buy positionSize contracts at buyLevel STOP
stopbougieL=Lowest[7](low)-2*pipsize
set stop loss abs((close-low))+2*pipsize
endif
if short=1 then
sellshort positionSize contracts at SellLevel STOP
stopbougieS=highest[7](high)+2*pipsize
set stop loss abs((close-high))+2*pipsize
endif
if longonmarket then
sell at stopbougieL stop
graph close
elsif shortonmarket then
exitshort at stopbougieS stop
endif
Whenever LONG = 1 or SHORT = 1 then the STOP LOSS order will be read and placed. The following should work as long as you are not wanting to use accumulating positions:
If long=1 then
buy positionSize contracts at buyLevel STOP
stopbougieL=Lowest[7](low)-2*pipsize
if not onmarket then
set stop loss abs((close-low))+2*pipsize
endif
endif
if short=1 then
sellshort positionSize contracts at SellLevel STOP
stopbougieS=highest[7](high)+2*pipsize
if not onmarket then
set stop loss abs((close-high))+2*pipsize
endif
endif
if longonmarket then
sell at stopbougieL stop
graph close
elsif shortonmarket then
exitshort at stopbougieS stop
endif
thx Vonasi, but actually I already used the “not onmarket” function, I just cut it wrongly when I copy paste the code here.
So no, on my charts, it still doesn’t work
Sorry my mistake. A SET STOP LOSS order need only be sent once and is then on the market until it is either cancelled or the value changed. Setting it to zero is supposed to cancel them but it did not use to work (maybe it does now?) An alternative is to use SET STOP %LOSS 100.
If long=1 then
buy positionSize contracts at buyLevel STOP
stopbougieL=Lowest[7](low)-2*pipsize
if not onmarket then
set stop loss abs((close-low))+2*pipsize
endif
endif
if short=1 then
sellshort positionSize contracts at SellLevel STOP
stopbougieS=highest[7](high)+2*pipsize
if not onmarket then
set stop loss abs((close-high))+2*pipsize
endif
endif
if longonmarket then
set stop ploss 0
sell at stopbougieL stop
graph close
elsif shortonmarket then
set stop ploss 0
exitshort at stopbougieS stop
endif
An alternative is to just use pending stop orders and not use SET STOP orders.
If long=1 then
buy positionSize contracts at buyLevel STOP
stopbougieL=Lowest[7](low)-2*pipsize
if not onmarket then
sell at close - abs((close-low))+2*pipsize stop
endif
endif
if short=1 then
sellshort positionSize contracts at SellLevel STOP
stopbougieS=highest[7](high)+2*pipsize
if not onmarket then
exitshort at close + abs((close-high))+2*pipsize stop
endif
endif
if longonmarket then
sell at stopbougieL stop
graph close
elsif shortonmarket then
exitshort at stopbougieS stop
endif
Thx very much Vonasi ,
your trick with SET STOP %LOSS 100 seems to be working! Testing the code on my strategy, it improves a bit the profit factor, which was the goal, as I noticed that sometimes, the market moves directly against you (meaning in the same bar when you took the trade), and it’s best to cut directly.
So I’m guessing that SET STOP pLOSS 0 is still not cancelling any set stop orders then?
works also, at least on backtest