Show both MACD and indicator strat on figure
Prepare for strategy learner.
This commit is contained in:
@@ -36,18 +36,21 @@ class ManualStrategy:
|
||||
print(volume)
|
||||
|
||||
def macd_strat(self, macd, orders):
|
||||
"""Strategy based on MACD cross."""
|
||||
|
||||
def strat(ser):
|
||||
m = macd.loc[ser.index]
|
||||
prev_macd, prev_signal = m.iloc[0]
|
||||
cur_macd, cur_signal = m.iloc[1]
|
||||
prev_macd, prev_signal, _ = m.iloc[0]
|
||||
cur_macd, cur_signal, _ = m.iloc[1]
|
||||
shares = 0
|
||||
if cur_macd < -1 and prev_macd < prev_signal and cur_macd > cur_signal:
|
||||
if cur_macd < -1 and prev_macd < prev_signal \
|
||||
and cur_macd > cur_signal:
|
||||
if self.holding == 0:
|
||||
shares = 1000
|
||||
elif self.holding == -1000:
|
||||
shares = 2000
|
||||
elif cur_macd > 1 and prev_macd > prev_signal and cur_macd < cur_signal:
|
||||
elif cur_macd > 1 and prev_macd > prev_signal \
|
||||
and cur_macd < cur_signal:
|
||||
if self.holding == 0:
|
||||
shares = -1000
|
||||
elif self.holding == 1000:
|
||||
@@ -58,6 +61,8 @@ class ManualStrategy:
|
||||
orders['Shares'] = orders['Shares'].rolling(2).apply(strat)
|
||||
|
||||
def three_indicator_strat(self, macd, rsi, price_sma, orders):
|
||||
"""Strategy based on three indicators. Thresholds selected based on
|
||||
scatter plots."""
|
||||
def strat(row):
|
||||
shares = 0
|
||||
_, _, macd_diff = macd.loc[row.name]
|
||||
@@ -87,7 +92,7 @@ class ManualStrategy:
|
||||
def testPolicy(self, symbol="IBM",
|
||||
sd=dt.datetime(2009, 1, 1),
|
||||
ed=dt.datetime(2010, 1, 1),
|
||||
sv=10000):
|
||||
sv=10000, macd_strat=False):
|
||||
|
||||
self.holding = 0
|
||||
df = util.get_data([symbol], pd.date_range(sd, ed))
|
||||
@@ -102,7 +107,8 @@ class ManualStrategy:
|
||||
rsi = indicators.rsi(df, symbol)
|
||||
price_sma = indicators.price_sma(df, symbol, [8])
|
||||
|
||||
# self.macd_strat(macd, orders)
|
||||
self.three_indicator_strat(macd, rsi, price_sma, orders)
|
||||
if macd_strat:
|
||||
self.macd_strat(macd, orders)
|
||||
else:
|
||||
self.three_indicator_strat(macd, rsi, price_sma, orders)
|
||||
return orders
|
||||
|
||||
|
||||
Reference in New Issue
Block a user