Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

On Security Cache Concept #6809

Closed
3 tasks done
jaredbroad opened this issue Dec 18, 2022 · 2 comments · Fixed by #7327
Closed
3 tasks done

On Security Cache Concept #6809

jaredbroad opened this issue Dec 18, 2022 · 2 comments · Fixed by #7327
Assignees
Labels

Comments

@jaredbroad
Copy link
Member

jaredbroad commented Dec 18, 2022

Creating a thread to discuss on security caching sugar, to remove some of the boiler plate scaffolding required building multi asset strategies.

Expected Behavior

Minimal boiler plate code possible. Ideally not require all users to create parallel dictionaries or management classes.

Actual Behavior

To tidily hold multiple securities alpha / trade management you often need to create boiler plate symbol data classes.

With python duck typing it happens for free; but the objects are not transferred between Alpha-Universe models. It would be ideal to suggest a route that transfers that data.

Potential Solution

A use pattern like this: applied with a class collection;

  • This eliminates the need for the Alpha/QCAlgorithm/Universe Dictionary<Symbol, SymbolData> class and adding/removing it with the OnSecurities changed event.
class SymbolData;
#Store and return or fetch
alpha = security.Store<SymbolData>("key", new SymbolData())
alpha = security.Fetch<SymbolData>("key")

if (alpha.EMAShort > alpha.EMALong) 
{
}

or applied directly with objects / no class:

  • Eliminates the boiler plate wrapper SymbolData class entirely.
# Store and return
long = security.Store<Indicator>("EMA_Long", EMA(security.Symbol, 200));
short = security.Store<Indicator>("EMA_Short", EMA(security.Symbol, 50));

if (short > long) 
{
   SetHoldings(security.Symbol, 1);
}

#Without saving object  / e.g. universes 
if (security.Fetch<Indicator>("EMA_Short") > security.Fetch<Indicator>("EMA_Long") )
{
}

Possibly this could be implemented under the hood in the existing SecurityCache class, or extending/replacing the current Security Cache class:

security.Cache.Set(key, value)
security.Cache.Get(key)

security.Cache.Set<T>(key, value)
T security.Cache.Get<T>(key)

Or direct indexer on the Security Object with dynamics+reflection❤️; stored to dynamic Security Cache sub collection under hood. When speed a concern people can switch to typed version.

  • Puts it on level field with python duck typing, and extends python duck typing to work anywhere in the algorithm.
OnSecuritiesChanged(changes)
 for security in changes.AddedSecurities {
     security.Long = EMA(security.Symbol, 200)
     security.Short = EMA(security.Symbol, 200)
}
...
if (security.Short > security.Long) 
...
security.Cache.SetDynamic(key, value)
security.Cache.GetDynamic(key)

Checklist

  • I have completely filled out this template
  • I have confirmed that this issue exists on the current master branch
  • I have confirmed that this is not a duplicate issue by searching issues
@jaredbroad
Copy link
Member Author

Quick test worked nicely, but for clear separation of properties vs more complex types perhaps the storage collection should be divided into custom-properties and custom-objects in general -- like:

Cache.Properties["EMA"] <<-- security.EMA  // accessible only with dynamic key word
Cache.Set("key", value) <<-- security.Set("key", value) // accessible with Get<T>
Cache.Get<T>("ema200") <<-- security.Get<Indicator>("ema200") // accessible with Get<T>

@jaredbroad
Copy link
Member Author

Potentially would solve the often requested "How do I pipe data through the framework?" question rather than attaching other signal objects to Insights.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants