@@ -148,193 +148,50 @@ def test_fill_between05(bolldata):
148148
149149
150150
151- ## print('''
152- ##
153- ## Use a dict to specify two y values, or two series, (y1 and y2) for `fill_between`:
154- ##
155- ## ''')
156- ##
157- ## mpf.plot(daily,figscale=0.7,fill_between=dict(y1=3090,y2=3120))
158- ## mpf.plot(daily,figscale=0.7,fill_between=dict(y1=3100,y2=daily['Close'].values))
159- ## mpf.plot(daily,figscale=0.7,fill_between=dict(y1=daily['Low'].values,y2=daily['High'].values))
160- ##
161- ## print('''
162- ##
163- ## Use a dict to specify other attributes (kwargs) for `fill_between`:
164- ##
165- ## To demonstrate use of the `where` kwarg to display a holding period,
166- ## we convert the datetime index into a dataframe, and use that to generate a boolean array:
167- ##
168- ## `where_values = pd.notnull(dates_df[ (dates_df>=buy_date) & (dates_df <= sell_date) ])['Date'].values`
169- ##
170- ## ''')
171- ##
172- ## dates_df = pd.DataFrame(daily.index)
173- ## buy_date = pd.Timestamp('2019-11-06')
174- ## sell_date = pd.Timestamp('2019-11-19')
175- ##
176- ## where_values = pd.notnull(dates_df[ (dates_df>=buy_date) & (dates_df <= sell_date) ])['Date'].values
177- ##
178- ## y1values = daily['Close'].values
179- ## y2value = daily['Low'].min()
180- ##
181- ## mpf.plot(daily,figscale=0.7,
182- ## fill_between=dict(y1=y1values,y2=y2value,where=where_values,alpha=0.5,color='g')
183- ## )
184- ##
185- ## print('''
186- ##
187- ## Use `panel=` in the `fill_between` dict to place the fill_between on a panel other than panel 0:
188- ##
189- ## In this example, we `fill_between` on the volume panel,
190- ## filling between the volume and the average volume.
191- ##
192- ## ''')
193- ##
194- ## mpf.plot(daily,volume=True,panel_ratios=(1.1,1),
195- ## type='candle',tight_layout=True,figratio=(1,1),
196- ## fill_between=dict(y1=daily['Volume'].values,
197- ## y2=daily['Volume'].mean(),
198- ## panel=1,alpha=0.5,color='lime'))
199- ##
200- ## print('''
201- ##
202- ## There are two ways to do multiple `fill_betweens`:
203- ## (1) Specify a list of `fill_between` dicts.
204- ## (2) Specify a fill_between (or list of fill_betweens) for each `mpf.make_addplot()`
205- ##
206- ## Here, for example, we specify a `fill_between=` a list of fill between dicts:
207- ##
208- ## ''')
209- ##
210- ## fb1 = dict(y1=daily['Open'].values , y2=daily['Close'].values , panel=0, alpha=0.3, color='magenta')
211- ## fb2 = dict(y1=daily['Volume'].values, y2=daily['Volume'].mean(), panel=1, alpha=0.5, color='lime')
212- ##
213- ## mpf.plot(daily,volume=True,panel_ratios=(1.1,1),
214- ## type='candle',tight_layout=True,figratio=(1,1),
215- ## fill_between=[fb1,fb2])
216- ##
217- ## print('''
218- ##
219- ## We can accomplish the same thing by specifying one fill_between in `mpf.plot()`
220- ## and the other in `mpf.make_addplot()`. This is useful if we are already using
221- ## `make_addplot()` to plot some additional data.
222- ##
223- ## NOTE: Since make_addplot() accepts a panel argument, one should NOT specify
224- ## panel in the fill_between dict used by make_addplot.
225- ##
226- ## ''')
227- ##
228- ## fb1 = dict(y1=daily['Open'].values , y2=daily['Close'].values , alpha=0.4, color='magenta')
229- ## fb2 = dict(y1=daily['Volume'].values, y2=daily['Volume'].mean(), alpha=0.5, color='lime')
230- ##
231- ## avol = [daily['Volume'].mean()]*len(daily)
232- ##
233- ## ap = mpf.make_addplot(avol,panel=1,fill_between=fb2,color='k',linestyle='-.',width=0.25)
234- ##
235- ## mpf.plot(daily,volume=True,panel_ratios=(1.1,1),
236- ## type='candle',tight_layout=True,figratio=(1,1),
237- ## fill_between=fb1,addplot=ap)
238- ##
239- ## print('''
240- ##
241- ## We can specify effectively a "multi-color" fill_between, by breaking it into
242- ## two separate fill_betweens, with two separate colors, each with a "where" clause to
243- ## indicate where the fill_between color should and should not appear along the datetime axis.
244- ##
245- ## This is useful, for example, if we want to highlight where a given value is
246- ## above or below the average value.
247- ##
248- ## Notice that when using multiple where clauses like this, it is helpful
249- ## to set `interpolate=True` in the `fill_between` dict, so that the space
250- ## between True values and False values also gets filled.
251- ##
252- ## ''')
253- ##
254- ## fb_above = dict(y1=daily['Volume'].values,
255- ## y2=daily['Volume'].mean(),
256- ## alpha=0.4, color='lime',
257- ## interpolate=True,
258- ## where=(daily['Volume'] > daily['Volume'].mean()).values)
259- ##
260- ## fb_below = fb_above.copy()
261- ## fb_below['color'] = 'magenta'
262- ## fb_below['where'] = (daily['Volume'] < daily['Volume'].mean()).values
263- ##
264- ## avol = [daily['Volume'].mean()]*len(daily)
265- ## ap = mpf.make_addplot(avol,panel=1,fill_between=[fb_above,fb_below],color='k',linestyle='-.',width=0.25)
266- ##
267- ## mpf.plot(daily,volume=True,panel_ratios=(0.8,1),
268- ## type='candle',tight_layout=True,figratio=(1,1),addplot=ap)
269- ##
270- ## print('''
271- ##
272- ## Here, as an additional example, we create "multi-color" fill_between for both panels:
273- ##
274- ## ''')
275- ##
276- ## fbvolume_above = dict(y1=daily['Volume'].values,
277- ## y2=daily['Volume'].mean(),
278- ## alpha=0.4, color='lime',
279- ## interpolate=True,
280- ## where=(daily['Volume'] > daily['Volume'].mean()).values)
281- ##
282- ## fbvolume_below = fbvolume_above.copy()
283- ## fbvolume_below['color'] = 'magenta'
284- ## fbvolume_below['where'] = (daily['Volume'] < daily['Volume'].mean()).values
285- ##
286- ## avol = [daily['Volume'].mean()]*len(daily)
287- ## ap = mpf.make_addplot(avol,panel=1,fill_between=[fbvolume_above,fbvolume_below],color='k',linestyle='-.',width=0.25)
288- ##
289- ## fbclose_above = dict(y1=daily['Open'].values , y2=daily['Close'].values , alpha=0.4,
290- ## interpolate=True,
291- ## color='lime',
292- ## where=(daily['Close']>daily['Open']).values
293- ## )
294- ##
295- ## fbclose_below = fbclose_above.copy()
296- ## fbclose_below['color'] = 'magenta'
297- ## fbclose_below['where'] = (daily['Close']<daily['Open']).values
298- ##
299- ## mpf.plot(daily,volume=True,panel_ratios=(1,1),
300- ## type='candle',tight_layout=True,figratio=(1,1),
301- ## fill_between=[fbclose_above,fbclose_below],
302- ## addplot=ap)
303- ##
304- ## print('''
305- ##
306- ## Finally, as a more pratical example, we use `fill_between` to color a MACD plot:
307- ##
308- ## ''')
309- ##
310- ## df = pd.read_csv('data/SPY_20110701_20120630_Bollinger.csv',index_col=0,parse_dates=True).loc['2011-07-01':'2011-12-30',:]
311- ##
312- ## # =======
313- ## # MACD:
314- ##
315- ## exp12 = df['Close'].ewm(span=12, adjust=False).mean()
316- ## exp26 = df['Close'].ewm(span=26, adjust=False).mean()
317- ## macd = exp12 - exp26
318- ## signal = macd.ewm(span=9, adjust=False).mean()
319- ## histogram = macd - signal
320- ##
321- ## fb_green = dict(y1=macd.values,y2=signal.values,where=signal<macd,color="#93c47d",alpha=0.6,interpolate=True)
322- ## fb_red = dict(y1=macd.values,y2=signal.values,where=signal>macd,color="#e06666",alpha=0.6,interpolate=True)
323- ## fb_green['panel'] = 1
324- ## fb_red['panel'] = 1
325- ## fb = [fb_green,fb_red]
326- ##
327- ## apds = [mpf.make_addplot(exp12,color='lime'),
328- ## mpf.make_addplot(exp26,color='c'),
329- ## mpf.make_addplot(histogram,type='bar',width=0.7,panel=1,
330- ## color='dimgray',alpha=1,secondary_y=True),
331- ## mpf.make_addplot(macd,panel=1,color='fuchsia',secondary_y=False),
332- ## mpf.make_addplot(signal,panel=1,color='b',secondary_y=False)#,fill_between=fb),
333- ## ]
334- ##
335- ## s = mpf.make_mpf_style(base_mpf_style='classic',rc={'figure.facecolor':'lightgray'})
336- ##
337- ## mpf.plot(df,type='candle',addplot=apds,figscale=1.6,figratio=(6,5),title='\n\nMACD',
338- ## style=s,volume=True,volume_panel=2,panel_ratios=(3,4,1),fill_between=fb)
339- ##
340- ## def test_fill_between02(bolldata):
151+ def test_fill_between06 (bolldata ):
152+
153+ #df = _get_data_subset(bolldata)
154+ df = bolldata
155+ fname ,tname ,rname = _get_file_names ('06' )
156+
157+ # =======
158+ # MACD:
159+
160+ exp12 = df ['Close' ].ewm (span = 12 , adjust = False ).mean ()
161+ exp26 = df ['Close' ].ewm (span = 26 , adjust = False ).mean ()
162+ macd = exp12 - exp26
163+ signal = macd .ewm (span = 9 , adjust = False ).mean ()
164+ histogram = macd - signal
165+
166+ fb_12up = dict (y1 = exp12 .values ,y2 = exp26 .values ,where = exp12 > exp26 ,color = "#93c47d" ,alpha = 0.6 ,interpolate = True )
167+ fb_12dn = dict (y1 = exp12 .values ,y2 = exp26 .values ,where = exp12 < exp26 ,color = "#e06666" ,alpha = 0.6 ,interpolate = True )
168+ fb_exp12 = [fb_12up ,fb_12dn ]
169+
170+ fb_macd_up = dict (y1 = macd .values ,y2 = signal .values ,where = signal < macd ,color = "#93c47d" ,alpha = 0.6 ,interpolate = True )
171+ fb_macd_dn = dict (y1 = macd .values ,y2 = signal .values ,where = signal > macd ,color = "#e06666" ,alpha = 0.6 ,interpolate = True )
172+ #fb_macd_up['panel'] = 1
173+ #fb_macd_dn['panel'] = 1
174+
175+ fb_macd = [fb_macd_up ,fb_macd_dn ]
176+
177+ apds = [mpf .make_addplot (exp12 ,color = 'lime' ),
178+ mpf .make_addplot (exp26 ,color = 'c' ),
179+ mpf .make_addplot (histogram ,type = 'bar' ,width = 0.7 ,panel = 1 ,
180+ color = 'dimgray' ,alpha = 0.65 ,secondary_y = True ),
181+ mpf .make_addplot (macd ,panel = 1 ,color = 'fuchsia' ,secondary_y = False ),
182+ mpf .make_addplot (signal ,panel = 1 ,color = 'b' ,secondary_y = False ,fill_between = fb_macd ),
183+ ]
184+
185+ s = mpf .make_mpf_style (base_mpf_style = 'blueskies' ,facecolor = 'aliceblue' )#,rc={'figure.facecolor':'lightcyan'})
186+
187+ mpf .plot (df ,type = 'candle' ,addplot = apds ,figscale = 1.6 ,figratio = (1 ,1 ),title = '\n \n MACD' ,
188+ style = s ,volume = True ,volume_panel = 2 ,panel_ratios = (3 ,4 ,1 ),tight_layout = True ,
189+ fill_between = fb_exp12 ,
190+ savefig = tname )
191+
192+ _report_file_sizes (tname ,rname )
193+
194+ result = compare_images (rname ,tname ,tol = IMGCOMP_TOLERANCE )
195+ if result is not None :
196+ print ('result=' ,result )
197+ assert result is None
0 commit comments