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 00nevest, 04-27-2024, 02:27 PM
          3 responses
          27 views
          0 likes
          Last Post 00nevest  
          Started by Newtrader101, Today, 07:36 AM
          0 responses
          1 view
          0 likes
          Last Post Newtrader101  
          Started by 21laienoch, Today, 07:25 AM
          0 responses
          6 views
          0 likes
          Last Post 21laienoch  
          Started by burtoninlondon, 04-28-2024, 12:38 AM
          1 response
          16 views
          0 likes
          Last Post burtoninlondon  
          Started by MacDad, 02-25-2024, 11:48 PM
          8 responses
          175 views
          0 likes
          Last Post NinjaTrader_Erick  
          Working...
          X