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

Calculate.OnBarClose and Calculate.OnEachTick

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

    Calculate.OnBarClose and Calculate.OnEachTick

    Hi,
    In the indicator I'm doing I need to execute part of the script on each Tick and the other part on bar close, ¿ how could I do it ?

    Thanks in advance.

    #2
    Hello superg3,

    Thanks for your post.

    You would set your code to Calculate.OnEach tick and then use the bool IsFirstTickOFBar to execute your once per bar code. For example

    protected override void OnBarUpdate()
    {
    if (IsFirstTickOfBar)
    {
    // execute code in here once per bar
    }
    // execute code here on each tick.


    We have a reference sample here: https://ninjatrader.com/support/help...either_cal.htm

    Paul H.NinjaTrader Customer Service

    Comment


      #3
      Originally posted by NinjaTrader_PaulH View Post
      Hello superg3,

      Thanks for your post.

      You would set your code to Calculate.OnEach tick and then use the bool IsFirstTickOFBar to execute your once per bar code. For example

      protected override void OnBarUpdate()
      {
      if (IsFirstTickOfBar)
      {
      // execute code in here once per bar
      }
      // execute code here on each tick.


      We have a reference sample here: https://ninjatrader.com/support/help...either_cal.htm
      Hi PaulH, I´m very gratefull for your continuous help.

      I've checked the info and added if (IsFirstTickOfBar) in different ways without succes. Please could you check if it is aplyed correctly in my code?

      Code:
      protected override void OnBarUpdate()
              {
                  Value[0] = Instrument.MasterInstrument.InstrumentType == InstrumentType.CryptoCurrency ? Core.Globals.ToCryptocurrencyVolume((long)Volume[0]) : Volume[0];
      
                  if (Value[0] >= VolRelev1)               
                      {
                      PlotBrushes[0][0] = ColorVolRel1;  // Do on each tick
      
                          if (IsFirstTickOfBar)                // Do on bar close
                              {
                                  if (Close[0] > Open[0])                                
                                  {
                                  DrawOnPricePanel            = true;
                                  Draw.Rectangle(this, "tag1" + CurrentBar, false, 0, Close[0] - TickSize, LongitudZona*-1, High[0] + TickSize, ColorMarcoZonaUp, ColorFondoZonaUp, OpacidadFondoZona);
                                  }                            
                              }
      
                          if (IsFirstTickOfBar)          // Do on bar close
                              {        
                                  if (Close[0] < Open[0])
                                  {
                                  DrawOnPricePanel            = true;
                                  Draw.Rectangle(this, "tag2" + CurrentBar, false, 0, Close[0] + TickSize, LongitudZona*-1, Low[0] - TickSize, ColorMarcoZonaDown, ColorFondoZonaDown, OpacidadFondoZona);
                                  }
                              }
      
                          if (IsFirstTickOfBar)       // Do on bar close
                              {    
                                  if (Close[0] == Open[0])
                                  {    
                                  DrawOnPricePanel            = true;
                                  Draw.Rectangle(this, "tag3" + CurrentBar, false, 3, Close[0] + TickSize, -3, Open[0] - TickSize, Brushes.Transparent, Brushes.Blue, OpacidadFondoZona);
                                  }    
                              }    
      
                      }
      
                  if (Value[0] >= VolRelev2)              
                      {
                      PlotBrushes[0][0] = ColorVolRel2;  // Do on each tick
                      }    
      
              }

      Thank you very much.

      Comment


        #4
        Hello superg3,

        Thanks for your reply.

        Something to keep in mind is that when using Calculate.OnEachTick or Calculate.OnPriceChange that the bar reference changes. [0] now points to the currently forming bar, therefore when it is the first tick of the bar, Close[0] will equal Open[0] and will equal High[0] and will equal Low[0] because it is the first and only tick on the bar. You would need to change your bar references to the just closed bar which are now at the index of [1], IE: if (Close[1] > Open[1])

        Does it make sense to check for the first tick of the bar only when Value[0] >= VolRelev1 ? If not then move it outside of that so that it can operate when it is the first tick of the bar because if Value[0] >= VolRelev1 is not true the code under that will not execute.

        You can combine all three of your bar condition checks under a single check of IsFirstTickOfbar, there is no need for 3 separate sections, although you can certainly do that if you prefer.

        The assignment DrawOnPricePanel = true; typically would only be done once and that would be at the top in the OnStateChange() under State.SetDefaults. You would only use this in OnBarUpdate() if you wanted to change from drawing objects in the price panel to an associated indicator panel and back.
        Paul H.NinjaTrader Customer Service

        Comment


          #5
          Hello PaulH,

          I will try to explain the behaviour I expect. Escuse my bad English.

          1.-The code should check if Value[0] >= VolRelev1 is true during volume bar formamation on each tick ( to view the bar formation) and if true paint it with specific color.
          2.- Also if Value[0] >= VolRelev1 is true When the volume bar ends to be formed , a rectangle should be drawn in the especific region of the asociated candle to the recenttly formed volume bar.

          I'm using if (Close[0] > Open[0] , if (Close[0] < Open[0] and if (Close[0] == Open[0]), to determine if the candle is green or red and draw the rectangle in the uper or lower wick.
          Last edited by superg3; 02-28-2019, 09:06 AM.

          Comment


            #6
            Hello superg3,

            Thanks for your reply and clarification.

            If you are not getting the expected performance then you will want to go through a debugging process to help you see what is going on and when within your code. Debugging typically involves print statements that are set to the New>Ninjascript output window. Here is a link to our debugging tips: https://ninjatrader.com/support/help...script_cod.htm
            Paul H.NinjaTrader Customer Service

            Comment


              #7
              Originally posted by NinjaTrader_PaulH View Post
              Hello superg3,

              Thanks for your reply and clarification.

              If you are not getting the expected performance then you will want to go through a debugging process to help you see what is going on and when within your code. Debugging typically involves print statements that are set to the New>Ninjascript output window. Here is a link to our debugging tips: https://ninjatrader.com/support/help...script_cod.htm

              Hello PaulH,

              here is the code how I was able to solve the problem,

              Code:
                      protected override void OnBarUpdate()
                              {
                                  Value[0] = Instrument.MasterInstrument.InstrumentType == InstrumentType.CryptoCurrency ? Core.Globals.ToCryptocurrencyVolume((long)Volume[0]) : Volume[0];
              
                                  if (Value[0] >= VolRelev1)               
                                      {
                                      PlotBrushes[0][0] = ColorVolRel1;      // Do on each tick
                                      }    
              
                                  if (Value[0] >= VolRelev2)              
                                      {
                                      PlotBrushes[0][0] = ColorVolRel2;      // Do on each tick
                                      }
              
                                  if (CurrentBar < 1)                        // avoid out-of-range error
                                     return;
                                  if (IsFirstTickOfBar)                   // Do on bar close
                                      {
                                          if (Value[1] >= VolRelev1)
                                              {
                                                     if (Close[1] > Open[1])                                
                                                         {    
                                                      DrawOnPricePanel            = true;
                                                      Draw.Rectangle(this, "tag1" + CurrentBar, false, 1, Close[1] - TickSize, LongitudZona*-1, High[1] + TickSize, ColorMarcoZonaUp, ColorFondoZonaUp, OpacidadFondoZona);
                                                      }
                                                   if (Close[1] < Open[1])
                                                      {
                                                      DrawOnPricePanel            = true;
                                                      Draw.Rectangle(this, "tag2" + CurrentBar, false, 1, Close[1] + TickSize, LongitudZona*-1, Low[1] - TickSize, ColorMarcoZonaDown, ColorFondoZonaDown, OpacidadFondoZona);
                                                      }
                                                  if (Close[1] == Open[1])
                                                      {    
                                                      DrawOnPricePanel            = true;
                                                      Draw.Rectangle(this, "tag3" + CurrentBar, false, 2, Close[1] + TickSize, 0, Open[1] - TickSize, Brushes.Transparent, Brushes.Blue, OpacidadFondoZona);
                                                      }    
                                              }     
                                       }    
                              }
              Thank you very much for your suppot.

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by algospoke, Yesterday, 06:40 PM
              2 responses
              19 views
              0 likes
              Last Post algospoke  
              Started by ghoul, Today, 06:02 PM
              3 responses
              14 views
              0 likes
              Last Post NinjaTrader_Manfred  
              Started by jeronymite, 04-12-2024, 04:26 PM
              3 responses
              45 views
              0 likes
              Last Post jeronymite  
              Started by Barry Milan, Yesterday, 10:35 PM
              7 responses
              20 views
              0 likes
              Last Post NinjaTrader_Manfred  
              Started by AttiM, 02-14-2024, 05:20 PM
              10 responses
              180 views
              0 likes
              Last Post jeronymite  
              Working...
              X