Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Stochastics K[1] returns a value of 0

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

    Stochastics K[1] returns a value of 0

    I'm trying to use the following code in a context in NT7:

    Signal=3;

    if ((Stochastics(3, 5, 2).K[0] - Stochastics(3, 5, 2).K[1] > 0)
    && (Stochastics(3, 5, 2).K[1] - Stochastics(3, 5, 2).K[2] > 0))
    Signal=2;

    Plot0.Set(Signal);

    But using "K[1]" or "K[2]" just makes the Plot0.Set return a "0"..
    Only "K[0]" is working.
    Why is that? Ain't I supposed to be able to use yesterdays Stochastics value?

    I can't even mention the K[1] or K[2] anywhere in the code.. no matter if plotted or not. As soon as K[1] or K[2] is mentioned, ANY variable chosen for the Plot0.Set will return a value of 0... until I put "//" before the code that contains the K[1] or K[2].

    What am I doing wrong?

    #2
    Hello,

    Thank you for the question.

    I am unsure of the answer based on what I can currently see, but I can provide a comment on what I can see.

    You said:
    ANY variable chosen for the Plot0.Set will return a value of 0
    In what you have provided Plot0 is being set to the value of Signal.
    You initially set Signal=3;

    But then you say it shows 0, that would indicate somewhere in your logic a 0 is being passed to the Set() method.

    You also said:

    I can't even mention the K[1] or K[2] anywhere in the code.. no matter if plotted or not
    Are you checking that this many bars exist before running the logic? I.E.
    Code:
    if(CurrentBar < 2) return;

    If you have a more complete sample that I could run, that would likely help to see the logic order.


    I look forward to being of further assistance.
    JesseNinjaTrader Customer Service

    Comment


      #3
      There is nothing in the code anywhere that sets the variable to "0".. That's part of what's so damn strange to me..
      Code looks like this in reality (I modified the example in my first comment just to point things out more clearly):

      protected override void OnBarUpdate()
      {

      SellSignal=1;

      if ((Stochastics(3, 5, 2).K[0] - Stochastics(3, 5, 2).K[1] > 0)
      && (Stochastics(3, 5, 2).K[1] - Stochastics(3, 5, 2).K[2] > 0))
      SellSignal=0;


      if ((Stochastics(3, 5, 2).K[0] - Stochastics(3, 5, 2).K[1] < 0)
      && (Stochastics(3, 5, 2).K[1] - Stochastics(3, 5, 2).K[2] < 0))
      SellSignal=0;


      Plot0.Set(SellSignal);

      Code is supposed to check for when Stockastics K changes direction.
      When I run it in Market Analyzer for all S&P500 stocks it just fills all the cells with the value "0".
      Most of them should not be 0 though.. according to statistics I mean in a majority of the time the Stochastics will be headed the same way as yesterday.

      But regardless.. all the cells remain 0 no matter what I do or try as long as K[1] or K[2] exist anywhere in the code..
      I can plot the price (Close[0]) instead f.x.. and that will also return a "0" until K[1]/[2] is removed from code.. or changed to [0].

      But I havent checked how many bars that exist... What is that? Don't they "exist" automatically? I mean the graphic chart is displayed with candlesticks back until several years ago... So they must exist somehow, or?
      Or how do I make them "exist" if they don't?
      Didn't get that part fully..

      Comment


        #4
        They don't "exist" if the current bar is bar 0. There can be not bar -1 so you need to not calculate until you have enough bars. If you are checking Stochastics(3, 5, 2).K[1] you need to make sure CurrentBar > 0, if accessing Stochastics(3, 5, 2).K[2] you need the CurrentBar to be > 1.
        eDanny
        NinjaTrader Ecosystem Vendor - Integrity Traders

        Comment


          #5
          Ok, I though current bar was always referred to as [0] in the code..??. and the bar before it automatically as [1] and so on...
          I must have missed something..
          In my world view, on a daily chart, the data coming from data provider is accessed by calling [0] for todays data.. and [1] for yesterdays data..

          Comment


            #6
            Well I added the code below as suggested earlier. Now it seems to work... I just don't understand what the code means.. Its just checking if enough bars or what? But what if there isn't? Or what decides how many bars there are? I mean in the chart there's 1000's of bars..
            if(CurrentBar < 2) return;

            Comment


              #7
              Hello,

              I'm glad you have resolved the problem, to answer your question
              Its just checking if enough bars or what?
              Exactly, this statement checks if the current bar is less than the specified amount of bars you will need. return prevents further code from happening. This would prevent the OnBarUpdate from executing further logic until enough bars are there.

              What happens if there are not enough bars? eDanny had explained this, We go into negative indexes which don't exist. The lowest index on the chart is 0 or the left most bar, on bar 0 if you want 2 bars ago that would be -2 which is invalid. On bar 2 if we go 2 bars back we get bar 0 which does exist.

              I look forward to being of further assistance.
              JesseNinjaTrader Customer Service

              Comment


                #8
                Ok, thanks. But you say the following:
                "This would prevent the OnBarUpdate from executing further logic until enough bars are there. "

                Yes.. but what makes enough bars be there then? What command loads them into memory making them enough if there are to few?

                Also.. are there no connection between this amount of bars and the bars seen on the graphical candlestick chart? Because I've set the "days to load" to at least 1 year of data in "data series" menu on the charts..

                I don't quite get the pieces together in my head

                Comment


                  #9
                  but what makes enough bars be there then? What command loads them into memory making them enough if there are to few?
                  CurrentBar is what determines if you are on a bar that is past the amount of data you need. This gets incremented for each bar that is processed.

                  OnBarUpdate is the command that is called for each bar when it gets processed.

                  When you specify X number of days to load in a chart, the platform retrieves the historical data for that time and that would be the amount of bars you initially start with.

                  After the bars load, every bar gets processed from left to right. This process goes bar by bar from index 0 or the left most bar up to the right most bar on the chart. The CurrentBar is a reference to what number bar is currently being processed. Also by left most I mean the oldest data to the newest data spanning the distance you can scroll the chart, not just what is visible.


                  Also.. are there no connection between this amount of bars and the bars seen on the graphical candlestick chart?

                  The connection is the time you are looking at. The chart window can only be so big and the data can be moved around. The visible bars are only that, the bars the are currently visible based on what time you are viewing, the scale and compression of the chart.


                  I look forward to being of further assistance.
                  JesseNinjaTrader Customer Service

                  Comment


                    #10
                    I must be missing some fundamental part in understanding this..

                    Say I have a chart in front of me with fx 365 days loaded. How do I refer to the most recent bar? In my view I refer to it as [0]....
                    Because if I want to plot the current price I write the code: plot.set(Close[0]).... and that always works....

                    Or am I supposed to refer to it as [365]?? I mean I don't even need to have a chart in front of me to write script for the Market Analyzer... what are the number of bars then being loaded? How do I then know the bar number when writing a script? I've always just used the [0]... F.x. Close[0]... not [365]..

                    Comment


                      #11
                      Hello m96gran,

                      To clarify, CurrentBar is a counter.

                      OnBarUpdate() will trigger for each bar of historical data that loads, starting with the first bar. As each bar triggers OnBarUpdate, this increases the CurrentBar counter.

                      In other words, if you Print(Close[0]); in your script, you won't just get a print for the last bar. You will get a print for every bar on the chart. Close[0] refers to the current bar that is processing including the historical bars that all process before bars made in real-time will close and also trigger OnBarUpdate().

                      The issue is, the first time OnBarUpdate runs for the first bar of historical data that would be processing when you scroll all the way to the left side of the chart, to the very first bar of historical data, CurrentBar has a value of 0.

                      If you scroll all the way to the left your chart, and some asks you, what color is the bar 1 bar before this bar? what would you say? You couldn't answer because there is no bar before the first on the chart.

                      NinjaTrader is giving you the same response.
                      When you use a bars ago value, you are asking what is the value of the bar one bar ago or two bars ago. But if you ask this on the very first bar, NinjaTrader will tell you there is no bar 1 bar ago and gives this indexing error.

                      The advice in this thread is that if you wait until there are at least three bars, and CurrentBar has a value of 2, and then ask what the value of the bar two bars ago, then there will actually be a bar two bars ago since were now processing on bar three. So no error will occur.

                      Adding a return stops the script from processing. This way the code that checks the value 2 bars ago, doesn't get called until at least 3 bars have processed.

                      You could even prevent the script from processing historically at all and only process when real-time bars start to close with:
                      if (Historical)
                      return;

                      The value of MaximumBarsLookBack is not involved here. This is used for performance to cause scripts to load quicker but never loading any more than 256 bars when set to MaximumBarsLookBack.TwoHundredFiftySix or loads all bars when set to MaximumBarsLookBack.Infinite.
                      Last edited by NinjaTrader_ChelseaB; 10-23-2016, 03:09 PM.
                      Chelsea B.NinjaTrader Customer Service

                      Comment


                        #12
                        Yes I understand that there must be enough bars. But there are 365 bars on the chart!.. or 5000 bars!.. so what do you mean by there must be enough bars?
                        There is always more than 2 bars on my charts...... Its not that I'm looking at a chart with only 1 bar existing on it...

                        And what if I close the chart? How many bars are there then? The script run in the market analyzer works independently of if a chart is open or not.....
                        Last edited by m96gran; 10-23-2016, 05:29 PM.

                        Comment


                          #13
                          Hello m96gran,

                          You may not quite be understanding that the bars on the chart don't quite matter in this way.

                          The script processes each historical data bar in order.

                          When the very first bar is being processed there are not 365 or 5000 bars or however many bars you can see on your chart.

                          When the first historical data bar processes, there is 1 bar only.

                          Does this make sense?

                          When the second historical data bar processes, there are 2 bars only.
                          Last edited by NinjaTrader_ChelseaB; 10-23-2016, 05:37 PM.
                          Chelsea B.NinjaTrader Customer Service

                          Comment


                            #14
                            Yes it definitely makes sense that the bars on the chart don't matter. Then I can forget about those and the settings for how many bars to load on the chart. Ok.

                            So... how do I tell the system to load enough bars then?
                            How can I see how many bars that have been loaded? ..and how do I tell it to load more bars if there is not enough bars? Whats loading them?? That's what I don't get.

                            Comment


                              #15
                              Hello m96gran,

                              Instead of number of bars loaded, think number of bars processed so far.

                              You know how many bars have processed so far because of the CurrentBar counter. Every time a bar processes, CurrentBar is increased by 1.

                              So if you want to make sure 3 bars have processed, make sure CurrentBar is 3 or higher.
                              Chelsea B.NinjaTrader Customer Service

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by ScottWalsh, 04-16-2024, 04:29 PM
                              6 responses
                              27 views
                              0 likes
                              Last Post ScottWalsh  
                              Started by frankthearm, Today, 09:08 AM
                              10 responses
                              35 views
                              0 likes
                              Last Post frankthearm  
                              Started by GwFutures1988, Today, 02:48 PM
                              0 responses
                              2 views
                              0 likes
                              Last Post GwFutures1988  
                              Started by mmenigma, Today, 02:22 PM
                              1 response
                              3 views
                              0 likes
                              Last Post NinjaTrader_Jesse  
                              Started by NRITV, Today, 01:15 PM
                              2 responses
                              9 views
                              0 likes
                              Last Post NRITV
                              by NRITV
                               
                              Working...
                              X