Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

conversion from OnBarClose to OnPriceChange

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

    conversion from OnBarClose to OnPriceChange

    Hi,
    I'm looking to convert an oscillator from an OnBarClose calculation to an OnPriceChange calculation. The problem I have is there is a counter which counts the number of consecutive bars the oscillator has been in overbought or oversold. Other than that, I think there are no other issues.

    Could you suggest how I can proceed with the conversion?

    Regards
    Kay Wai

    #2
    Hello kaywai,

    Thanks for your post.

    You can create your script with Calculate.OnPriceChange and then you can use the system bool IsFirstTickOfBar which will only be true once per bar to increment your counter.

    Note that this is the very first tick of the bar so you may want to evaluate the just closed bar [1] for the condition to count:

    if (IsFirstTickOfBar && theOscillator[1] is oversold)
    {
    myCounter++;
    }

    References:

    https://ninjatrader.com/support/help...either_cal.htm (this is a strategy example but it is an example of use)

    Comment


      #3
      Hi PaulH,
      That part of the oscillator worked nicely. thanks much!

      Another issue cropped up now. As the oscillator I'm working with uses Highs and Lows for its calculation, I'm seeing "rough" edges on the oscillator line itself and I need to press "F5" to reload ninjascript to remove the rough edge. How would I resolve this?

      Regards

      Kay Wai

      Comment


        #4
        Hello kaywai,

        Thanks for your post.

        When you reload Ninjascript what is happening is that the indicator is running through the historical data and is plotting based on the historical values of the finished bar. When a script is run on historical data, even though it is set to Calculate.OnPriceChange it will perform as Calculate.OnBarClose. This means that historically the oscillator plots are based on the values of the finished bar and when it reaches real-time and uses Calculate.OnPriceChange it is likely that the last price change of the bar does not represent the same values as the completed bar.

        You may want to change your coding so that the plotting of the oscillator is updated on the first tick of the bar using the final values of the just-closed bar.

        For example

        if (IsFirstTickOfBar)
        {
        // your calculations for plotting here
        yourPlot[1] = your calculated value; // reset the previous bar plot to the final values
        }

        Comment


          #5
          Hi PaulH.

          Thank you for your reply. Let me try to figure that out.

          Regards
          Kay Wai

          Comment


            #6

            PaulH, I appreciate your post explaining the functionality of Reload NinjaScript. I hope it's Ok that I jump onto this thread to discuss an indicator.

            The indicator I have is set to calculate "On each tick" in the live market and produce signals, but these signals plot differently compared to the following two scenarios. When I open a new chart without tick-replay enabled and when I reload NinjaScript with live market data. In these two scenarios the signals are exactly the same. Any signals that were plotted live display differently after a Reload NinjaScript.

            I did a comparison and I don't want the live market indicator signals. I want the indicator signals as if I just populated a new chart (without tick-replay) or if a "Reload NinjaScript" was performed on a live chart. I thought that a simple solution would be to change the setting to calculate "On bar close." However, the indicator does not plot in the live market "On bar close." The indicator settings are below and I'll explain what those represent.

            Momentum Period Type: Minute
            Momentum Period Value: 1
            Standard Deviation Period: 15
            Standard Deviation Multiplier: 2

            The settings represent 15 one minute bars, that's tracking the average of the delta over those 15 one minute bars. The indicator signals when it gets at or above 2 standard deviations from that 15 period average. In this case the indicator will plot on the bar with either 2 arrows (2 standard deviations threshold met) or 3 arrows (3 standard deviations threshold met.) If the Standard Deviation Multiplier is changed from 2 to 1, then the indicator will plot on the bar with either 1, 2, or 3 arrows.

            Is it required to calculate "On each tick" in real-time in order to track the average delta over 15 one minute bars? Is it possible to get the delta of one minute bars "On bar close?" Could the reason why the indicator does not plot when set to "On bar close" in the live market is because of an existing check in the code (if "On bar close" is in place then do nothing.) Could it be that "On bar close" is disable in the code? If the indicator was able to calculate "On bar close" in the live market would those signals be identical to the "Reload NinjaScript" signals that I prefer, but done on a per bar basis instead of all NinjaScripts.

            Comment


              #7
              Hello Tavor,

              Thanks for your post.

              The first step is always to check the "Log" tab of the NinjaTrader control center for any error messages. When indicator errors out you would not see any plots at all.

              What I would suggest here is to debug your indicator using Print statements to help understand why the Plot would not be plotting when using calculate.OnBarClose with live data.

              Typically you could use Print(Time[0]+ (indicator value here).ToString());

              Reference: https://ninjatrader.com/support/help...html?print.htm

              If this is not an indicator you have created you may want to go back to the source and ask those questions as only someone who has the source code would really be able to answer them.


              Comment


                #8
                PaulH, thank you for the recommendations. If the indicator behind the scenes is tracking the average of the delta over 15 one minute bars and then produces a signal onto a non-time based bar when certain standard deviations thresholds are met, it should be able to plot those signals "On bar close?" Correct? If it's set to "On bar close," in the live market, does it look back on the historical data and plots on the historical values of the finished bar? Can you get and track the delta of bars from historical data without real-time tick data?

                Comment


                  #9
                  Hello Tavor,

                  Thanks for your reply.

                  Please review the help guide here concerning Multi timeFrame as this will show you the difference between on bar close and not: https://ninjatrader.com/support/help...nstruments.htm

                  I could not advise how your indicator works as I have not seen (and am not asking to see) the script.

                  To answer your questions you would need to add print statements to observe how and what data the indicator is using when.

                  "Can you get and track the delta of bars from historical data without real-time tick data?", Yes if you have access to historical tick data.

                  Comment


                    #10
                    Hi PaulH,

                    Referring back to the same indicator that we've been discussing above. The final part of the indicator combines the oscillator with price action. And when the conditions are met, a down or up arrow is printed on the price panel depending on the condition. Trouble is I can't get the arrows to print in "OnPriceChange" calculation and I've used "IsFirstTickOfBar" but it works fine in "OnBarClose" calculation. Am I missing something here? Please note that with the code I'm sharing below, I've taken it the Draw.Arrows away from "IsFirstTickOfBar".

                    Code:
                    if (TDREI >= 40)
                    {
                       if(IsFirstTickOfBar)
                       {
                          REIisoverbought = true;
                          if (REIisoverbought == true)
                          {
                               overboughtcounter++;
                               if (overboughtcounter == period)
                               {
                                  OBRect = CurrentBar;
                                  OBPOQTag = "OBPOQ"+CurrentBar;
                               }
                               if ((overboughtcounter >= period))
                               {
                                    Draw.Rectangle(this, OBPOQTag, true, CurrentBar-OBRect, 40, 0, 100, Brushes.Magenta, Brushes.Magenta, 10);
                               }
                            }
                         }
                        if (overboughtcounter < period)
                        {
                              if ( Close[1] < Close[2])
                              {
                                  if (Open[0]> Low[1])
                                  {
                                      if (Low[0] < Low[1])
                                      {
                    
                                          Draw.ArrowDown(this,"BuyPut1"+CurrentBar, true, 0, High[0]+ 0.1, Brushes.Blue, true);
                    
                                      }
                                  }
                              }
                              if (Close[1] < Close[2])
                              {
                                  if (Open[0] < Low[1])
                                  {
                                      if ( Open[0] < Low[2])
                                      {
                                          Draw.ArrowUp(this, "BuyCall2"+CurrentBar, true, 0, Low[0] - 0.1, Brushes.Orange, true);
                                      }
                                  }
                              }
                             else return;
                         }
                    }
                    Hope you can help.

                    Regards
                    Kay Wai
                    Last edited by kaywai; 01-08-2022, 12:06 PM.

                    Comment


                      #11
                      Hello kaywai,

                      Thanks for your post.

                      When you use IsFirstTickOfBar this is occurring on the very first tick of the currently forming bar and if you want to draw arrows based on the closed bar conditions then you would need to refer to [1] not [0] in your bars ago. [1] would be the just closed bar and [0] is the currently forming bar and you are on the very first tick of this bar.



                      Comment

                      Latest Posts

                      Collapse

                      Topics Statistics Last Post
                      Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                      0 responses
                      578 views
                      0 likes
                      Last Post Geovanny Suaza  
                      Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                      0 responses
                      334 views
                      1 like
                      Last Post Geovanny Suaza  
                      Started by Mindset, 02-09-2026, 11:44 AM
                      0 responses
                      101 views
                      0 likes
                      Last Post Mindset
                      by Mindset
                       
                      Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                      0 responses
                      554 views
                      1 like
                      Last Post Geovanny Suaza  
                      Started by RFrosty, 01-28-2026, 06:49 PM
                      0 responses
                      551 views
                      1 like
                      Last Post RFrosty
                      by RFrosty
                       
                      Working...
                      X