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

Prevent my strategy from trading on Holidays and Weekends

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

    Prevent my strategy from trading on Holidays and Weekends

    Hello NT Team,

    I want to run my strategy on autopilot 24/7/365, this is why I want it to handle all potential and possible errors that could lead to immediate stop.

    in addition to handling overnight through disabling "on session close" and making the strategy to "keep running" during connection loss and few other things, now I want my strategy to keep running but to ALWAYS check carefully if
    Code:
    DateTime.Now
    is not a weekend and not a holiday, I searched inside the NT Doc and NT Forum, and I came across this :

    Code:
    // Print all holidays included in the Bars object's Trading Hours template
    foreach(KeyValuePair<DateTime, string> holiday in  TradingHours.Holidays)
    {
      Print(holiday);
    }
    
    // Print all partial holidays included in the Bars object's Trading Hours template
    foreach(KeyValuePair<DateTime, PartialHoliday> hol iday in TradingHours.PartialHolidays)
    {
      Print(holiday);
    }
    but I'm still not able to know how can I say to strategy :

    Code:
    if (DateTime.Now is a Weekend or a holiday)
    {
            bool isFreeDay = true;
    }
    Please take into consideration that when I Print this :

    Code:
    Print(sessionIterator.GetTradingDayEndLocal(sessionIterator.ActualSessionEnd));
    Or this :

    Code:
    Print(sessionIterator.GetTradingDayEndLocal(sessionIterator.ActualTradingDayExchange));
    I always get 10 P.M as a result, whereby when the time is 9:15 P.M all positions are closed and the day earnings close and move to initial capital, they no more appear as earnings. is it because some brokers DayTrading time finishes at that time ? if yes, then how to get their "GetTradingDayEndBROKER" for example DORMAN Trading or IBRK.

    Thank you

    #2
    Hello MohammedAmine,

    Thanks for your post.

    An important concept to keep in mind is that the Strategy will only allow bars as defined by the trading hours template. If no bars, the OnBarUpdate() cannot be called.

    We would recommend using Time[0] instead of DateTime.Now. Time[0] is the close date time of the bar.

    You can check to see if the next day will be a holiday or partial holiday in the OnBarUpdate():

    Code:
    foreach(KeyValuePair<DateTime, string> holiday in TradingHours.Holidays)
    {
      if (Time[0].AddDays(1).Date == holiday.Key.Date && Time[0].AddDays(1).Date!= savedH.Date)
      {
        Print (Time[0]+" next day is a holiday: "+holiday.Value);
        savedH = Time[0].AddDays(1);
      }
    }
    foreach(KeyValuePair<DateTime, PartialHoliday> holiday in TradingHours.PartialHolidays)
    {
      if (Time[0].AddDays(1).Date == holiday.Key.Date && Time[0].AddDays(1).Date!= savedPH.Date)
      {
        Print (Time[0].Date+" Next day is partial: "+holiday.Value);
        savedPH = Time[0].AddDays(1);
      }
    }
    in the above example, savedH and savedPH are private DateTimes and are used to only print once when a holiday or partial holiday is found (assuming you are using intraDay data, for daily bars you would not need this)
    In the above example we add 1 day to the Close time of the bar to test if the next day of the bar is the holiday or partial holiday.

    So the example gives you a way to determine that the next day is a partial or complete holiday and you can code accordingly, depending on what you want to do.


    Regarding, "I always get 10 P.M as a result, whereby when the time is 9:15 P.M all positions are closed and the day earnings close and move to initial capital, they no more appear as earnings. is it because some brokers DayTrading time finishes at that time ? if yes, then how to get their "GetTradingDayEndBROKER" for example DORMAN Trading or IBRK."

    I would suggest looking through the account class and then the AccountItems:

    Paul H.NinjaTrader Customer Service

    Comment


      #3
      Hello Paul, Thnx a lot

      but as u can see I can not say to strategy if tomorrow is a holiday, then do not trade tomorrow, it will be a bit complicated to do so, because we should manage holidays starting DATE and TIME, and also ENDING date and time ...

      so since the OnBarUpdate() is not triggered unless we have "moving bars" and the bars are not moving until we are "NOT" in holidays or weekends, does it mean that, as long as I "keep the strategy running" non-sop, and as long as i disabled the "ExitOnSessionClose", do u think this is pretty enough to skip trading during holidays and generating errors or bails ?

      Thank you

      Comment


        #4
        Hello MohammedAmine,

        A trading hours template will filter what bars are involved in the data series and will prevent OnBarUpdate from being called on those filtered bars because they are not in the data series the strategy was applied to. The strategy will take no actions on bars it does not process (however if orders are pending, they may still get filled if you have employed a trading hours template that does not encompass the entire trading session, (IE RTH verse ETH)).



        Paul H.NinjaTrader Customer Service

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by ScottW, Today, 06:09 PM
        0 responses
        2 views
        0 likes
        Last Post ScottW
        by ScottW
         
        Started by Board game geek, 10-29-2023, 12:00 PM
        14 responses
        244 views
        0 likes
        Last Post DJ888
        by DJ888
         
        Started by Waxavi, 04-19-2024, 02:10 AM
        4 responses
        56 views
        0 likes
        Last Post sonia0101  
        Started by cmtjoancolmenero, Today, 03:58 PM
        0 responses
        9 views
        0 likes
        Last Post cmtjoancolmenero  
        Started by Segwin, 05-07-2018, 02:15 PM
        11 responses
        1,779 views
        0 likes
        Last Post aligator  
        Working...
        X