Needing help to code so that the stop moves to entry (0) when the trade is in profit by a number of points?
// indicators and parameters
defparam cumulateorders = false
defparam flatbefore = 083000
defparam flatafter =170000
possize = 10
IF TIME = 090000 THEN //include the 0910 candle
Top = highest[6](high) // go back 8 candles
Bottom = lowest[6](low) // go back 8 candles
LongsForDay = 0
MaxPos = 1
ENDIF
IF TradeIndex = BarIndex THEN
LongsForDay = LongsForDay +1
ENDIF
IF TIME >=090000 AND LongsForDay < MaxPos THEN
BUY possize CONTRACTS AT Top +11 STOP
SELLSHORT possize CONTRACTS AT Bottom -11 stop
ENDIF
set target profit 50
set stop loss 170
IF CLOSE-TradePrice>25 THEN
set stop loss 0
ENDIF
I suggest to replace
IF CLOSE-TradePrice>25 THEN
with
IF CLOSE-TradePrice>25*pipsize THEN
to make sure it works with alla instruments.
Your code seems fine, the only correction to make is line 30, since I don’t think setting a SL to 0 works, you’d better replace thet line with
SELLSHORT possize CONTRACTS AT Tradeprice stop
You have long and short in the code so your stoploss change will not work anyway for both. Also Roberts suggestion of changing just the one line won’t work as if the price drops under the 25 pip gap the order will no longer be sent to market. You need something like this I think:
if not onmarket then
breakevenflag = 0
endif
IF onmarket and ABS(CLOSE-TradePrice) > 25 THEN
breakevenflag = 1
endif
if breakevenflag = 1 then
sell at positionprice stop
exitshort at positionprice stop
ENDIF
Thanks, Ive made the changes however I get a syntax error, please help?
// indicators and parameters
defparam cumulateorders = false
defparam flatbefore = 083000
defparam flatafter =170000
possize = 10
IF TIME = 090000 THEN //include the 0910 candle
Top = highest[6](high) // go back 8 candles
Bottom = lowest[6](low) // go back 8 candles
LongsForDay = 0
MaxPos = 1
ENDIF
IF TradeIndex = BarIndex THEN
LongsForDay = LongsForDay +1
ENDIF
IF TIME >=090000 AND LongsForDay < MaxPos THEN
BUY possize CONTRACTS AT Top +11 STOP
SELLSHORT possize CONTRACTS AT Bottom -11 stop
ENDIF
set target profit 50
set stop loss 170
IF not onmarket then
breakevenflag = 0
ENDIF
IF onmarket and ABS(CLOSE-TradePrice) > 25 THEN
breakevenflag = 1
ENDIF
IF breakevenflag = 1 THEN
sellshort possize contracts at positionPrice stop
sell possize contracts at positionprice stop
endif
Sorry I see it didnt copy it all
// indicators and parameters
defparam cumulateorders = false
defparam flatbefore = 083000
defparam flatafter =170000
possize = 10
IF TIME = 090000 THEN //include the 0910 candle
Top = highest[6](high) // go back 8 candles
Bottom = lowest[6](low) // go back 8 candles
LongsForDay = 0
MaxPos = 1
ENDIF
IF TradeIndex = BarIndex THEN
LongsForDay = LongsForDay +1
ENDIF
IF TIME >=090000 AND LongsForDay < MaxPos THEN
BUY possize CONTRACTS AT Top +11 STOP
SELLSHORT possize CONTRACTS AT Bottom -11 stop
ENDIF
set target profit 50
set stop loss 170
IF not onmarket then
breakevenflag = 0
ENDIF
IF onmarket and ABS(CLOSE-TradePrice) > 25 THEN
breakevenflag = 1
ENDIF
IF breakevenflag = 1 THEN
sellshort possize contracts at positionprice stop
sell possize contracts at positionprice stop
endif
//IF CLOSE-TradePrice>25 THEN
//set stop loss 0
ENDIF
OK so clearly you can see I’m not a coder!
Please use the ‘Insert PRT Code’ button when posting code. I have tidied up your posts as best as I can but there was a lot of lines that were not PRT code so I had to guess a little.
I think your syntax error is because you have an ENDIF at the end of the code that you do not need.
Thank you so much, lets see how it behaves live tomorrow!
Ok, so it doesn’t work live
Line 32 does set BREAKEVENFLAG whenever you reach 25 pips, no matter whether you are in profit or loss, since it doesn’t tell between LONG and SHORT trades, I suggest replacing lines 32-34 with
IF longonmarket and (CLOSE-TradePrice) > 25 THEN
breakevenflag = 1
ELSIF shortonmarket and (TradePrice-close) > 25 THEN
breakevenflag = 1
ENDIF
you could actually combine the two IFs in just one line, but that would make the code much less clear.
Thanks for spotting that Robert – you can tell that I never trade short – so my brain does not work very well in that direction!
// indicators and parameters
defparam cumulateorders = false
defparam flatbefore = 083000
defparam flatafter =170000
possize = 2
IF TIME = 090000 THEN //include the 0910 candle
Top = highest[6](high) // go back 8 candles
Bottom = lowest[6](low) // go back 8 candles
LongsForDay = 0
MaxPos = 1
ENDIF
IF TradeIndex = BarIndex THEN
LongsForDay = LongsForDay +1
ENDIF
IF TIME >=090000 AND LongsForDay < MaxPos THEN
BUY possize CONTRACTS AT Top +11 STOP
SELLSHORT possize CONTRACTS AT Bottom -11 stop
ENDIF
set target profit 50
set stop loss 170
IF not onmarket then
breakevenflag = 0
ENDIF
IF longonmarket and (CLOSE-TradePrice) > 25 THEN
breakevenflag = 1
ELSIF shortonmarket and (CLOSE-TradePrice-CLOSE) > 25 THEN
breakevenflag = 1
ENDIF
//IF breakevenflag = 1 THEN
//sellshort possize contracts at positionprice stop
//sell possize contracts at positionprice stop
//ENDIF
Is the above correct?
When I try activate it for automatic trading I get:
– Code is invalid. Please correct it.
To write code, please use the <> “insert PRT code” button to make code easier to read and understand. Thank you.
Is the above correct?
When I try activate it for automatic trading I get:
– Code is invalid. Please correct it.
The only warning I get is that the variable BREAKEVENFLAG is not used. Why keeping it if you don’t use it?