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);
153 var symbol = security.Symbol.HasCanonical() ? security.Symbol.Canonical : security.Symbol;
154 Log.
Trace($
"LiveTradingRealTimeHandler.RefreshMarketHoursToday({security.Type}): Market hours set: Symbol: {symbol} {localMarketHours} ({security.Exchange.Hours.TimeZone})");
167 ResetSymbolPropertiesDatabase();
172 var security = kvp.Value;
179 var symbol = security.Symbol.HasCanonical() ? security.Symbol.Canonical : security.Symbol;
180 Log.
Trace($
"LiveTradingRealTimeHandler.RefreshSymbolPropertiesToday(): Symbol properties set: " +
181 $
"Symbol: {symbol} {security.SymbolProperties}");
195 else if (_realTimeThread ==
null)
199 _realTimeThread =
new Thread(Run) { IsBackground =
true, Name =
"RealTime Thread" };
200 _realTimeThread.Start();
212 base.ScanPastEvents(time);
223 _realTimeThread.StopSafely(TimeSpan.FromMinutes(1), _cancellationTokenSource);
224 _cancellationTokenSource.DisposeSafely();
237 var hours = _forceExchangeAlwaysOpen
256 security.UpdateSymbolProperties(symbolProperties);
272 private void ResetSymbolPropertiesDatabase()