Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

NinjaScript doesnt work on daily/weekly bars

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

    NinjaScript doesnt work on daily/weekly bars

    Hi guys,

    I have done this code:

    Code:
     protected override void OnBarUpdate()
            {
                // Use this method for calculating your indicator values. Assign a value to each
                // plot below by replacing 'Close[0]' with your own formula.
    			
    			Plot0.Set(Close[0]);
    			BarColor = Color.Yellow;
    			PlotColors[0][0] = Color.Yellow;
    			
    			if ((Close[0] > Close[1]))
    			{
    			PlotColors[0][0] = Color.Lime;
    				BarColor = Color.Green;
    			}
    			else
    			{
    				if ((Close[0] < Close[1]))
    				{
    					BarColor = Color.Red;
    					PlotColors[0][0] = Color.Red;
    				}
    			}
    			
                
            }
    Works fine on intraday data but doesn't work on daily or weekly bars.

    Any help?

    #2
    Hello rcfarias, and thank you for your question.

    I was able to get your indicator to work by adding the following line of code to the beginning of OnBarUpdate :

    Code:
    [FONT=Courier New]if (CurrentBar < 1) return;[/FONT]
    Please let us know if there are any other ways we can help.
    Jessica P.NinjaTrader Customer Service

    Comment


      #3
      Might I ask why it works on intraday without the CurrentBar < 1 check?

      Close[1] is never present for the first intraday bar. I assume NT makes an exception on intraday charts. Also I don't see these CurrentBar checks in the ninjascript for indicators like EMA that seem to plot starting before the chart has enough bars for the indicator's lookback period.

      Comment


        #4
        Hello borland, and thank you for your question.

        I will need to investigate intrabar v daily+ series to see why there is a difference in data processing, and in particular why the 0'th member is not always available.

        What made me check for CurrentBar > 1 to begin with, however, had little to do with any indicator's code in particular. It is likely that the other code you have seen did not explicitly reference data that has not been populated. However, in rcfarias' code, data such as Close[1] is referenced directly. Typically speaking, it is a good practice to make sure that the CurrentBar value is equal to or greater than any directly referenced BarsAgo or Period values.

        It is possible that, as intraday charts work off tick data, there is always a value available to the 0'th member of any array.
        Jessica P.NinjaTrader Customer Service

        Comment


          #5
          Hello Borland,

          As an update, I have composed the attached indicator, and tested this with both daily and 30 minute bars in my primary series. When the primary series uses any time series daily or greater, the indicator encounters no problems. When the primary series uses any intraday series, I receive this message :

          Error on calling 'OnBarUpdate' method for indicator 'TestDailyZero' on bar 0: You are accessing an index with a value that is invalid since its out of range. I.E. accessing a series [barsAgo] with a value of 5 when there are only 4 bars on the chart.
          This indicates that series values are not populated for daily series while an intraday series is present.

          If I modify this code so that Closes[1][0] is only checked when BarsInProgress == 1, I no longer see any errors.

          Putting all this together, this means that if any intraday data is available to your indicator or strategy at all, there will be a window in which OnBarUpdate is called with populated intraday values, and unpopulated daily+ values.

          Please let us know if there are any other ways we can help.
          Attached Files
          Jessica P.NinjaTrader Customer Service

          Comment


            #6
            JessicaP,

            Well, that's what I've been trying to do, but if I add too many days, no trade triggers happen. I've attached my simple strategy file.

            If I shorten the CurrentBar check for the daily to something like 20, then trades get triggered. But when they do trigger, they don't seem to aligned with the trade logic(indicators).

            Even if I configure the 5m chart's Days To Load at 200, the chart only populates about 7 months of bars.

            Do you see what's wrong with my script? How do I load more days with each of the Added dataseries?
            Attached Files

            Comment


              #7
              Hello borland,

              In light of the extra investigation I did since you asked me your question earlier, and what I learned answering it, I would highly recommend relying on a flag that is set the first time BarsInProgress == 5, and another flag that is set the first time BarsInProgress == 8 (in the example you gave me, these are the indices of your 2 daily series), and then only proceeding to read data from these series once both these flags have been tripped. Here is an example,

              Code:
              [FONT=Courier New]        // Other variables
                      private bool flagFor5 = false;
                      private bool flagFor8 = false;[/FONT]
              ...

              Code:
              [FONT=Courier New]protected override void OnBarUpdate()
              {
                  flagFor5 = flagFor5 ? flagFor5 : BarsInProgress == 5;
                  flagFor8 = flagFor8 ? flagFor8 : BarsInProgress == 8;
                  if (flagFor5 && flagFor8)
                  {
                      // Daily bars are safe to use here
                  }
              }[/FONT]
              Please let us know if there are any other ways we may help.
              Jessica P.NinjaTrader Customer Service

              Comment


                #8
                Moving my post to a new thread.
                Last edited by borland; 07-18-2016, 06:45 PM.

                Comment


                  #9
                  Originally posted by NinjaTrader_JessicaP View Post
                  Hello rcfarias, and thank you for your question.

                  I was able to get your indicator to work by adding the following line of code to the beginning of OnBarUpdate :

                  Code:
                  [FONT=Courier New]if (CurrentBar < 1) return;[/FONT]
                  Please let us know if there are any other ways we can help.
                  It's working! Thanks a lot!

                  Comment

                  Latest Posts

                  Collapse

                  Topics Statistics Last Post
                  Started by Javierw.ok, Today, 04:12 PM
                  0 responses
                  2 views
                  0 likes
                  Last Post Javierw.ok  
                  Started by timmbbo, Today, 08:59 AM
                  2 responses
                  10 views
                  0 likes
                  Last Post bltdavid  
                  Started by alifarahani, Today, 09:40 AM
                  6 responses
                  40 views
                  0 likes
                  Last Post alifarahani  
                  Started by Waxavi, Today, 02:10 AM
                  1 response
                  18 views
                  0 likes
                  Last Post NinjaTrader_LuisH  
                  Started by Kaledus, Today, 01:29 PM
                  5 responses
                  15 views
                  0 likes
                  Last Post NinjaTrader_Jesse  
                  Working...
                  X