Back

Passing a Slice to another object (generally: running algorithm logic outside of starting object)

Hi,

I'm trying to design a set-up where, within one project, I have multiple different strategies, each a class of its own. They each have a run-method to take in a Slice.

In the main/starting object (the one specified in config.json), I then want to instantiate one of these strategies and pass it the Slice it needs.

This currently doesn't work - the first time I use the Slice object in my sub-strategy, the program exits telling me I'm referencing something that is not an instance of an object. The debugger shows clearly that there is a Slice object being passed. I'm also able to use that Slice object in the main strategy -- only when it gets passed to the sub-strategy, it stops working.

 

Is this kind of a set-up possible within Lean? I have a feeling it's not really meant to be working with any more than one algorithm inheriting QCAlgorithm. Is there a better way to do this?

 

If it is possible, I'd be happy to send a minimal working example over to show what I'm trying to do.

 

If not, I'll have to think of a different way to achieve this.

Thanks in advance,

Douglas

Update Backtest







read this maybe it helps you, that is the way you have to go

https://www.quantconnect.com/forum/discussion/3682/algo-framework-etf-momentum-rebalancing-using-mean-variance-optimization
0

that is something simpler but i dont know if it works

clone and take a look at daniels code:

https://www.quantconnect.com/forum/discussion/860/qcu-complete-algorithm-framework-help
0

docu for first post

https://www.quantconnect.com/docs/algorithm-framework/overview
0

Thanks - that's helpful but is a bit more extensive than what I'd like to achieve.

Is it possible to, in general, call a "logic" algorithm from the algorithm that's run by the launcher? I'd like that "logic" algorithm to contain everything the strategy needs, such as creating its own indicators.

I guess what I'm trying to achieve is a "light" version of this Framework you've highlighted.

0

Hi Douglas -- welcome to QuantConnect @ LEAN. There are almost no restrictions to what you can do. Recommend just trying what you're asking for -- make the classes -- pass the slice around.

0

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.


Hi Jared, nice to speak to you again. 

I tried just what you suggested. I'll revert with a minimal working example where my issue is shown. 

0

Here we go - the error first:

ERROR:: AlgorithmManager.Run(): RuntimeError: Slice: System.NullReferenceException: Object reference not set to an instance of an object.

The error then refers to the line that runs the .Add() method on _close, close to the end of the below snippet.

The minimal working example that produces it follows.

Thank you in advance for any help!

using System;
using System.Collections.Generic;
using System.Linq;
using QuantConnect.Data;
using QuantConnect.Indicators;
using QuantConnect.Configuration;

namespace QuantConnect.Algorithm.CSharp
{
/// <summary>
/// Main entrypoint for all algorithms, does general startup procedures and selects the symbol(s) and strategy(ies) to run
/// </summary>
public class MainAlgo : QCAlgorithm
{
public AlgoSignal signal;

public override void Initialize()
{
SetStartDate( Config.GetValue<DateTime>( "startDate", new DateTime( 2017, 11, 1 ) ) );
SetEndDate( Config.GetValue<DateTime>( "endDate", new DateTime( 2017, 12, 1 ) ) );
SetCash( 10000 );

var symbol = AddForex( "EURUSD", Resolution.Minute, Market.FXCM ).Symbol;

signal = new AlgoSignal( symbol );

}

public override void OnData( Slice slice )
{
signal.Run( slice );
}
}

/// <summary>
/// The actual signal logic
/// </summary>
public class AlgoSignal : QCAlgorithm
{
private RollingWindow<decimal> _close;

public Symbol symbol;

public AlgoSignal( Symbol symbol )
{
this.symbol = symbol;
}

public void Run( Slice slice )
{
// Add close to rolling window
_close.Add( slice.Bars[symbol].Close );

// do more stuff
}
}
}
0

please clone daniels framwork which is a framework provided by QC originally.

there is this example covered what you try to achieve

start with something working

check main.cs than alphamanager in brain code

0

Update Backtest





0

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.


Loading...

This discussion is closed