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

Seasonal Analysis in Ninja Script

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

    Seasonal Analysis in Ninja Script

    Hi Support Team,

    I strongly try to create a ninja script which does allow to enter the day of the month and the month itself and to backtest the last 10 or 20 years.
    The strategy builder I used for the beginning and later I adjusted the code. Basically the code does work, but the problem is that when the Startday or Endday of the month is on weekends, the ninja script does not work and takes other years (see attachement).


    Would be appreciated for your support to get this code fixed.

    Thanks and have a great weekend.

    Regards

    Stefan






    #2
    Hello Stefan1982,

    Thank you for your post.

    It sounds like you need to add additional logic to error check your input parameters when you have a new year.

    So something like this:

    Code:
    DateTime ValidDay = new DateTime(Time[0].Year, inputMonth, inputDay);
    if (ValidDay.DayOfWeek == DayOfWeek.Saturday)
    {
        //do stuff to hanlde differently, the input month/year results in in an enter/exit on saturday
    }
    We'd suggest checking that when Time[0].Year changes. The DateTime class has methods like AddMinute() and AddDay() that you could utilize to make sure your entries are on a weekday as you'd intend.

    Please let us know if we may be of further assistance to you.
    Kate W.NinjaTrader Customer Service

    Comment


      #3
      Hi Kate,

      many many thanks for your good support.


      I added the function what your recommended, but it does not work, results are the same. Somehow I need to conncet valid date properly to the startdate???

      Do you have an idea, how to fix this and to show all years? Many thanks

      Stefan
      Attached Files
      Last edited by Stefan1982; 08-10-2020, 02:42 AM.

      Comment


        #4
        Hello Stefan,

        Thank you for your reply.

        So you're creating a new DateTime 4 times when you really only need two, but then you aren't using that adjusted DateTime in your conditions for your entries and exits. Try something like this:

        Code:
                    DateTime ValidStartDay = new DateTime(Time[0].Year, Startmonth, Startday);
                    DateTime ValidEndDay = new DateTime(Time[0].Year, Endmonth, Endday);
        
                    if (ValidStartDay.DayOfWeek == DayOfWeek.Saturday)
                    {
                        ValidStartDay = ValidStartDay.AddDays(2);
                    }
                    else if (ValidStartDay.DayOfWeek == DayOfWeek.Sunday)
                    {
                        ValidStartDay = ValidStartDay.AddDays(1);
                    }
        
                    if(ValidEndDay.DayOfWeek == DayOfWeek.Saturday)
                    {
                        ValidEndDay = ValidEndDay.AddDays(2);
                    }
                    else if (ValidEndDay.DayOfWeek == DayOfWeek.Sunday)
                    {
                        ValidEndDay = ValidEndDay.AddDays(1);
                    }
        
                    if(Times[0][0].Day == ValidStartDay.Day && Times[0][0].Month == ValidStartDay.Month)
                    {
                        EnterLong();
                    }
                    if(Times[0][0].Day == ValidEndDay.Day && Times[0][0].Month == ValidEndDay.Month)
                    {
                        ExitLong();
                    }
        Please let us know if we may be of further assistance to you.
        Kate W.NinjaTrader Customer Service

        Comment


          #5
          Hey Kate,

          I just checked and looks very good. Many thanks for your ***** support.

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by cre8able, 02-11-2023, 05:43 PM
          3 responses
          236 views
          0 likes
          Last Post rhubear
          by rhubear
           
          Started by frslvr, 04-11-2024, 07:26 AM
          8 responses
          113 views
          1 like
          Last Post NinjaTrader_BrandonH  
          Started by stafe, 04-15-2024, 08:34 PM
          10 responses
          46 views
          0 likes
          Last Post stafe
          by stafe
           
          Started by rocketman7, Today, 09:41 AM
          3 responses
          11 views
          0 likes
          Last Post NinjaTrader_Jesse  
          Started by traderqz, Today, 09:44 AM
          2 responses
          10 views
          0 likes
          Last Post NinjaTrader_Gaby  
          Working...
          X