Currently, i look for a user input which is a string such as 00:00-02:00,08:00-14:00
[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; }
private List<Tuple<TimeSpan, TimeSpan>> noTradePeriodList;
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());
}
}
}
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;
}
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.

Comment