BradParticipant
Junior
Good day All
I am using a code provided by Nicolas to adjust my exit when the price has moved a set number of points. Breakeven code for your automated trading strategy – Learning – ProRealTime (prorealcode.com)
//When a trade has moved by a set number of points(60), the stop loss is moved to protect a set number of points(35):
IF LongOnMarket AND (CLOSE - TradePrice[1]) >= (60 * PipSize) THEN
AdjustMySL = 1
ENDIF
ExitMyAdjustedLong = TradePrice[1] + 35
IF AdjustMySL THEN
SELL AT ExitMyAdjustedLong STOP
ENDIF
IF NOT LongOnMarket THEN
AdjustMySL = 0
ENDIF
Can I repeat this code with adjusted wording and values to adapt my exit should the price continue higher (long position)?
//When a trade has moved by a set number of points(60), the stop loss is moved to protect a set number of points(35):
IF LongOnMarket AND (CLOSE - TradePrice[1]) >= (60 * PipSize) THEN
AdjustMySL = 1
ENDIF
ExitMyAdjustedLong = TradePrice[1] + 35
IF AdjustMySL THEN
SELL AT ExitMyAdjustedLong STOP
ENDIF
IF NOT LongOnMarket THEN
AdjustMySL = 0
ENDIF
//Second Move
//When a trade has moved by a set number of points(90), the stop loss is moved to protect a set number of points(50):
IF LongOnMarket AND (CLOSE - TradePrice[1]) >= (90 * PipSize) THEN
AdjustMySL2 = 1
ENDIF
ExitMyAdjustedLong2 = TradePrice[1] + 50
IF AdjustMySL2 THEN
SELL AT ExitMyAdjustedLong2 STOP
ENDIF
IF NOT LongOnMarket THEN
AdjustMySL2 = 0
ENDIF
I realize a trailing SL can be used, but I would like to know if the above coding is possible and, if so, if it is coded correctly.
Thanks
Brad
It’s almost correct.
Replace line 2 with:
IF LongOnMarket AND (CLOSE - TradePrice[1]) >= (60 * PipSize) AND AdjustMySL = 0 THEN
and replace line 19 with:
IF LongOnMarket AND (CLOSE - TradePrice[1]) >= (90 * PipSize) AND AdjustMySL2 = 0 THEN
to prevent the first move from being executed again AFTER move two has been triggered.
replace also line 8 with:
IF AdjustMySL AND AdjustMySL2 = 0 THEN
for the same reason.
BradParticipant
Junior
Thanks, Roberto! I’ll give it a shot and let you know how it goes.
Brad
BradParticipant
Junior
Hi Roberto
I have applied the corrections you pointed out. Thank you.
I have now applied the same logic to include a third exit. Do you mind checking if I have written this code correctly, please?
I have also applied a trailing stop as an additional exit condition. Is the code correct in saying that if the price reaches 500 points, a trailing stop of 100 points will start? So if the price moves from 1000 to 1500 and then reverses, the price will exit at 1400?
//First Move 100/50
//When a trade has moved by a set number of points(100), the stop loss is moved to protect a set number of points(50):
IF LongOnMarket AND (CLOSE - TradePrice[1]) >= (100 * PipSize) AND AdjustMySL1 = 0 THEN
AdjustMySL1 = 1
ENDIF
ExitMyAdjustedLong = TradePrice[1] + 50
IF (AdjustMySL1 = 1) AND (AdjustMySL2 = 0) AND (AdjustMySL3 = 0) THEN
SELL AT ExitMyAdjustedLong STOP
ENDIF
IF NOT LongOnMarket THEN
AdjustMySL1 = 0
ENDIF
//Second Move 200/100
//When a trade has moved by a set number of points(200), the stop loss is moved to protect a set number of points(100):
IF LongOnMarket AND (CLOSE - TradePrice[1]) >= (200 * PipSize) AND AdjustMySL2 = 0 THEN
AdjustMySL2 = 1
ENDIF
ExitMyAdjustedLong2 = TradePrice[1] + 100
IF (AdjustMySL2 = 1) AND (AdjustMySL1 = 0) AND (AdjustMySL3 = 0) THEN
SELL AT ExitMyAdjustedLong2 STOP
ENDIF
IF NOT LongOnMarket THEN
AdjustMySL2 = 0
ENDIF
//Third Move 300/150
//When a trade has moved by a set number of points(300), the stop loss is moved to protect a set number of points(150):
IF LongOnMarket AND (CLOSE - TradePrice[1]) >= (300 * PipSize) AND AdjustMySL3 = 0 THEN
AdjustMySL3 = 1
ENDIF
ExitMyAdjustedLong3 = TradePrice[1] + 150
IF (AdjustMySL3 = 1) THEN
SELL AT ExitMyAdjustedLong3 STOP
ENDIF
IF NOT LongOnMarket THEN
AdjustMySL3 = 0
ENDIF
//Trailing Stop. If Price reaches 500 points, a trailing SL of 100 points will trigger.
IF LongOnMarket AND (CLOSE - TradePrice[1]) >= (500 * PipSize) THEN
SET STOP PTRAILING 100
ENDIF
It’s almost correct.
TradePrice[1], despite it may work in some cases, should be replaced by:
TradePrice(1) //or simply TradePrice
then I changed the first 5 lines and one of the IF….
IF NOT LongOnMarket THEN
AdjustMySL1 = 0
AdjustMySL2 = 0
AdjustMySL3 = 0
ENDIF
//First Move 100/50
//When a trade has moved by a set number of points(100), the stop loss is moved to protect a set number of points(50):
IF LongOnMarket AND (CLOSE - TradePrice(1)) >= (100 * PipSize) AND AdjustMySL1 = 0 THEN
AdjustMySL1 = 1
ENDIF
ExitMyAdjustedLong = TradePrice(1) + 50
IF (AdjustMySL1 = 1) AND (AdjustMySL2 = 0) AND (AdjustMySL3 = 0) THEN
SELL AT ExitMyAdjustedLong STOP
ENDIF
//Second Move 200/100
//When a trade has moved by a set number of points(200), the stop loss is moved to protect a set number of points(100):
IF LongOnMarket AND (CLOSE - TradePrice(1)) >= (200 * PipSize) AND AdjustMySL2 = 0 THEN
AdjustMySL2 = 1
ENDIF
ExitMyAdjustedLong2 = TradePrice(1) + 100
IF (AdjustMySL2 = 1) AND (AdjustMySL3 = 0) THEN
SELL AT ExitMyAdjustedLong2 STOP
ENDIF
//Third Move 300/150
//When a trade has moved by a set number of points(300), the stop loss is moved to protect a set number of points(150):
IF LongOnMarket AND (CLOSE - TradePrice(1)) >= (300 * PipSize) AND AdjustMySL3 = 0 THEN
AdjustMySL3 = 1
ENDIF
ExitMyAdjustedLong3 = TradePrice(1) + 150
IF (AdjustMySL3 = 1) THEN
SELL AT ExitMyAdjustedLong3 STOP
ENDIF
//Trailing Stop. If Price reaches 500 points, a trailing SL of 100 points will trigger.
IF LongOnMarket AND (CLOSE - TradePrice(1)) >= (500 * PipSize) THEN
SET STOP PTRAILING 100
ENDIF
BradParticipant
Junior
Thanks, Roberto
I’m assuming the “()” corrections and moving “AdjustMySL1 = 0 and AdjustMySL2 = 0” to the top of the code should be applied to my initial code as well?
Updated ‘additional move’ code:
IF NOT LongOnMarket THEN
AdjustMySL1 = 0
AdjustMySL2 = 0
ENDIF
//When a trade has moved by a set number of points(60), the stop loss is moved to protect a set number of points(35):
IF LongOnMarket AND (CLOSE - TradePrice(1)) >= (60 * PipSize) AND AdjustMySL1 = 0 THEN
AdjustMySL1 = 1
ENDIF
ExitMyAdjustedLong = TradePrice(1) + 35
IF AdjustMySL1 AND (AdjustMySL2 = 0) THEN
SELL AT ExitMyAdjustedLong STOP
ENDIF
//Second Move //When a trade has moved by a set number of points(90), the stop loss is moved to protect a set number of points(50):
IF LongOnMarket AND (CLOSE - TradePrice(1)) >= (90 * PipSize) AND AdjustMySL2 = 0 THEN
AdjustMySL2 = 1
ENDIF
ExitMyAdjustedLong2 = TradePrice(1) + 50
IF AdjustMySL2 THEN
SELL AT ExitMyAdjustedLong2 STOP
ENDIF