timeseries-julia-python
random julia and python scripts analyzing stock timeseries data
ta-lib-example.py
(3448B)
1 from pandas_datareader import data
2 import pandas as pd
3 import numpy as np
4 import talib as ta
5 import matplotlib.pyplot as plt
6 import matplotlib.dates as mdates
7 import matplotlib.gridspec as gridspec
8 from matplotlib.dates import date2num
9 from mplfinance.original_flavor import candlestick_ohlc as candlestick
10 import datetime
11
12 ticker = 'OPK'
13
14 # Download sample data
15 sec_id = data.get_data_yahoo(ticker, '2014-06-01')
16
17 # Data for matplotlib finance plot
18 sec_id_ochl = np.array(pd.DataFrame({'0':date2num(sec_id.index.to_pydatetime()),
19 '1':sec_id.Open,
20 '2':sec_id.Close,
21 '3':sec_id.High,
22 '4':sec_id.Low}))
23
24 # Technical Analysis
25 SMA_FAST = 50
26 SMA_SLOW = 200
27 RSI_PERIOD = 14
28 RSI_AVG_PERIOD = 15
29 MACD_FAST = 12
30 MACD_SLOW = 26
31 MACD_SIGNAL = 9
32 STOCH_K = 14
33 STOCH_D = 3
34 SIGNAL_TOL = 3
35 Y_AXIS_SIZE = 12
36
37 analysis = pd.DataFrame(index = sec_id.index)
38
39 analysis['sma_f'] = pd.rolling_mean(sec_id.Close, SMA_FAST)
40 analysis['sma_s'] = pd.rolling_mean(sec_id.Close, SMA_SLOW)
41 analysis['rsi'] = ta.RSI(sec_id.Close.as_matrix(), RSI_PERIOD)
42 analysis['sma_r'] = pd.rolling_mean(analysis.rsi, RSI_AVG_PERIOD) # check shift
43 analysis['macd'], analysis['macdSignal'], analysis['macdHist'] = ta.MACD(sec_id.Close.as_matrix(), fastperiod=MACD_FAST, slowperiod=MACD_SLOW, signalperiod=MACD_SIGNAL)
44 analysis['stoch_k'], analysis['stoch_d'] = ta.STOCH(sec_id.High.as_matrix(), sec_id.Low.as_matrix(), sec_id.Close.as_matrix(), slowk_period=STOCH_K, slowd_period=STOCH_D)
45
46 analysis['sma'] = np.where(analysis.sma_f > analysis.sma_s, 1, 0)
47 analysis['macd_test'] = np.where((analysis.macd > analysis.macdSignal), 1, 0)
48 analysis['stoch_k_test'] = np.where((analysis.stoch_k < 50) & (analysis.stoch_k > analysis.stoch_k.shift(1)), 1, 0)
49 analysis['rsi_test'] = np.where((analysis.rsi < 50) & (analysis.rsi > analysis.rsi.shift(1)), 1, 0)
50
51 # Prepare plot
52 fig, (ax1, ax2, ax3, ax4) = plt.subplots(4, 1, sharex=True)
53 ax1.set_ylabel(ticker, size=20)
54
55 #size plot
56 fig.set_size_inches(15,30)
57
58 # Plot candles
59 candlestick(ax1, sec_id_ochl, width=0.5, colorup='g', colordown='r', alpha=1)
60
61 # Draw Moving Averages
62 analysis.sma_f.plot(ax=ax1, c='r')
63 analysis.sma_s.plot(ax=ax1, c='g')
64
65 #RSI
66 ax2.set_ylabel('RSI', size=Y_AXIS_SIZE)
67 analysis.rsi.plot(ax = ax2, c='g', label = 'Period: ' + str(RSI_PERIOD))
68 analysis.sma_r.plot(ax = ax2, c='r', label = 'MA: ' + str(RSI_AVG_PERIOD))
69 ax2.axhline(y=30, c='b')
70 ax2.axhline(y=50, c='black')
71 ax2.axhline(y=70, c='b')
72 ax2.set_ylim([0,100])
73 handles, labels = ax2.get_legend_handles_labels()
74 ax2.legend(handles, labels)
75
76 # Draw MACD computed with Talib
77 ax3.set_ylabel('MACD: '+ str(MACD_FAST) + ', ' + str(MACD_SLOW) + ', ' + str(MACD_SIGNAL), size=Y_AXIS_SIZE)
78 analysis.macd.plot(ax=ax3, color='b', label='Macd')
79 analysis.macdSignal.plot(ax=ax3, color='g', label='Signal')
80 analysis.macdHist.plot(ax=ax3, color='r', label='Hist')
81 ax3.axhline(0, lw=2, color='0')
82 handles, labels = ax3.get_legend_handles_labels()
83 ax3.legend(handles, labels)
84
85 # Stochastic plot
86 ax4.set_ylabel('Stoch (k,d)', size=Y_AXIS_SIZE)
87 analysis.stoch_k.plot(ax=ax4, label='stoch_k:'+ str(STOCH_K), color='r')
88 analysis.stoch_d.plot(ax=ax4, label='stoch_d:'+ str(STOCH_D), color='g')
89 handles, labels = ax4.get_legend_handles_labels()
90 ax4.legend(handles, labels)
91 ax4.axhline(y=20, c='b')
92 ax4.axhline(y=50, c='black')
93 ax4.axhline(y=80, c='b')
94
95 plt.show()