Hi,
I am trying to write a simple code to move my stop loss to breakeven once I am 50 ticks in profit, but nothing I try seems to work.
for example, I want to set my stop loss to 50 points and then move it to breakeven once my trade moves 50 points in profit.
It should be simple but for some reason it doesnt work when I try different codes to write it.
any help would be appreciated.
Hello good. Here you have an example.
// ===========================
// Trail Stop steps
// ===========================
triggerDistance = 50
if not onmarket then
MoveToBreakEven=0
endif
// Example entry/exit condition
conditionsEntry=close CROSSES OVER Average[20](close)
conditionsExit=close CROSSES UNDER Average[20](close)
IF NOT ONMARKET AND conditionsEntry THEN
BUY 1 CONTRACT AT MARKET
stoploss = close-triggerDistance*pointsize
newStopLevel = close + (triggerDistance * pointsize)
set stop price stoploss
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
// Trail stop
// if close >= newStopLevel + (triggerDistance * pointsize) and MoveToBreakEven=1 then
// stoploss = newStopLevel
// newStopLevel = newStopLevel+triggerDistance*pointsize
// set stop price stoploss
// endif
// Exit
if conditionsExit then
sell at market
endif
ENDIF
// ===========================
// Graphic
// ===========================
// Trailing Stop and Next Level
graphonprice newStopLevel coloured(0, 0, 255) AS "NextLevel"
graphonprice stoploss coloured(255, 0, 0) AS "Trail Stop"
// Moving Average
graphonprice Average[20](close) coloured(255, 165, 0) AS "SMA"
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
I changed your code so that it now has 50-point SL and 100-point TP. In addition I added a couple of ENDIFs at the end to run it properly.
I also made a few further changes:
//————————————————————————-
// 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 = 50
once MoveToBreakEven=0
// 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 50
SET TARGET pPROFIT 100
MoveToBreakEven=0
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 50
SET TARGET pPROFIT 100
MoveToBreakEven=0
ENDIF
// Management of open position
IF LONGONMARKET THEN
// Move stop to breakeven
IF ((close - TradePrice) >= triggerDistance*pointsize) and MoveToBreakEven=0 THEN
MoveToBreakEven=1
set stop breakeven
endif
endif
IF shortONMARKET THEN
// Move stop to breakeven
IF ((TradePrice - close) >= triggerDistance*pointsize) and MoveToBreakEven=0 THEN
MoveToBreakEven=1
set stop breakeven
endif
endif