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

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 GussJ, 03-04-2020, 03:11 PM
          11 responses
          3,229 views
          0 likes
          Last Post xiinteractive  
          Started by andrewtrades, Today, 04:57 PM
          1 response
          14 views
          0 likes
          Last Post NinjaTrader_Manfred  
          Started by chbruno, Today, 04:10 PM
          0 responses
          7 views
          0 likes
          Last Post chbruno
          by chbruno
           
          Started by josh18955, 03-25-2023, 11:16 AM
          6 responses
          441 views
          0 likes
          Last Post Delerium  
          Started by FAQtrader, Today, 03:35 PM
          0 responses
          12 views
          0 likes
          Last Post FAQtrader  
          Working...
          X