How to do this in C#?
def CoarseSelectionFunction(self, coarse):
if self.rebalence_flag or self.first_month_trade_flag:
# drop stocks which have no fundamental data or have too low prices
selected = [x for x in coarse if (x.HasFundamentalData) and (float(x.Price) > 5)]
# rank the stocks by dollar volume
filtered = sorted(selected, key=lambda x: x.DollarVolume, reverse=True)
return [ x.Symbol for x in filtered[:200]]
else:
return self.symbols
def FineSelectionFunction(self, fine):
if self.rebalence_flag or self.first_month_trade_flag:
hist = self.History([i.Symbol for i in fine], 1, Resolution.Daily)
try:
filtered_fine = [x for x in fine if (x.ValuationRatios.EVToEBITDA > 0)
and (x.EarningReports.BasicAverageShares.ThreeMonths > 0)
and float(x.EarningReports.BasicAverageShares.ThreeMonths) * hist.loc[str(x.Symbol)]['close'][0] > 2e9]
except:
filtered_fine = [x for x in fine if (x.ValuationRatios.EVToEBITDA > 0)
and (x.EarningReports.BasicAverageShares.ThreeMonths > 0)]
top = sorted(filtered_fine, key = lambda x: x.ValuationRatios.EVToEBITDA, reverse=True)[:self.num_screener]
self.symbols = [x.Symbol for x in top]
self.rebalence_flag = 0
self.first_month_trade_flag = 0
self.trade_flag = 1
return self.symbols
else:
return self.symbols
I tried:
public IEnumerable<Symbol> FineSelectionFunction(IEnumerable<FineFundamental> fine)
{
if (!_reSelectUniverse)
return Enumerable.Empty<Symbol>();
var hist = History(fine.Select(x => x.Symbol), 1, Resolution.Daily);
try
{
filtered = from x in fine
where x.SecurityReference.SecurityType == "ST00000001"
where x.SecurityReference.IsPrimaryShare
where x.ValuationRatios.EVToEBITDA > 0
where x.EarningReports.BasicAverageShares.ThreeMonths > 0
where ((float) x.EarningReports.BasicAverageShares.ThreeMonths) * hist ????) > 2 * 1000 * 1000 * 1000)
select x;
filtered_fine = [x for x in fine if (x.ValuationRatios.EVToEBITDA > 0)
and(x.EarningReports.BasicAverageShares.ThreeMonths > 0)
and float(x.EarningReports.BasicAverageShares.ThreeMonths) * hist.loc[str(x.Symbol)]['close'][0] > 2e9]
}
catch
{
except:
filtered_fine = [x for x in fine if (x.ValuationRatios.EVToEBITDA > 0)
and(x.EarningReports.BasicAverageShares.ThreeMonths > 0)]
}
return filtered.OrderByDescending(x => x.ValuationRatios.EVToEBITDA).Select(x => x.Symbol);
}
no success so far...
Alexandre Catarino
I have attached a backtest of a simplified C# version of your code.
The key part is the FineSelectionFunction method:
public IEnumerable<Symbol> FineSelectionFunction(IEnumerable<FineFundamental> fine) { if (_reSelectUniverse) { _symbols = fine .Where(x => x.SecurityReference.SecurityType == "ST00000001") .Where(x => x.SecurityReference.IsPrimaryShare) .Where(x => x.ValuationRatios.EVToEBITDA > 0) .Where(x => x.EarningReports.BasicAverageShares.ThreeMonths > 0) .Where(x => { var averageShares = x.EarningReports.BasicAverageShares.ThreeMonths; var history = History(x.Symbol, 1, Resolution.Daily); var close = history.FirstOrDefault()?.Close; // If history is empty, close will be null // In this case, we will not consider the security if (close == null) { return false; } return averageShares * close > 2 * 1000 * 1000 * 1000; }) .OrderByDescending(x => x.ValuationRatios.EVToEBITDA) .Select(x => x.Symbol); } return _symbols; } }
Note that we make a history request for a single symbol only if the previsous conditions were met. If the request turns out to return an empty enumerable, that symbol will not be added.
Quant Trader
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.
To unlock posting to the community forums please complete at least 30% of Boot Camp.
You can continue your Boot Camp training progress from the terminal. We hope to see you in the community soon!