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

Open price of the year

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

    Open price of the year

    Hello

    I'm trying to get the open price for the year. I have the below code that I thought would work but its not. What am I doing wrong? Suggestion? Thanks

    Code:
                moment = Times[1][0];
                moment2 = Times[2][0];
    
                int day = moment.Day;
                int month = moment.Month;
                
                if(day == 1 && month == 1)
                {
                    d = CurrentDayOHL(BarsArray[1]).CurrentOpen[0];
                }
                
                int day2 = moment2.Day;
                int month2 = moment2.Month;
                
                if(day2 == 1 && month2 == 1)
                {
                    e = CurrentDayOHL(BarsArray[2]).CurrentOpen[0];
                }

    #2
    You can print how many days back the first day of the year is back like this

    Print(""+Time[0].DayOfYear);

    Then just use that value to call the open of that bar if your on the daily chart. Otherwise add another bar series if you are looking at intraday bars..

    int DaysBackToFirstDay = Time[0].DayOfYear);

    Open of year then = Open[DaysBackToFirstDay]

    Comment


      #3
      Hello Calhawk01,

      Thank you for your post.

      Marty087's example is good for what you are looking for. Have you tried it out?
      Cal H.NinjaTrader Customer Service

      Comment


        #4
        Originally posted by NinjaTrader_Cal View Post
        Hello Calhawk01,

        Thank you for your post.

        Marty087's example is good for what you are looking for. Have you tried it out?

        Man i wish i was good as your guys at coding lol. I just tried a million different ways to leverage what marty said. Nothing works. I was able to get # of days back, but then when I use it, it says im accessing a bar that doesnt exist. Loaded lots of data too. THats just one of the error. Tried different ways to get my answer, didnt work (thats what I usually do lol).

        for now, I ended up just creating a double variable, that i can manually change each time i load a chart, it represents the open of the year. very manual but it works. Super annoying. So i would super duper appreciate if you can share the code!

        Comment


          #5
          Hi CalHawk,

          Scrap the last solution, I didn't think that this doesn't allow for weekends and would have returned the wrong open anyway.

          See code below. I reckon the best way to do it is to add the month data series as seen in the initialize section below. Then call the open of the first month. In order to know how many months back to call we need to know what month we are in.

          I dont know why I had to -2 from month to produce the correct open I thought it would have to be -1..... But -2 works. If you end up getting Decembers open instead of Jan, change that [month-2] section to [month-1]... Could have something to do with with market being closed... who knows!

          cheers.



          Code:
                  protected override void Initialize()
                  {
                    
                      Overlay				= false;
          	     Add(PeriodType.Month, 1);
          
                  }
          
                  /// <summary>
                  /// Called on each bar update event (incoming tick)
                  /// </summary>
                  protected override void OnBarUpdate()
                  {
                     	if (CurrentBar < 12)return; 
          			
          		DateTime moment = Time[0];
          			
          		int month = moment.Month;
          			
          		Print("OPEN OF YEAR - "+Opens[1][month-2]);
          			
                  }

          Comment


            #6
            Code:
                        if (CurrentBarArray[0] > 30 && CurrentBarArray[1] > 30 && CurrentBarArray[2] > 30)
                        {
                            if (BarsInProgress == 0) 
                            {
                        
                            a = Closes[2][0]-Opens[2][30];
                            b = Closes[1][0]-Opens[1][30];
                            c = a/ticksize2;    
                            d = b/ticksize1;
                            e = c*usd2;
                            f = d*usd1;
                            g = e-f;
                            
                            {
                                Ratio.Set(g);
                            }
                            }
                        }
            finally got this to work, going to just look back 30 days and use that as the starting point.

            however, if I change the Opens[2][250] // look back 250 bars, I get an error for nothing have enough bars. How can I fix that? do I have to change the current bar to 250? Isnt there another way to do this? Because I set currentbar > 250, then i'm essentially not using 1 year worth of data

            Comment


              #7
              CalHawk01,

              You need to load the same number of days that you want to look back for all your added data series.

              If you look back for 30 days on your primary for a 1 min and then want to get the 250th bar on a monthly chart, these are two vastly different amount of days.

              The monthly chart would only have 1 as you requested only 30 days.

              Try increasing the number of days look back to get more days and more bars for the higher time-frame data series.

              Let me know if I can be of further assistance.
              Cal H.NinjaTrader Customer Service

              Comment


                #8
                "3/21/2015 10:22:25 PM Default Error on setting indicator plot for indicator 'Year'. Value outside of valid range. "

                Code:
                        protected override void OnBarUpdate()
                        {
                            
                            if (Time[0].DayOfYear == 1)
                            {
                                a = CurrentDayOHL(BarsArray[2]).CurrentOpen[0];
                            }
                            
                            if (Time[0].DayOfYear == 1)
                            {
                                b = CurrentDayOHL(BarsArray[1]).CurrentOpen[0];
                            }
                
                            
                            c = (Closes[2][0]/a-1);
                            d = (Closes[1][0]/b-1);
                                e = (c - d);
                            
                            
                            if (CurrentBarArray[0] > 0 && CurrentBarArray[1] > 0 && CurrentBarArray[2] > 0)
                            {
                                if (BarsInProgress == 0) 
                                {
                                    Ratio.Set(e);
                                }
                            }
                            
                        }
                Why the error?

                Comment


                  #9
                  Hello calhawk01,

                  Verify you have enough bars for each series.
                  Code:
                  if(CurrentBars[0] <= BarsRequired
                  || CurrentBars[1] <= BarsRequired
                  || CurrentBars[2] <= BarsRequired)
                  return;
                  Currently, you check the Time[0] but do not specify which bar series and then proceed to pull an indicator value from a specific bar series.

                  Comment


                    #10
                    Originally posted by calhawk01 View Post
                    "3/21/2015 10:22:25 PM Default Error on setting indicator plot for indicator 'Year'. Value outside of valid range. "

                    Code:
                            protected override void OnBarUpdate()
                            {
                                
                              [COLOR="Blue"]  if (Time[0].DayOfYear == 1)[/COLOR]
                                {
                                    a = CurrentDayOHL(BarsArray[2]).CurrentOpen[0];
                                }
                                
                                [COLOR="blue"]if (Time[0].DayOfYear == 1)[/COLOR]
                                {
                                    b = CurrentDayOHL(BarsArray[1]).CurrentOpen[0];
                                }
                    
                                
                                c = (Closes[2][0]/a-1);
                                d = (Closes[1][0]/b-1);
                                    e = (c - d);
                                
                                
                                if (CurrentBarArray[0] > 0 && CurrentBarArray[1] > 0 && CurrentBarArray[2] > 0)
                                {
                                    if (BarsInProgress == 0) 
                                    {
                                        Ratio.Set(e);
                                    }
                                }
                                
                            }
                    Why the error?
                    Check for a "division by zero", which would occur if either of your preceding if statements (highlighted in blue) has not been processed.

                    Comment


                      #11
                      Finally got it to work!

                      Thanks both of you.

                      Code:
                              protected override void Initialize()
                              {
                                  Add(new Plot(Color.Black, "SpreadYTDUSD"));
                      
                                  
                                  
                                  Add(FirstInstrument, BarsPeriods[0].Id, BarsPeriods[0].Value);
                                     Add(SecondInstrument, BarsPeriods[0].Id, BarsPeriods[0].Value);
                      
                                  
                                  CalculateOnBarClose    = false;
                                  Overlay                = false;
                                  DrawOnPricePanel    = false;
                                  DisplayInDataBox    = true; 
                      
                              }
                              
                              private double a,b,c,d,e=0;
                      Code:
                              protected override void OnBarUpdate()
                              {        
                                  if(CurrentBars[0] <= BarsRequired
                                      || CurrentBars[1] <= BarsRequired
                                      || CurrentBars[2] <= BarsRequired)
                                      return;
                                  
                                  if (BarsInProgress == 0 ) 
                                  
                                  if (Times[1][0].DayOfYear == 1)
                                  {
                                      a = CurrentDayOHL(BarsArray[2]).CurrentOpen[0];
                                      b = CurrentDayOHL(BarsArray[1]).CurrentOpen[0];
                                  }
                                  
                                  if( b > 0 && a >0)
                                  {
                                      c = (((Closes[2][0]-a)/ticksize2)*usd2);
                                      d = (((Closes[1][0]-b)/ticksize1)*usd1);
                                      e = (c - d);    
                                  }
                                      
                                      {
                                          Ratio.Set(e);
                                      }
                                  
                              }
                      three questions:

                      1) above only works on minute data for some reason. If i load daily bars, i get zero values. why?

                      2) how can i access ticksize & tick value of multiple instruments? So that I don't have to manually plug them in.

                      3) does this only work for ETH template? For some reason the above indicator only works when I load the ETH template. But zero values are returned on RTH
                      CurrentDayOHL(BarsArray[2]).CurrentOpen[0];
                      Last edited by staycool3_a; 03-25-2015, 08:40 PM.

                      Comment


                        #12
                        1) above only works on minute data for some reason. If i load daily bars, i get zero values. why?
                        The literature clearly says that you can use CurrentDayOHL() only on intraday bars.

                        Comment


                          #13
                          Hello calhawk01,

                          Thank you for your response.

                          1. Koganam is correct on the CurrentDayOHL().

                          2. It is generally not recommended to load BarsPeriods in Initialize() as the primary series bars may not have yet loaded. The only way to do this would be to input the values.

                          3. Are there any errors listed on the Log tab when this occurs?

                          Comment


                            #14
                            Originally posted by NinjaTrader_PatrickH View Post
                            Hello calhawk01,

                            Thank you for your response.

                            1. Koganam is correct on the CurrentDayOHL().

                            2. It is generally not recommended to load BarsPeriods in Initialize() as the primary series bars may not have yet loaded. The only way to do this would be to input the values.

                            3. Are there any errors listed on the Log tab when this occurs?


                            No errors

                            Is there a currentdayOHL type of variable that can be used for both Intra bars and daily?

                            and why does it only work with ETH template?
                            Last edited by staycool3_a; 03-26-2015, 06:26 PM.

                            Comment


                              #15
                              Originally posted by calhawk01 View Post
                              No errors

                              Is there a currentdayOHL type of variable that can be used for both Intra bars and daily?

                              and why does it only work with ETH template?

                              how about we try to get the price at 930 on the first day of the year:

                              Code:
                                          if (Times[1][0].DayOfYear == 1 && ToTime(Times[1][0]) == ToTime(9, 30, 0))
                                          {
                                              a = Closes[2][0];
                                              b = Closes[1][0];
                              
                                          }
                              this compiles but returns zero values. and why does the previous code only work on ETH??
                              Last edited by staycool3_a; 03-26-2015, 08:19 PM.

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by CortexZenUSA, Today, 12:46 AM
                              0 responses
                              0 views
                              0 likes
                              Last Post CortexZenUSA  
                              Started by usazencortex, Today, 12:43 AM
                              0 responses
                              2 views
                              0 likes
                              Last Post usazencortex  
                              Started by sidlercom80, 10-28-2023, 08:49 AM
                              168 responses
                              2,262 views
                              0 likes
                              Last Post sidlercom80  
                              Started by Barry Milan, Yesterday, 10:35 PM
                              3 responses
                              10 views
                              0 likes
                              Last Post NinjaTrader_Manfred  
                              Started by WeyldFalcon, 12-10-2020, 06:48 PM
                              14 responses
                              1,429 views
                              0 likes
                              Last Post Handclap0241  
                              Working...
                              X