Overall Statistics
Total Trades
235
Average Win
0.72%
Average Loss
-0.29%
Compounding Annual Return
-20.505%
Drawdown
14.400%
Expectancy
-0.173
Net Profit
-12.641%
Sharpe Ratio
-1.235
Probabilistic Sharpe Ratio
3.250%
Loss Rate
76%
Win Rate
24%
Profit-Loss Ratio
2.47
Alpha
0
Beta
0
Annual Standard Deviation
0.155
Annual Variance
0.024
Information Ratio
-1.235
Tracking Error
0.155
Treynor Ratio
0
Total Fees
$737.12
Estimated Strategy Capacity
$38.00
from QuantConnect import *
from QuantConnect.Parameters import *
from QuantConnect.Benchmarks import *
from QuantConnect.Brokerages import *
from QuantConnect.Util import *
from QuantConnect.Interfaces import *
from QuantConnect.Algorithm import *
from QuantConnect.Algorithm.Framework import *
from QuantConnect.Algorithm.Framework.Selection import *
from QuantConnect.Algorithm.Framework.Alphas import *
from QuantConnect.Algorithm.Framework.Portfolio import *
from QuantConnect.Algorithm.Framework.Execution import *
from QuantConnect.Algorithm.Framework.Risk import *
from QuantConnect.Indicators import *
from QuantConnect.Data import *
from QuantConnect.Data.Consolidators import *
from QuantConnect.Data.Custom import *
from QuantConnect.Data.Fundamental import *
from QuantConnect.Data.Market import *
from QuantConnect.Data.UniverseSelection import *
from QuantConnect.Notifications import *
from QuantConnect.Orders import *
from QuantConnect.Orders.Fees import *
from QuantConnect.Orders.Fills import *
from QuantConnect.Orders.Slippage import *
from QuantConnect.Scheduling import *
from QuantConnect.Securities import *
from QuantConnect.Securities.Equity import *
from QuantConnect.Securities.Forex import *
from QuantConnect.Securities.Interfaces import *
from datetime import date, datetime, timedelta
from QuantConnect.Python import *
from QuantConnect.Storage import *
QCAlgorithmFramework = QCAlgorithm
QCAlgorithmFrameworkBridge = QCAlgorithm
import math
import numpy as np
import pandas as pd
import scipy as sp

class MicroGrowth(QCAlgorithm):

    def Initialize(self):
        #self.SetStartDate(2020, 2, 12)  # Set Start Date
        self.SetStartDate(2011, 2, 28)
        self.SetEndDate(2011, 10, 1)
        self.SetCash(100000)  # Set Strategy Cash
        #self.Settings.FreePortfolioValuePercentage = 0.6
        self.SetBrokerageModel(BrokerageName.InteractiveBrokersBrokerage, AccountType.Cash)
        self.UniverseSettings.Resolution = Resolution.Daily
        self.AddUniverse(self.CoarseSelectionFunction, self.FineSelectionFunction)
        self.lastmonth = -1
        self.lastday = -1
        self.monthinterval = 1
        self.Symbols = None
        self.tobeliquidated = None
        self.numsecurities = 25
        #self.SetWarmUp(timedelta(365))
            
    def CoarseSelectionFunction(self,coarse):
        if self.IsWarmingUp: return
        if self.lastmonth == -1 or self.Time.month != self.lastmonth:
            self.lastmonth = self.Time.month
            self.lastday = self.Time.day
            return [x.Symbol for x in coarse if x.HasFundamentalData]
        else:
            return Universe.Unchanged
    
    def FineSelectionFunction(self,fine):
        #momo_dict = {}
        security_momo_list = []
        MKTCAP_dict = {}
        #exclude delisted and TOPS (due to split value issue)
        excluded_delisted = [i for i in fine if isinstance(i.SecurityReference.DelistingDate.date(),datetime) == False and i.Symbol.Value != "TOPS"]
        
        #filter by mkt_cap
        for i in fine:
            if isinstance(i.MarketCap,(float,int)) and i.MarketCap != 0:
                MKTCAP_dict[i]=i.MarketCap
        microcap = [i for i in excluded_delisted if isinstance(MKTCAP_dict.get(i),(int,float)) and MKTCAP_dict.get(i)>25e6 and MKTCAP_dict.get(i)<250e6]
        
        #filter by Price-to-Sales Ratio < 1 (defined to be null if result <= 0)
        micro_PSR = [i for i in microcap if isinstance(i.ValuationRatios.PSRatio,(float,int)) and i.ValuationRatios.PSRatio < 1 and i.ValuationRatios.PSRatio > 0]
        
        #sorting by momentum
        
        for i in micro_PSR:
            hist = self.History(i.Symbol, 180 * self.monthinterval + 1, Resolution.Daily)
            if 'close' not in list(hist.columns):
                self.Debug(f'{i.Symbol.Value} DOES NOT HAVE "close". List of headers: {list(hist.columns)}')
                continue
            close_list = hist['close'].tolist()
            #self.Error(f'{i.Symbol.Value} INDICES: {[col for col in hist.columns]}')
            if len(close_list) == 180 *self.monthinterval + 1:
                #self.Debug(f'LENGTH IS: {len(close_list)}')
                curr_price = close_list[-1]
                price_6M = close_list[0]
                price_2M = close_list[60*self.monthinterval]
                price_1M = close_list[30*self.monthinterval]
                #if i.Symbol.Value == "TOPS":
                #    self.Log(f'CURRENT PRICE: {curr_price},    BASELINE PRICE: {baseline_price}')
                momo_1M = curr_price/price_1M
                momo_2M = curr_price/price_2M/2
                momo_6M = curr_price/price_6M/6
                if momo_1M > momo_2M and momo_2M > momo_6M:
                    security_momo_list.append([i.Symbol,momo_1M])
        
        security_momo_list_sorted = sorted(security_momo_list,key = lambda i : i[1],reverse = True)
        output = [f[0] for f in security_momo_list_sorted[:self.numsecurities]]
        #self.Debug(f'{[f.Value for f in output]}')
        #output = [f[0] for f in security_momo_list]
        self.Symbols = output
        return output
        
    def OnSecuritiesChanged(self, changes):
        self.tobeliquidated = [security.Symbol for security in changes.RemovedSecurities]
        for sym in self.tobeliquidated:
            self.Liquidate(sym)
        
        pct = self.Portfolio.Cash / self.Portfolio.TotalPortfolioValue
        
        for sym in self.Symbols:
            self.SetHoldings(str(sym),1/self.numsecurities * pct)
        return