Overall Statistics
Total Trades
0
Average Win
0%
Average Loss
0%
Compounding Annual Return
0%
Drawdown
0%
Expectancy
0
Net Profit
0%
Sharpe Ratio
0
Probabilistic Sharpe Ratio
0%
Loss Rate
0%
Win Rate
0%
Profit-Loss Ratio
0
Alpha
0
Beta
0
Annual Standard Deviation
0
Annual Variance
0
Information Ratio
0
Tracking Error
0
Treynor Ratio
0
Total Fees
$0.00
Estimated Strategy Capacity
$0
Lowest Capacity Asset
Portfolio Turnover
0%
# region imports
from AlgorithmImports import *
import datetime
# endregion

class TestFundamentalIssue(QCAlgorithm):

    def Initialize(self):
        self.SetStartDate(2020, 11, 5)
        self.SetEndDate(2020, 12, 5)
        self.SetCash(100000)
        self.SetWarmUp(1)
        self.UniverseSettings.Resolution = Resolution.Daily
        self.AddUniverse(self.CoarseSelectionFunction, self.SelectFine)
        self.SetBrokerageModel(InteractiveBrokersBrokerageModel())
        self.SetExecution(ImmediateExecutionModel())

        self.rebalence_flag = 0
        self.SPY = self.AddEquity("SPY",Resolution.Hour).Symbol
        self.Schedule.On(self.DateRules.MonthEnd("SPY", 2), self.TimeRules.At(1, 0), Action(self.monthly_rebalance))
    
    def monthly_rebalance(self):
        self.rebalence_flag = 1

    def CoarseSelectionFunction(self, coarse):  
        if not self.rebalence_flag:
            return Universe.Unchanged
    
        sortedByDollarVolume = sorted([x for x in coarse if x.HasFundamentalData and x.DollarVolume > 3000000 and x.Price >0],
                                     key = lambda x: x.DollarVolume, reverse=True)[:200]

        return [x.Symbol for x in sortedByDollarVolume]

    def SelectFine(self, fine):
        if not self.rebalence_flag:
            return Universe.Unchanged
            
        self.rebalence_flag = 0

        fine = ([x for x in fine if x.CompanyReference.CountryId == "USA" and x.CompanyReference.PrimaryExchangeID in ["NYS","NAS"]
                                            and (self.Time - x.SecurityReference.IPODate).days > 180 #180
                                            and x.MarketCap > 2e9
                                            ])

        fun_filter = (lambda x: x.OperationRatios.ROA.ThreeMonths if not np.isnan(x.OperationRatios.ROA.ThreeMonths) else 0)

        fine = ([x for x in fine if fun_filter(x) > 0])
        
        if self.Time > datetime.datetime(2020,11,10):
            fun_map = {x.Symbol.Value:x for x in fine}
            self.Log('GPS1:' + str(fun_filter(fun_map['GPS'])))
        
        sortedByratio = sorted(fine, key=fun_filter, reverse=True)[:100]

        if self.Time > datetime.datetime(2020,11,10):
            fun_map = {x.Symbol.Value:x for x in sortedByratio}
            self.Log('cut:' + str(fun_filter(sortedByratio[-1])))
        
        return [x.Symbol for x in sortedByratio]