Automatically fetch news for the new day after midnight
This commit is contained in:
parent
899bcc0bf7
commit
8eb8f40541
@ -40,10 +40,8 @@ namespace NinjaTrader.NinjaScript.Indicators
|
||||
|
||||
public class News : Indicator
|
||||
{
|
||||
// TODO: Date should be determined programatically.
|
||||
private string newsUrl = "https://www.forexfactory.com/calendar?day=" + DateTime.Now.ToString("MMMdd.yyyy").ToLower();
|
||||
|
||||
private List<NewsEvent> newsEvents = new List<NewsEvent>();
|
||||
private DateTime currentDate = DateTime.Now.Date;
|
||||
|
||||
protected override void OnStateChange()
|
||||
{
|
||||
@ -79,9 +77,12 @@ namespace NinjaTrader.NinjaScript.Indicators
|
||||
{
|
||||
try
|
||||
{
|
||||
newsEvents.Clear();
|
||||
|
||||
HttpClient client = new HttpClient();
|
||||
client.DefaultRequestHeaders.UserAgent.ParseAdd("Mozilla/5.0 (Windows NT 10.0; Win64; x64)");
|
||||
|
||||
string newsUrl = "https://www.forexfactory.com/calendar?day=" + DateTime.Now.ToString("MMMdd.yyyy").ToLower();
|
||||
HttpResponseMessage response = await client.GetAsync(newsUrl);
|
||||
response.EnsureSuccessStatusCode();
|
||||
string responseBody = await response.Content.ReadAsStringAsync();
|
||||
@ -152,14 +153,22 @@ namespace NinjaTrader.NinjaScript.Indicators
|
||||
// Format the time as "hh:mmtt" (e.g., "06:30am")
|
||||
return userTime.ToString("hh:mmtt").ToLower();
|
||||
}
|
||||
catch (Exception ex)
|
||||
catch (Exception)
|
||||
{
|
||||
Print("Error converting time: " + ex.Message);
|
||||
return time; // Return original time if conversion fails
|
||||
}
|
||||
}
|
||||
|
||||
protected override void OnBarUpdate() { }
|
||||
protected override void OnBarUpdate() {
|
||||
if (State == State.Realtime)
|
||||
{
|
||||
if (DateTime.Now.Date != currentDate)
|
||||
{
|
||||
RequestNewsEvents();
|
||||
currentDate = DateTime.Now.Date;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected override void OnRender(ChartControl chartControl, ChartScale chartScale)
|
||||
{
|
||||
@ -183,7 +192,7 @@ namespace NinjaTrader.NinjaScript.Indicators
|
||||
headerSize = MeasureString("Event", headerTextFormat);
|
||||
maxColWidth = Math.Max(maxColWidth, headerSize.Width);
|
||||
|
||||
foreach (var newsEvent in newsEvents)
|
||||
foreach (var newsEvent in new List<NewsEvent>(newsEvents)) // New list in order to avoid concurrent modification
|
||||
{
|
||||
var timeSize = MeasureString(newsEvent.Time, eventTextFormat);
|
||||
var currencySize = MeasureString(newsEvent.Currency, eventTextFormat);
|
||||
|
Loading…
Reference in New Issue
Block a user