-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTCF Strategy.py
More file actions
79 lines (61 loc) · 3.79 KB
/
TCF Strategy.py
File metadata and controls
79 lines (61 loc) · 3.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import warnings
warnings.filterwarnings("ignore", message="The 'unit' keyword in TimedeltaIndex construction is deprecated.*")
warnings.filterwarnings("ignore", message="Setting an item of incompatible dtype is deprecated")
warnings.filterwarnings("ignore", message="The default fill_method='pad' in DataFrame.pct_change is deprecated")
import math
import yfinance as yf
import numpy as np
import pandas as pd
pd.options.mode.chained_assignment = None
from datetime import timedelta
from scipy.stats import norm
# This function scrapes historical data to find the weekly return mean and volatility
def dataScraper(tickers):
endDate = pd.Timestamp.now()
startDate = endDate - timedelta(weeks = 52 * 5)
historicalData = yf.download(tickers, start=startDate, end=endDate, interval="1wk")['Close']
return historicalData
def portfolioConstructor(metricsData, availableCapital):
# Convert to Log Returns
stockMetrics = metricsData
stockMetrics = np.log(stockMetrics.pct_change() + 1)
stockMetrics = stockMetrics.tail(len(stockMetrics)-1)
# Calculate Mean & Volatility
stockMetrics.loc['Mean'] = stockMetrics.mean()
stockMetrics.loc['Volatility'] = stockMetrics.std()
# Reformat
stockMetrics = stockMetrics.tail(3)
stockMetrics.index = ['Recent Movement', 'Mean', 'Volatility']
# Z-Score
stockMetrics.loc['Z-Score'] = (stockMetrics.loc['Recent Movement'] - 2*stockMetrics.loc['Mean']) / stockMetrics.loc['Volatility']
# Select Z-Scores that meet the threshold
threshold = norm.ppf(0.90)
stockMetrics.loc['Threshold Z-Score'] = (abs(stockMetrics.loc['Z-Score']) > threshold) * stockMetrics.loc['Z-Score']
# Asymmetries Adjustment
weeklyLoanFee = (1 + 0.15) ** (1/52) - 1
stockMetrics.loc['Z-Score'] = stockMetrics.loc['Z-Score'].apply(lambda x: (1 - weeklyLoanFee) * x if x > 0 else x)
# Weight Calculation
sumZ = sum(abs(stockMetrics.loc['Threshold Z-Score']))
sumNZ = stockMetrics.loc['Threshold Z-Score'][stockMetrics.loc['Threshold Z-Score'] > 0].sum()
if math.isnan(sumZ):
sumZ = 1
# Weightings
stockMetrics.loc['Portfolio Weight'] = -stockMetrics.loc['Threshold Z-Score'] / (sumZ - sumNZ/2)
stockMetrics['Cash'] = 0
stockMetrics.loc['Portfolio Weight', 'Cash (Margin)'] = (1 - sum(stockMetrics.loc['Portfolio Weight']))
# Positions
stockMetrics.loc['Opening Position'] = availableCapital * stockMetrics.loc['Portfolio Weight']
return stockMetrics
############## Call Function
# S&P 100 Tickers
tickers = ["MSFT", "AAPL", "NVDA", "AMZN", "META", "GOOG", "BRK-B", "LLY", "AVGO", "JPM", "TSLA", "UNH", "V", "XOM", "MA", "JNJ", "PG", "HD", "MRK", "COST", "ABBV", "ADBE", "AMD", "CRM", "CVX", "NFLX", "WMT", "PEP", "KO", "ACN", "BAC", "TMO", "MCD", "CSCO", "LIN", "ABT", "ORCL", "CMCSA", "INTC", "INTU", "DIS", "WFC", "VZ", "AMGN", "IBM", "CAT", "DHR", "QCOM", "NOW", "UNP", "PFE", "GE", "SPGI", "TXN", "AMAT", "PM", "ISRG", "RTX", "COP", "HON", "T", "BKNG", "LOW", "GS", "NKE", "AXP", "BA", "PLD", "SYK", "MDT", "ELV", "NEE", "LRCX", "TJX", "VRTX", "BLK", "MS", "ETN", "PANW", "PGR", "SBUX", "C", "DE", "MDLZ", "ADP", "CB", "UPS", "REGN", "BMY", "ADI", "GILD", "MU", "MMC", "BSX", "CI", "LMT", "CVS", "SCHW"]
# Backtest Details
availableCapital = 5000000
# Grab Historical Data
metricsData = dataScraper(tickers)
# Compute the Portfolio
stockMetrics = portfolioConstructor(metricsData, availableCapital)
positions = stockMetrics.tail(2).loc[:, (stockMetrics.tail(2) != 0).any()]
positions.loc['Opening Position'] = positions.loc['Opening Position'].apply(lambda x: '${:,.2f}'.format(x))
positions.loc['Portfolio Weight'] = positions.loc['Portfolio Weight'].apply(lambda x: '{:,.2f}%'.format(x * 100))
print(positions)