Back

Help: Converting 3 Lines of QC Python Code to C# (QC Universe Fine Fundamental Selection)

Hi All,

Sorry to ask a noob question, but I am trying to use fine fundamental to sort small cap stocks like in the tutorial link at the bottom. I'm new to C# and just can't seem to get the code right for the the for loop/sort function in the below python example.

This is snippet of code I am struggling with. It is the for loop I can't get working

def FineSelectionFunction(self, fine):
if self.yearly_rebalance:
fine = [x for x in fine if (x.ValuationRatios.PERatio > 0)
and (x.EarningReports.BasicAverageShares.ThreeMonths > 0)
and (x.EarningReports.BasicEPS.TwelveMonths > 0)]
##THE BELOW FOR LOOP I CANT CONVERT PROPERLY##
for i in fine:
i.MarketCap = float(i.EarningReports.BasicAverageShares.ThreeMonths * (i.EarningReports.BasicEPS.TwelveMonths*i.ValuationRatios.PERatio))
sorted_market_cap = sorted(fine, key=lambda x: x.MarketCap)
self.filtered_fine = [i.Symbol for i in sorted_market_cap[:20]]
self.yearly_rebalance = False
return self.filtered_fine
else:
return []

This is the code I currently have: note it is incomplete

public IEnumerable<Symbol> FineSelectionFunction(IEnumerable<FineFundamental> fine)
{
// return list of symbols
var fineStocks = (from f in fine
//where f => f.ValuationRatios.PERatio > 0 && f.EarningReports.BasicAverageShares.ThreeMonths > 0 && f.EarningReports.BasicEPS.TwelveMonths > 0
where f.ValuationRatios.PERatio > 0 && f.EarningReports.BasicAverageShares.ThreeMonths > 0 && f.EarningReports.BasicEPS.TwelveMonths > 0
orderby f.ValuationRatios.PERatio ascending
//select f.Symbol).Take(10);
select f.Symbol);

 

I would appreciate a kind sir to help a noob out please :)

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.


For reference: this is the tutorial link the initial code came from: 

https://www.quantconnect.com/tutorials/strategy-library/small-capitalization-stocks-premium-anomaly

 

0

Hi James,

I believe the problem is MarketCap is not a pre-defined property of FineFundamental object. In python you can add a new property to object, but it’s not doable in C#. Therefore we have to use something else to achieve the same result.

Here is my attempt using the “let” clause to calculate and store the market cap value for each FineFundamental object. Hope it helps!

public IEnumerable<Symbol> FineSelectionFunction(IEnumerable<FineFundamental> fine)
{
if (_yearly_rebalance){
_yearly_rebalance = false;
return (from f in fine
where f.ValuationRatios.PERatio > 0
&& f.EarningReports.BasicAverageShares.ThreeMonths > 0
&& f.EarningReports.BasicEPS.TwelveMonths > 0
let marketCap = (float) (f.EarningReports.BasicAverageShares.ThreeMonths
* f.EarningReports.BasicEPS.TwelveMonths
* f.ValuationRatios.PERatio)
orderby marketCap
select f.Symbol).Take(20);
}
else{
return new List<Symbol>();
}
}

 

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