Hi, I have the code below but when the trade was in profit this week, the code did not protect the number of points I stated. Any idea on what maybe wrong with the code below?
I would also like to add a trailing stop ie if the trade is in profit 1 point greater than when the breakeven point starts, i’d like the stop loss (points to keep) to increase by 1 point too; and to continue to do so until target is met. Any thoughts on how to introduce this code would be appreciated.
Thanks
// Definition of code parameters
DEFPARAM CumulateOrders = False // Cumulating positions deactivated
Once LastTradeDate = 0
// Prevents the system from creating new orders to enter the market or increase position size before the specified time
noEntryBeforeTime = 090000
timeEnterBefore = time >= noEntryBeforeTime
// Prevents the system from placing new orders to enter the market or increase position size after the specified time
noEntryAfterTime = 160000
timeEnterAfter = time < noEntryAfterTime
// Prevents the system from placing new orders on specified days of the week
daysForbiddenEntry = OpenDayOfWeek = 6 OR OpenDayOfWeek = 0
//how much pips/points in gain to activate the breakeven function?
startBreakeven = 20
//how much pips/points to keep in profit above or below our entry price when the breakeven is activated (beware of spread)
PointsToKeep = 15
// Conditions to enter long positions
indicator1 = RSI[7](close)
c1 = (indicator1 >= 83)
ignored, ignored, ignored, ignored, indicator2, ignored, ignored = CALL "WA Explosion COMBO"[150, 30, 15, 15]
c2 = (indicator2 >= 260)
indicator3 = ExponentialAverage[5](close)
indicator4 = ExponentialAverage[17](close)
c3 = (indicator3 > indicator4)
indicator5 = ExponentialAverage[62](close)
indicator6 = ExponentialAverage[5](close)
c4 = (indicator5 < indicator6)
IF (c1 AND c2 AND c3 AND c4) AND timeEnterBefore AND timeEnterAfter AND not daysForbiddenEntry AND LastTradeDate<>OpenDate THEN
BUY 1 CONTRACT AT MARKET
LastTradeDate = OpenDate
ENDIF
// Conditions to enter short positions
indicator7 = RSI[7](close)
c5 = (indicator7 <= 22)
ignored, ignored, ignored, ignored, ignored, indicator8, ignored = CALL "WA Explosion COMBO"[150, 30, 15, 15]
c6 = (indicator8 >= 260)
indicator9 = ExponentialAverage[5](close)
indicator10 = ExponentialAverage[17](close)
c7 = (indicator9 < indicator10)
indicator11 = ExponentialAverage[62](close)
indicator12 = ExponentialAverage[5](close)
c8 = (indicator11 > indicator12)
IF (c5 AND c6 AND c7 AND c8) AND timeEnterBefore AND timeEnterAfter AND not daysForbiddenEntry AND LastTradeDate<>OpenDate THEN
SELLSHORT 1 CONTRACT AT MARKET
LastTradeDate = OpenDate
ENDIF
// Stops and targets
SET STOP pLOSS 14
SET TARGET pPROFIT 25
//reset the breakevenLevel when no trade are on market
IF NOT ONMARKET THEN
breakevenLevel=0
ENDIF
// --- BUY SIDE ---
//test if the price have moved favourably of "startBreakeven" points already
IF LONGONMARKET AND close-tradeprice(1)>=startBreakeven*pipsize THEN
//calculate the breakevenLevel
breakevenLevel = tradeprice(1)+PointsToKeep*pipsize
ENDIF
//place the new stop orders on market at breakevenLevel
IF breakevenLevel>0 THEN
SELL AT breakevenLevel STOP
ENDIF
// --- end of BUY SIDE ---
// --- SELL SIDE ---
IF SHORTONMARKET AND close-tradeprice(1)-close>startBreakeven*pipsize THEN
//calculate the breakevenLevel
breakevenLevel = tradeprice(1)-PointsToKeep*pipsize
ENDIF
//place the new stop orders on market at breakevenLevel
IF breakevenLevel>0 THEN
EXITSHORT AT breakevenLevel STOP
ENDIF
// --- end of SELL SIDE ---
Is the WA Explosion COMBO the Indicator below please?
https://www.prorealcode.com/prorealtime-indicators/waddah-attar-explosion/
I’m going to run it on my Platform and try to see what is going on.
I’ve never had much success with Trailing Stops, but maybe it’s because they didn’t work and I’ve never bothered to investigate so I though your code would be a good one to collaborate on! 🙂
But please anybody else contribute also as I often get my understanding by experimenting (using GRAPH etc) whereas you coding wizards may spot the issue from just looking at the above code … so please feel free to dive in! 🙂
Thanks for the reply,
Thats not the code I have for the explosion combo, I have the one attached.
I found this trailing stop with incremental steps below, I do not know if it works but when I backtested it on FTSE, 1 trade showed up as b/e. I thought it should have shown up +10 as the trade went up +20 before falling back to b/e.
Thanks
// Definition of code parameters
DEFPARAM CumulateOrders = False // Cumulating positions deactivated
Once LastTradeDate = 0
// Prevents the system from creating new orders to enter the market or increase position size before the specified time
noEntryBeforeTime = 090000
timeEnterBefore = time >= noEntryBeforeTime
// Prevents the system from placing new orders to enter the market or increase position size after the specified time
noEntryAfterTime = 160000
timeEnterAfter = time < noEntryAfterTime
// Prevents the system from placing new orders on specified days of the week
daysForbiddenEntry = OpenDayOfWeek = 6 OR OpenDayOfWeek = 0
// Conditions to enter long positions
indicator1 = RSI[7](close)
c1 = (indicator1 >= 83)
ignored, ignored, ignored, ignored, indicator2, ignored, ignored = CALL "WA Explosion COMBO"[150, 30, 15, 15]
c2 = (indicator2 >= 260)
indicator3 = ExponentialAverage[5](close)
indicator4 = ExponentialAverage[17](close)
c3 = (indicator3 > indicator4)
indicator5 = ExponentialAverage[62](close)
indicator6 = ExponentialAverage[5](close)
c4 = (indicator5 < indicator6)
IF (c1 AND c2 AND c3 AND c4) AND timeEnterBefore AND timeEnterAfter AND not daysForbiddenEntry AND LastTradeDate<>OpenDate THEN
BUY 1 CONTRACT AT MARKET
LastTradeDate = OpenDate
ENDIF
// Conditions to enter short positions
indicator7 = RSI[7](close)
c5 = (indicator7 <= 22)
ignored, ignored, ignored, ignored, ignored, indicator8, ignored = CALL "WA Explosion COMBO"[150, 30, 15, 15]
c6 = (indicator8 >= 260)
indicator9 = ExponentialAverage[5](close)
indicator10 = ExponentialAverage[17](close)
c7 = (indicator9 < indicator10)
indicator11 = ExponentialAverage[62](close)
indicator12 = ExponentialAverage[5](close)
c8 = (indicator11 > indicator12)
IF (c5 AND c6 AND c7 AND c8) AND timeEnterBefore AND timeEnterAfter AND not daysForbiddenEntry AND LastTradeDate<>OpenDate THEN
SELLSHORT 1 CONTRACT AT MARKET
LastTradeDate = OpenDate
ENDIF
// Stops and targets
SET STOP pLOSS 14
SET TARGET pPROFIT 25
//************************************************************************
// trailing stop function (fixed after breakeven)
//reset the stoploss value
IF NOT ONMARKET THEN
trailingstart = 10 //10 trailing will start @trailinstart points profit
trailingstep = 5 //5 trailing step to move the "stoploss"
initialSL = 14 //14 initial SL when a trade is entered
newSL = 0
ENDIF
//manage long positions
IF LONGONMARKET THEN
CurrentProfit = close - tradeprice(1)
//first move
IF newSL = 0 AND CurrentProfit >= (trailingstart * pipsize) THEN
newSL = tradeprice(1) + (trailingstep * pipsize) - (initialSL * pipsize)
InitialSL = initialSL - (trailingstep * pipsize)
ENDIF
//next moves
IF newSL > 0 AND (close - newSL) >= (trailingstep * pipsize) AND initialSL > 0 THEN
newSL = newSL + min(trailingstep * pipsize,initialSL * pipsize)
initialSL = max(0,initialSL - (trailingstep * pipsize))
ENDIF
ENDIF
//manage short positions
IF SHORTONMARKET THEN
CurrentProfit = tradeprice(1) - close
//first move
IF newSL = 0 AND CurrentProfit >= (trailingstart * pipsize) THEN
newSL = tradeprice(1) - (trailingstep * pipsize) + (initialSL * pipsize)
InitialSL = initialSL - (trailingstep * pipsize)
ENDIF
//next moves
IF newSL > 0 AND (newSL - close) >= (trailingstep * pipsize) AND initialSL > 0 THEN
newSL = newSL - min(trailingstep * pipsize,initialSL * pipsize)
initialSL = max(0,initialSL - (trailingstep * pipsize))
ENDIF
ENDIF
//stop order to exit the positions
IF newSL > 0 THEN
SELL AT newSL STOP
EXITSHORT AT newSL STOP
ENDIF
You are now making it more difficult / confusing as you now have two different codes for Trailing Stop issues … in the same Topic.
You would have been better opening a new Topic for the second Trailing Stop code issue so then it keeps it simple and the reading quicker etc for folks wanting to help .
Thanks for WA Explosion Combo, I will run it later, but I have now run out time now after I had to put both your codes above into the diff checker below to check out the differences! 🙂
https://www.diffchecker.com/diff
Line 82 in the code in your first post makes no sense.
IF SHORTONMARKET AND close-tradeprice(1)-close>startBreakeven*pipsize THEN
should be changed to this:
IF SHORTONMARKET AND tradeprice(1)-close >= startBreakeven*pipsize THEN
Thank you both.
Apologies I thought I was making it easier by introducing another type of code (sorry new to this).
Will try that amendment Vonasi Thanks.
Adeel