Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Opening Range Breakout

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Opening Range Breakout

    I had this over in general programming qith a different question but think it might be better here.
    I attempted to adda trade counter to this so it doesnt trade if there have been more than 3 trades in the session. It compiles ok, but when I ran it no orders came up after the first 20 minutes as expected. Is there something wrong with the the orders part of this that someone can see?
    Any help would be much appreciated.

    Thanks
    Code:
    // 
    // Copyright (C) 2007, NinjaTrader LLC <[URL="http://www.ninjatrader.com"]www.ninjatrader.com[/URL]>.
    // NinjaTrader reserves the right to modify or overwrite this NinjaScript component with each release.
    //
    #region Using declarations
    using System;
    using System.ComponentModel;
    using System.Diagnostics;
    using System.Drawing;
    using System.Drawing.Drawing2D;
    using System.Xml.Serialization;
    using NinjaTrader.Cbi;
    using NinjaTrader.Data;
    using NinjaTrader.Indicator;
    using NinjaTrader.Strategy;
    #endregion
    // This namespace holds all strategies and is required. Do not change it.
    namespace NinjaTrader.Strategy
    {
        /// <summary>
        /// Simple strategy that monitors for a breakout.
        /// </summary>
        [Description("Simple strategy that monitors for a breakout.")]
        public class SampleBreakoutStrategy : Strategy
        {
            #region Variables
      private double highestHigh = 0;
      private double lowestLow = 0;
      private int tradeCounter = 0; 
            #endregion
            /// <summary>
            /// This method is used to configure the strategy and is called once before any strategy method is called.
            /// </summary>
            protected override void Initialize()
            {
                CalculateOnBarClose = true;
            }
            /// <summary>
            /// Called on each bar update event (incoming tick)
            /// </summary>
            protected override void OnBarUpdate()
            {
       // Resets the highest high and lowest low at the start of every new session
       if (Bars.FirstBarOfSession && FirstTickOfBar)
       { tradeCounter = 0;
        highestHigh = High[0];
        lowestLow = Low[0];
       }
       
       // Stores the highest high from the first 30 bars
       if (Bars.BarsSinceSession < 30 && High[0] > highestHigh)
        highestHigh = High[0];
       // Stores the lowest low from the first 30 bars
       if (Bars.BarsSinceSession < 30 && Low[0] < lowestLow)
        lowestLow = Low[0];
       
        //Enter Long at highest high + 1 of first 30 min. trade if number of trades today is less than 3
        //Enter Short at lowest low - 1 of first 30 min. trade if number of trades today is less than 3
        if (tradeCounter < 3)
        {
         EnterLongStop(highestHigh + TickSize);
         tradeCounter++;
         EnterShortStop(lowestLow - TickSize);
         tradeCounter++;
         
        }
       
       
              
            }
        }
    }

    #2
    maninjapan,

    You cannot submit simultaneous working orders in opposite directions at the same time. Please choose either a long or short. If you want to use the other, you can cancel the first and then submit the second.

    Also, your counter would not fly in this instance. You need to keep resubmitting your orders to keep them alive and because you are incrementing your counter every single time you keep the order alive, you hit your 3 limit very quickly and then stop keeping it alive.
    Josh P.NinjaTrader Customer Service

    Comment


      #3
      ok, well I want t the system not to trade more than 3 times a day, is this not the correct code to be using then?

      Comment


        #4
        Sure, but you are incrementing the counter too early. You should only increment the counter when you actually get filled, not when you simply submit the order. As stated in my previous post, when you keep incrementing on submission, you increment on every single bar that keeps the order alive and then you exceed your limit without even trading once.
        Josh P.NinjaTrader Customer Service

        Comment


          #5
          Im currently trying testing the current breakout srtategy and am running it on SB and KC, both are set with the same starting time 8:00am, so I would expect an order on one side or the other the moment the clock ticks past 8:30am. I got a print in the output window with the high/low and an order for SB at 8:31 but didnt get anything from KC until 8:33. and after looking at the high and lows it used it counted the bars all the way up to 8:32.
          Am I doing something wrong in the code here?
          Code:
            protected override void Initialize()
                  {
                      CalculateOnBarClose = true;
                  }
                  /// <summary>
                  /// Called on each bar update event (incoming tick)
                  /// </summary>
                  protected override void OnBarUpdate()
           
                  {
             // Resets the highest high and lowest low at the start of every new session
             if (Bars.FirstBarOfSession && FirstTickOfBar)
             {
              highestHigh = High[0];
              lowestLow = Low[0];
             }
             
             // Stores the highest high from the first 30 bars
             if (Bars.BarsSinceSession < 30 && High[0] > highestHigh)
              highestHigh = High[0];
             // Stores the lowest low from the first 30 bars
             if (Bars.BarsSinceSession < 30 && Low[0] < lowestLow)
              lowestLow = Low[0];
             
             //Print the symbol and Highs and Lows of the first 30 minutes 
             if (Bars.BarsSinceSession == 30 )
              PrintWithTimeStamp(Instrument.FullName + " " + "High" + " " + highestHigh + " " + "Low" + lowestLow);
              
             
            
             
                 //Setup the Stop in the direction of  close to the high/low midpoint
              if (Bars.BarsSinceSession >= 30 && Close[0] > (highestHigh + lowestLow)/2)
                          //Enter Long at highest high + 1 of first 30 min.
              EnterLongStop(highestHigh + TickSize);
             else if (Bars.BarsSinceSession >= 30 && Close[0] < (highestHigh + lowestLow)/2)
              //Enter Short at lowest low - 1 of first 30 min.
                          EnterShortStop(lowestLow - TickSize);

          Comment


            #6
            What you should do is print on each step of the way and find where you have the bug in your code.
            Josh P.NinjaTrader Customer Service

            Comment


              #7
              Originally posted by NinjaTrader_Josh View Post
              What you should do is print on each step of the way and find where you have the bug in your code.
              Thanks JOsh, this is all new to me though, any example scripts I can refer to? This is my first attempt at this.

              Comment


                #8
                maninjapan,

                For debugging, please see this tip: http://www.ninjatrader-support2.com/...ead.php?t=3418
                Josh P.NinjaTrader Customer Service

                Comment


                  #9
                  Thanks a lot, Ill go through it. Just one quick question first.
                  I am getting the highs and lows not only for todays sessions but for the previous 3 or 4 days as well. How do adjust this so its only calling today's High and Low?
                  Code:
                   if (Bars.BarsSinceSession == 30 )
                      PrintWithTimeStamp(Instrument.FullName + " " + "High" + " " + highestHigh + " " + "Low" + lowestLow);

                  Thanks for your help

                  Comment


                    #10
                    maninjapan,

                    When your code processes 3 or 4 days ago, it is processing the data moving forward so during those days the prints came out of, they were actually the current day and you will for sure see them.
                    Josh P.NinjaTrader Customer Service

                    Comment


                      #11
                      SO I cant adjust this so it just returns the numbers for the current session?

                      Comment


                        #12
                        Current session is relative. Every session is the current session when it is being processed. If you want an absolute session you should just use Time[] comparisons and check dates.
                        Josh P.NinjaTrader Customer Service

                        Comment


                          #13
                          well, Ive spent about 6 hours on this today, trying to figure out where this strategy is going wrong. And I havent really got anywhere.
                          I've had a go at attempting to debug with OnOrderUpdate and found out real quick why its labeled advanced.....

                          What I can get in the output window are the highs and lows of what is supposed to be the first 30 bars, but depending on the symbol sometimes it is taking the highs and lows up to the 32nd or 33rd bar before posting in the output window. Does anyone have any comments based on the code below??? OR perhaps an example for dummies on how to post orders to the output window....

                          Comment


                            #14
                            maninjapan,

                            I suggest you print values on every single bar and take it one step at a time by hand.

                            Print(Time[0] + " " + CurrentBar + " " + highestHigh + " " + lowestLow);

                            Then you want to compare what you were doing by hand versus what was actually done by the strategy.
                            Josh P.NinjaTrader Customer Service

                            Comment


                              #15
                              just tried this on CAT, it printed every minute with the highs/lows as expected, however the other print that I already had in there myself,
                              Code:
                               //Print the symbol and Highs and Lows of the first 30 minutes 
                                 if (Bars.BarsSinceSession == 30 )
                                  PrintWithTimeStamp(Instrument.FullName + " " + "High" + " " + highestHigh + " " + "Low" + lowestLow);
                              didnt appear until 13 minutes after the 30th bar since open of the session I set....
                              The order also appeared at the same time, so it was either 'stuck' for some reason, or my code is wrong (highly probable).
                              The thing is that this worked fine on AAPL which I tried before CAT....

                              This is the problem that I ahd been experiencing.....

                              Any ideas on this would be greatly appreciated,
                              Thanks

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                              0 responses
                              671 views
                              0 likes
                              Last Post Geovanny Suaza  
                              Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                              0 responses
                              379 views
                              1 like
                              Last Post Geovanny Suaza  
                              Started by Mindset, 02-09-2026, 11:44 AM
                              0 responses
                              111 views
                              0 likes
                              Last Post Mindset
                              by Mindset
                               
                              Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                              0 responses
                              575 views
                              1 like
                              Last Post Geovanny Suaza  
                              Started by RFrosty, 01-28-2026, 06:49 PM
                              0 responses
                              582 views
                              1 like
                              Last Post RFrosty
                              by RFrosty
                               
                              Working...
                              X