Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Adddays functions not to include weekends

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

    Adddays functions not to include weekends

    I am trying to write code that looks at the same period of time X barsago, I have got this far, but the adddays function includes weekends.

    My question is, will this adddays function cross over the month

    and how do i exclude weekends when I want to look back further the 5 working days or cross a weekend?


    Code:
                if (FirstTickOfBar && (ToTime(Time[0]) >= 143000 && ToTime(Time[0]) <= 210000))    
                { 
                timeOfInterest = new DateTime(Time[0].Year, Time[0].Month, Time[0].Day, Time[0].Hour, Time[0].Minute, 0);
                
                barsAgo =0;    
                VolumeData=0;
                Volumeseries2.Set (Volume[0]+Volumeseries2[1]);
                    
                    for (int x = 1; x <= 1; x++)     
                    { 
                     barsAgo = GetBar(timeOfInterest.AddDays(-x)); 
                    VolumeData=Volume[barsAgo]+VolumeData;    
                    }     
                Volumeseries3.Set((VolumeData/1)+Volumeseries3[1]);

    #2
    Getting a bit further but stuck on line

    if((Time[barsAgo].DayOfWeek == DayOfWeek.Saturday) != (Time[barsAgo+1].DayOfWeek == DayOfWeek.Sunday)) Weekendjump=1+Weekendjump;

    How to join to booleans together in an IF statement?

    I can not use != is not equal too

    Code:
                if (FirstTickOfBar && (ToTime(Time[0]) >= 143000 && ToTime(Time[0]) <= 210000))    
                { 
                timeOfInterest = new DateTime(Time[0].Year, Time[0].Month, Time[0].Day, Time[0].Hour, Time[0].Minute, 0);
                
                barsAgo =0;    
                VolumeData=0;
                Weekendjump=0;            
                Volumeseries2.Set (Volume[0]+Volumeseries2[1]);
                    
                    for (int x = 1; x <= 1; x++)     
                    { 
                        
                     barsAgo = GetBar(timeOfInterest.AddDays(-x)); 
                    if(Time[barsAgo].DayOfWeek == DayOfWeek.Sunday) Weekendjump=2+Weekendjump;
                    if((Time[barsAgo].DayOfWeek == DayOfWeek.Saturday) != (Time[barsAgo+1].DayOfWeek == DayOfWeek.Sunday)) Weekendjump=1+Weekendjump;
                    VolumeData=Volume[barsAgo+(-Weekendjump)]+VolumeData;    
                    }     
                Volumeseries3.Set((VolumeData/1)+Volumeseries3[1]);
    Last edited by tinkerz; 02-09-2010, 03:23 PM.

    Comment


      #3
      Hello Tinkerz,

      You could take a look at this reference sample:


      It looks like you are already using part of it, but the part that might help you is this snippet:
      Code:
          if (Time[0].DayOfWeek == DayOfWeek.Monday)
           timeOfInterest = new DateTime(Time[0].Year, Time[0].Month, Time[0].Day - 3, 9, 30, 0);
          else
           timeOfInterest = new DateTime(Time[0].Year, Time[0].Month, Time[0].Day - 1, 9, 30, 0);
      If it's monday it will look subtract 3 days. If it's any other day of the week it will subtract one day.

      If you puruse the method in your second post, you can join booleans together with the && operator.
      Ryan M.NinjaTrader Customer Service

      Comment


        #4
        Originally posted by tinkerz View Post
        Getting a bit further but stuck on line

        if((Time[barsAgo].DayOfWeek == DayOfWeek.Saturday) != (Time[barsAgo+1].DayOfWeek == DayOfWeek.Sunday)) Weekendjump=1+Weekendjump;

        How to join to booleans together in an IF statement?
        tinkerz, you could create two intermediate boolean variables and then compare those:
        Code:
        bool isSat;
        bool isSun;
        
        if(Time[barsAgo].DayOfWeek == DayOfWeek.Saturday)
            isSat = true;
        else
            isSat = false;
        
        if(Time[barsAgo+1].DayOfWeek == DayOfWeek.Sunday)
            isSun = true;
        else
            isSun = false;
        
        // if I'm reading your logic right
        if (isSat != isSun)
            Weekendjump=1+Weekendjump;
        As for your other question, AddDays() will cross months.

        Perhaps you could just add 3 days on Fridays to get to Monday?
        AustinNinjaTrader Customer Service

        Comment


          #5
          tinkerz;

          There's an apparent logic flaw in the code snippet you originally posted.
          Assuming you want add Sunday's and Saturday's volume to the Friday volume your Weekendjump variable needs to be set for every iteration of the loop.
          Also, there's a less verbose way to calculate the value of 2,1 or 0:
          Code:
                      if (FirstTickOfBar && (ToTime(Time[0]) >= 143000 && ToTime(Time[0]) <= 210000))    
                      { 
                      timeOfInterest = new DateTime(Time[0].Year, Time[0].Month, Time[0].Day;
           
                      barsAgo =0;    
                      VolumeData=0;
                      Volumeseries2.Set (Volume[0]+Volumeseries2[1]);
           
                          for (int x = 1; x <= 1; x++)     
                          { 
           
                            barsAgo = GetBar(timeOfInterest.AddDays(-x)); 
          [SIZE=2]               Weekendjump [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]=[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] Time[barsAgo][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff].[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]DayOfWeek [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]==[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#2b91af][SIZE=2][COLOR=#2b91af]DayOfWeek[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff].[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]Sunday [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]?-[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#ffff00][SIZE=2][COLOR=#ffff00]2[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] : Time[barsAgo][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff].[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]DayOfWeek [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]==[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#2b91af][SIZE=2][COLOR=#2b91af]DayOfWeek[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff].[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]Saturday [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]?-[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#ffff00][SIZE=2][COLOR=#ffff00]1[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] : [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]-[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#ffff00][SIZE=2][COLOR=#ffff00]0[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]);[/SIZE]
                            VolumeData=Volume[barsAgo+Weekendjump]+VolumeData;    
                          }     
                      Volumeseries3.Set((VolumeData/1)+Volumeseries3[1]);
          » Fred «

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by Geovanny Suaza, 02-11-2026, 06:32 PM
          0 responses
          627 views
          0 likes
          Last Post Geovanny Suaza  
          Started by Geovanny Suaza, 02-11-2026, 05:51 PM
          0 responses
          359 views
          1 like
          Last Post Geovanny Suaza  
          Started by Mindset, 02-09-2026, 11:44 AM
          0 responses
          105 views
          0 likes
          Last Post Mindset
          by Mindset
           
          Started by Geovanny Suaza, 02-02-2026, 12:30 PM
          0 responses
          562 views
          1 like
          Last Post Geovanny Suaza  
          Started by RFrosty, 01-28-2026, 06:49 PM
          0 responses
          567 views
          1 like
          Last Post RFrosty
          by RFrosty
           
          Working...
          X