Commit Graph

56 Commits

Author SHA1 Message Date
Michael Sena dbf3248f53 Fix LiveTradingRealTimeHandler pack-up creating the potential for a NullReferenceException 2020-06-23 12:58:58 +10:00
Martin Molinero c92c3a3f1b Removing alpha thread from engine
- Moving alpha thread from engine into handler class.
- Normalizing thread stop logic
2020-03-04 15:40:27 -03:00
Martin Molinero 1dc6ed06f7 Address reviews
- Dispose of TimeMonitor on RealTimeHandler exit
2020-03-03 18:45:43 -03:00
Martin Molinero 8e1d0f7e4f Remove RealTime thread from engine
- Remove RealTime thread from the engine
- Engine will call DataFeed.Exit() even if algorithm initialization
failed, to avoid 30 sec delay
2020-03-03 16:50:25 -03:00
Martin Molinero b3feea86a1 Refactor IsolatorLimitResultProvider
- Replace `new Task` at `IsolatorLimitResultProvider.Consume()` for
single instance base timer.
- Adding unit test
2020-03-02 18:23:20 -03:00
Martin Molinero 3a5a2c582f Lazy OnEndOfDay ScheduledEvent
- Only add OnEndOfDay ScheduledEvent if the algorithm implements the
method. Adding unit tests
- Avoid creating a new baseData instance at
`SubscriptionDataSourceReader`
- Adding static `FineFundamental` instance since creating new ones is
expensive
2019-12-19 19:07:16 -03:00
Michael Handschuh 22d56e5ffb Properly handle events raised in ScheduledEvents
Adds logic to intercept exceptions raised by user code in the
task used to wrap the scheduled event execution.
2019-10-19 14:52:34 -04:00
Michael Handschuh 09cc78599a Add mechanism for scheduled events to use 'additional time'
We restrict each algorithm time loop to a pre-determined amount of time.
Exceeding this limit will cause the algorithm to immediately terminate.
This quickly becomes an issue when considering users running trainable
models that have a long initialization period that exceeds the time loop
maximum.

This change provides a mechanism through which a long-running scheduled
event is permitted to keep running and is permitted to avoid the time loop
permitted by requesting additional time. Requests for additional time are
limited according to a leaky bucket implementation whose parameters are
set via the job's controls structure. The fundamental time unit for the
algorithm is a single minute.

Here's how it works. If a scheduled event takes longer than one full wall
clock second then a request is made to the leaky bucket for one more minute.
If the scheduled event continues to take more time, it will continue to
request additional minutes. Each requested minute will prevent the algorithm's
time loop check from terminating the algorithm. When the bucket is empty and
no more minutes are available to be requested, a TimeoutException is thrown
causing a cascade that ends in the algorithm's termination and status being
flipped to RuntimeError.

Additionally, this applies equally to ALL scheduled events. While some helpers
were added with the naming of Train and TrainNow to the ScheduleManager, these
methods don't do anything special and the infrastructure doesn't otherwise
flag them as different, so this feature becomes part of the core Scheduled
Event feature set.

Further, the live scheduled events were not touched and are still pending
further discussion regarding the value added by enforcing a time restriction
when simulation time and wall clock time are equivalent.

Fixes #3319
2019-10-19 14:52:34 -04:00
Martin Molinero a92bee5bf1 Fix OnEndOfDay not fired
- Adding `IRealTimeHandler.OnSecurityChanged()` will be used to update
the `OnEndOfDay` security related scheduled events
- Adding `BaseRealTimeHandler.cs` to reduce code duplication in the
`Backtesting` and `LiveTrading` `RealTimeHandlers`
- Adding CSharp and Python regression tests
- Deprecating `OnEndOfDay()` callback because of two reasons, mainly
because Python does not support two methods with the same name, but also
because different assets have different market close times.
- `ScheduledEvents` set at the same time will now be deterministic
2019-06-20 17:35:28 -03:00
Martin-Molinero 994b1f44f6 saving current progress 2018-03-18 12:46:02 -07:00
Martin-Molinero 259a0ab8ff Fix exception thrown login vs plugin 2018-03-14 17:28:55 -07:00
Stefano Raggi 91ba63529b Remove Guid string from ScheduledEvent.Name
Fixes #1674
2018-03-07 21:33:33 +01:00
AlexCatarino 136ca90f6c Fixes message input in ScheduledEventException instance creation
Passes exception to Log.Error in RealTimeHandlers in order to include stack information
2018-02-20 17:39:03 +00:00
AlexCatarino af7b592dda Passes inner exception to ScheduleEvent exception
Without the inner exception, information on the user exception is lost. In python algorithm, the stack trace is completely lost.
Removes ScheduledEventExceptionMessage property as it is redundant to Message property inherited from parent class.
2018-02-19 19:11:55 +00:00
Stefano Raggi f37d0cc962 Optimize loops for performance on cashbook and securities 2017-12-20 21:58:59 +01:00
Stefano Raggi c6aba7bfec Reduce full locking on ConcurrentDictionary objects
This PR is an attempt to reduce contention in concurrent dictionaries, replacing method calls using full locks with lock-free equivalents:

- dictionary.Count -> dictionary.Skip(0).Count()
- dictionary.Keys -> dictionary.Select(x => x.Key)
- dictionary.Values -> dictionary.Select(x => x.Value)

The most frequent usages of these methods are: CashBook, SecurityManager, UniverseManager and indirectly, SecurityPortfolioManager.

The reasons for this update are explained very clearly in this article:
https://arbel.net/2013/02/03/best-practices-for-using-concurrentdictionary/
2017-12-20 17:18:54 +01:00
Stefano Raggi b8ff1fa745 Fix initialization of IsActive in LiveTradingRealTimeHandler 2017-11-01 22:32:16 +01:00
Michael Handschuh 7068a5aeba Fix scheduled event scheduling
Scheduled events were seemingly only being submitted for internal feed
securities -- the opposite of what we're looking for.
2017-10-20 13:49:56 -04:00
Andrew 2c156ce04a Add schedule event exception logic to BacktestingRealTimeHandler
Catching the exception in BacktestingRealTimeHandler prevents unhandled errors in backtesting and provides more specific logging to make debugging easier for users
2017-09-25 11:04:13 -04:00
Andrew be986a5a8f End execution of Lean if an error in a scheduled event is thrown 2017-09-25 09:51:45 -04:00
Andrew eabd36966d Added ScheduledEventException
These changes fix a bug so that when an exception is thrown with the LiveTradingResultHandler, the error is handled and the next scheduled event can happen as scheduled.
2017-09-22 18:23:15 -04:00
Stefano Raggi 6a8bb27b44 Log TimeZone in LiveTradingRealTimeHandler.RefreshMarketHoursToday 2017-03-17 18:09:07 +01:00
Stefano Raggi 4149aba9d7 Fixed scheduled event firing time in backtesting
If backtesting, we need to check if there are realtime events in the past
which didn't fire because at the scheduled times there was no data (i.e. markets closed) and fire them with the correct date/time.
In live mode, no changes are needed.
2017-02-23 20:25:49 +01:00
Michael Handschuh 42465ffd52 Clean up IsInternalFeed usages 2016-04-12 19:06:49 -04:00
Michael Handschuh 3e434709ea Change IApi.MarketToday to IEnumerable<MarketHoursSegment> 2016-01-29 18:50:09 -05:00
Michael Handschuh 4eed7e02fd Change IApi.MarketToday to accept a symbol 2016-01-29 18:27:45 -05:00
Michael Handschuh f3cc448ad5 Update SecurityExchange.UpdateMarketHours to use segments 2016-01-29 18:12:57 -05:00
snugs 8374b99e18 Don't fire EOD events for internal feeds 2015-09-28 13:28:09 -04:00
snugs 730430b1bb Adds scheduling feature
Adds the ScheduleManager which allows an algorithm to add/remove scheduled events
Check out the ScheduledEventsAlgorithm for syntax
ScheduledEvents are at their core an IEnumerator<DateTime> that defines the event times coupled with a callback
IDateRule defines dates for events
ITimeRule defines time(s) on a given date for events
2015-08-10 10:56:14 -04:00
snugs ffa2c948f9 Convert back from UTC for refresh date check 2015-08-08 12:35:07 -04:00
snugs 7fa9a4b1be Implements self re-scheduling events
A ScheduledEvent can be defined using an enumerator of event times and a callback
2015-08-05 13:13:08 -04:00
snugs aa0877be24 Fixes bug in live real time handler 2015-08-04 18:23:01 -04:00
snugs 011ac4b12c Try/catch on Run methods
In some cases we need to exit the applications, in others we just want to log.
For instance, if the transaction handler throws an unexpected exception then we
can't be confident in the state of our orders but if the live result handler fails,
the algo is still OK and consistent state, we just may not receive a packet for that time step
2015-08-02 23:54:31 -04:00
snugs a038f6cb89 Fixes tz bug in real time handler 2015-08-02 23:54:31 -04:00
snugs c853a4ddbd Adds some logging/error handling 2015-07-28 14:21:00 -04:00
snugs 27cd9b1fed Fixes bug in fire EOD events in live mode 2015-07-24 19:28:56 -04:00
snugs 6bd38ece77 Adds international/timezone support
Time sync:
	* Data feeds are required to time sync in UTC time
	* TimeSlice.Time is now in UTC

IAlgorithm
	* Time is now exclusively the algorithm's local time zone
	* Added UtcTime
	* SetDateTime( DateTime ) accepts a UTC time and is internally converted

SubscriptionDataConfig
	* Adds market and time zone as required ctor parameters

SecurityExchange
	* Now passes most calls directly through to SecurityExchangeHours class

SecurityExchangeHours
	* Holds market hours for each day of week (LocalMarketHours)
	* Talks in terms of local times in the SecurityExchangeHours.TimeZone time zone

Data/market-hours/
	* New data folder to hold market hour information
	* Includes market-hours-database.csv to hold market hours per market/symbol/security (see doc in file)
	* Includes holidays-usa.csv to hold holidays for 'usa' market
		+ The holiday files follow the pattern 'holidays-*.csv' where * is the market

TimeKeeper
	* Receives updates in UTC time
	* Passes that to LocalTimeKeeper's who lazily evaluate the time in their respective time zones
	* Eventually this can grow to be the sole source of time in the algorithm's scope

MISC:
	* Fixes exception thrown when exiting LiveTradingDataFeed
	* Fixes exception thrown when exiting FileSystemDataFeed
	* Fixes exception thrown when exiting StatusPing
	* Simplify FillForwardEnumerator logic with GetNextMarketOpen
	* Adds many time zones, see TimeZones.cs
2015-07-08 18:45:34 -04:00
snugs 0ef2be5bf8 Respect time from algo manager in live real time handler 2015-06-20 13:58:50 -04:00
snugs 4de2ec4070 Make Engine an instance
Updated all interfaces to accept dependencies via Initialize methods
2015-06-18 13:50:11 -04:00
snugs c1736571cc Load real time handlers via MEF configuration 2015-06-17 22:34:01 -04:00
QuantConnect fde0cbed3a Strip out block quote section header comments 2015-06-13 16:04:27 -04:00
snugs 89b6e98af8 Fixes warnings mostly relating to xml comments
Also cleans up some block comments

Via PR#72 - thanks @ammachado!
2015-05-13 10:56:47 -04:00
QuantConnect 8d28ff3f08 Slight changes to only create realtime events for day if the time hasn't passed yet 2015-04-30 14:46:07 -04:00
snugs d80b1733a9 Pre ib launch review 2015-04-24 19:12:16 -04:00
snugs 8b6e5fe826 Change json on MarketToday and improve default impl 2015-04-21 16:52:28 -04:00
QuantConnect db6fb56e56 Bug fix potential error with negative thread sleep timing 2015-03-08 15:40:27 -03:00
QuantConnect 5be77cbaaf Large shuffle to move MarketToday clock into RealTime handler and out of DataFeed. Added public accessor for market today hours. Simplify and tidy live real time handler 2015-02-25 15:33:10 -03:00
Jared Broad df00cc1f3a Fixed access to modified closure bug with wrong EOD event symbol 2015-02-19 00:40:10 -03:00
Jared Broad 90b162b85d Rework RealtimeEvent class to use datetime - fix #11, simplified live realtime event handler, eliminate backtest realtime thread 2015-02-18 16:49:42 -03:00
Jared Broad d4c8c10f02 Rework to move synchronous events into transaction hander and result handler plugins so the events could be controlled on a plugin level. e.g. This allowed removing 1 thread context switch from primary algo loop and enabling live trading result handler to trigger its own updates every second independent of algorithm event timing 2015-02-06 12:50:55 -03:00