Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Help with Range Based Strategy

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

    Help with Range Based Strategy

    Hi,

    I'm new to programming and was wondering whether someone can point me in the right direction with the following strategy:

    Say I define lunch hour as 12pm-1pm. I want the strategy to go long if right after 1pm the price breaks the high of that "lunch hour" and short if the price breaks the low, by 2 ticks. I want to get out at a 1pt profit or out at a 3pt stop loss. What if I want to trade 2 contracts and set the stop loss to B/E on the second contract after the target on the 1st contract has been reached and trail that second contract stop to half the move from my entry?

    I want the strategy to last until the end of day, but only execute 1 side each. That is, if it goes long, then no more longs, and if it goes short, no more shorts. The strategy can execute 1 long and 1 short if the above conditions are met.

    #2
    Insearch,

    This is all possible through programming. Please see this reference sample for a breakout strategy: http://www.ninjatrader-support2.com/...ead.php?t=3223
    Josh P.NinjaTrader Customer Service

    Comment


      #3
      Originally posted by NinjaTrader_Josh View Post
      Insearch,

      This is all possible through programming. Please see this reference sample for a breakout strategy: http://www.ninjatrader-support2.com/...ead.php?t=3223
      Thx. This seems to roll the range . . . What should I do if I want to "lock" the lunch hour range . . . .

      Also, how do I define the start and end times?

      Comment


        #4
        if (ToTime(Time[0]) >= 120000 && ToTime(Time[0]) < 130000)
        Josh P.NinjaTrader Customer Service

        Comment


          #5
          Would this logic return the high and low price for the time range specified?

          protectedoverridevoid OnBarUpdate()
          {

          // Defining the high and low range
          if (ToTime(Time[0]) >= 120000 && ToTime(Time[0]) < 130000)
          {
          double highestHigh = MAX(High, ToTime (Time [120000]) – ToTime (Time [130000]));

          double lowestLow = MIN(Low, ToTime (Time [120000]) – ToTime (Time [130000]));

          }

          Comment


            #6
            Insearch,

            The second parameter for MAX() is the Period as in how many bars you want to check. Period is not related to time. It is bar driven.

            Time[120000] is the time of bars ago # 120000. It is not the time at 12:00:00.

            If you want the highest high and lowest low of a certain time please see this reference sample: http://www.ninjatrader-support2.com/...ead.php?t=8600
            Josh P.NinjaTrader Customer Service

            Comment


              #7
              Thanks. That is for an indicator, so wanted to to see if that can be somehow used in a strategy. In that code, there's this piece:

              // Calculate the number of bars ago for the start and end bars of the specified time range
              int startBarsAgo = GetBar(startDateTime);
              int endBarsAgo = GetBar(endDateTime);

              Is there any way to put a time stamp in the "startDateTime" and "endDateTime". That is, can I use ToTime in there to specify the time?

              Comment


                #8
                Insearch,

                The concepts from the indicator can be carried over to a strategy.

                ToTime() does not work in letting you specify a time. All it does is takes a DateTime object and converts it to an integer that represents time in HHMMSS format.

                GetBar() gets the bar that a specific time happened on. In the example startDateTime and endDateTime are DateTime objects. Running ToTime() on them will just make them into integers representing the time.
                Josh P.NinjaTrader Customer Service

                Comment


                  #9
                  ok!

                  What is the syntax for getting the high of the first bar in the time range?

                  Would the code below do this do it . . . I doubt, as definining highestHigh as High[0] would make high of every current bar the "active high" . . . . .

                  // Defining the high time range
                  if (ToTime(Time[0]) >= 120000 && ToTime(Time[0]) <= 130000)
                  highestHigh = High[
                  0];

                  Comment


                    #10
                    You need to have an additional check inside there to confirm that the recent high being processed is actually greater than the stored high.

                    Code:
                    if (ToTime(Time[0])..........)
                    {
                         if (High[0] > highestHigh)
                              highestHigh = High[0];
                    }
                    Josh P.NinjaTrader Customer Service

                    Comment


                      #11
                      If that is my first line, then there really is no stored highestHigh, no? I.e. the strategy code would actually never store a "highestHigh"?

                      here's the code as you suggested, but don't we need to store highestHigh first?

                      protectedoverridevoid OnBarUpdate()
                      {

                      {
                      // Defining the high in the specified time range
                      if (ToTime(Time[0]) >= 120000 && ToTime(Time[0]) <= 130000)
                      {
                      if (High[0] > highestHigh)
                      highestHigh = High[
                      0];
                      }

                      Comment


                        #12
                        When you declare highestHigh as a double you can set it to zero. The first time it runs through and checks this High[0] will be > 0 and then it will take on the high value.

                        Code:
                        private double highestHigh = 0;
                        Josh P.NinjaTrader Customer Service

                        Comment


                          #13
                          understood. That declaration is made in teh initialize section or onbarupdate section?

                          Comment


                            #14
                            In the Variables region of your code above Initialize().
                            Josh P.NinjaTrader Customer Service

                            Comment


                              #15
                              Inputing it like below, gives me an error message:

                              ///<summary>
                              /// This method is used to configure the strategy and is called once before any strategy method is called.
                              ///</summary>
                              privatedouble highestHigh = 0
                              protectedoverridevoid Initialize()

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                              0 responses
                              669 views
                              0 likes
                              Last Post Geovanny Suaza  
                              Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                              0 responses
                              378 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
                              580 views
                              1 like
                              Last Post RFrosty
                              by RFrosty
                               
                              Working...
                              X