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.503
Tracking Error
0.179
Treynor Ratio
0
Total Fees
$0.00
from System.Drawing import Color
import numpy as np

class Fundamentals(QCAlgorithm):
    
    def Initialize(self):
        self.SetStartDate(2005, 7, 1)                       # the starting date for our backtest
        #self.SetEndDate(2019, 2, 1)                        # the ending date for our backtest (if no date, then it will test up until today)
        self.start_value = 5000                             # define the amount of starting cash
        self.SetCash(self.start_value)                      # initialize our wallet
        self.EQY_LIST = ["MSFT", "AAPL","GOOG","FB"]                                  # define the stock ticker
        self.GetSymbol = {}
        
        for ticker in self.EQY_LIST:
            symbol = self.AddEquity(ticker, Resolution.Daily).Symbol
            self.GetSymbol[ticker] = symbol
            # add a stock to the list of those we want to trade, and how often to pull data
        #self.symbols = Symbol.Create(self.EQY, SecurityType.Equity, Market.USA)
        #self.symbols = Symbol.Create(self.EQY, SecurityType.Equity, Market.USA)
        
        self.AddUniverse(self.SelectCoarse, self.SelectFine) # Dummy Universe Selection
    
        self.Consolidate(self.EQY_LIST[0], Calendar.Monthly, self.OnMonthlyBar)
    
    
    def SelectCoarse(self, coarse):
        # Return symbols we've already pre-selected
        return [c.Symbol for c in coarse if c.Symbol.Value in self.EQY_LIST]
        
    def SelectFine(self, fine):
        
        return [f.Symbol for f in fine if f.Symbol.Value in self.EQY_LIST] # This will subscribe us to fundamental data for those securities
    
    
    def OnMonthlyBar(self, monthlyBar):
        
        for ii in self.EQY_LIST:
            symbol = self.GetSymbol[ii]
        
            x = self.Securities[symbol].Fundamentals
            
            if x is None: 
                return
            
            F = FScore(x.FinancialStatements.IncomeStatement.NetIncome.TwelveMonths, 
                x.FinancialStatements.CashFlowStatement.CashFlowFromContinuingOperatingActivities.TwelveMonths,
                x.OperationRatios.ROA.ThreeMonths, x.OperationRatios.ROA.OneYear,
                x.FinancialStatements.BalanceSheet.ShareIssued.ThreeMonths, x.FinancialStatements.BalanceSheet.ShareIssued.TwelveMonths,  
                x.OperationRatios.GrossMargin.ThreeMonths, x.OperationRatios.GrossMargin.OneYear,
                x.OperationRatios.LongTermDebtEquityRatio.ThreeMonths, x.OperationRatios.LongTermDebtEquityRatio.OneYear,
                x.OperationRatios.CurrentRatio.ThreeMonths, x.OperationRatios.CurrentRatio.OneYear,
                x.OperationRatios.AssetsTurnover.ThreeMonths, x.OperationRatios.AssetsTurnover.OneYear).ObjectiveScore()
                
            self.Log("symbol" + " : " + str(F))
            
            self.Plot("F Score", symbol ,F)








        
class FScore(object):
    def __init__(self, netincome, operating_cashflow, roa_current,
                 roa_past, issued_current, issued_past, grossm_current, grossm_past,
                 longterm_current, longterm_past, curratio_current, curratio_past,
                 assetturn_current, assetturn_past):
        self.netincome = netincome
        self.operating_cashflow = operating_cashflow
        self.roa_current = roa_current
        self.roa_past = roa_past
        self.issued_current = issued_current
        self.issued_past = issued_past
        self.grossm_current = grossm_current
        self.grossm_past = grossm_past
        self.longterm_current = longterm_current
        self.longterm_past = longterm_past
        self.curratio_current = curratio_current
        self.curratio_past = curratio_past
        self.assetturn_current = assetturn_current
        self.assetturn_past = assetturn_past

    def ObjectiveScore(self):
        fscore = 0
        fscore += np.where(self.netincome > 0, 1, 0)
        fscore += np.where(self.operating_cashflow > 0, 1, 0)
        fscore += np.where(self.roa_current > self.roa_past, 1, 0)
        fscore += np.where(self.operating_cashflow > self.roa_current, 1, 0)
        fscore += np.where(self.longterm_current <= self.longterm_past, 1, 0)
        fscore += np.where(self.curratio_current >= self.curratio_past, 1, 0)
        fscore += np.where(self.issued_current <= self.issued_past, 1, 0)
        fscore += np.where(self.grossm_current >= self.grossm_past, 1, 0)
        fscore += np.where(self.assetturn_current >= self.assetturn_past, 1, 0)
        return fscore