Thank you – very helpful.
I am trying to write a simple code that has a 50 point stop and a 100 point target. If the trade goes 50 points in my favour, I want to move the stop loss to breakeven – but it doesnt seem to work. see below – ignore the strategy it is not a good one and apologies for the messy code:
//————————————————————————-
// Main code : US Tech – 3 mins
//————————————————————————-
// Definition of code parameters
DEFPARAM CumulateOrders = False // Cumulating positions deactivated
// Prevents the system from creating new orders to enter the market or increase position size before the specified time
noEntryBeforeTime = 153300
timeEnterBefore = time >= noEntryBeforeTime
// Prevents the system from placing new orders to enter the market or increase position size after the specified time
noEntryAfterTime = 153600
timeEnterAfter = time < noEntryAfterTime
// Prevents the system from placing new orders on specified days of the week
daysForbiddenEntry = OpenDayOfWeek = 6 OR OpenDayOfWeek = 0
triggerDistance = 30
if not onmarket then
MoveToBreakEven=0
endif
// Conditions to enter long positions
indicator1 = SAR[0.02,0.02,0.2]
c1 = (low > indicator1)
c2 = (open < close)
c3 = (close[1] > open[1])
c4 = Close – open > 25
c6 = high – close < 35
IF (c1 and c2 and c4 and c6) AND timeEnterBefore AND timeEnterAfter AND not daysForbiddenEntry THEN
BUY 1 PERPOINT AT MARKET
stoploss = close-triggerDistance*pointsize
newStopLevel = close + (triggerDistance * pointsize)
set stop ploss 30
SET TARGET pPROFIT 60
ENDIF
// Conditions to enter short positions
indicator2 = SAR[0.02,0.02,0.2]
c7 = (high < indicator2)
c8 = (close < open)
c9 = (close[1] < open[1])
c10 = Open – close > 25
c12 = close – low < 35
IF (c7 and c8 and c10 and c12) AND timeEnterBefore AND timeEnterAfter AND not daysForbiddenEntry THEN
SELLSHORT 1 PERPOINT AT MARKET
stoploss1 = close+triggerDistance*pointsize
newStopLevel1 = close – (triggerDistance * pointsize)
set stop ploss 30
SET TARGET pPROFIT 60
ENDIF
// Management of open position
IF LONGONMARKET THEN
// Move stop to breakeven
IF close >= newStopLevel and MoveToBreakEven=0 THEN
MoveToBreakEven=1
stoploss=positionprice //breakeven
newStopLevel = newStopLevel+triggerDistance*pointsize
set stop price stoploss
endif
IF shortONMARKET THEN
// Move stop to breakeven
IF close <= newStopLevel1 and MoveToBreakEven=0 THEN
MoveToBreakEven=1
stoploss1=positionprice //breakeven
newStopLevel1 = newStopLevel1-triggerDistance*pointsize
set stop price stoploss1
endif