Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

Partner 728x90

Collapse

Not Getting Past Time Series Line

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

    Not Getting Past Time Series Line

    Hello NT,

    I'm testing long entries on ES 5 minute bars as follows:

    1. Record the open of the first bar of the day (8:30am-8:35am) and refer to it as "OpenPrice"

    2. At the close of the second bar of the day (8:40am) go long if:

    a) The low of bar 2 is greater than OpenPrice

    b) The 5 bar RSI indicator is below 50 OR both the close is greater than the previous bar close and the close is greater than previous bar close AND the close is greater than close 2 bars ago AND the daily close is less than the daily close 2 bars ago.

    For item 1 above I used the following code to store the open of the first bar of the day to "OpenPrice"

    Code:
     protected override void OnBarUpdate()
    {
    if (BarsInProgress != 0)
    return;
    
    if (CurrentBars[0] < 0)
    return;
    
    if (Times[0][0].TimeOfDay == new TimeSpan(08,30,0))
    {
    OpenPrice = Open[0];
    return;
    For item 2 I used the Strategy Builder to generate the following code:

    Code:
     // Set 1
    if (
    // Condition group 1
    ((Times[0][0].TimeOfDay == new TimeSpan(8, 40, 0))
    && (Low[0] > OpenPrice)
    && (RSI1.Avg[0] < 50))
    // // Condition group 2
    || ((Times[0][0].TimeOfDay == new TimeSpan(8, 40, 0))
    && (Low[0] > OpenPrice)
    && (Close[0] > Close[1])
    && (Close[0] > Close[2])
    && (Closes[1][0] < Closes[1][2]))
    )
    {
    EnterLong(Convert.ToInt32(DefaultQuantity), "");
    For testing exits I used the following code:

    Code:
     if (BarsSinceEntryExecution() >= exit5_X)
    {
    ExitLong();
    }
    All together it looks like this:

    Code:
     OpenPrice = 4000;
    exit5_X = 5;
    }
    else if (State == State.Configure)
    {
    }
    else if (State == State.DataLoaded)
    {
    RSI1 = RSI(Close, 5, 1);
    
    }
    }
    
    [NinjaScriptProperty]
    public int exit5_X
    { get; set; }
    
    protected override void OnBarUpdate()
    {
    if (BarsInProgress != 0)
    return;
    
    if (CurrentBars[0] < 0)
    return;
    
    if (Times[0][0].TimeOfDay == new TimeSpan(08,30,0))
    {
    OpenPrice = Open[0];
    return;
    
    // Set 1
    if (
    // Condition group 1
    ((Times[0][0].TimeOfDay == new TimeSpan(8, 40, 0))
    && (Low[0] > OpenPrice)
    && (RSI1.Avg[0] < 50))
    // // Condition group 2
    || ((Times[0][0].TimeOfDay == new TimeSpan(8, 40, 0))
    && (Low[0] > OpenPrice)
    && (Close[0] > Close[1])
    && (Close[0] > Close[2])
    && (Closes[1][0] < Closes[1][2]))
    )
    {
    EnterLong(Convert.ToInt32(DefaultQuantity), "");
    }
    if (BarsSinceEntryExecution() >= exit5_X)
    {
    ExitLong();
    }
    }
    
    }
    I've tried commenting out various lines to see where the problem is but it seems it is not getting past the first time series line: if (Times[0][0].TimeOfDay == new TimeSpan(08,30,0)). How do I get past that line?

    Thanks,
    Ben

    #2
    Hello Ben,

    To confirm, if you add a print with the time one line above the return where the code stops executing for this bar, no print appears?

    If so, print above the condition the values in the condition along with the time of the bar to understand why this is not evaluating as true.

    Below is a link to a forum post that demonstrates using prints to understand behavior.
    https://ninjatrader.com/support/foru...121#post791121

    Print the time of the bar, print Times[0][0].TimeOfDay, print new TimeSpan(08,30,0).

    Save the output to a text file and attach this with your next post.
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Hi Chelsea,

      I will watch the video in more detail tomorrow. In the meantime I added the print lines as follows:

      Code:
       if (Times[0][0].TimeOfDay == new TimeSpan(08,30,0))
      {
      OpenPrice = Open[0];
      Print (Times[0][0].TimeOfDay);
      Print (new TimeSpan(08,30,0));
      return;
      I then ran it on the Strategy Analyzer. Not sure if there is another way to run it or where the output is supposed to be printed.

      Regards,
      Ben

      Comment


        #4
        Hi Chelsea, I'm working my way through the instructions from your last reply. Attached is a text file from the print file lines I inserted in my post above.
        Attached Files

        Comment


          #5
          Hello harr5754,

          If that print is appearing from within the condition set, then you know this is actually evaluating as true sometimes, which is helpful. Print the time of the bar and you can see for which bars that condition is evaluating as true. Are you certain this is the condition that is not evaluating as true?

          As a heads up, the return will stop all execution below that point for that run of the method.

          To find out why the condition is not true, the print should be above the condition, so that this prints on every bar, as demonstrated in the video linked in post #2.

          Code:
          Print(string.Format("{0} | Times[0][0].TimeOfDay: {1} == new TimeSpan(08,30,0): {2}", Time[0], Times[0][0].TimeOfDay, new TimeSpan(08,30,0));
          
           if (Times[0][0].TimeOfDay == new TimeSpan(08,30,0))
          {
          }
          Chelsea B.NinjaTrader Customer Service

          Comment


            #6
            Hi Chelsea, the only purpose of the "if (Times[0][0].TimeOfDay == new TimeSpan(08,30,0))" statement is to store the opening price to the variable "OpenPrice" as per the statement "OpenPrice = Open[0];".

            Is there a simpler way to do that?

            The return may have stopped the code I'm trying to execute. Do I need that to store the OpenPrice?

            Comment


              #7
              Hello harr5754,

              The first bar of a session is marked with Bars.IsFirstBarOfSession as true.


              if (Bars.IsFirstBarOfSession)
              OpenPrice = Open[0];

              You could also use the CurrentDayOHL().CurrentOpen[0] at any time.
              Chelsea B.NinjaTrader Customer Service

              Comment


                #8
                Thanks, Chelsea. I replaced the time series code with

                if (Bars.IsFirstBarOfSession)
                OpenPrice = Open[0];

                and got the following error message:

                Error on calling 'OnBarUpdate' method on bar 187: Index was outside the bounds of the array. Not sure what index and array this is about?

                Comment


                  #9
                  Hello harr5754,

                  Are you certain this line of code is causing the error?
                  Is OpenPrice a double?

                  Attached is a test script with these lines of code that is working.
                  Attached Files
                  Chelsea B.NinjaTrader Customer Service

                  Comment


                    #10
                    Hi Chelsea, it looks like your code printed out the session open for every 5 minute bar each day in the test time frame. My OpenPrice is a double and my code looks like it printed the session open for every bar for the first day in the test time frame and then it went off the reservation with the following:

                    Error on calling 'OnBarUpdate' method on bar 187: Index was outside the bounds of the array.

                    The bug must be somewhere else. Here is the rest of my code:

                    Code:
                     protected override void OnBarUpdate()
                    {
                    if (BarsInProgress != 0)
                    return;
                    
                    if (CurrentBars[0] < 0)
                    return;
                    
                    if (Bars.IsFirstBarOfSession)
                    OpenPrice = Open[0];
                    
                    Print(OpenPrice);
                    
                    // Set 1
                    if (
                    // Condition group 1
                    ((Times[0][0].TimeOfDay == new TimeSpan(8, 40, 0))
                    && (Low[0] > OpenPrice)
                    && (RSI1.Avg[0] < 50)
                    && (Closes[1][0] < Closes[1][2]))
                    // // Condition group 2
                    || ((Times[0][0].TimeOfDay == new TimeSpan(8, 40, 0))
                    && (Low[0] > OpenPrice)
                    && (Close[0] > Close[1])
                    && (Close[0] > Close[2])
                    && (Closes[1][0] < Closes[1][2]))
                    )
                    {
                    EnterLong(Convert.ToInt32(DefaultQuantity), "");
                    }
                    if (BarsSinceEntryExecution() >= exit5_X)
                    {
                    ExitLong();
                    }
                    
                    }
                    Any suggestions on where to put my next print line?

                    Comment


                      #11
                      Hello harr5754,

                      The example I have provided gets the open on the first bar of the session, and then continues to print the same open value until the start of the next session.

                      Using prints on every line before an action, which print is the last print to appear before the error? (This would likely be the line of code causing the error)
                      Does the print of the open price appear in the output window?
                      Chelsea B.NinjaTrader Customer Service

                      Comment


                        #12
                        Hi Chelsea,

                        The print code you provided was very helpful. I moved it around while commenting out various parts of the conditions and was able to determine that the RSI line is at least one of the lines preventing trades. I am just looking to include a simple 5 bar RSI, but NinjaTrader includes a smoothing parameter which I'm not familiar with. NT would not let me set it 0 so I set it to 1.

                        Code:
                         else if (State == State.DataLoaded)
                        {
                        RSI1 = RSI(Close, 5, 1);
                        }
                        Code:
                         if (
                        // Condition group 1
                        
                        ((Times[0][0].TimeOfDay == new TimeSpan(8, 40, 0))
                        && (Low[0] > OpenPrice)
                        && (RSI1.Avg[0] < 50))
                        {
                        EnterLong(Convert.ToInt32(DefaultQuantity), "");
                        I generated the RSI code from the Strategy Builder, but something must have gone wrong somewhere. Would appreciate if you are able to see where the problem is.

                        Thanks,
                        Ben

                        Comment


                          #13
                          Hello Ben,

                          The Smooth parameter is used for making an average and must include at least 1 bar.

                          If this condition is not evaluating as true, above the condition set print the time of the bar and print all values used in the condition set and include proper labels to identify what each value is.

                          Print Time[0], print Times[0][0].TimeOfDay, print new TimeSpan(8, 40, 0), print Low[0], print OpenPrice, print RSI1.Avg[0], print 50.

                          Include the output with your next post.
                          Chelsea B.NinjaTrader Customer Service

                          Comment


                            #14
                            Hi Chelsea, Attached is the output file. Here is the code I added above Condition group 1:

                            Code:
                            Print(string.Format("{0} | Times[0][0].TimeOfDay: {1} == new TimeSpan(08,40,0): {2} ", Time[0], Times[0][0].TimeOfDay, new TimeSpan(08,40,0)));
                            Print(OpenPrice);
                            Print(Low[0]);
                            Print(RSI1.Avg[0]);
                            Print(50);
                            Not sure if I'm reading it correctly, but it looks like there were some days where it should have traded.
                            Attached Files

                            Comment


                              #15
                              Hello Ben,

                              It's best practice to properly label the values in prints to know what that value is and how the values are being compared without having to refer to the code.

                              For example:
                              Print(string.Format("{0} | Times[0][0].TimeOfDay: {1} == new TimeSpan(08,40,0): {2} &&\r\n{0} | Low[0]: {3} > OpenPrice: {4} && RSI1.Avg[0]: {5} < 50", Time[0], Times[0][0].TimeOfDay, new TimeSpan(08,40,0), Low[0], OpenPrice, RSI1.Avg[0]));

                              This would be to find out why the condition was not true on a specific bar.

                              If you are wanting to find out if the condition is ever true, add a print inside of the action block of the condition.
                              Print(string.Format("{0} | condition was true"));

                              Also, enable TraceOrders (in State.Configure) to see if the condition is true, but the order is being ignored, rejected, or cancelled.


                              I do see a print where the condition appears to be true:
                              8/3/2021 8:40:00 AM | Times[0][0].TimeOfDay: 08:40:00 == new TimeSpan(08,40,0): 08:40:00
                              4389
                              4381.5
                              20.9969907281974
                              50
                              Printing the time in the condition action block would confirm the condition did evaluate as true on that bar.

                              Then I would want to see the output from TraceOrders to see if this order was ignored. (such as from being in a position already or something)
                              Last edited by NinjaTrader_ChelseaB; 08-24-2021, 02:34 PM.
                              Chelsea B.NinjaTrader Customer Service

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by trilliantrader, 04-18-2024, 08:16 AM
                              4 responses
                              18 views
                              0 likes
                              Last Post trilliantrader  
                              Started by mgco4you, Today, 09:46 PM
                              1 response
                              10 views
                              0 likes
                              Last Post NinjaTrader_Manfred  
                              Started by wzgy0920, Today, 09:53 PM
                              0 responses
                              10 views
                              0 likes
                              Last Post wzgy0920  
                              Started by Rapine Heihei, Today, 08:19 PM
                              1 response
                              10 views
                              0 likes
                              Last Post NinjaTrader_Manfred  
                              Started by Rapine Heihei, Today, 08:25 PM
                              0 responses
                              10 views
                              0 likes
                              Last Post Rapine Heihei  
                              Working...
                              X