ScheduledUniverseSelectionModel With FineSelection Filtering order by Momentum

Back

Tyring to use selection data filtering like here for data pulled from Dropbox. I have a dropbox with n numbers of symbols , which i want to filter further. I would like to order by momentum desc but to do so i need IEnumerable<CoarseFundamental> retruned instead of IEnumerable<Symbol>, How can i change my code to get 

Top 10 stokcs order by Momentum from dropbox list

 

 

namespace QuantConnect.Algorithm.CSharp
{

public class RobinHoodAlgorithm : QCAlgorithm
{
// the changes from the previous universe selection
private SecurityChanges _changes = SecurityChanges.None;
private readonly Dictionary<DateTime, List<string>> _backtestSymbolsPerDay = new Dictionary<DateTime, List<string>>();
private const string LiveUrl = @"https://www.dropbox.com/s/2l73mu97gcehmh7/daily-stock-picker-live.csv?dl=1";
private const string BacktestUrl = @"https://www.dropbox.com/s/ae1couew5ir3z9y/daily-stock-picker-backtest.csv?dl=1";


/// <summary>
/// Initialise the data and resolution required, as well as the cash and start-end dates for your algorithm. All algorithms must initialized.
/// </summary>
/// <seealso cref="QCAlgorithm.SetStartDate(System.DateTime)"/>
/// <seealso cref="QCAlgorithm.SetEndDate(System.DateTime)"/>
/// <seealso cref="QCAlgorithm.SetCash(decimal)"/>
public override void Initialize()
{
UniverseSettings.Resolution = Resolution.Hour;

SetStartDate(2017, 07, 04);
SetEndDate(2018, 07, 04);
SetUniverseSelection(
new ScheduledUniverseSelectionModel(
DateRules.EveryDay(),
TimeRules.AfterMarketOpen("SPY", 15, true),
FineSelectionFunction
)
);


}


public IEnumerable<Symbol> FineSelectionFunction(DateTime dateTime)
{

var symbols = SelectSymbols(dateTime);

ConcurrentDictionary<Symbol, SelectionData>
_stateData = new ConcurrentDictionary<Symbol, SelectionData>();

var stocks = (from c in symbols
let selData = _stateData.GetOrAdd(c, sym => new SelectionData(sym, 10))
where selData.Update(c.Time, c.)
orderby avg.Momentum descending
select c.Symbol).Take(10).ToList();
return stocks;

}


private IEnumerable<Symbol> SelectSymbols(DateTime dateTime)
{
var url = LiveMode ? LiveUrl : BacktestUrl;
var file = Download(url);
if (LiveMode)
{
// fetch the file from dropbox
// if we have a file for today, break apart by commas and return symbols
if (file.Length > 0) return file.ToCsv().Select(x => QuantConnect.Symbol.Create(x, SecurityType.Equity, Market.USA));
// no symbol today, leave universe unchanged
return Universe.Unchanged;
}

// backtest - first cache the entire file
if (_backtestSymbolsPerDay.Count == 0)
{
// split the file into lines and add to our cache
foreach (var line in file.Split(new[] { '\n', '\r' }, StringSplitOptions.RemoveEmptyEntries))
{
var csv = line.ToCsv();
var date = DateTime.ParseExact(csv[0], "yyyyMMdd", null);
var symbols = csv.Skip(1).ToList();
_backtestSymbolsPerDay[date] = symbols;
}
}
// if we have symbols for this date return them, else specify Universe.Unchanged
List<string> result;
if (_backtestSymbolsPerDay.TryGetValue(dateTime.Date, out result))
{
return result.Select(x => QuantConnect.Symbol.Create(x, SecurityType.Equity, Market.USA)); ;
}
return Universe.Unchanged;
}



public override void OnData(Slice slice)
{

}

public override void OnSecuritiesChanged(SecurityChanges changes)
{

}

}
class SelectionData
{
public readonly Symbol Symbol;
public readonly Momentum Momentum;
public readonly ExponentialMovingAverage Ema;

public SelectionData(Symbol symbol, int period)
{
Symbol = symbol;
Momentum = new Momentum(symbol.Value,period);
Ema = new ExponentialMovingAverage(period);

}
public bool Update(DateTime time, decimal value)
{
var ready = Momentum.Update(time, value);
Ema.Update(time, value);
return ready;
}


}

}

 

 

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.


Hi SherpaTrader,

We need to initialize the _stateData dictionary earlier, subscribe to the symbols, and then we need to Update the data in the _stateData with data in OnData or with using History warm up. Then, we’d need to fix/remove the where clause in the LINQ used in the Scheduled Universe selection function. 

Best,
Shile Wen

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