From 899bcc0bf7cd635a90f46ad54bb0b33ec822ec40 Mon Sep 17 00:00:00 2001 From: moshferatu Date: Wed, 15 May 2024 10:47:30 -0700 Subject: [PATCH] Convert the time to the time zone of the chart and add formatting --- indicators/News.cs | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/indicators/News.cs b/indicators/News.cs index d16ff48..677aa2a 100644 --- a/indicators/News.cs +++ b/indicators/News.cs @@ -126,6 +126,9 @@ namespace NinjaTrader.NinjaScript.Indicators // Remove any extraneous HTML tags from the time string 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 }); } } @@ -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 OnRender(ChartControl chartControl, ChartScale chartScale)