Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

1st day of a month

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

    1st day of a month

    Hi,

    is it possible to check whether 1,2 or 3 days ago it was different month from the current month (i.e. whether it is 1-3 business day of a current month)?

    I only want to trade during first 3 business days of a month, if possible.

    Thanks for an answer, Jozef

    #2
    Hello jozef.r,

    Thank you for your post and welcome to the NinjaTrader Support Forum!

    You can use Bars.GetDayBar(int).Time.Day to pull the day of a previous day bar.

    So you could use Bars.GetDayBar(1).Time.Day for yesterday's date. You would then need to figure a check to see if 1,2, or 3 days ago = the last trading day of the previous month.

    For information on Bars.GetDayBar() please visit the following link: http://www.ninjatrader.com/support/h.../getdaybar.htm

    Please let me know if I may be of further assistance.

    Comment


      #3
      Now, a follow-up question would be:

      Why the following script would not work?

      protected override void OnBarUpdate()
      { if (!(Bars.GetDayBar(3).Time.Month==Bars.GetDayBar(0) .Time.Month)) {
      // Condition set 1
      if ((CrossAbove(Close, SMA(60), 1)) & )
      {
      EnterLong(DefaultQuantity, "");
      }

      // Condition set 2
      if (CrossBelow(Close, SMA(60), 1))
      {
      ExitLong("", "");
      }
      }
      }

      Comment


        #4
        Hello Jozef,

        Thank you for your response.

        Please try the following:
        Code:
        if (Bars.GetDayBar(3).Time.Month!=Bars.GetDayBar(0) .Time.Month)
        Please let me know if I may be of further assistance.

        Comment


          #5
          still, does not seem to work.

          here is the entire strategy:

          protected override void Initialize()
          {

          CalculateOnBarClose = true;
          }

          /// <summary>
          /// Called on each bar update event (incoming tick)
          /// </summary>
          protected override void OnBarUpdate()
          { if ((Bars.GetDayBar(3).Time.Month!=Bars.GetDayBar(0). Time.Month)) {
          // Condition set 1
          if ((CrossAbove(Close, SMA(60), 1)) & )
          {
          EnterLong(DefaultQuantity, "");
          }

          // Condition set 2
          if (CrossBelow(Close, SMA(60), 1))
          {
          ExitLong("", "");
          }
          }
          }

          #region Properties
          [Description("")]
          [GridCategory("Parameters")]
          public int MA1
          {
          get { return mA1; }
          set { mA1 = Math.Max(1, value); }
          }

          [Description("")]
          [GridCategory("Parameters")]
          public int MA60
          {
          get { return mA60; }
          set { mA60 = Math.Max(60, value); }
          }
          #endregion
          }

          Comment


            #6
            Hello Jozef,

            Thank you for your response.

            Is the strategy not taking positions? Are you receiving any error messages?

            I look forward to your response.

            Comment


              #7
              no positions taken.

              no error messages.

              Comment


                #8
                Hello Jozef,

                Thank you for your response.

                That would be my mistake as I set the condition to check if the two dates month do not match and then enter positions. We need the opposite of this.

                Please use the following code:
                Code:
                if (Bars.GetDayBar(3).Time.Month==Bars.GetDayBar(0) .Time.Month)

                Comment


                  #9
                  no, i only want to take positions during first 3 days of a month, so the original condition was correct. but i do not know why no trades.

                  Comment


                    #10
                    Hello Jozef,

                    Thank you for your patience.

                    I had to test this one on my end a little further and I found the proper condition to be the following:
                    Code:
                    if (Bars.GetDayBar(3) != null && Bars.GetDayBar(3).Time.Month != Time[0].Month)
                    We have to check that there is a value 3 days ago and we must use Time[0].Month to access the current bar's month (this month).

                    Please let me know if I may be of further assistance.

                    Comment

                    Latest Posts

                    Collapse

                    Topics Statistics Last Post
                    Started by timmbbo, Today, 08:59 AM
                    1 response
                    2 views
                    0 likes
                    Last Post NinjaTrader_ChelseaB  
                    Started by KennyK, 05-29-2017, 02:02 AM
                    2 responses
                    1,281 views
                    0 likes
                    Last Post marcus2300  
                    Started by fernandobr, Today, 09:11 AM
                    0 responses
                    3 views
                    0 likes
                    Last Post fernandobr  
                    Started by itrader46, Today, 09:04 AM
                    1 response
                    6 views
                    0 likes
                    Last Post NinjaTrader_Clayton  
                    Started by bmartz, 03-12-2024, 06:12 AM
                    5 responses
                    33 views
                    0 likes
                    Last Post NinjaTrader_Zachary  
                    Working...
                    X