-
Notifications
You must be signed in to change notification settings - Fork 0
/
moving_avg_crossover.py
52 lines (42 loc) · 2.18 KB
/
moving_avg_crossover.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
"""
@Author: Ambikeshwar
"""
import matplotlib.dates as mdates
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
from backtesting import BackTesting
class MovingAverageCrossOver(BackTesting):
SHORT_WINDOW = 55
LONG_WINDOW = 233
def __init__(self, ticker):
super(MovingAverageCrossOver, self).__init__(ticker)
def generate_signals(self):
self.signals = pd.DataFrame(index=self.asset_prices.index)
self.signals['signal'] = 0.0
self.signals['short_mavg'] = self.asset_prices['Close'].rolling(window=self.SHORT_WINDOW, min_periods=1,
center=False).mean()
self.signals['long_mavg'] = self.asset_prices['Close'].rolling(window=self.LONG_WINDOW, min_periods=1,
center=False).mean()
self.signals['signal'][self.SHORT_WINDOW:] = np.where(self.signals['short_mavg'][self.SHORT_WINDOW:]
> self.signals['long_mavg'][self.SHORT_WINDOW:], 1.0, 0.0)
self.signals['positions'] = self.signals['signal'].diff()
self.signals.reset_index(inplace=True)
self.signals.set_index(self.asset_prices.index, inplace=True)
self.signals['Date'] = self.signals['Date'].map(mdates.date2num)
def plot_signals(self):
fig = plt.figure()
ax1 = fig.add_subplot(111, ylabel='Price in $')
ax1.plot(self.signals.index.map(mdates.date2num), self.asset_prices['Close'])
self.signals[['short_mavg', 'long_mavg']].plot(ax=ax1, lw=2.)
ax1.plot(self.signals.ix[self.signals.positions == 1.0].index,
self.signals.short_mavg[self.signals.positions == 1], "^", markersize=10, color="m")
ax1.plot(self.signals.ix[self.signals.positions == -1.0].index,
self.signals.short_mavg[self.signals.positions == -1], "v", markersize=10, color="k")
plt.show()
if __name__ == '__main__':
strategy = MovingAverageCrossOver('0001.HK')
strategy.generate_signals()
strategy.plot_signals()
strategy.backtest_portfolio()
strategy.plot_portfolio()