Save Brokerage Upto 60% to 90%

Opening Range Breakout (ORB) - Intraday Trading System

Discussion in 'Day Trading' started by Cubt, Sep 23, 2013.

  1. Happy_Singh

    Happy_Singh Happy

    Joined:
    May 27, 2013
    Messages:
    1,486
    Likes Received:
    3,800
    Trophy Points:
    113
    Location:
    Here & Now :)
    Amibroker gives that in backtest results . . .
    look for the columns called MAE, MFE

    thats what exactly RKK wanted . . .

    MFE - max favorable excursion
    MAE - max adverse excursion


    :) Happy


    EDIT:

    and putting in profit stops is very easy . . . use

    ApplyStop(1,2,R);

    where R is the opening range you are getting
     
    Last edited: Sep 25, 2013
  2. amitrandive

    amitrandive Well-Known Member

    Joined:
    Jul 26, 2010
    Messages:
    8,799
    Likes Received:
    24,029
    Trophy Points:
    113
    Seniors

    Please verify . I have posted entire code with the modifications suggested in this thread.

    Amit

    function ParamOptimize( pname, defaultval, minv, maxv, step )
    {
    return Optimize( pname,
    Param( pname, defaultval, minv, maxv, step ),
    minv, maxv, step );
    }




    _SECTION_BEGIN("Augubhai's ORB System v1.1");

    //--Intraday time frame
    TimeFrameSet(in5Minute); //If reseting, check formula for TimeFrameInMinutes
    TimeFrameInMinutes = 5;

    //--Define all params
    EntryBufferPct = ParamOptimize("Entry Buffer %", 0, 0, 2, 0.1);
    SLPct = ParamOptimize("SL %", 1.4, 0.1, 10, 0.1);
    TargetPct = ParamOptimize("Target %", 0, 0, 20, 0.5);
    MaxTarget = 100;
    TargetPct = IIf(TargetPct == 0, MaxTarget, TargetPct);
    EntryTimeStart = ParamOptimize("Entry Time Start (Minutes)", 5, 5, 120, 5);
    EntryBarStart = floor(EntryTimeStart/TimeFrameInMinutes) - 1;
    EntryTimeEnd = ParamOptimize("Entry Time End (Minutes)", 25, 10, 180, 5);
    EntryBarEnd = floor(EntryTimeEnd/TimeFrameInMinutes) - 1;
    EntryBarEnd = IIf(EntryBarEnd < EntryBarStart, EntryBarStart, EntryBarEnd);

    //--Plot Price Candle Chart
    SetChartOptions(0,chartShowArrows|chartShowDates);
    _N(Title = StrFormat("{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%) {{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 ) ) ));
    Plot( C, "Close", colorBlack, styleNoTitle | GetPriceStyle() );

    //--New Day & Time. End Day & Time . End Day & Time is null till end of day 1
    NewDay = (Day()!= Ref(Day(), -1)) OR BarIndex() == 0;
    printf("\n NewDay : " + NewDay );
    EndDay = (Day()!= Ref(Day(), 1));
    printf("\n EndDay : " + EndDay );
    FirstBarTime = ValueWhen(NewDay,TimeNum(),1);
    EndTime = ValueWhen(EndDay,TimeNum(),1);
    SquareOffTime = EndTime;

    //--Calculate ORB, and SL
    HighestOfDay = HighestSince(NewDay,H,1);
    LowestOfDay = LowestSince(NewDay,L,1);
    BarsSinceNewDay = BarsSince(NewDay);
    ORBH = ValueWhen(BarsSinceNewDay<=EntryBarStart,HighestOfDay ,1) * (1 + (EntryBufferPct/100));
    ORBL = ValueWhen(BarsSinceNewDay<=EntryBarStart,LowestOfDay ,1) * (1 - (EntryBufferPct/100));
    ORBHSL = ORBH * (1-(SLPct/100));
    //ORBHSL = ORBL;
    ORBLSL = ORBL * (1+(SLPct/100));
    //ORBLSL = ORBH;
    ORBHTarget = ORBH * (1+(TargetPct/100));
    ORBLTarget = ORBL * (1-(TargetPct/100));

    //--Find Buy, Sell, Short & Cover Signals
    BuySignal = (H >= ORBH) AND (BarsSinceNewDay > EntryBarStart);
    printf("\nBuySignal : " + BuySignal );
    ShortSignal = (L <= ORBL) AND (BarsSinceNewDay > EntryBarStart) ;
    printf("\nShortSignal : " + ShortSignal );
    BarsSinceLastBuySignal = (BarsSince(Ref(BuySignal,-1)) + 1);
    BarsSinceLastShortSignal = (BarsSince(Ref(ShortSignal,-1)) + 1);
    BarsSinceLastEntrySignal = Min(BarsSinceLastBuySignal, BarsSinceLastShortSignal);
    BothEntrySignalsNull = IsNull(BarsSinceLastBuySignal) AND IsNull(BarsSinceLastShortSignal); //true for start of Day 1
    printf("\n\nBarsSinceNewDay : " + BarsSinceNewDay );
    printf("\n BarsSinceLastEntrySignal : " + BarsSinceLastEntrySignal);
    Buy = (H >= ORBH) AND (BarsSinceNewDay > EntryBarStart) AND (BarsSinceNewDay <= EntryBarEnd) AND ((BarsSinceNewDay < BarsSinceLastEntrySignal) OR BothEntrySignalsNull );
    Sell = (L <= ORBHSL) OR (H >= ORBHTarget) OR (TimeNum() > SquareOffTime-1) AND (BarsSinceNewDay > BarsSinceLastBuySignal);
    Short = (L <= ORBL) AND (BarsSinceNewDay > EntryBarStart) AND (BarsSinceNewDay <= EntryBarEnd) AND ((BarsSinceNewDay < BarsSinceLastEntrySignal) OR BothEntrySignalsNull );
    Cover = (H >= ORBLSL) OR (L <= ORBLTarget) OR (TimeNum() > SquareOffTime-1) AND (BarsSinceNewDay > BarsSinceLastShortSignal);
    printf("\nBuy : " + Buy );
    printf("\nSell : " + Sell );
    printf("\nShort : " + Short );
    printf("\nCover : " + Cover );

    //--Handle if ORB broken both sides on same bar
    //--And remove duplicate Sell & Cover signals, since ExRem did not work as needed when Buy & Sell on same bar
    orbBothSides = IIf(Buy AND Short, 1, 0);
    Buy = IIf(orbBothSides AND C <= O, 0, Buy);
    Short = IIf(orbBothSides AND C > O, 0, Short);
    Sell = IIf(orbBothSides AND C > O AND (L <= ORBHSL), 1, Sell);
    Sell = IIf((BarsSince(Buy) < (BarsSince(Ref(Sell,-1))+1)) OR (BarsSince(Buy) AND IsNull(BarsSince(Ref(Sell,-1)))),Sell,0);
    Cover = IIf(orbBothSides AND C <= O AND (H >= ORBLSL), 1, Cover);
    Cover = IIf((BarsSince(Short) < (BarsSince(Ref(Cover,-1))+1)) OR (BarsSince(Short) AND IsNull(BarsSince(Ref(Cover,-1)))),Cover,0);
    printf("\n\norbBothSides : " + orbBothSides);
    printf("\nBuy : " + Buy );
    printf("\nSell : " + Sell );
    printf("\nShort : " + Short );
    printf("\nCover : " + Cover );

    //--Special Condition for 18 & 19 May 2009 for the Indian Market
    Buy =IIf(DateNum()==1090518 OR (DateNum()==1090519 AND NewDay),0,Buy );
    Sell =IIf(DateNum()==1090518 OR (DateNum()==1090519 AND NewDay),0,Sell );
    Short =IIf(DateNum()==1090518 OR (DateNum()==1090519 AND NewDay),0,Short );
    Cover =IIf(DateNum()==1090518 OR (DateNum()==1090519 AND NewDay),0,Cover );

    //--Set prices
    BuyPrice = IIf(Buy, ORBH, Null);
    SellPrice = IIf(Sell, IIf(H >= ORBHTarget, ORBHTarget, Max(ORBHSL, L)), Null);
    ShortPrice = IIf(Short, ORBL, Null);
    CoverPrice = IIf(Cover, IIf(L <= ORBLTarget, ORBLTarget, Min(ORBLSL, H)), Null);

    //--Plot ORB, and SL
    Plot(ORBHSL,"",colorRed,styleDashed);
    Plot(ORBLSL,"",colorRed,styleDashed);
    Plot(IIf(TargetPct == MaxTarget, Null, ORBHTarget),"",colorGreen,styleDashed);
    Plot(IIf(TargetPct == MaxTarget, Null, ORBLTarget),"",colorGreen,styleDashed);
    PlotOHLC( ORBL, ORBH, ORBL, ORBH, "", colorYellow, styleCloud);

    //--Plot Signals
    shape1 = Buy * shapeUpArrow + Sell * shapeDownArrow;
    PlotShapes( shape1, IIf( Buy, colorGreen, colorGreen), 0, IIf( Buy, Low, High ) );
    shape2 = Cover * shapeUpArrow + Short * shapeDownArrow;
    PlotShapes( shape2, IIf( Cover, colorRed, colorRed), 0, IIf( Cover, Low, High ) );
    GraphXSpace = 5;

    //--Restore time frame
    TimeFrameRestore();
    _SECTION_END();
     
    Last edited: Sep 25, 2013
    guptak03, jamit_05 and chilgu like this.
  3. amitrandive

    amitrandive Well-Known Member

    Joined:
    Jul 26, 2010
    Messages:
    8,799
    Likes Received:
    24,029
    Trophy Points:
    113
    :clapping:
     
  4. Cubt

    Cubt Well-Known Member

    Joined:
    Mar 22, 2013
    Messages:
    1,002
    Likes Received:
    1,967
    Trophy Points:
    113
    Location:
    Chennai
    Amit,

    i tested it again but still am getting the same results. No change, can you check ur code again, pls.
     
  5. amitrandive

    amitrandive Well-Known Member

    Joined:
    Jul 26, 2010
    Messages:
    8,799
    Likes Received:
    24,029
    Trophy Points:
    113
    Cubt

    I have just pasted the lines suggested by HappySingh in "Augubhai's ORB System v1.1" AFL for the benefit of all.

    I do not know AFL coding.

    Thanks
    Amit
     
  6. Happy_Singh

    Happy_Singh Happy

    Joined:
    May 27, 2013
    Messages:
    1,486
    Likes Received:
    3,800
    Trophy Points:
    113
    Location:
    Here & Now :)
    Not checked the above code but the exact statement you need to add to it would be

    Code:
    R = ORBH-ORBL;
    ApplyStop(1,2,R);

    :) Happy
     

  7. Cubt

    Cubt Well-Known Member

    Joined:
    Mar 22, 2013
    Messages:
    1,002
    Likes Received:
    1,967
    Trophy Points:
    113
    Location:
    Chennai
    Happy,

    It would be great if you could make the following changes in the code.


    1. After breakout of 5 Mins bar, the position needs to be exited once the Target of 10 points is reached.

    2. Stop loss is when 5 mins bar closes above/below the opening range. (i.e. above/below 1st 5 mins bar)

    3. If neither Target or Stop Loss is hit, the position should be exited @ 03:20 PM @ market price.

    4. When backtesting, it should be considered as Intraday trade. i.e no positions should be carried over.


    I guess the 3rd point will not happen as the target is very less, either the target or stop loss will be triggered before 3:20PM. If you feel the same, you can skip this part in the code.

    I tried making the changes in the code, but its not showing the correct results. I think am writing a wrong code in the

    stop loss part. Pls help. Thanks.

    ORBHSL = ORBH;
    ORBLSL = ORBL;
    ORBHTarget = ORBH + 10;
    ORBLTarget = ORBL - 10;

    actual AFL
    Code:
    function ParamOptimize( pname, defaultval, minv, maxv, step )
    {
    return Optimize( pname,
    Param( pname, defaultval, minv, maxv, step ),
    minv, maxv, step );
    }
    
    
    
    
    _SECTION_BEGIN("Augubhai's ORB System v1.1"); 
    
    //--Intraday time frame 
    TimeFrameSet(in5Minute); //If reseting, check formula for TimeFrameInMinutes 
    TimeFrameInMinutes = 5;
    
    //--Define all params 
    EntryBufferPct = ParamOptimize("Entry Buffer %", 0, 0, 2, 0.1);
    SLPct = ParamOptimize("SL %", 1.4, 0.1, 10, 0.1);
    TargetPct = ParamOptimize("Target %", 0, 0, 20, 0.5);
    MaxTarget = 100;
    TargetPct = IIf(TargetPct == 0, MaxTarget, TargetPct); 
    EntryTimeStart = ParamOptimize("Entry Time Start (Minutes)", 5, 5, 120, 5);
    EntryBarStart = floor(EntryTimeStart/TimeFrameInMinutes) - 1;
    EntryTimeEnd = ParamOptimize("Entry Time End (Minutes)", 25, 10, 180, 5);
    EntryBarEnd = floor(EntryTimeEnd/TimeFrameInMinutes) - 1;
    EntryBarEnd = IIf(EntryBarEnd < EntryBarStart, EntryBarStart, EntryBarEnd);  
    
    //--Plot Price Candle Chart
    SetChartOptions(0,chartShowArrows|chartShowDates);
    _N(Title = StrFormat("{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%) {{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 ) ) ));
    Plot( C, "Close", colorBlack, styleNoTitle | GetPriceStyle() ); 
    
    //--New Day & Time. End Day & Time . End Day & Time is null till end of day 1  
    NewDay = (Day()!= Ref(Day(), -1)) OR BarIndex() == 0; 
    printf("\n NewDay  : " + NewDay ); 
    EndDay = (Day()!= Ref(Day(), 1)); 
    printf("\n EndDay  : " + EndDay );
    FirstBarTime = ValueWhen(NewDay,TimeNum(),1); 
    EndTime = ValueWhen(EndDay,TimeNum(),1);
    SquareOffTime = EndTime;
    
    //--Calculate ORB, and SL
    HighestOfDay = HighestSince(NewDay,H,1); 
    LowestOfDay = LowestSince(NewDay,L,1); 
    BarsSinceNewDay = BarsSince(NewDay);
    ORBH = ValueWhen(BarsSinceNewDay<=EntryBarStart,HighestOfDay ,1) * (1 + (EntryBufferPct/100)); 
    ORBL = ValueWhen(BarsSinceNewDay<=EntryBarStart,LowestOfDay ,1) * (1 - (EntryBufferPct/100)); 
    ORBHSL = ORBH * (1-(SLPct/100)); 
    //ORBHSL = ORBL; 
    ORBLSL = ORBL * (1+(SLPct/100));
    //ORBLSL = ORBH; 
    ORBHTarget = ORBH * (1+(TargetPct/100));
    ORBLTarget = ORBL * (1-(TargetPct/100));
    
    //--Find Buy, Sell, Short & Cover Signals
    BuySignal = (H >= ORBH) AND (BarsSinceNewDay  > EntryBarStart); 
    printf("\nBuySignal : " + BuySignal ); 
    ShortSignal = (L <= ORBL) AND (BarsSinceNewDay  > EntryBarStart) ; 
    printf("\nShortSignal  : " + ShortSignal ); 
    BarsSinceLastBuySignal = (BarsSince(Ref(BuySignal,-1)) + 1);
    BarsSinceLastShortSignal = (BarsSince(Ref(ShortSignal,-1)) + 1);
    BarsSinceLastEntrySignal = Min(BarsSinceLastBuySignal, BarsSinceLastShortSignal);
    BothEntrySignalsNull = IsNull(BarsSinceLastBuySignal) AND IsNull(BarsSinceLastShortSignal); //true for start of Day 1
    printf("\n\nBarsSinceNewDay : " + BarsSinceNewDay ); 
    printf("\n BarsSinceLastEntrySignal : " + BarsSinceLastEntrySignal); 
    Buy = (H >= ORBH) AND (BarsSinceNewDay  > EntryBarStart) AND (BarsSinceNewDay <= EntryBarEnd) AND ((BarsSinceNewDay < BarsSinceLastEntrySignal) OR BothEntrySignalsNull ); 
    Sell = (L <= ORBHSL) OR (H >= ORBHTarget) OR (TimeNum() > SquareOffTime-1) AND (BarsSinceNewDay > BarsSinceLastBuySignal); 
    Short = (L <= ORBL) AND (BarsSinceNewDay  > EntryBarStart) AND (BarsSinceNewDay <= EntryBarEnd) AND ((BarsSinceNewDay < BarsSinceLastEntrySignal) OR BothEntrySignalsNull ); 
    Cover = (H >= ORBLSL) OR (L <= ORBLTarget) OR (TimeNum() > SquareOffTime-1) AND (BarsSinceNewDay > BarsSinceLastShortSignal); 
    printf("\nBuy : " + Buy ); 
    printf("\nSell : " + Sell ); 
    printf("\nShort : " + Short ); 
    printf("\nCover : " + Cover ); 
    
    //--Handle if ORB broken both sides on same bar
    //--And remove duplicate Sell & Cover signals, since ExRem did not work as needed when Buy & Sell on same bar
    orbBothSides = IIf(Buy AND Short, 1, 0); 
    Buy = IIf(orbBothSides AND C <= O, 0, Buy); 
    Short = IIf(orbBothSides AND C > O, 0, Short); 
    Sell = IIf(orbBothSides AND C > O AND (L <= ORBHSL), 1, Sell); 
    Sell = IIf((BarsSince(Buy) < (BarsSince(Ref(Sell,-1))+1)) OR (BarsSince(Buy) AND IsNull(BarsSince(Ref(Sell,-1)))),Sell,0);
    Cover = IIf(orbBothSides AND C <= O AND (H >= ORBLSL), 1, Cover); 
    Cover = IIf((BarsSince(Short) < (BarsSince(Ref(Cover,-1))+1)) OR (BarsSince(Short) AND IsNull(BarsSince(Ref(Cover,-1)))),Cover,0);
    printf("\n\norbBothSides : " + orbBothSides); 
    printf("\nBuy : " + Buy ); 
    printf("\nSell : " + Sell ); 
    printf("\nShort : " + Short ); 
    printf("\nCover : " + Cover ); 
    
    //--Special Condition for 18 & 19 May 2009 for the Indian Market
    Buy =IIf(DateNum()==1090518 OR (DateNum()==1090519 AND NewDay),0,Buy );
    Sell =IIf(DateNum()==1090518 OR (DateNum()==1090519 AND NewDay),0,Sell );
    Short =IIf(DateNum()==1090518 OR (DateNum()==1090519 AND NewDay),0,Short );
    Cover =IIf(DateNum()==1090518 OR (DateNum()==1090519 AND NewDay),0,Cover );
    
    //--Set prices
    BuyPrice = IIf(Buy, ORBH, Null); 
    SellPrice = IIf(Sell, IIf(H >= ORBHTarget, ORBHTarget, Max(ORBHSL, L)), Null); 
    ShortPrice = IIf(Short, ORBL, Null); 
    CoverPrice = IIf(Cover, IIf(L <= ORBLTarget, ORBLTarget, Min(ORBLSL, H)), Null); 
    
    //--Plot ORB, and SL 
    Plot(ORBHSL,"",colorRed,styleDashed); 
    Plot(ORBLSL,"",colorRed,styleDashed); 
    Plot(IIf(TargetPct == MaxTarget, Null, ORBHTarget),"",colorGreen,styleDashed);
    Plot(IIf(TargetPct == MaxTarget, Null, ORBLTarget),"",colorGreen,styleDashed);
    PlotOHLC( ORBL, ORBH, ORBL, ORBH, "", colorYellow, styleCloud); 
     
    //--Plot Signals
    shape1 = Buy * shapeUpArrow + Sell * shapeDownArrow; 
    PlotShapes( shape1, IIf( Buy, colorGreen, colorGreen), 0, IIf( Buy, Low, High ) ); 
    shape2 = Cover * shapeUpArrow + Short * shapeDownArrow; 
    PlotShapes( shape2, IIf( Cover, colorRed, colorRed), 0, IIf( Cover, Low, High ) ); 
    GraphXSpace = 5; 
    
    //--Restore time frame 
    TimeFrameRestore(); 
    _SECTION_END(); 
     
  8. Happy_Singh

    Happy_Singh Happy

    Joined:
    May 27, 2013
    Messages:
    1,486
    Likes Received:
    3,800
    Trophy Points:
    113
    Location:
    Here & Now :)
    Hi

    Before we change any code try setting a profit stop of 10 points . . .
    can set it in Backtest Settings >> Stops >> Profit > Points > 10 > exit intraday at stop

    Without the SL if you are getting enough number of winners then only one should try introducing a SL.

    :) Happy
     
  9. Cubt

    Cubt Well-Known Member

    Joined:
    Mar 22, 2013
    Messages:
    1,002
    Likes Received:
    1,967
    Trophy Points:
    113
    Location:
    Chennai
    Thanks Happy.

    I checked it and i could see 80% of total trades achieved the target. With the present AFL, the entry and stop loss condition varies completely.

    Could you please help out in implementing the below conditions.

    1. After breakout of 5 Mins bar, the position needs to be exited once the Target of 10 points is reached.

    2. Stop loss is when 5 mins bar closes above/below the opening range. (i.e. above/below 1st 5 mins bar)
     
    anup likes this.
  10. anup

    anup Well-Known Member

    Joined:
    Sep 26, 2012
    Messages:
    1,777
    Likes Received:
    2,209
    Trophy Points:
    113
    80% is good success ratio... :thumb::thumb:
     
    kiran_thiru likes this.
Loading...

Share This Page