Back

Adding/Removing Securities to onData based on Universe

I have a fine filtered Universe and I would like to handle the data more like the classic algorithms after I have filtered as necessary using the Course/Fine universes.  Is it possible to have the results of the Course/Fine filters add/remove these securities to those subscribed to and available in onData?

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.


Not sure I quite understand your question, but assets selected by universe

selection are automatically added to your algorithm.
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 looks like the algorithm doesn't actually go into onData with the way I have it set up.  But, I'd like to handle the positions in the onData method, if possible.

namespace QuantConnect.Algorithm.CSharp
{

public class BasicTemplateFrameworkAlgorithm : QCAlgorithmFramework
{

List<string> top5 = new List<string>();

public override void Initialize()
{
SetStartDate(2019, 1, 28); //Set Start Date
SetEndDate(2019, 1, 30); //Set End Date
SetCash(100000); //Set Strategy Cash

// Coarse Fine Universe Selection Models.
SetUniverseSelection(new FineFundamentalUniverseSelectionModel(CoarseSelectionFunction, FineSelectionFunction));

SetAlpha(new NullAlphaModel());

SetPortfolioConstruction(new NullPortfolioConstructionModel());

SetExecution(new ImmediateExecutionModel());

SetRiskManagement(new MaximumDrawdownPercentPerSecurity(0.05m));

}

public IEnumerable<Symbol> CoarseSelectionFunction(IEnumerable<CoarseFundamental> coarse)
{
var numberOfSymbolsCoarse = 100;

// select only symbols with fundamental data and sort descending by daily dollar volume
var sortedByDollarVolume = coarse
.Where(x => x.HasFundamentalData)
.OrderByDescending(x => x.DollarVolume);

// take the top entries from our sorted collection
var top5 = sortedByDollarVolume.Take(numberOfSymbolsCoarse);

// we need to return only the symbol objects
return top5.Select(x => x.Symbol);
}

// sort the data by P/E ratio and take the top 'numberOfSymbolsFine'
public IEnumerable<Symbol> FineSelectionFunction(IEnumerable<FineFundamental> fine)
{
var numberOfSymbolsFine = 5;

// sort descending by P/E ratio
var sortedByPeRatio = fine.OrderByDescending(x => x.ValuationRatios.PERatio);

// take the top entries from our sorted collection
var topFine = sortedByPeRatio.Take(numberOfSymbolsFine);

// we need to return only the symbol objects
List<string> temptop5 = new List<string>();
var temp = topFine.Select(x => x.Symbol);
foreach (String s in temp)
{
string str = s.Substring(0,3);
Log("adding " + s.Substring(0,3));
temptop5.Add(s.Substring(0,3));
}
top5 = temptop5;
return topFine.Select(x => x.Symbol);
}


public override void OnOrderEvent(OrderEvent orderEvent)
{
if (orderEvent.Status.IsFill())
{
// Debug($"Purchased Stock: {orderEvent.Symbol}");
}
}

public void onData(TradeBars data)
{
Log("Is Invested: " + Portfolio.Invested);
if(!Portfolio.Invested)
{
Log("Market order 50%: " + top5[0]);
MarketOrder(top5[0],0.5);
Log("Market order 20%: " + top5[1]);
MarketOrder(top5[1],0.2);
Log("Market order 10%: " + top5[2]);
MarketOrder(top5[2],0.1);
}
}
}
}
0

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;
}
}
}
}

 

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.


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