-
Notifications
You must be signed in to change notification settings - Fork 43
/
strategy.py
90 lines (60 loc) · 2.62 KB
/
strategy.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import pandas as pd
import numpy as np
import math
import compute
# Create column Stance
# Create column Accumulated Close
def SMA_Crossover(df, fast=42, slow=252):
# create the moving average values and
# simultaneously append them to new columns in our existing DataFrame.
df['42d'] = np.round(compute.running_average(df['Adj Close'], windowsize=42), 2)
df['252d'] = np.round(compute.running_average(df['Adj Close'], windowsize=252), 2)
# Generate stance: 0, -1, 1.
df['42-252'] = df['42d'] - df['252d']
offset = 0
#add condition to ensure first stance change is never -1.
df['Stance'] = np.where(df['42-252'] > offset, 1, 0) #42ma being offset amount above 252 value.
df['Stance'] = np.where(df['42-252'] < offset, -1, df['Stance']) #offset amount below 252 value
# print df['Stance'].value_counts()
# Generate accumulated closing price
compute.accumulated_close(df) #instead of everything below:
# df_ma['Market Returns'] = np.log(df_ma['Adj Close'] / df_ma['Adj Close'].shift(1))
# df_ma['Strategy'] = df_ma['Market Returns'] * df_ma['Stance'].shift(1)
def RSI(df, lowerCutoff=30, upperCutoff=70, period=14):
df['RSI'] = pd.Series(compute.RSI(df['Adj Close'], period=period))
df['Stance'] = 0
offset = 0
last_stance = 0
upper_cutoff_set_once = False
for index, row in df.iterrows():
if row['RSI'] > upperCutoff:
upper_cutoff_set_once = True
last_stance = 1
elif upper_cutoff_set_once and row['RSI'] < lowerCutoff:
last_stance = -1
df.set_value(index, 'Stance', last_stance)
compute.accumulated_close(df)
def Bollinger_Band(df):
# 1. Compute rolling mean
rolling_mean = compute.rolling_std_mean(df['Adj Close'], window=20)
# 2. Compute rolling standard deviation
rolling_std = compute.rolling_std(df['Adj Close'], window=20)
# 3. Compute upper and lower bands
upper_band, lower_band = compute.bollinger_bands(rolling_mean, rolling_std)
df['Stance'] = 0
last_stance = 0
upper_cutoff_set_once = False
upper_band.fillna(method='backfill', inplace=True)
lower_band.fillna(method='backfill', inplace=True)
df['Upper Band'] = upper_band
df['Lower Band'] = lower_band
for index, row in df.iterrows():
if row['Adj Close'] > row['Upper Band']:
upper_cutoff_set_once = True
last_stance = 1
elif upper_cutoff_set_once and row['Adj Close'] < row['Lower Band']:
last_stance = -1
df.set_value(index, 'Stance', last_stance)
compute.accumulated_close(df)
def MACD(df):
print 'Pending.'