19 using System.Threading;
36 private Thread _realTimeThread;
37 private CancellationTokenSource _cancellationTokenSource =
new();
38 private readonly
bool _forceExchangeAlwaysOpen =
Config.
GetBool(
"force-exchange-always-open");
61 public override bool IsActive {
get;
protected set; }
68 base.Setup(algorithm, job, resultHandler, api, isolatorLimitProvider);
80 Add(
new ScheduledEvent(
"RefreshMarketHoursAndSymbolProperties", times, (name, triggerTime) =>
104 while (!_cancellationTokenSource.IsCancellationRequested)
109 var nextSecond = time.RoundUp(TimeSpan.FromSeconds(1));
110 var delay = Convert.ToInt32((nextSecond - time).TotalMilliseconds);
111 Thread.Sleep(delay < 0 ? 1 : delay);
116 var scheduledEvent = kvp.Key;
121 catch (Exception exception)
123 Algorithm.SetRuntimeError(exception, $
"Scheduled event: '{scheduledEvent.Name}' at {time}");
129 Log.
Trace(
"LiveTradingRealTimeHandler.Run(): Exiting thread... Exit triggered: " + _cancellationTokenSource.IsCancellationRequested);
144 var security = kvp.Value;
147 var localMarketHours = security.Exchange.Hours.GetMarketHours(date);
148 Log.
Trace($
"LiveTradingRealTimeHandler.RefreshMarketHoursToday({security.Type}): Market hours set: Symbol: {security.Symbol} {localMarketHours} ({security.Exchange.Hours.TimeZone})");
161 ResetSymbolPropertiesDatabase();
166 var security = kvp.Value;
169 Log.
Trace($
"LiveTradingRealTimeHandler.RefreshSymbolPropertiesToday(): Symbol properties set: " +
170 $
"Symbol: {security.Symbol} {security.SymbolProperties}");
184 else if (_realTimeThread ==
null)
188 _realTimeThread =
new Thread(Run) { IsBackground =
true, Name =
"RealTime Thread" };
189 _realTimeThread.Start();
201 base.ScanPastEvents(time);
212 _realTimeThread.StopSafely(TimeSpan.FromMinutes(1), _cancellationTokenSource);
213 _cancellationTokenSource.DisposeSafely();
226 var hours = _forceExchangeAlwaysOpen
245 security.UpdateSymbolProperties(symbolProperties);
261 private void ResetSymbolPropertiesDatabase()