Commit Graph

78 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 7ea241b735 Fix backtesting event bug
- After skipping scheduled events it will re order them
- Fix bug where when only a single scheduled event was present it
wouldn't fire
2020-03-03 12:56:50 -03:00
Martin Molinero b45b7e427f Address reviews
- Address review improve regression and unit test
- Fix same time scheduled event order
- Adding comments in unit test
- Increasing unit test sleep time
2020-03-03 10:45:37 -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 2f4f7dc2cb ScheduledEvents fire in order
- Scheduled events will fire in order even after market hours
- Improving scheduled events performance
- Adding regression and unit test
2020-03-02 11:05:54 -03:00
Martin Molinero 6cd12abff4 Remove duplicate EndOfDayEvent for Symbols
- Remove duplicate EndOfDayEvent for Symbols added with `AddData` calls
- Adding regression test
2020-01-14 18:06:52 -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
Martin Molinero 891f94fbeb Address reviews
- Set `CanRunLocally => false` for training regression algorithms
- Reverting sorting changes at `BacktestingRealTimeHandler` due to
performance degradation of current scheduled event benchmark algorithm
2019-10-21 21:18:06 -03:00
Michael Handschuh b371847a19 Usings cleanup, doc, unused parms, etc
Per review comments provided in #3743 regarding the
training/long-running scheduled events and the leaky
bucket algorithm. See the PR for more information.
2019-10-19 14:52:34 -04:00
Michael Handschuh 28be3bbe3d Refactors scheduled events to execute on main thread
In order to continue to provide debugging support in the QC cloud, the
scheduled events were moved from inside of a task to the algorithm's
main execution thread. This necesitated a different methodology for
managing timeouts. Instead of raising an exception when attempting to
request additional time when none is remaining, we're now simply allowing
the isolator's limit to be reached by virtue of not incrementing the
additional minutes in the time manager. This uncovered a bug in LEAN
engine where if the isolator terminates an algorithm, then the status
of the algorithm (on the algorithm manager instance) isn't properly
updated to indicate RuntimeError. This is in direct conflict with the
status update that is provided to the api, which is RuntimeError, so
this change remedies that issue as well. One of the regression algorithms
depends on this status value being properly flipped to RuntimeError in
the event that the isolator limit is reached.

See #3319
2019-10-19 14:52:34 -04: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
Michael Handschuh e4d1005c4a Update Lean.Engine to respect CA1304 and CA1305
Updates all occurrences of parsing/ToString-ing to go through the new
StringExtensions methods that use CultureInfo.InvariantCulture

See #3045
2019-09-12 15:30:45 -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
AlexCatarino 27d14d047f Fixes missing inner exception information in ScheduledEventException
When a `ScheduledEventException` was thrown, `IAlgorithm.RunTimeError` was not set as `ScheduledEventException` , but as `Exception` with the orginal exception message and no inner exception.
2018-04-13 19:23:45 +01: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 20a325276e Fix scheduled event firing order in backtesting
When backtesting with multiple scheduled events, these were fired in unpredictable order, not respecting the expected sequence sorted by time.
2018-01-03 14:27:08 +01: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 70b170358e Turn debug logging on for regression
Also removes some redundant/duplicate logging
2016-04-07 04:11:53 -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 be950e756a Log full errors/stack traces
Many places in the code used Log.Error(err.Message) or equivalent which
strips out all the really useful information, such as the stack trace
and inner exceptions. Using Log.Error(exception) is the correct way to
log an error as it will correctly write all the message details, also,
by passing the full Exception object we can improve the logging in this
one place and all call sites will automatically benefit from the improvements
2015-12-02 12:35:33 -05:00
snugs 7b6c82841f Initial implementation of SecurityIdentifier 2015-11-12 13:55:42 -05:00
snugs 8374b99e18 Don't fire EOD events for internal feeds 2015-09-28 13:28:09 -04:00
snugs c67ff638f4 Renames Symbol.SID to Symbol.Permtick
Coming soon we'll have a full SID system, so renames to make backwards compatibility easier
2015-09-20 23:14:53 -04:00
snugs e1938baafe Clean up using directives 2015-09-01 22:17:35 -04:00
snugs df79ce30c2 Adds the Symbol type
This includes updating all usages of symbol as a security identifier to use the new type.
The type includes a unique field, SID, as well as the current ticker's value. This allows
for consistent addressability while also allowing the ticker to evolve over time with the
mapping changes.

Effort was made to maintain compile and runtime backwards compatibility.
2015-09-01 22:17:10 -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 844a040be5 Initial impl of coarse universe selection
Adds concept of Subscription to contain everything a data feed needs in order to process  single data feed item
Moves preparation of all data to data feed thread, algo thread receives data in format it needs
QCAlgorithm.SetUniverse( func ) allows selection based on market/symbol/dollar volume/price
Remove laziness from Slice as optimization, no order by in real time handler
2015-08-06 17:52:23 -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