Convert the time to the time zone of the chart and add formatting

This commit is contained in:
moshferatu 2024-05-15 10:47:30 -07:00
parent c386ee3c47
commit 899bcc0bf7

View File

@ -126,6 +126,9 @@ namespace NinjaTrader.NinjaScript.Indicators
// Remove any extraneous HTML tags from the time string // Remove any extraneous HTML tags from the time string
time = Regex.Replace(time, "<.*?>", string.Empty); time = Regex.Replace(time, "<.*?>", string.Empty);
// Convert the time to the user's time zone
time = ConvertToUserTimeZone(time);
newsEvents.Add(new NewsEvent { Time = time, Currency = currency, Title = title }); newsEvents.Add(new NewsEvent { Time = time, Currency = currency, Title = title });
} }
} }
@ -136,6 +139,26 @@ namespace NinjaTrader.NinjaScript.Indicators
} }
} }
private string ConvertToUserTimeZone(string time)
{
try
{
// Assumed to be in US Eastern Time
DateTime easternTime = DateTime.ParseExact(time, "h:mmtt", System.Globalization.CultureInfo.InvariantCulture);
TimeZoneInfo easternTimeZone = TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time");
DateTime userTime = TimeZoneInfo.ConvertTime(easternTime, easternTimeZone, Core.Globals.GeneralOptions.TimeZoneInfo);
// Format the time as "hh:mmtt" (e.g., "06:30am")
return userTime.ToString("hh:mmtt").ToLower();
}
catch (Exception ex)
{
Print("Error converting time: " + ex.Message);
return time; // Return original time if conversion fails
}
}
protected override void OnBarUpdate() { } protected override void OnBarUpdate() { }
protected override void OnRender(ChartControl chartControl, ChartScale chartScale) protected override void OnRender(ChartControl chartControl, ChartScale chartScale)