Announcement

Collapse
No announcement yet.

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?

    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 Geovanny Suaza, 02-11-2026, 06:32 PM
              0 responses
              597 views
              0 likes
              Last Post Geovanny Suaza  
              Started by Geovanny Suaza, 02-11-2026, 05:51 PM
              0 responses
              343 views
              1 like
              Last Post Geovanny Suaza  
              Started by Mindset, 02-09-2026, 11:44 AM
              0 responses
              103 views
              0 likes
              Last Post Mindset
              by Mindset
               
              Started by Geovanny Suaza, 02-02-2026, 12:30 PM
              0 responses
              556 views
              1 like
              Last Post Geovanny Suaza  
              Started by RFrosty, 01-28-2026, 06:49 PM
              0 responses
              555 views
              1 like
              Last Post RFrosty
              by RFrosty
               
              Working...
              X