Back

Instantiate QCAlgorithm inside QCAlgorithm

Hi everyone! I would like to do what is sketched in the following code snippet.

When the robot is running, every Saturday (when the market is close) it would optimize the value of optimizedParameter that it would use in the next week. The optimization requires running
a backtest on the two previous weeks.

Is it possible to do that? I tried to instatiate another object derived from QCAlgorithm to run the backtest but to no avail.

Best regards,

namespace QuantConnect {
public class BasicTemplateAlgorithm : QCAlgorithm {

float optimizedParameter;

public override void Initialize() {

Schedule.On(DateRules.Every(DayOfWeek.Saturday), TimeRules.At(12, 0), () => {
bool notOptimum = false;
while (notOptimum) {
// Pick next candidate value for optimizedParameter

// Run backtest on the previous 2 weeks, using optimizedParameter
}
});
}
public override void OnData(Slice data) {
// Do something that depends on optimizedParameter
}
}
}

 

Update Backtest








My guess is you can't currently create QCAlgorithm instances (without modifying Lean) since there's too much stuff in the internal details of that class to work detached from its context. The workaround is to create your own "clean" class for your algorithm logic that can accept either live or history data, and then instantiate that class since it will have no restrictions.

1

Hmm... I think you might be able to use some sort of workaround. If "Running a backtest" could be translated as "iterate past data to check possible outcomes if I changed this and that", you could simply store your Slices into a two weeks long List<Slice> and adjust the code logic of a "running a backtest" to a simple iteration method that would be called as scheduled.

0

Have you tried with the genetic algorithm implementation by James Smith?

I think is the right thing to do in this case.

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.


It is not possible, unless, like Petter Hansson pointed out, you modify Lean.
The engine (AlgorithmManager) calls the algorithm methods when there is new data event, so you would have to emulate that part. 

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.


Thank you all for the suggestions!

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