88import matplotlib .pyplot as plt
99import matplotlib .ticker as mticker
1010import matplotlib .dates as mdates
11- from matplotlib .finance import candlestick
11+ from matplotlib .finance import candlestick
1212import pylab
1313
1414# adjusted font size for the plots
@@ -20,7 +20,7 @@ def rsiFunc(prices, n=14):
2020 deltas = np .diff (prices )
2121 seed = deltas [:n + 1 ]
2222 up = seed [seed >= 0 ].sum () / n
23- down = - seed [seed < 0 ].sum () / n
23+ down = - seed [seed < 0 ].sum () / n
2424 rs = up / down
2525 rsi = np .zeros_like (prices )
2626 rsi [:n ] = 100. - 100. / (1. + rs )
@@ -32,12 +32,12 @@ def rsiFunc(prices, n=14):
3232 else :
3333 upval = 0.
3434 downval = - delta
35- up = (up * (n - 1 ) + upval ) / n
35+ up = (up * (n - 1 ) + upval ) / n
3636 down = (down * (n - 1 ) + downval ) / n
3737 rs = up / down
3838 rsi [i ] = 100. - 100. / (1. + rs )
3939 return rsi
40-
40+
4141# define moving average function
4242def movingaverage (values , window ):
4343 weights = np .repeat (1.0 , window ) / window
@@ -48,11 +48,11 @@ def graphData(stock, MA1=12, MA2=26):
4848 try :
4949 # define the data file
5050 stockFile = "./data/" + stock + ".txt"
51-
51+
5252 # Load data into numpy arrays
53- date , closep , highp , lowp , openp , volume = np .loadtxt (stockFile , delimiter = "," ,
53+ date , closep , highp , lowp , openp , volume = np .loadtxt (stockFile , delimiter = "," ,
5454 unpack = True , converters = {0 : mdates .strpdate2num ("%Y%m%d" )})
55-
55+
5656 #-----------------------------------------------------------------------------------------------
5757 # building data for drawing plotting candlestick chart. Basically, a an array of comma separated
5858 # values. The order of elements is very specific, so check the documentation for candlestick
@@ -64,20 +64,20 @@ def graphData(stock, MA1=12, MA2=26):
6464 appendLine = date [x ], openp [x ], closep [x ], highp [x ], lowp [x ], volume [x ]
6565 candleArray .append (appendLine )
6666 x += 1
67-
67+
6868 # moving averages for 12 and 26 days
6969 Av1 = movingaverage (closep , MA1 )
7070 Av2 = movingaverage (closep , MA2 )
7171 # Starting point for graphs
7272 SP = len (date [MA2 - 1 :])
7373 # Creating Moving Average labels
74- label1 = str (MA1 ) + " SMA"
75- label2 = str (MA2 ) + " SMA"
76-
77-
78- # changing the face color of the graphics
74+ label1 = str (MA1 ) + " SMA"
75+ label2 = str (MA2 ) + " SMA"
76+
77+
78+ # changing the face color of the graphics
7979 fig = plt .figure (facecolor = "#07000D" )
80-
80+
8181 # create room and plot candlestick chart
8282 ax1 = plt .subplot2grid ((5 ,4 ), (1 ,0 ), rowspan = 4 , colspan = 4 , axisbg = "#07000D" )
8383 candlestick (ax1 , candleArray [- SP :], width = 0.75 , colorup = "#9EFF15" , colordown = "#FF1717" )
@@ -109,9 +109,9 @@ def graphData(stock, MA1=12, MA2=26):
109109 pylab .setp (textEd [0 :5 ], color = "white" )
110110 # Tilt the labels to 45 degrees
111111 for label in ax1 .xaxis .get_ticklabels ():
112- label .set_rotation (45 )
113-
114-
112+ label .set_rotation (45 )
113+
114+
115115 # set up RSI area
116116 ax0 = plt .subplot2grid ((5 ,4 ), (0 ,0 ), sharex = ax1 , rowspan = 1 , colspan = 4 , axisbg = "#07000d" )
117117 #plot RSI
@@ -134,10 +134,10 @@ def graphData(stock, MA1=12, MA2=26):
134134 ax0 .set_yticks ([30 ,70 ])
135135 ax0 .yaxis .label .set_color ("white" )
136136 plt .ylabel ("RSI" )
137-
138-
137+
138+
139139 # Plot volume on the same range as ax1
140- volumeMin = 0
140+ volumeMin = 0
141141 ax1v = ax1 .twinx ()
142142 # subtract moving average calculations
143143 ax1v .fill_between (date [- SP :], volumeMin , volume [- SP :], facecolor = "#00FFE8" , alpha = .5 )
@@ -154,23 +154,23 @@ def graphData(stock, MA1=12, MA2=26):
154154 # Change axis color
155155 ax1v .tick_params (axis = "x" , colors = "white" )
156156 ax1v .tick_params (axis = "y" , colors = "white" )
157-
158-
157+
158+
159159 # Setting up the overall appearance of the plot
160160 plt .subplots_adjust (left = .08 , bottom = .14 , right = .95 , top = .95 , wspace = .20 , hspace = 0 )
161161 plt .suptitle (stock , color = "white" )
162162 plt .setp (ax0 .get_xticklabels (), visible = False )
163163 plt .show ()
164164 fig .savefig ("./data/" + stock + ".png" , facecolor = fig .get_facecolor ())
165-
165+
166166 except Exception , e :
167167 print "main loop" , str (e )
168-
168+
169169if __name__ == "__main__" :
170170 # testing the calls
171171 # a list of stocks to process (for testing)
172172 stocksToPull = 'AAPL' , 'GOOG' , 'AMZN' , 'EBAY' , 'CMG' , 'MSFT' , 'C' , 'BA' , 'TSLA'
173-
173+
174174 graphData (stocksToPull [3 ])
175-
175+
176176
0 commit comments