Commit Graph

90 Commits

Author SHA1 Message Date
Michael Handschuh 48c691c2bb Use buying power model in PortfolioTarget.Percent
Also adds convenience ToString override
2018-03-26 16:42:25 -04:00
Michael Handschuh 708b0e91b8 Dispose of InsightManager extensions on exit
This isn't technically required, but it's best practice to explicitly
dispose of resources that require it.
2018-03-22 14:51:42 -04:00
Michael Handschuh 0d5fca0733 Add insight serialization tests
Moved costruction of insight from serialized insight to the insight
object so it could access private properties, such as the id
2018-03-12 16:28:41 -04:00
Michael Handschuh 6b239674e2 Renames Alpha -> Insight
The term 'alpha' is used to describe the entire algorithm. Therefore, 'alpha'
produces insights. From this we have things like IAlphaModel, which is the model
defining how insights are produced. We have IAlphaHandler, which defines how the
insights from a single 'alpha' (the algorithm) are managed, analyzed, and stored.
Types closer to the individual prediction level, such as InsightDirection, or
InsightScore relate directly to exactly 1 insight. The distinction between the
two became more clear as we developed the insights API, and from that effort it
was decided to harmonize alpha/insight terminology across the various QC systems.
2018-03-09 16:12:56 -05:00
Michael Handschuh e0f6365154 Move prediction serialization to dedicated DTO
As we strayed further from the serialization happy path, things started
getting unwieldy quickly. By defining a dedicated DTO to represent the
serialized type, we don't have to much around as much with json.net to
get it to transform/project our type to what we want. The projection is
handled completely within user code and json.net s used to serialize the
simple/flat result object.
2018-03-09 14:04:26 -05:00
Michael Handschuh 3f7b7702a4 Add Alpha.ReferenceValue
Gets the current value for the specified 'type'.
For example, this could be the price of the asset at the moment
the prediction was made.
2018-03-08 16:33:29 -05:00
Michael Handschuh 78c238a0e5 Add JsonConstructor and JsonProperty to Alpha
This resolves #1623 where we were unable to properly deserialize a
serialized alpha string. The issue was json.net didn't know what
constructor to use. In addition, json.net didn't know it was supposed
to set using the private property setters which isn't default behavior.
2018-02-27 15:08:10 -05:00
Michael Handschuh b35c0adab9 Add Alpha ctor that accepted GeneratedTimeUtc
When run in an algorithm this time is set by the algorithm. This constructor is added
to make testing alphas a little easier where we can't rely on the algorithm to set the
generated and close times properly.
2018-01-12 12:08:40 -05:00
Michael Handschuh d461544b21 Enforce deterministic scoring by using deterministic enumerables
This change fixes the non-determinism seen in the rolling averaged alpha scores. This was
caused by usage of ConcurrentDictionary coupled with the key being a new guid. The new guid
was the source of non-determinism as it caused the same conceptual alpha from backtest A to
end up in a different 'bucket' on backtest B due to a different guid. The concurrency isn't
actually needed or desirable in this context. The AlphaManager must be invoked synchronously
to avoid inconsistent/non-deterministic analysis. Given this, the collections were changed
to use HashSet<T> and now produces deterministic results independent of the alpha's id.
2018-01-12 12:08:40 -05:00
Michael Handschuh 69d3a10a93 Decompose DefaultAlphaHandler into IAlphaManagerExtension
The IAlphaManagerExtension defines a type that needs to react to events produced
by the AlphaManager. The actual events were removed in favor of a interface to
handle the events. This removes the need to wire events and instead just pass the
extensions to the alph manager and it will handle invoking the extensions at the
appropriate time.

This change removes all charting and statistics aggregation logic from the alpha
handler and moves it into dedicated types, AlphaChartingManagerExtension and
AlphaStatisticsManagerExtension. The resulting types are highly decoupled from the
LEAN ecosystem allowing them to be easily unit tested, whereas before the logic
was embedded in a handler with many many dependencies which would be very hard to
properly unit test.

As part of this change (and in preparation for moving scoring to the alpha thread)
the resolution of SecurityValues was removed from the alpha manager. In this new
pattern, the alpha manager is pushed generated alphas and security values at each
time step.
2018-01-12 12:08:40 -05:00
Michael Handschuh cfea54aecd Fixes average estimated value and delays avg score sampling 2017-12-28 12:24:59 -05:00
Michael Handschuh b07928bd58 Properly account for market hours in alpha prediction periods
If we make a prediction for 1 day in the future, we actually mean 1 trading day.
This change updates the alpha analysis logic to take into account the security's
market hours.
2017-12-28 11:27:41 -05:00
Michael Handschuh 45f97d38e9 Remove ConcurrentDictionary.IsEmpty call
It turns out that checking if it's empty is much more expensive than enumerating
an empty collection. This is because IsEmpty acquires all the locks in order to
give a 'snapshot' answer. Enumerating the dictionary directly (not .Keys or .Values)
instead acquires a single finer grained lock at a time. IIRC, one lock will manage a
few buckets internally.
2017-12-28 11:27:41 -05:00
Michael Handschuh b39fd19dc7 Remove Alpha.AlphaPeriodEndTimeUtc
The end time is a little more complicated than calendar math since we'll need to
account for weekends and market open/close hours
2017-12-28 11:27:40 -05:00
Michael Handschuh 3bfd567009 Properly seed ema of population scores
Not seeding this value cause a very heavy 0 starting value to keep the average
suppressed throughout the entire backtest, or until enough alphas are generated
to overcome the initial ema seed value.
2017-12-28 11:27:40 -05:00
Michael Handschuh dfce99f575 Sample alpha scores on next time step, ignore mag scores if mag=null 2017-12-28 11:27:40 -05:00
Michael Handschuh a2481387cd Directly enumerator concurrent dictonary instead of values
Enumerating the values directly requires the dictionary to acquire all global locks
vs enumerating the dictionary's key values pairs uses fine-grained locking at the
bucket level.
2017-12-20 10:44:56 -05:00
Michael Handschuh 2295e524f4 Add IAlphaRuntimeStatisticsGenerator and send to result handler
Provides estimates of alpha value as well as performs online computations of
alpha scores and other KPIs.

Sends alpha stats to result handler
Update live result with framework flag
2017-12-20 08:33:30 -05:00
Michael Handschuh 5a81bce5f6 Add volume and quote currency conversion to SecurityValues 2017-12-20 08:06:30 -05:00
Michael Handschuh 5337cb0b5e Fixes reporting of finalized alpha scores 2017-12-19 18:18:33 -05:00
Michael Handschuh b0032f2376 Range bound alpha scores to [0, 1]
These are intended to be percent-esque scores.
2017-12-17 05:19:15 -05:00
Michael Handschuh 3161fd600b Rename PercentChange -> Magnitude 2017-12-15 20:10:34 -05:00
Michael Handschuh b4b3aae112 Rename Signal -> Alpha
Whoa... I think I got everything :)
2017-12-15 18:24:58 -05:00
Michael Handschuh 2bc7905ea2 Add signal count and signal asset breakdown charts
Modifies the way we sample charts to be more like the equity sampling that we do.
In this case, we compute a sampling period based off of 1000 samples for the entire
backtest. In live mode, we'll just sample each minute.
2017-12-15 16:01:09 -05:00
Michael Handschuh f700bc0a6e Add Signal and Portoflio PercentChange convenience mehtods 2017-12-14 15:44:52 -05:00
Michael Handschuh 7af16009a8 Fixes bug in Signal.Clone/Force save all on handler exit
A bug in Signal.Clone caused some serious issues for the analysis engine since it thought
every signal was already past it's analysis period.

Added DefaultSignalHandler.OnExit so derived types can clean up before the thread exits.
2017-12-12 17:09:01 -05:00
Michael Handschuh c879f4ee83 Chart average signal scores daily 2017-12-12 15:57:00 -05:00
Michael Handschuh 6532d4b253 Remove SignalAnalysisResults
We're not putting signal scores directly on the signal object so this result
type is no longer needed.
2017-12-12 15:26:20 -05:00
Michael Handschuh 13b6d6eae3 Initial implementation of signal analysis
This commit provides the required infrastructure for analyzing algorithm generated
signals. The analysis of signals is mainly performed via ISignalScoreFunction which
computes scores for a signal. The SignalAnalysisContext provides contextual information
regarding the analysis of a particular signal object and also allows for the storing
of arbitrary state by consumers -- scoring functions can benefit greatly from having
this state tracked (consider any iterative function, or maybe just some heavy calcs to
be persisted between function invocations).
2017-12-12 14:01:44 -05:00
Michael Handschuh f0e86572e4 Add SignalScore.UpdatedTimeUtc
This saves off the last time the signal scores were updated so we know how
recent the scores actually are.
2017-12-12 12:33:47 -05:00
Michael Handschuh 36311be93f Remove ISignal, add Signal.GeneratedTimeUtc
This abstraction point is completely unwarranted. The signal object is really
just a DTO and it's extensible as it is currently defined. This also allows us
to enforce certain behaviors, such as internal set of GeneratedTimeUtc.

Removes GeneratedTimeUtc from the result object as it's now directly on the signal.
2017-12-12 11:57:03 -05:00
Michael Handschuh ae18fdaf77 Handle closing signals in default signal handler
Indented formatting for 'signal-results.json'.
Add GeneratedTimeUtc to SignalAnalysisResult (this will soon be moved to ISignal where it belongs).
2017-12-12 11:08:30 -05:00
Michael Handschuh 60ddbca0e4 Update signal handler to persist signals
Revert changes to result handlers from previously persisting signals in there.
Minor changes to the signal analysis result class.
2017-12-12 09:18:32 -05:00
Michael Handschuh cd13f77db7 Add signal scoring objects
These objects define and hold results from signal analysis
2017-12-12 08:56:20 -05:00
Michael Handschuh d4674e6f73 Signal.PercentChange doc update, ToString to include Id 2017-12-12 08:56:20 -05:00
Michael Handschuh d0b7c9b31e Make ISignal.Period a required field
Requiring a period here forces signal models to place a time frame on
when their signal is valid. This also allows consumers of signals to
have some expectation of when a prediction should come to fruition.
2017-12-10 08:41:29 -05:00
Michael Handschuh ca9d5182a0 Add ISymbol.Id unique identifier
This guid uniquely identifies a signal instance
2017-12-10 08:41:29 -05:00
Michael Handschuh c264132447 Move json serialization attributes to inteface
It's not unreasonable to think users will provide their own ISignal implementation
and we'll want them all behaving by the same serialization rules to make consumption
much easier.
2017-12-08 10:07:54 -05:00
Michael Handschuh 7e63f2ae42 Add SignalCollection and ISignal.Clone
SignalCollection groups signals by the time they were generated.
This will be used as a transport medium out of the algorithm.
2017-12-08 10:07:54 -05:00
Michael Handschuh 24cebd2aa1 Move signal/target data structures to common
Since these are really just data structures they belon in the common library. Also,
it stands to reason that we'll want to reuse them in other components, such as the
result handler.
2017-12-08 10:07:54 -05:00