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
There you go:
DEFPARAM CumulateOrders = false
DEFPARAM FlatAfter = 214500
ONCE Offset = 5 * PipSize
ONCE TP = 75
ONCE SL = 20
IF Not OnMarket THEN
NewSL = 0
ENDIF
IF LongOnMarket THEN
EmaBelow = 0
ENDIF
IF ShortOnMarket THEN
EmaAbove = 0
ENDIF
IF OpenTime >= 040000 AND OpenTime <= 060000 THEN
Ema89 = average[89,1](close)
IF OpenTime = 040000 THEN
HH = high
LL = low
EmaBelow = 1
EmaAbove = 1
ENDIF
HH = max(HH,high)
LL = min(LL,low)
IF Ema89 <= HH THEN
EmaAbove = 0
ELSIF Ema89 >= LL THEN
EmaBelow = 0
ENDIF
ENDIF
TradeON = OpenTime >= 060000 AND OpenTime <= 163000
IF TradeON AND Not OnMarket AND EmaAbove THEN
SELLSHORT 1 Contract AT LL - Offset STOP
SET TARGET pPROFIT TP
SET STOP pLOSS SL
ENDIF
IF TradeON AND Not OnMarket AND EmaBelow THEN
BUY 1 Contract AT HH + Offset STOP
SET TARGET pPROFIT TP
SET STOP pLOSS SL
ENDIF
IF LongOnMarket THEN
MyProfit = close - PositionPrice
IF MyProfit >= SL * PipSize THEN
IF NewSL = 0 THEN
NewSL = PositionPrice
ELSE
TempSL= PositionPrice + (abs(close - PositionPrice) / 2)
NewSL = max(NewSL,TempSL)
ENDIF
ENDIF
ELSIF ShortOnMarket THEN
MyProfit = PositionPrice - close
IF MyProfit >= SL * PipSize THEN
IF NewSL = 0 THEN
NewSL = PositionPrice
ELSE
TempSL= PositionPrice - (abs(PositionPrice - close) / 2)
NewSL = min(NewSL,TempSL)
ENDIF
ENDIF
ENDIF
IF Onmarket AND NewSL > 0 THEN
SELL AT NewSL STOP
EXITSHORT AT NewSL STOP
ENDIF
Thanks 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?
DEFPARAM CumulateOrders = false
DEFPARAM FlatAfter = 204500
ONCE Offset = 5 * PipSize
ONCE TP = 80
ONCE SL = 12
IF Not OnMarket THEN
NewSL = 0
ENDIF
REM Money Management
Capital = 20000
Risk = 0.01 // = % Risk
REM Calculate contracts
equity = Capital + StrategyProfit
maxrisk = round(equity*Risk)
PositionSize = abs(round((maxrisk/SL)/PointValue*100)*pipsize/100)
IF LongOnMarket THEN
EmaBelow = 0
ENDIF
IF ShortOnMarket THEN
EmaAbove = 0
ENDIF
IF OpenTime >= 040000 AND OpenTime < 060000 THEN
Ema89 = average[89,1](close)
IF OpenTime = 040000 THEN
HH = high
LL = low
EmaBelow = 1
EmaAbove = 1
ENDIF
HH = max(HH,high)
LL = min(LL,low)
IF Ema89 <= HH THEN
EmaAbove = 0
ELSIF Ema89 >= LL THEN
EmaBelow = 0
ENDIF
ENDIF
IF intradaybarindex=0 or day<>day[1] then
count=0
ENDIF
IF ( (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])) then
count=count+1
endif
TradeON = OpenTime >= 060000 AND OpenTime <= 163000
IF TradeON AND Not OnMarket AND EmaAbove AND Count<3 THEN
SELLSHORT PositionSize Contract AT LL - Offset STOP
SET TARGET pPROFIT TP
SET STOP pLOSS SL
ENDIF
IF TradeON AND Not OnMarket AND EmaBelow AND Count<3 THEN
BUY PositionSize Contract AT HH - Offset STOP
SET TARGET pPROFIT TP
SET STOP pLOSS SL
ENDIF
IF LongOnMarket THEN
MyProfit = close - PositionPrice
IF MyProfit >= SL * PipSize THEN
IF NewSL = 0 THEN
NewSL = PositionPrice
ELSE
TempSL= PositionPrice + (abs(close - PositionPrice) / 2)
NewSL = max(NewSL,TempSL)
ENDIF
ENDIF
ELSIF ShortOnMarket THEN
MyProfit = PositionPrice - close
IF MyProfit >= SL * PipSize THEN
IF NewSL = 0 THEN
NewSL = PositionPrice
ELSE
TempSL= PositionPrice - (abs(PositionPrice - close) / 2)
NewSL = min(NewSL,TempSL)
ENDIF
ENDIF
ENDIF
IF Onmarket AND NewSL > 0 THEN
SELL AT NewSL STOP
EXITSHORT AT NewSL STOP
ENDIF
Try replacing line 41 with:
If STRATEGYPROFIT <> STRATEGYPROFIT[1] Then
Count = Count + 1
Endif
and use Count <2 at lines 45 and 50.
Using <3 will set your max number of trades to 3.
Link to above added as Log 248 to here …
Snippet Link Library