Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

No trading times in strategy

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

    No trading times in strategy

    I am trying to use user defined inputs for no trading times and i am running into some issues, i am certain i have overcomplicated it so would appreciate if someone can help me find a simpler way to address it.

    Currently, i look for a user input which is a string such as 00:00-02:00,08:00-14:00

    Code:
    [NinjaScriptProperty]
    [Display(Name = "No Trade Times (EST)", Description = "Specify the no-trade times in the format HH:mm-HH:mm,HH:mm-HH:mm (EST).", Order = 9, GroupName = "Trade Criteria")]
    public string NoTradeTimes { get; set; }
    And then I store this into a list using the series of code below. My system time is a different time zone, so I have to convert the time into my system time after recieving the EST time zone. (On question i have is Would it be easier for me to just change my ninjatrader timezone to EST instead?)

    Code:
    private List<Tuple<TimeSpan, TimeSpan>> noTradePeriodList;

    Code:
    private void ParseNoTradeTimes()
    {
    noTradePeriodList = new List<Tuple<TimeSpan, TimeSpan>>();
    
    if (!string.IsNullOrEmpty(NoTradeTimes))
    {
    string[] timeRanges = NoTradeTimes.Split(',');
    
    foreach (string timeRange in timeRanges)
    {
    string[] startEndTimes = timeRange.Trim().Split('-');
    if (startEndTimes.Length != 2)
    {
    Print(Time[0] + ": Invalid time range format: " + timeRange);
    continue;
    }
    
    TimeSpan startTime;
    TimeSpan endTime;
    
    if (!TimeSpan.TryParse(startEndTimes[0].Trim(), out startTime) ||
    !TimeSpan.TryParse(startEndTimes[1].Trim(), out endTime))
    {
    Print(Time[0] + ": Invalid time format in time range: " + timeRange);
    continue;
    }
    
    // Convert user's input from EST to local system time
    TimeZoneInfo estTimeZone = TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time");
    DateTime estStartDateTime = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, startTime.Hours, startTime.Minutes, startTime.Seconds);
    DateTime estEndDateTime = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, endTime.Hours, endTime.Minutes, endTime.Seconds);
    
    TimeZoneInfo localTimeZone = TimeZoneInfo.Local;
    DateTime localStartDateTime = TimeZoneInfo.ConvertTime(estStartDateTime, estTimeZone, localTimeZone);
    DateTime localEndDateTime = TimeZoneInfo.ConvertTime(estEndDateTime, estTimeZone, localTimeZone);
    
    noTradePeriodList.Add(new Tuple<TimeSpan, TimeSpan>(localStartDateTime.TimeOfDay, localEndDateTime.TimeOfDay));
    Print("Parsed time range: " + startTime.ToString() + " - " + endTime.ToString());
    }
    }
    }​
    ​
    And then i check if current time is within the no trade timezone

    Code:
    private bool IsWithinNoTradeTimes()
    {
    if (noTradePeriodList == null || noTradePeriodList.Count == 0)
    {
    // No "no trade times" specified, so trade anytime
    return false;
    }
    
    // Get the current time in Eastern Standard Time (EST)
    TimeZoneInfo estTimeZone = TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time");
    DateTime estCurrentTime = TimeZoneInfo.ConvertTime(Time[0], estTimeZone);
    
    // Check if the current time falls within any of the specified "no trade times" in EST
    foreach (var timeRange in noTradePeriodList)
    {
    TimeSpan estStartTime = timeRange.Item1;
    TimeSpan estEndTime = timeRange.Item2;
    
    
    if (estStartTime <= estCurrentTime.TimeOfDay && estCurrentTime.TimeOfDay <= estEndTime)
    {
    // Current time is within a "no trade time" period in EST
    return true;
    }
    }
    
    // Current time is not within any "no trade time" period in EST
    return false;
    }
    So the issues i have:

    1) It works when i put in ONE range of time such as 08:00-10:00 without any comma delimited second parameter, but the moment i put in a second time range it doesnt work at all.
    2) I do not know how it handles going accross midnight? Such as if i were to use 23:00-02:00, it seems that it is not working either. ​

    #2
    Hello.
    I apologize in advance, perhaps I misunderstood your question
    Maybe you will want change the time in Ninja Trader to your time zone.
    And then compare the incoming time data with either Ninja Trader time or system time.
    if the time is on different days, you also should to process the date.
    Date of the next day, weekends, and holidays. it depends on you logic.

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by NullPointStrategies, Today, 05:17 AM
    0 responses
    50 views
    0 likes
    Last Post NullPointStrategies  
    Started by argusthome, 03-08-2026, 10:06 AM
    0 responses
    126 views
    0 likes
    Last Post argusthome  
    Started by NabilKhattabi, 03-06-2026, 11:18 AM
    0 responses
    69 views
    0 likes
    Last Post NabilKhattabi  
    Started by Deep42, 03-06-2026, 12:28 AM
    0 responses
    42 views
    0 likes
    Last Post Deep42
    by Deep42
     
    Started by TheRealMorford, 03-05-2026, 06:15 PM
    0 responses
    46 views
    0 likes
    Last Post TheRealMorford  
    Working...
    X