Inquiry coding inside bar momentum strategy

Viewing 15 posts - 1 through 15 (of 15 total)
  • Author
    Posts
  • #175909 quote
    phoentzs
    Participant
    Master

    Hello, I have a request to code the Inside Bar Momentum strategy of baby pips. The following link contains the exact rules. Can someone code that for me, please?

    https://www.babypips.com/trading/new-forex-system-inside-bar-momentum-strategy

    #175930 quote
    robertogozzi
    Moderator
    Master

    There you go (not tested, though):

    Once EntryL = 0
    Once EntryS = 0
    Bullish =  close > open
    Bearish = close < open
    Inside   = (high <= high[1]) and (low >= low[1]) and (range < range[1])
    //Inside   = (high < high[1]) and (low > low[1])
    If Inside then
       EntryL = high[1] + (range * 0.1)
       EntryS = low[1]   - (range * 0.1)
       slL     = abs(EntryL - (high[1] - (range * 0.2)))
       slS     = abs(EntryS - (low[1] + (range * 0.2)))
       tpL    = abs(EntryL - (high[1] + (range * 0.8)))
       tpS    = abs(EntryS - (low[1]  - (range * 0.8)))
    Endif
    If Not OnMarket And EntryL Then
       Buy 1 contract at EntryL STOP
       Sellshort 1 contract at EntryS STOP
       // initially set a unique SL & TP to
       // the max of the two
       Set Target Profit max(tpL,tpS)
       Set Stop   Loss  max(slL,slS)
    Else
       // set real SL and TP after first bar on market
       If ShortOnMarket then
          Set Target Profit tpS
          Set Stop   Loss   slS
       Else
          Set Target Profit tpL
          Set Stop   Loss   slL
       Endif
       EntryL = 0
       EntryS = 0
       // close if another INSIDE bar shows
       If Inside then
          SELLSHORT at Market
          SELL      at Market
       Endif
    Endif
    PLermite thanked this post
    #175931 quote
    robertogozzi
    Moderator
    Master

    I coded  two different ways to define an INSIDE bar.

    Line 5 (which I used) requires than only one among prior Low and High be greater/lower, the other one can be EQUAL.

    Line 6 (commented) requires that BOTH prior High and Low be greater/lower.

    You can uncomment line 6 and comment out line 5, if preferred.

    GraHal thanked this post
    #175939 quote
    phoentzs
    Participant
    Master

    That’s very good. Thanks! This is exactly what I imagined. Of course, when I start it in an H4 timeframe, I don’t have a TP or SL for 4 hours which can be fatal at times. Can I also use these terms as an MTF? H4 in M1? Then I would have a maximum of 1 minute in the M1 timeframe without SL and TP.

    #175955 quote
    robertogozzi
    Moderator
    Master

    You HAVE a TP & SL, though it can be slightly higher and will be updated the next bar.

    #175964 quote
    phoentzs
    Participant
    Master

    Oh, thank you for the hint.

    #175996 quote
    phoentzs
    Participant
    Master

    On the one hand, the instructions “bullish” and “bearish” must still be included in the input conditions. Nonetheless, this type of system seems to work quite well in manual trading, but not really functional as an algo. At least I haven’t gotten it profitable until now. Not even with MTF. I think range breakouts are easier to automate than individual candles. At least not with stop orders. Unless someone has another idea?

    #176005 quote
    robertogozzi
    Moderator
    Master

    I usually add them, as they are no more returned as an error (like it was in v10.3). You can safely remove lines 3-4.

    #176008 quote
    phoentzs
    Participant
    Master

    Did you have a positive result in your backtest?

    #176016 quote
    robertogozzi
    Moderator
    Master

    No, not good results.

    I tried changing some lines.

    Lines 10-11 to set the SL to the previous LOW/HIGH:

    slL    = abs(EntryL - (low[1]  - (range * 0.2)))
    slS    = abs(EntryS - (high[1] + (range * 0.2)))

    Lines 12-13 to set a TP as a factor of SL:

    tpL    = slL * 3//abs(EntryL - (high[1] + (range * 0.8)))
    tpS    = slS * 3//abs(EntryS - (low[1]  - (range * 0.8)))

    and commented out lines 35-36, so that the strategy doesn’t exit on a subsequent INSIDE bar.

    Positive results (without a trailing stop) a Daily TF (200K units and 2-pip spread, I did not change it each time) are best on AudJpy, AudNzd, AudUsd, CadJpy, EurAud, EurUsd, GbpJpy, GbpUsd, Italy 40, NzdJpy and UsdChf.

    Once EntryL = 0
    Once EntryS = 0
    //Bullish   = close > open
    //Bearish   = close < open
    Inside  = (high <= high[1]) and (low >= low[1]) and (range < range[1])
    //Inside   = (high < high[1]) and (low > low[1])
    If Inside then
    EntryL = high[1] + (range * 0.1)
    EntryS = low[1]  - (range * 0.1)
    slL    = abs(EntryL - (low[1]  - (range * 0.2)))
    slS    = abs(EntryS - (high[1] + (range * 0.2)))
    tpL    = slL * 3//abs(EntryL - (high[1] + (range * 0.8)))
    tpS    = slS * 3//abs(EntryS - (low[1]  - (range * 0.8)))
    Endif
    If Not OnMarket And EntryL Then
    Buy       1 contract at EntryL STOP
    Sellshort 1 contract at EntryS STOP
    // initially set a unique SL & TP to
    // the max of the two
    Set Target Profit max(tpL,tpS)
    Set Stop   Loss  max(slL,slS)
    Else
    // set real SL and TP after first bar on market
    If ShortOnMarket then
    Set Target Profit tpS
    Set Stop   Loss   slS
    Else
    Set Target Profit tpL
    Set Stop   Loss   slL
    Endif
    EntryL = 0
    EntryS = 0
    // close if another INSIDE bar shows
    If Inside then
    //SELLSHORT at Market
    //SELL      at Market
    Endif
    Endif
    #176017 quote
    robertogozzi
    Moderator
    Master

    Adding this trailing stop improved results slightly:

    //*********************************************************************************
    // https://www.prorealcode.com/blog/trading/complete-trailing-stop-code-function/
    // (righe 17- 56) with the addition of DISTANCE
    //
    //trailing stop function
    IF (BarIndex - TradeIndex) >= 0 THEN                                 //0
    trailingstart = 40   //40 trailing will start @trailinstart points profit
    trailingstep  = 10   //10 trailing step to move the "stoploss"
    distance      = 10   //10 pips distance from caurrent price (if required by the broker)
    //reset the stoploss value
    IF NOT ONMARKET THEN
    newSL=0
    ENDIF
    
    //manage long positions
    IF LONGONMARKET THEN
    //first move (breakeven)
    IF newSL=0 AND close-tradeprice(1)>=trailingstart*pipsize THEN
    newSL = tradeprice(1)+trailingstep*pipsize
    ENDIF
    //next moves
    IF newSL>0 AND close-newSL>=trailingstep*pipsize THEN
    newSL = newSL+trailingstep*pipsize
    ENDIF
    ENDIF
    
    //manage short positions
    IF SHORTONMARKET THEN
    //first move (breakeven)
    IF newSL=0 AND tradeprice(1)-close>=trailingstart*pipsize THEN
    newSL = tradeprice(1)-trailingstep*pipsize
    ENDIF
    //next moves
    IF newSL>0 AND newSL-close>=trailingstep*pipsize THEN
    newSL = newSL-trailingstep*pipsize
    ENDIF
    ENDIF
    
    //stop order to exit the positions
    IF newSL>0 THEN
    IF LongOnMarket THEN
    IF (close + distance) > newSL THEN
    SELL AT newSL STOP
    ELSIF (close - distance) < newSL THEN
    SELL AT newSL LIMIT
    ELSE
    SELL AT Market
    ENDIF
    ELSIF ShortOnmarket THEN
    IF (close + distance) < newSL THEN
    EXITSHORT AT newSL STOP
    ELSIF (close - distance) > newSL THEN
    EXITSHORT AT newSL LIMIT
    ELSE
    EXITSHORT AT Market
    ENDIF
    ENDIF
    ENDIF
    ENDIF
    //*********************************************************************************
    #176019 quote
    phoentzs
    Participant
    Master

    Thanks very much. I’ll have a look at myself tonight.

    #177129 quote
    GraHal
    Participant
    Master

    Using the code on the link below, I am getting rejected loads for being too close / less than IG minimum etc.

    Inquiry coding inside bar momentum strategy

    What can I add so the code is not rejcted please?

    PS

    I even ticked the Guaranteed box and the IG can move stops box but it made no difference … still got rejected for beng too close to price etc

    #177133 quote
    robertogozzi
    Moderator
    Master

    You have to check, each time a pending order is to be placed, whether its entry price sits within the rquired distance from the current price (CLOSE).
    I have added DISTANCE, as first line, to which you want to assign the distance (in Pips) required by IG for the traded instrument/asset:

    Once Distance = 10 * PipSize
    Once EntryL   = 0
    Once EntryS   = 0
    Bullish       =  close > open
    Bearish       = close < open
    Inside        = (high <= high[1]) and (low >= low[1]) and (range < range[1])
    //Inside      = (high < high[1]) and (low > low[1])
    If Inside then
    EntryL = high[1] + (range * 0.1)
    EntryS = low[1]   - (range * 0.1)
    slL     = abs(EntryL - (high[1] - (range * 0.2)))
    slS     = abs(EntryS - (low[1] + (range * 0.2)))
    tpL    = abs(EntryL - (high[1] + (range * 0.8)))
    tpS    = abs(EntryS - (low[1]  - (range * 0.8)))
    Endif
    If Not OnMarket And EntryL Then
    // LONG  entry
    IF close > (EntryL + Distance) THEN
    Buy 1 contract at EntryL LIMIT
    ELSIF close < (EntryL - Distance) THEN
    Buy 1 contract at EntryL STOP
    ELSE
    Buy 1 contract at Market
    ENDIF
    // SHORT entry
    IF close > (EntryS + Distance) THEN
    Sellshort 1 contract at EntryS STOP
    ELSIF close < (EntryS - Distance) THEN
    Sellshort 1 contract at EntryS LIMIT
    ELSE
    Sellshort 1 contract at Market
    ENDIF
    // initially set a unique SL & TP to
    // the max of the two
    Set Target Profit max(tpL,tpS)
    Set Stop   Loss  max(slL,slS)
    Else
    // set real SL and TP after first bar on market
    If ShortOnMarket then
    Set Target Profit tpS
    Set Stop   Loss   slS
    Else
    Set Target Profit tpL
    Set Stop   Loss   slL
    Endif
    EntryL = 0
    EntryS = 0
    // close if another INSIDE bar shows
    If Inside then
    SELLSHORT at Market
    SELL      at Market
    Endif
    Endif
    GraHal thanked this post
    #177134 quote
    robertogozzi
    Moderator
    Master

    If you don’t want to enter at market because you may refer to wait longer for the proper conditions for a pending order, simply comment out lines 23 and 31.

    GraHal thanked this post
Viewing 15 posts - 1 through 15 (of 15 total)
  • You must be logged in to reply to this topic.

Inquiry coding inside bar momentum strategy


ProOrder: Automated Strategies & Backtesting

New Reply
Author
author-avatar
phoentzs @phoentzs Participant
Summary

This topic contains 14 replies,
has 3 voices, and was last updated by robertogozzi
4 years, 5 months ago.

Topic Details
Forum: ProOrder: Automated Strategies & Backtesting
Language: English
Started: 08/22/2021
Status: Active
Attachments: No files
Logo Logo
Loading...