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

Overnight Levels not Working during RTH

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

    Overnight Levels not Working during RTH

    Hello,

    I wrote an indicator that plots the Overnight's Open, High and Low. When I use this indicator in a strategy, during hours between 1700 and 0830, any condition using those plots works perfectly. But when I attempt to use those plots (ONOpen, ONHigh, ONLow) between 0830 and 1700 (for example: if ( CrossBelow(Low, Overnight1.ONOpen, 1)) the strategy will take the trade at 0831 every time even though the condition is false.

    I used print statements to monitor this behavior. Is there another method I can use to help me find out why the indicator plots works only during the Overnight session and not the RTH session?
    Thanks.

    protected override void OnBarUpdate()
    {
    if(BarsInProgress != 0 )
    return;

    if (CurrentBars[0] < 1)
    return;


    if (Times[0][0].TimeOfDay >= new TimeSpan(17, 00, 00) || Times[0][0].TimeOfDay <= new TimeSpan(08,30, 00))
    {
    ONOpen[0] = CurrentDayOHL1.CurrentOpen[0];
    ONHigh[0] = CurrentDayOHL1.CurrentHigh[0];
    ONLow[0] = CurrentDayOHL1.CurrentLow[0];
    }

    }
    Attached Files

    #2
    Hello AdeptistJune,

    Thanks for your post.

    It may be best to provide a small and testable export that illustrates just the specific issue.

    Exporting as source code - https://ninjatrader.com/support/help...tAsSourceFiles

    It looks like the script is using an additional data series. What data series and Trading Hours templates are used?

    What is the condition that controls the "Condition true" print? What are the current values in this condition when it is is being evaluated?

    For example, is the condition: if (CrossBelow(Low, Overnight1.ONOpen, 1)) ?

    If so, what are Low[0], Low[1], Overnight1.ONOpen[0] and Overnight1.ONOpen[1] when the condition is checked?

    I look forward to hearing from you.
    JimNinjaTrader Customer Service

    Comment


      #3
      Hello Jim,

      I exported the files. When the condition is being checked, the low is Low[0], and Overnight1.ONOpen[0].
      Attached Files

      Comment


        #4
        Hello AdeptistJune,

        Thanks for providing those files, it helps answer some questions on additional data.

        We should keep in mind that Series<T>'s and plots will have one value associated with each bar, and you are assigning plot values only when the time check is true.

        To see why a condition is becoming true when you do not expect it, you would need to print out the values used to evaluate the condition.

        For example, you have:

        CrossAbove(Close, OvernightLevels1.ONHigh, 1))

        CrossAbove becomes true when the current value is above while the previous value was at or below. So in order to see how this condition evaluates, you would need to print Close[0], Close[1], OvernightLevels1.ONHigh[0], and OvernightLevels1.ONHigh[1]

        I would recommend using the following print to see why the condition became true.

        Code:
        Print(String.Format("Time[0]: {0} Close[0]: {1} Close[1]: {2}, OvernightLevels1.ONHigh[0]: {3} OvernightLevels1.ONHigh[1]: {4}", Time[0], Close[0], Close[1], OvernightLevels1.ONHigh[0], OvernightLevels1.ONHigh[1]));
        if ((Position.MarketPosition == MarketPosition.Flat)
          && (Times[0][0].TimeOfDay >= new TimeSpan(08, 30, 00) && Times[0][0].TimeOfDay <= new TimeSpan(17,00, 00))
          && CrossAbove(Close, OvernightLevels1.ONHigh, 1))
        Let us know if you have questions on the output from that find of print.
        JimNinjaTrader Customer Service

        Comment


          #5
          Hello, Mr. Jim,

          When you say plots will have one value associated with each bar: What does that mean?

          And yes, I'm assigning plot values only when the time check is true.

          Also, I just noticed in the prints that between 0830 and 1700,
          OvernightLevels1.ONHigh[0] is 0, and OvernightLevels1.ONHigh[1] is 0
          Last edited by AdeptistJune; 12-28-2021, 12:35 PM.

          Comment


            #6
            Hello AdeptistJune,

            A plot/series has one value per bar. For example, an SMA has 1 plot, and that plot has 1 calculated value per bar on the chart. Each calculated value in that plot represents a moving average of the last X bars.

            But when I attempt to use those plots (ONOpen, ONHigh, ONLow) between 0830 and 1700 (for example: if ( CrossBelow(Low, Overnight1.ONOpen, 1)) the strategy will take the trade at 0831 every time even though the condition is false.
            Understanding why this condition is becoming true when you evaluate it to be false would mean that the values need to be checked.

            In other words, to find out why the condition has become true at 8:31, we need to print out the values used in that condition at 8:31. The example print can show this, and you will see the current/previous Close price, and the current/previous ONHigh price. If the current Close is greater than the current ONHigh price, and the previous Close is at or less than the previous ONHigh price, the cross over condition will become true.

            If you are not assigning the plots a value, the value for that plot will be 0. If the plot values are 0 when the condition gets checked, it will affect how the CrossAbove condition evaluates. (Always use prints to see exactly how a condition evaluates if you do not expect it to be true.)

            If you need to hold onto plot values outside of your time check, you could consider using class level variables to hold onto the values, and reference the variables outside of the time check.


            JimNinjaTrader Customer Service

            Comment


              #7
              Mr Jim,

              I would like to hold onto plot values outside of my time check. Do you have any resources that will teach me how to use class level variables, so that I can reference the variables outside of the time check.

              Comment


                #8
                Hello AdeptistJune,

                You would create a class level double, and assign the variable the same time you assign your plot. Then outside of your time check, assign the plot value the value from your private variable.

                A publicly available C# resource on variable scope can be found here - https://www.geeksforgeeks.org/scope-...es-in-c-sharp/
                JimNinjaTrader Customer Service

                Comment


                  #9
                  Hello Jim,

                  Is there another way to hold onto plot values outside of your time check?

                  Comment


                    #10
                    Hello AdeptistJune,

                    Each bar will have a new plot value.

                    If you assign a plot value during your time check and want it to persist for new bars outside of the time check, you will have to assign plot values outside of the time check.

                    This could be done by using private variables and storing the plot value in the variable to recall outside of the time check, or you could consider assigning the current plot value with the plot value of the previous bar when you are outside of the time check.

                    I.E. (when timecheck if false) ONOpen[0] = ONOpen[1];

                    Note that there you need to make sure at least 1 bar is processed before you can reference barsAgo 1. Reference here
                    JimNinjaTrader Customer Service

                    Comment


                      #11
                      Hello, Jim

                      You are right. Now when I use print statements, the plot values are being assigned outside of the time check. I'm a visual and hands on learner. Thanks for being patient with me.

                      Comment

                      Latest Posts

                      Collapse

                      Topics Statistics Last Post
                      Started by Noerclou, Today, 04:55 AM
                      0 responses
                      2 views
                      0 likes
                      Last Post Noerclou  
                      Started by llanqui, Yesterday, 09:59 AM
                      2 responses
                      17 views
                      0 likes
                      Last Post llanqui
                      by llanqui
                       
                      Started by ThoriSten, Today, 03:56 AM
                      0 responses
                      6 views
                      0 likes
                      Last Post ThoriSten  
                      Started by PhillT, 04-19-2024, 02:16 PM
                      3 responses
                      23 views
                      0 likes
                      Last Post mangel2000  
                      Started by TraderBCL, Today, 02:37 AM
                      0 responses
                      4 views
                      0 likes
                      Last Post TraderBCL  
                      Working...
                      X