Overall Statistics
Total Trades
5751
Average Win
0.01%
Average Loss
0.00%
Compounding Annual Return
14.041%
Drawdown
21.000%
Expectancy
2.754
Net Profit
52.423%
Sharpe Ratio
0.828
Loss Rate
14%
Win Rate
86%
Profit-Loss Ratio
3.37
Alpha
0.33
Beta
-15.099
Annual Standard Deviation
0.128
Annual Variance
0.016
Information Ratio
0.712
Tracking Error
0.128
Treynor Ratio
-0.007
Total Fees
$6264.95
# https://quantpedia.com/Screener/Details/26
from QuantConnect.Data.UniverseSelection import *
import math
import numpy as np
import pandas as pd
import scipy as sp

# ----------------------------------------------------------------
# To do
# ----------------------------------------------------------------
# x Rebalance Monthly
# x Change weighting of positions to equal weights
# x Plot development of Breakpoints over time
# - Narrow down to single asset (for testing purposes) 
# - Add calculation of target price

# - Overlay covered call strategy linking strikes with breakpoints
# - Overlay LT & ST replication (short put & long call)
# - Extend to multiple tickers
# - Extend to short leg 

# ----------------------------------------------------------------
# Some Notes
# ----------------------------------------------------------------
# self.lowercase variables are variables defined by oneself
# self.Uperrcase variables reference QC API



class BooktoMarketAnomaly(QCAlgorithm):

    def Initialize(self):
        self.Debug('______________________________________________________________________________________________________________________________')
        self.Debug('Initializing Backtest')

        self.SetStartDate(2016, 1, 1)   
        self.SetEndDate(2019, 3, 15)         
        self.SetCash(1000000) 
        
        # Granularity - Daily Resolution
        self.UniverseSettings.Resolution = Resolution.Daily
        self.sorted_by_bm = None
        
        self.current = []
        self.monthly_rebalance = False
        
        # Universe + Settings
        self.UniverseSettings.Resolution = Resolution.Daily
        self.AddUniverse(self.CoarseSelectionFunction, self.FineSelectionFunction)
        
        # Benchmark
        self.SetBenchmark("SPY")
        self.AddEquity("SPY", Resolution.Daily)
        
        # Schedule functions
        # Trigger an event every day a specific symbol is trading --> here monthly
        self.Schedule.On(self.DateRules.MonthStart("SPY"), self.TimeRules.AfterMarketOpen("SPY"), self.rebalance)
        
        # Plotting
        # Chart - Master Container for the Chart:
        breakpPlot = Chart('Fundamentals')
        breakpPlot.AddSeries(Series('Breakpoint-Min', SeriesType.Line))
        breakpPlot.AddSeries(Series('Breakpoint-Max', SeriesType.Line))
        self.AddChart(breakpPlot)

        
    def CoarseSelectionFunction(self, coarse):
        if self.monthly_rebalance:
            # drop stocks which have no fundamental data (or have low price)
            self.filtered_coarse = [x.Symbol for x in coarse if (x.HasFundamentalData)]
            return self.filtered_coarse
        else: 
            self.Log('CoarseSelectionFunction went into else...')
            return []       

    def FineSelectionFunction(self, fine):
        if self.monthly_rebalance:
            # Filter stocks with positive PB Ratio
            fine = [x for x in fine if (x.ValuationRatios.PBRatio > 0)] 
            
            # Calculate the market cap and add the "MarketCap" property to fine universe object
            for i in fine:
                i.MarketCap = float(i.EarningReports.BasicAverageShares.ThreeMonths * (i.EarningReports.BasicEPS.TwelveMonths*i.ValuationRatios.PERatio))
                
            # Syntax : sorted(iterable, key, reverse) --> reverse means from highest (expensive) to lowest (cheap)
            top_market_cap = sorted(fine, key = lambda x: x.MarketCap, reverse=True)[:int(len(fine)*0.2)]
            
            # sorted stocks in the top market-cap list by book-to-market ratio -> cheapest first
            top_bm = sorted(top_market_cap, key = lambda x: 1 / x.ValuationRatios.PBRatio, reverse=True)[:int(len(top_market_cap)*0.2)]
            self.sorted_by_bm = [i.Symbol for i in top_bm] 
            
            top_bm_sorted = sorted(top_bm, key = lambda x: x.ValuationRatios.PBRatio, reverse=True)
            top_bm_tickers = [i.Symbol for i in top_bm_sorted] 
            top_bm_ratios = [i.ValuationRatios.PBRatio for i in top_bm_sorted] 
            self.ticker_PB = np.column_stack((top_bm_tickers, top_bm_ratios))
            
            self.Debug('Top PB Ratio ' + str(top_bm_tickers[0]) + ': ' + str(top_bm_ratios[0]))
            self.Debug('Bottom PB Ratio '  + str(top_bm_tickers[-1]) + ': ' + str(top_bm_ratios[-1]))
            
            # Save cut-off breakpoint for plot
            self.breakpoint_max = max(top_bm_ratios)
            self.breakpoint_min = min(top_bm_ratios)
            
            total_market_cap = np.sum([i.MarketCap for i in top_bm])
            
            # calculate the weight with the market cap
            self.weights = {}
            for i in top_bm:
                self.weights[str(i.Symbol)] = 1/len(self.sorted_by_bm) #i.MarketCap/total_market_cap
            
            self.Log(self.ticker_PB)    
            return self.sorted_by_bm
        else:
            self.Log('FineSelectionFunction went into else...')
            return []
            
    def rebalance(self):
        # form yearly to monthly rebalance
        self.monthly_rebalance = True
        self.Debug('Rebalancing on ' + str(self.Time))
    
    # this event fires whenever we have changes to our universe
    def OnSecuritiesChanged(self, changes):
        self.changes = changes
        self.Debug('Universe Changed on ' + str(self.Time))

    def OnData(self, data):
        if not self.monthly_rebalance: return 
        
        if self.sorted_by_bm:
            stocks_invested = [x.Key for x in self.Portfolio if x.Value.Invested]
            # liquidate stocks not in the trading list 
            for i in stocks_invested:
                if i not in self.sorted_by_bm:
                    self.Liquidate(i)   
                    
            # goes long stocks with the highest book-to-market ratio   
            for i in self.sorted_by_bm:
                # Changed this to simple weight +1 for single asset
                self.SetHoldings(i, self.weights[str(i)])
            
            if self.current == self.sorted_by_bm: 
                self.Debug('True')
            else:
                self.Debug('False')
            self.current = self.sorted_by_bm
            
            self.monthly_rebalance = False
            
            # Later in your OnData(self, data):
            self.Plot('Fundamentals', 'Breakpoint Min', self.breakpoint_min)
            self.Plot('Fundamentals', 'Breakpoint Max', self.breakpoint_max)