Breakout strategy – coding help

Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • #25518 quote
    cbrycey23cbrycey23
    Participant
    Average

    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

    #145431 quote
    robertogozzirobertogozzi
    Moderator
    Legend

    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
    #146282 quote
    cbrycey23cbrycey23
    Participant
    Average

    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
    
    #146315 quote
    robertogozzirobertogozzi
    Moderator
    Legend

    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.

    cbrycey23 thanked this post
    #146325 quote
    GraHalGraHal
    Participant
    Legend

    Link to above added as Log 248 to here …

    Snippet Link Library

    robertogozzi thanked this post
Viewing 5 posts - 1 through 5 (of 5 total)
  • You must be logged in to reply to this topic.
ProRealAI ProRealAI New

Stuck on this ProBuilder code?

Describe what this topic is trying to build, in plain English, and ProRealAI writes the ProRealTime™ indicator, screener or system for you.

Available in 7 languages
Try ProRealAI

Breakout strategy – coding help


ProOrder: Automated Strategies & Backtesting

New Reply
Author
author-avatar
cbrycey23 @cbrycey23 Participant
Summary

This topic contains 4 replies,
has 3 voices, and was last updated by GraHalGraHal
6 years ago.

Topic Details
Forum: ProOrder: Automated Strategies & Backtesting
Language: English
Started: 02/18/2017
Status: Active
Attachments: No files
ProRealCode ProRealCode
Loading...