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

Wont Print() in OnBarUpdate

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

    Wont Print() in OnBarUpdate

    Update: its printing

    but CurrentBar returns bars 1 to all the bars of the chart instead of price of the currentbar?
    Last edited by frankduc; 08-26-2019, 06:56 AM.

    #2
    Hello frankduc,

    Thanks for your post.

    The statement, Print(CurrentBar); will print to the output window and it will print every time the OnBarUpdate() method is called. If this is not printing then please check your "log" tab for any errors related to the script you are using. I would also suggest printing a label so you know what is expected, for example Print ("CurrentBar: "+CurrentBar); If you are printing the same info from different methods then you may want to further expand on the labels, for example: Print ("In OBU, CurrentBar: "+CurrentBar);

    Regarding the statement, "Print(index);" I could not advise as you have not clarified if the variable "index" is declared at the class level, or is declared in the OnBarUpdate() or is declared in OnRender(). Do you see any compile errors? As before I would suggest adding further labels to your print statement to help you know where you are printing from.

    Do you see any errors in the "Log" tab of the control center related to your script when it is run?
    Paul H.NinjaTrader Customer Service

    Comment


      #3
      I get this error in the OW.

      SampleDBAup': Error on calling 'OnBarUpdate' method on bar 510: Object reference not set to an instance of an object.

      Actually, I try to recode my indicator from OnRender to OBU.

      If i try to print foundindex it wont show up in OW but i suppose the error i mentioned above is related to foundindex.


      for(int barIndex = index; barIndex <= ChartBars.ToIndex; barIndex--)
      {
      sumOfVolumes4 += Bars.GetVolume(barIndex);
      if (sumOfVolumes4 >= sumvolfibo)
      {
      foundIndex = barIndex;
      Print("foundIndex" + foundIndex);
      break;

      Draw.VerticalLine(this, "tag1", foundIndex, Brushes.Green);

      }
      }

      Also the vertical line wont appear but its no surprise, currentbar = 510 and the first bar at the right is 1108. CurrentBar is first bar at the right, right?

      Many of my variables are at class level, like index, foundindex.


      Comment


        #4
        Hello frankduc,

        Thanks for your reply.

        When you get a run-time type error then the script will stop running which would be why you are not seeing anything further in the output window.

        You will need to resolve the error in order to see further prints. I would suggest printing all of the values you've shown (using individual print statements) to see which is the object not found and go from there.

        CurrentBar is the bar number of the bar that OnBarUpdate() is processing when OnBarUpdate() is called.
        Reference: https://ninjatrader.com/support/help...currentbar.htm
        Paul H.NinjaTrader Customer Service

        Comment


          #5
          If i input in my calculation (Close.GetValueAt(CurrentBar) it should give me the close price of the first bar on the right?

          Right now it gives me all the close price of bar 1 to bar 510. The object that gives me trouble is currentbar the rest is all ok. Do wee need to call currentbar at class level? not make sense anyway.

          Should i class level all my variables or keep them local in OBU?

          Comment


            #6
            Hello frankduc,

            Thanks for your reply.

            CurrentBar is a system property that you do not need to declare.

            In the OnBarUpdate, if you need the current close and you are using Calculate.OnEachTick or Calculate.OnPriceChange, then all you need is to use Close[0] which will provide the current price.

            The decision to class level the variable depends on if you intend to access them outside of the method they are declared in, otherwise you can declare them locally in the method they are used.
            Paul H.NinjaTrader Customer Service

            Comment


              #7
              I still get this error while trying to Print()

              Indicator 'SampleDBAup': Error on calling 'OnBarUpdate' method on bar 0: Object reference not set to an instance of an object.

              I did made the change for
              Close[0]
              and its on
              Calculate.OnEachTick.

              Its returning 2872 instead of 2861. I dont get it. For a moment it briefly returned the right answer.

              What's amazing my calculation of the cma is fine and is returning the right answer. To calculate the cma you need that close[0] return the right answer but when printed it show the wrong close.

              Comment


                #8
                Update from last post.

                I input Draw.VerticalLine(this, "tag1", foundIndex, Brushes.Green); using foundindex in OBU but keeping foundIndex calculated from OR and there's that hour gap between the 2 line. Any idea why?
                I mean one is calculated on RenderTarget.DrawLine(startPoint, endPoint, customDXBrush, 2);

                They should be both equal?
                Attached Files

                Comment


                  #9
                  Hello frankduc,

                  Thanks for your replies.

                  When using Calculate.OnEachTick, with live data, Close[0] is the current price, that is the actual current value of the instrument.

                  If you are actually looking at historical data then close would be the Close of the bar.

                  In Draw.VerticalLine(), the method expects this (foundIndex) to be a bars ago value to draw the line.
                  Paul H.NinjaTrader Customer Service

                  Comment


                    #10
                    In Draw.VerticalLine(), the method expects this (foundIndex) to be a bars ago value to draw the line.
                    I just found out about that.
                    Still even if i reverse foundIndex to turn it to barsago which i did, that does not explain why sometimes on a volume chart you dont get the same answer as a minute chart and i mean a huge difference between the two. Sometimes the line wont even appear for no reason at all, in particular when you use load data bases on bars. If the number of bars set in bars to load is not equal anymore to the number of bars in the chart the line wont show up unless you change the number of bars.

                    Also the line OBU is late compare to the line OnRender
                    RenderTarget.DrawLine.

                    I am not a fan of OBU or maybe there's something i did wrong.

                    Thanks anyway.
                    Last edited by frankduc; 08-26-2019, 12:20 PM.

                    Comment


                      #11
                      Hello frankduc,

                      Thanks for your reply.

                      "Sometimes the line wont even appear for no reason at all," Its been my experience that computer programs only do what they are programmed to do. If something is not functioning as expected then we advise debugging to verify your code is performing as expected or to help clarify your understanding of how the code is working. Reading the help guide on the Ninjascript methods used in the script will also help to generate an understanding how how the code should work.

                      An option you can always use is to hire a 3rd party programmer to create your indicator/strategy. If this is of interest please let us know and we can provide link(s) to these service providers.
                      Paul H.NinjaTrader Customer Service

                      Comment

                      Latest Posts

                      Collapse

                      Topics Statistics Last Post
                      Started by Barry Milan, Yesterday, 10:35 PM
                      5 responses
                      16 views
                      0 likes
                      Last Post NinjaTrader_Manfred  
                      Started by DanielSanMartin, Yesterday, 02:37 PM
                      2 responses
                      13 views
                      0 likes
                      Last Post DanielSanMartin  
                      Started by DJ888, 04-16-2024, 06:09 PM
                      4 responses
                      12 views
                      0 likes
                      Last Post DJ888
                      by DJ888
                       
                      Started by terofs, Today, 04:18 PM
                      0 responses
                      11 views
                      0 likes
                      Last Post terofs
                      by terofs
                       
                      Started by nandhumca, Today, 03:41 PM
                      0 responses
                      8 views
                      0 likes
                      Last Post nandhumca  
                      Working...
                      X