Breakout strategy – coding help
Forums › ProRealTime English forum › ProOrder support › Breakout strategy – coding help
- This topic has 4 replies, 3 voices, and was last updated 4 years ago by GraHal.
-
-
02/18/2017 at 6:13 PM #25518
Hi all,
First post here. I’ve been trying to code a breakout strategy, but I haven’t been able to get all of it to work. I have searched around the site for any useful code posted by others, but haven’t found anything that does what I need it to. Details are below:
- GBP/USD 15 mins
- Breakout box for 4am-6am (GMT) including 6am bar
- If EMA89 crosses inside this box, no trades that day
- Order closed if not filled by 4:30PM
- Flat after 9:45pm
Entry
- If EMA89 is always above breakout box in this time period, sell 5 pips below the low of breakout box
- If EMA89 is always below breakout box in this time period, buy 5 pips above the high of breakout box
Exit
- Target: 75 pips
- Stop: 20 pips. Stop moved to breakeven when profit is 20 pips, and trailed to halfway between high and breakeven.
Is it possible to put this strategy into code? I can give more detail if necessary
Kind regards,
Carl
09/26/2020 at 12:24 AM #145431There you go:
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566DEFPARAM CumulateOrders = falseDEFPARAM FlatAfter = 214500ONCE Offset = 5 * PipSizeONCE TP = 75ONCE SL = 20IF Not OnMarket THENNewSL = 0ENDIFIF LongOnMarket THENEmaBelow = 0ENDIFIF ShortOnMarket THENEmaAbove = 0ENDIFIF OpenTime >= 040000 AND OpenTime <= 060000 THENEma89 = average[89,1](close)IF OpenTime = 040000 THENHH = highLL = lowEmaBelow = 1EmaAbove = 1ENDIFHH = max(HH,high)LL = min(LL,low)IF Ema89 <= HH THENEmaAbove = 0ELSIF Ema89 >= LL THENEmaBelow = 0ENDIFENDIFTradeON = OpenTime >= 060000 AND OpenTime <= 163000IF TradeON AND Not OnMarket AND EmaAbove THENSELLSHORT 1 Contract AT LL - Offset STOPSET TARGET pPROFIT TPSET STOP pLOSS SLENDIFIF TradeON AND Not OnMarket AND EmaBelow THENBUY 1 Contract AT HH + Offset STOPSET TARGET pPROFIT TPSET STOP pLOSS SLENDIFIF LongOnMarket THENMyProfit = close - PositionPriceIF MyProfit >= SL * PipSize THENIF NewSL = 0 THENNewSL = PositionPriceELSETempSL= PositionPrice + (abs(close - PositionPrice) / 2)NewSL = max(NewSL,TempSL)ENDIFENDIFELSIF ShortOnMarket THENMyProfit = PositionPrice - closeIF MyProfit >= SL * PipSize THENIF NewSL = 0 THENNewSL = PositionPriceELSETempSL= PositionPrice - (abs(PositionPrice - close) / 2)NewSL = min(NewSL,TempSL)ENDIFENDIFENDIFIF Onmarket AND NewSL > 0 THENSELL AT NewSL STOPEXITSHORT AT NewSL STOPENDIF2 users thanked author for this post.
10/03/2020 at 4:12 PM #146282Thanks Roberto, this is great. I’ve been playing around with this to improve performance (now on 1hr timeframe, added code to reinvest profits, etc), but I now want to limit this to only 2 orders per day. The system will currently take the same trade again on the same day if it has already opened and closed a trade. I’ve added some code I found here https://www.prorealcode.com/topic/3-trades-per-day/ but this hasn’t made a difference. Can you see what I’ve done wrong?
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879DEFPARAM CumulateOrders = falseDEFPARAM FlatAfter = 204500ONCE Offset = 5 * PipSizeONCE TP = 80ONCE SL = 12IF Not OnMarket THENNewSL = 0ENDIFREM Money ManagementCapital = 20000Risk = 0.01 // = % RiskREM Calculate contractsequity = Capital + StrategyProfitmaxrisk = round(equity*Risk)PositionSize = abs(round((maxrisk/SL)/PointValue*100)*pipsize/100)IF LongOnMarket THENEmaBelow = 0ENDIFIF ShortOnMarket THENEmaAbove = 0ENDIFIF OpenTime >= 040000 AND OpenTime < 060000 THENEma89 = average[89,1](close)IF OpenTime = 040000 THENHH = highLL = lowEmaBelow = 1EmaAbove = 1ENDIFHH = max(HH,high)LL = min(LL,low)IF Ema89 <= HH THENEmaAbove = 0ELSIF Ema89 >= LL THENEmaBelow = 0ENDIFENDIFIF intradaybarindex=0 or day<>day[1] thencount=0ENDIFIF ( (Not OnMarket AND OnMarket[1] AND Not OnMarket[2]) OR (tradeindex(1)=tradeindex(2) and tradeindex(1)=barindex[1] and tradeindex(1)>0) or (not onmarket and onmarket[1])) thencount=count+1endifTradeON = OpenTime >= 060000 AND OpenTime <= 163000IF TradeON AND Not OnMarket AND EmaAbove AND Count<3 THENSELLSHORT PositionSize Contract AT LL - Offset STOPSET TARGET pPROFIT TPSET STOP pLOSS SLENDIFIF TradeON AND Not OnMarket AND EmaBelow AND Count<3 THENBUY PositionSize Contract AT HH - Offset STOPSET TARGET pPROFIT TPSET STOP pLOSS SLENDIFIF LongOnMarket THENMyProfit = close - PositionPriceIF MyProfit >= SL * PipSize THENIF NewSL = 0 THENNewSL = PositionPriceELSETempSL= PositionPrice + (abs(close - PositionPrice) / 2)NewSL = max(NewSL,TempSL)ENDIFENDIFELSIF ShortOnMarket THENMyProfit = PositionPrice - closeIF MyProfit >= SL * PipSize THENIF NewSL = 0 THENNewSL = PositionPriceELSETempSL= PositionPrice - (abs(PositionPrice - close) / 2)NewSL = min(NewSL,TempSL)ENDIFENDIFENDIFIF Onmarket AND NewSL > 0 THENSELL AT NewSL STOPEXITSHORT AT NewSL STOPENDIF10/04/2020 at 8:30 AM #146315Try replacing line 41 with:
123If STRATEGYPROFIT <> STRATEGYPROFIT[1] ThenCount = Count + 1Endifand use Count <2 at lines 45 and 50.
Using <3 will set your max number of trades to 3.
1 user thanked author for this post.
10/04/2020 at 9:40 AM #146325Link to above added as Log 248 to here …
1 user thanked author for this post.
-
AuthorPosts
Find exclusive trading pro-tools on