Overall Statistics
Total Trades
8708
Average Win
0.00%
Average Loss
0.00%
Compounding Annual Return
-10.401%
Drawdown
10.000%
Expectancy
-0.279
Net Profit
-2.817%
Sharpe Ratio
-0.543
Loss Rate
68%
Win Rate
32%
Profit-Loss Ratio
1.27
Alpha
0.435
Beta
-30.017
Annual Standard Deviation
0.153
Annual Variance
0.023
Information Ratio
-0.656
Tracking Error
0.153
Treynor Ratio
0.003
Total Fees
$8929.46
# 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(2018, 1, 1)   
        self.SetEndDate(2018, 4, 5)         
        self.SetCash(1000000) 
        
        # Granularity - Daily Resolution
        self.UniverseSettings.Resolution = Resolution.Daily
        self.sorted_by_bm = None
        
        self.current = []
        self.lastMonth = -1
        
        # Universe + Settings
        self.UniverseSettings.Resolution = Resolution.Daily
        self.AddUniverse(self.CoarseSelectionFunction, self.FineSelectionFunction)
        
        # Benchmark
        self.SetBenchmark("SPY")
        self.AddEquity("SPY", Resolution.Daily)
        

        # 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.Time.month == self.lastMonth:
            return []
        self.filtered_coarse = [x.Symbol for x in coarse if (x.HasFundamentalData)]
        
        return self.filtered_coarse
        
    def FineSelectionFunction(self, fine):
        if self.Time.month == self.lastMonth:
            return []
        self.lastMonth = self.Time.month
        # 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
    
    # 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 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

            # Later in your OnData(self, data):
            self.Plot('Fundamentals', 'Breakpoint Min', self.breakpoint_min)
            self.Plot('Fundamentals', 'Breakpoint Max', self.breakpoint_max)