Hi guys,
I need help.
I want to set up the stop loss or the exit condition to the open price of the preceding bar of the tradeindex(1) as per the blue dashes the attached pic. There must be an obvious way to do it but I am stuck.
I am testing it on the following code:
// 3 Candle Pattern
//Strategy
If Close[2]>Open[2] and Close[1]>Open[1] and Close<Open[2] then
Bear=1
Bull=0
ElsIf Close[2]<Open[2] and Close[1]<Open[1] and Close>Open[2] then
Bear=0
Bull=1
EndIf
// Stop Loss Management
If longonmarket and tradeprice(1) - close < 0 THEN
sell 1 contract at market
ENDIF
If shortonmarket and tradeprice(1) - close > 0 THEN
exitshort at market
ENDIF
//Timeframe (1 mn)
If Bull=1 and Bear[1]=1 then
buy 1 contract at market
//DrawArrowUP(BarIndex,Low)Coloured("Green")
ElsIf Bear=1 and Bull[1]=1 then
sellshort 1 contract at market
//DrawArrowDown(BarIndex,High)Coloured("Red")
EndIf
Thanks for your help
ok, so it’s the open of the bar when code is read, you can try to replace both line 24 and line 27 by :
set stop price open
if you run into issues of minimum distance, use min(open, minid)
where minid would be your broker’s minimum distance for this asset
Thanks @JC_Bywan.
I want to keep the same SL value for the duration of the position. I don’t want to update the SL at each new bar.
By suggesting to add set stop price open in lines 24 and 27, that’s what should happen because they are inside the same if statement as the order launching, so SL would be set only then and not updated at each new bar. You can also try to do it through a variable SL in order to graph it and visualize if behaviour matches your expectations:
If Bull=1 and Bear[1]=1 then
buy 1 contract at market
SL=open
set stop price SL
ElsIf Bear=1 and Bull[1]=1 then
sellshort 1 contract at market
SL=open
set stop price SL
EndIf
graphonprice SL
Got it !! I ll play with it.
Thanks