Overall Statistics Total Trades 12367 Average Win 0.07% Average Loss -0.07% Compounding Annual Return -0.833% Drawdown 41.500% Expectancy -0.028 Net Profit -13.441% Sharpe Ratio 0.005 Probabilistic Sharpe Ratio 0.000% Loss Rate 53% Win Rate 47% Profit-Loss Ratio 1.05 Alpha -0.003 Beta 0.047 Annual Standard Deviation 0.122 Annual Variance 0.015 Information Ratio -0.419 Tracking Error 0.203 Treynor Ratio 0.012 Total Fees \$883.24
import numpy as np
from scipy.optimize import minimize

def MonthDiff(d1, d2):
return (d1.year - d2.year) * 12 + d1.month - d2.month

def Return(values):
return (values[-1] - values) / values

def Volatility(values):
values = np.array(values)
returns = (values[1:] - values[:-1]) / values[:-1]
return np.std(returns)

# Custom fee model
class CustomFeeModel(FeeModel):
def GetOrderFee(self, parameters):
fee = parameters.Security.Price * parameters.Order.AbsoluteQuantity * 0.00005
return OrderFee(CashAmount(fee, "USD"))

# Quandl free data
class QuandlFutures(PythonQuandl):
def __init__(self):
self.ValueColumnName = "settle"

# Quandl short interest data.
class QuandlFINRA_ShortVolume(PythonQuandl):
def __init__(self):
self.ValueColumnName = 'SHORTVOLUME'    # also 'TOTALVOLUME' is accesible

# Quantpedia data
# NOTE: IMPORTANT: Data order must be ascending (datewise)
class QuantpediaFutures(PythonData):
def GetSource(self, config, date, isLiveMode):
return SubscriptionDataSource("data.quantpedia.com/backtesting_data/futures/{0}.csv".format(config.Symbol.Value), SubscriptionTransportMedium.RemoteFile, FileFormat.Csv)

def Reader(self, config, line, date, isLiveMode):
data = QuantpediaFutures()
data.Symbol = config.Symbol

if not line.isdigit(): return None
split = line.split(';')

data.Time = datetime.strptime(split, "%d.%m.%Y") + timedelta(days=1)
data['settle'] = float(split)
data.Value = float(split)

return data

# NOTE: Manager for new trades. It's represented by certain count of equally weighted brackets for long and short positions.
# If there's a place for new trade, it will be managed for time of holding period.
def __init__(self, algorithm, long_size, short_size, holding_period):
self.algorithm = algorithm  # algorithm to execute orders in.

self.long_size = long_size
self.short_size = short_size
self.weight = 1 / (self.long_size + self.short_size)

self.long_len = 0
self.short_len = 0

# Arrays of ManagedSymbols
self.symbols = []

self.holding_period = holding_period    # Days of holding.

managed_symbol = ManagedSymbol(symbol, self.holding_period, long_flag)

if long_flag:
# If there's a place for it.
if self.long_len < self.long_size:
self.symbols.append(managed_symbol)
self.algorithm.SetHoldings(symbol, self.weight)
self.long_len += 1
else:
# If there's a place for it.
if self.long_len < self.short_size:
self.symbols.append(managed_symbol)
self.algorithm.SetHoldings(symbol, - self.weight)
self.short_len += 1

# Decrement holding period and liquidate symbols.
def TryLiquidate(self):
symbols_to_delete = []
for managed_symbol in self.symbols:
managed_symbol.days_to_liquidate -= 1

# Liquidate.
if managed_symbol.days_to_liquidate == 0:
symbols_to_delete.append(managed_symbol)
self.algorithm.Liquidate(managed_symbol.symbol)
if managed_symbol.long_flag: self.long_len -= 1
else: self.short_len -= 1

# Remove symbols from management.
for managed_symbol in symbols_to_delete:
self.symbols.remove(managed_symbol)

class ManagedSymbol():
def __init__(self, symbol, days_to_liquidate, long_flag):
self.symbol = symbol
self.days_to_liquidate = days_to_liquidate
self.long_flag = long_flag

class PortfolioOptimization(object):
def __init__(self, df_return, risk_free_rate, num_assets):
self.daily_return = df_return
self.risk_free_rate = risk_free_rate
self.n = num_assets # numbers of risk assets in portfolio
self.target_vol = 0.05

def annual_port_return(self, weights):
# calculate the annual return of portfolio
return np.sum(self.daily_return.mean() * weights) * 252

def annual_port_vol(self, weights):
# calculate the annual volatility of portfolio
return np.sqrt(np.dot(weights.T, np.dot(self.daily_return.cov() * 252, weights)))

def min_func(self, weights):
# method 1: maximize sharp ratio
return - self.annual_port_return(weights) / self.annual_port_vol(weights)

# method 2: maximize the return with target volatility
#return - self.annual_port_return(weights) / self.target_vol

def opt_portfolio(self):
# maximize the sharpe ratio to find the optimal weights
cons = ({'type': 'eq', 'fun': lambda x: np.sum(x) - 1})
bnds = tuple((0, 1) for x in range(2)) + tuple((0, 0.25) for x in range(self.n - 2))
opt = minimize(self.min_func,                               # object function
np.array(self.n * [1. / self.n]),            # initial value
method='SLSQP',                              # optimization method
bounds=bnds,                                 # bounds for variables
constraints=cons)                            # constraint conditions

opt_weights = opt['x']

return opt_weights
# https://quantpedia.com/strategies/earnings-announcement-premium/
#
# The investment universe consists of all stocks from the CRSP database. At the beginning of every calendar month, stocks are ranked in ascending
# order on the basis of the volume concentration ratio, which is defined as the volume of the previous 16 announcement months divided by the total
# volume in the previous 48 months. The ranked stocks are assigned to one of 5 quintile portfolios. Within each quintile, stocks are assigned to
# one of two portfolios (expected announcers and expected non-announcers) using the predicted announcement based on the previous year. All stocks
# are value-weighted within a given portfolio, and portfolios are rebalanced every calendar month to maintain value weights. The investor invests
# in a long-short portfolio, which is a zero-cost portfolio that holds the portfolio of high volume expected announcers and sells short the
# portfolio of high volume expected non-announcers.

import fk_tools
from collections import deque

def Initialize(self):
self.SetStartDate(2003, 1, 1)
self.SetCash(100000)

self.symbol = 'SPY'

self.period = 21
self.month_period = 48

# Volume daily data.
self.data = {}

# Volume monthly data.
self.monthly_volume = {}

self.course_count = 1000
self.weight = {}

self.selection_flag = False
self.rebalance_flag = False
self.UniverseSettings.Resolution = Resolution.Daily

self.Schedule.On(self.DateRules.MonthStart(self.symbol), self.TimeRules.AfterMarketOpen(self.symbol), self.Selection)

def OnSecuritiesChanged(self, changes):
security.SetFeeModel(fk_tools.CustomFeeModel(self))

def CoarseSelectionFunction(self, coarse):
if not self.selection_flag:
return Universe.Unchanged

self.selection_flag = False

selected = sorted([x for x in coarse if x.HasFundamentalData and x.Market == 'usa' and x.Price > 5],
key=lambda x: x.DollarVolume, reverse=True)

return [x.Symbol for x in selected[:self.course_count]]

def FineSelectionFunction(self, fine):
fine = [x for x in fine if x.EarningReports.BasicAverageShares.ThreeMonths > 0 and x.EarningReports.BasicEPS.TwelveMonths > 0 and x.ValuationRatios.PERatio > 0]

# Ratio/market cap pair.
volume_concentration_ratio_market_cap = {}
for stock in fine:
symbol = stock.Symbol

# Store daily price and volume data.
if symbol not in self.data:
self.data[symbol] = deque(maxlen = self.period)

# Month worth of daily data is ready.
if len(self.data[symbol]) == self.data[symbol].maxlen:
# Store last month volume.
if symbol not in self.monthly_volume:
self.monthly_volume[symbol] = deque(maxlen = self.month_period)

monthly_vol = sum([x for x in self.data[symbol]])
last_month_date = self.Time - timedelta(days=self.Time.day)
last_file_date = stock.FinancialStatements.FileDate
was_announcement_month = (last_file_date.year == last_month_date.year and last_file_date.month == last_month_date.month)    # Last month was announcement date.
self.monthly_volume[symbol].append(VolumeData(last_month_date, monthly_vol, was_announcement_month))

# 48 months of volume data is ready.
if len(self.monthly_volume[symbol]) == self.monthly_volume[symbol].maxlen:
# Volume concentration ratio calc.
announcement_months_volume = sum([x.Volume for x in self.monthly_volume[symbol] if x.Was_announcement_month][-16:])
total_volume = sum([x.Volume for x in self.monthly_volume[symbol]])

if announcement_months_volume != 0 and total_volume != 0:
# Market cap calc.
market_cap =  float(stock.EarningReports.BasicAverageShares.ThreeMonths * (stock.EarningReports.BasicEPS.TwelveMonths*stock.ValuationRatios.PERatio))

# Store ratio, market cap pair.
volume_concentration_ratio = announcement_months_volume / total_volume
volume_concentration_ratio_market_cap[symbol] = [volume_concentration_ratio, market_cap]

fine_symbols = [x.Symbol for x in fine]

# Remove old data, so we only store consecutive data.
symbols_to_remove = []
for symbol in self.monthly_volume:
if symbol not in fine_symbols:
symbols_to_remove.append(symbol)
for symbol in symbols_to_remove:
del self.monthly_volume[symbol]

# NOTE: Do we want ot remove symbol from daily data also? This way we save memory dramatically. - storing only self.course_count of symbols and its history compared to expanding dict of symbols with its history.
# Therefore we only store actually selected symbols and delete those, which are not in self.course_count-long selection.
# Otherwise, we would be storing whole lot of data which will not be used anymore. But those may appear in self.course_count-long selection later.
# It appears to be minor difference in final equity and only 70 trades more has been opened with every peace of daily data stored.
#
# Storing every data throughout the backtest is more precise tho and it depends on use case and backtested strategy I guess.
# del self.data[symbol]

if len(volume_concentration_ratio_market_cap) == 0: return fine_symbols

# Volume sorting.
sorted_by_volume = sorted(volume_concentration_ratio_market_cap.items(), key = lambda x: x, reverse = True)
quintile = int(len(sorted_by_volume) / 5)
high_volume = [x for x in sorted_by_volume[:quintile]]

# Filering announcers and non-announcers.
month_to_lookup = self.Time.month
year_to_lookup = self.Time.year - 1

long = []
short = []
for data in high_volume:
symbol = data
announcement_dates = [[x.Date.year, x.Date.month] for x in self.monthly_volume[symbol] if x.Was_announcement_month]
if [year_to_lookup, month_to_lookup] in announcement_dates:
long.append(data)
else:
short.append(data)

# Market cap weighting.
total_market_cap = sum([x for x in long + short])
long_symbols = [x for x in long]
for symbol, volume_concentration_ratio_market_cap in long + short:
if symbol in long_symbols:
self.weight[symbol] = volume_concentration_ratio_market_cap / total_market_cap
else:
self.weight[symbol] = - volume_concentration_ratio_market_cap / total_market_cap

self.rebalance_flag = True

return fine_symbols

def OnData(self, data):
# Store daily volume data.

# for symbol in self.data:
for symbol in self.Securities.Keys:
if symbol.Value == 'SPY': continue

if self.Securities.ContainsKey(symbol):
volume = self.Securities[symbol].Volume
if volume != 0:
self.data[symbol].append(volume)
else:
# Append latest price as a next one in case there's 0 as price.
if len(self.data[symbol]) > 0:
last_data = self.data[symbol][-1]
self.data[symbol].append(last_data)

# Rabalance.
if not self.rebalance_flag:
return
self.rebalance_flag = False

count = len(self.weight)
if count == 0: return

stocks_invested = [x.Key for x in self.Portfolio if x.Value.Invested]
symbols_to_rebalance = [x for x in self.weight.items()]
for symbol in stocks_invested:
if symbol not in symbols_to_rebalance:
self.Liquidate(symbol)

# self.Liquidate()

for symbol, w in self.weight.items():
if self.Securities[symbol].Price != 0:  # Prevent error message.
self.SetHoldings(symbol, 0.9 * w)

self.weight.clear()

def Selection(self):
self.selection_flag = True

# Monthly volume data.
class VolumeData():
def __init__(self, date, monthly_volume, was_announcement_month):
self.Date = date
self.Volume = monthly_volume
self.Was_announcement_month = was_announcement_month