-
Notifications
You must be signed in to change notification settings - Fork 0
/
Trading_strategies_frequency_approach.py
65 lines (51 loc) · 1.96 KB
/
Trading_strategies_frequency_approach.py
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
import numpy as np
import pandas as pd
import yfinance as yf
from pylab import mpl, plt
plt.style.use("seaborn-v0_8-whitegrid")
mpl.rcParams['font.family'] = 'serif'
def load_raw_data(tickers, start_date, end_date):
raw = yf.download(tickers, start_date, end_date)['Adj Close']
raw = raw.reindex(columns=tickers)
raw = pd.DataFrame(raw)
return raw
def highlight_max(s):
is_max = s == s.max()
return ['background_color: yellow' if v else '' for v in is_max]
def data_info(raw, symbol, lags):
data = pd.DataFrame(raw[symbol])
data['returns'] = np.log(data / data.shift(1))
data.dropna(inplace=True)
data['direction'] = np.sign(data['returns']).astype(int)
cols = []
for lag in range(1, lags + 1):
col = 'lag_{}'.format(lag)
data[col] = data['returns'].shift(lag)
cols.append(col)
data.dropna(inplace=True)
cols_bin = []
for col in cols:
col_bin = col + '_bin'
data[col_bin] = np.digitize(data[col], bins=[0])
cols_bin.append(col_bin)
print(data[cols_bin + ['direction']].head())
grouped = data.groupby(cols_bin + ['direction'])
print(grouped.size())
res = grouped['direction'].size().unstack(fill_value=0)
print(res.style.apply(highlight_max, axis=1))
print(res)
data['pos_freq'] = np.where(data[cols_bin].sum(axis=1) == 2, -1, 1)
print((data['direction'] == data['pos_freq']).value_counts())
data['strat_freq'] = data['pos_freq'] * data['returns']
print(data[['returns', 'strat_freq']].sum().apply(np.exp))
data[['returns', 'strat_freq']].cumsum().apply(np.exp).plot(figsize=(10, 6))
plt.show()
return data, cols
if __name__ == '__main__':
tickers = ['SPY', 'NVDA']
start_date = '2018-01-01'
end_date = '2023-06-02'
raw_ = load_raw_data(tickers, start_date, end_date)
symbol = 'SPY'
lags = 2
data_, cols_ = data_info(raw_, symbol, lags)