Hi Stephen,
Since you're working with a QC Framework Algorithm, the OnData() method not the best place to send trade orders. If you want to use a QC Framework Algorithm, then the data gets passed and used to create insights in the Update() method of the Alpha Model. You can create a custom Alpha Model or use one of QuantConnect's found here. The Alpha Model will need to generate insights and then these will be passed to the Execution Model which will place orders (the Execution Model can be customized as well, or you can use on of QuantConnect's found here), I've attached a backtest with a few lines of code sketching out how this can be accomplished, and reading the documentation and sourcing information from the community forum will provide a lot of help and guidance.
However, if you were to do this in the OnData() method of a classic QC Algorithm or if you want to use a QCAlgorithmFrameworkBridge to upgrade a classic algorithm to produce insights like an Alpha Model, then the OnData() section would look something like the code snippet I've attached below.
The most important thing to note is that any variable you instantiate in either the CoarseSelectionFunction() or the FineSelectionFunction() can't be accessed in the OnData() method.
public void OnData(TradeBars data)
{
Log("Is Invested: " + Portfolio.Invested);
if(!Portfolio.Invested)
{
var indexer = 0;
double[] values = { 0.5, 0.2, 0.1 };
List<decimal> weights = values.Select(i => Convert.ToDecimal(i)).ToList();
// Note that the top5 variable is no longer accessible outside of the FineUniverseSelection function
foreach (String symbol in Portfolio.Keys)
{
if (indexer < 3)
{
Log("Set Holdings " + (100*weights[indexer]).ToString() + "%: " + symbol);
SetHoldings(symbol, weights[indexer]);
indexer += 1;
}
}
}
}
Â