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

First bar loaded on a chart

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

    First bar loaded on a chart

    Code:
            protected override void Initialize()
            {
                Add(new Plot(Color.FromKnownColor(KnownColor.DarkViolet), PlotStyle.Line, "SPREAD"));
                
                Add(FirstInstrument, BarsPeriods[0].Id, BarsPeriods[0].Value);
                   Add(SecondInstrument, BarsPeriods[0].Id, BarsPeriods[0].Value);
    
                Overlay                = false;
            }
    Code:
                if (Times[1][0].DayOfYear == 7 )
                {
                    a = CurrentDayOHL(BarsArray[0]).CurrentOpen[0];
                    b = CurrentDayOHL(BarsArray[1]).CurrentOpen[0];
                }
                
                if( b > 0 && a >0)
                {
                    c = (Closes[0][0]/a)-1;
                    d = (Closes[1][0]/b)-1;
                    e = (c - d);    
                }
    hello,

    i have a fairly simple indicator that calculates a spread between two instruments. The reason why I used day 7 as the starting point for calculating the percentage return for the instrument is because i could not find a way to use the first bar loaded on the chart. So now it's really annoying because the indicator will not work if I don't have enough data loaded. In other words, I have to load a year worth of data so the "7th day" is also loaded on the chart for this indicator to work. Is there a way to avoid the above "starting point" so that any time i load a chart, it uses the first value as the starting point. In other words, if i load 30 days worth of data.. i want to use the open of the first bar on the chart for both instruments. a code sample will be very very appreciated.

    thank you in advance.

    #2
    Hello calhawk01,

    Thanks for your post.

    What is the base data series barperiod ID and value?
    Paul H.NinjaTrader Customer Service

    Comment


      #3
      Originally posted by NinjaTrader_Paul View Post
      Hello calhawk01,

      Thanks for your post.

      What is the base data series barperiod ID and value?
      what? are you talking about the data series?

      Code:
         Add(FirstInstrument, BarsPeriods[0].Id, BarsPeriods[0].Value);
                     Add(SecondInstrument, BarsPeriods[0].Id, BarsPeriods[0].Value);
      .id let's my data series adapt to whatever the underlying chart settings are and value also adapts to the underlying chart settings. but that's not my question. my question is regarding.. how do i get the first bar of the chart? no matter what the chart settings are.. if i'm in a weekly chart.. and there are 20 days loaded.. i want the first bar loaded on the chart as a starting point to be used in my calculation. if i have 181 days loaded and i'm on a daily chart.. i want the first day's value to be used in my calculation.. and so on..

      Comment


        #4
        Originally posted by calhawk01 View Post
        Code:
                protected override void Initialize()
                {
                    Add(new Plot(Color.FromKnownColor(KnownColor.DarkViolet), PlotStyle.Line, "SPREAD"));
                    
                    Add(FirstInstrument, BarsPeriods[0].Id, BarsPeriods[0].Value);
                       Add(SecondInstrument, BarsPeriods[0].Id, BarsPeriods[0].Value);
        
                    Overlay                = false;
                }
        Code:
                    if (Times[1][0].DayOfYear == 7 )
                    {
                        a = CurrentDayOHL(BarsArray[0]).CurrentOpen[0];
                        b = CurrentDayOHL(BarsArray[1]).CurrentOpen[0];
                    }
                    
                    if( b > 0 && a >0)
                    {
                        c = (Closes[0][0]/a)-1;
                        d = (Closes[1][0]/b)-1;
                        e = (c - d);    
                    }
        hello,

        i have a fairly simple indicator that calculates a spread between two instruments. The reason why I used day 7 as the starting point for calculating the percentage return for the instrument is because i could not find a way to use the first bar loaded on the chart. So now it's really annoying because the indicator will not work if I don't have enough data loaded. In other words, I have to load a year worth of data so the "7th day" is also loaded on the chart for this indicator to work. Is there a way to avoid the above "starting point" so that any time i load a chart, it uses the first value as the starting point. In other words, if i load 30 days worth of data.. i want to use the open of the first bar on the chart for both instruments. a code sample will be very very appreciated.

        thank you in advance.
        Instead of:
        Code:
         if (Times[1][0].DayOfYear == 7 )
        you want to use:
        Code:
        if (CurrentBars[1] >= 7)

        Comment


          #5
          Originally posted by koganam View Post
          Instead of:
          Code:
           if (Times[1][0].DayOfYear == 7 )
          you want to use:
          Code:
          if (CurrentBars[1] >= 7)
          you mean ?

          Code:
          if (CurrentBars[1] [COLOR="Red"]==[/COLOR] 7)
          
                      {
                          a = CurrentDayOHL(BarsArray[0]).CurrentOpen[0];
                          b = CurrentDayOHL(BarsArray[1]).CurrentOpen[0];
                      }
          this will freeze time and store the a and b values. not sure why you said, >=?

          Comment


            #6
            Originally posted by calhawk01 View Post
            you mean ?

            Code:
            if (CurrentBars[1] [COLOR="Red"]==[/COLOR] 7)
            
                        {
                            a = CurrentDayOHL(BarsArray[0]).CurrentOpen[0];
                            b = CurrentDayOHL(BarsArray[1]).CurrentOpen[0];
                        }
            this will freeze time and store the a and b values. not sure why you said, >=?
            Code:
                        if(CurrentBars[0] ==1)			
            				
            			{
            			a = CurrentDayOHL(BarsArray[0]).CurrentOpen[0];
            		    b = CurrentDayOHL(BarsArray[1]).CurrentOpen[0];
            			}
            			
            			if( b > 0 && a >0)
            			{
            				c = (Closes[0][0]/a)-1;
            				d = (Closes[1][0]/b)-1;
            				e = (c - d)*50000;	
            			}
            
                        SPREAD.Set(e);
            Above works. Thanks!

            Comment


              #7
              Originally posted by calhawk01 View Post
              you mean ?

              Code:
              if (CurrentBars[1] [COLOR="Red"]==[/COLOR] 7)
              
                          {
                              a = CurrentDayOHL(BarsArray[0]).CurrentOpen[0];
                              b = CurrentDayOHL(BarsArray[1]).CurrentOpen[0];
                          }
              this will freeze time and store the a and b values. not sure why you said, >=?
              My mistake for thinking that you had made a mistake and wanted running values instead of a single static frozen value. If you want just one frozen value, then, of course, your correction is right.

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by funk10101, Today, 12:02 AM
              1 response
              10 views
              0 likes
              Last Post NinjaTrader_LuisH  
              Started by GLFX005, Today, 03:23 AM
              1 response
              6 views
              0 likes
              Last Post NinjaTrader_Erick  
              Started by nandhumca, Yesterday, 03:41 PM
              1 response
              12 views
              0 likes
              Last Post NinjaTrader_Gaby  
              Started by The_Sec, Yesterday, 03:37 PM
              1 response
              11 views
              0 likes
              Last Post NinjaTrader_Gaby  
              Started by vecnopus, Today, 06:15 AM
              0 responses
              1 view
              0 likes
              Last Post vecnopus  
              Working...
              X