From f4c7849dcacf8517828d66da80d5d92ead52bc1f Mon Sep 17 00:00:00 2001 From: Faisal Date: Fri, 11 Nov 2022 23:29:56 +0300 Subject: [PATCH 1/4] added markevery feature to plotting.py --- src/mplfinance/plotting.py | 45 ++++++++++++++++++++++++++++++++------ 1 file changed, 38 insertions(+), 7 deletions(-) diff --git a/src/mplfinance/plotting.py b/src/mplfinance/plotting.py index 4e849f92..8abedf67 100644 --- a/src/mplfinance/plotting.py +++ b/src/mplfinance/plotting.py @@ -107,6 +107,10 @@ def _valid_plot_kwargs(): 'type' : { 'Default' : 'ohlc', 'Description' : 'Plot type: '+str(_get_valid_plot_types()), 'Validator' : lambda value: value in _get_valid_plot_types() }, + 'markevery' : { 'Default' : None, + 'Description' : 'markevery: size color shape ', + 'Validator' : lambda value: isinstance(value,(tuple, list, int))}, + 'style' : { 'Default' : None, 'Description' : 'plot style; see `mpf.available_styles()`', @@ -407,7 +411,8 @@ def plot( data, **kwargs ): moving averages, renko, etc. Also provide ability to plot trading signals, and/or addtional user-defined data. """ - + import sys + sys.stdout = sys.__stdout__ config = _process_kwargs(kwargs, _valid_plot_kwargs()) # translate alias types: @@ -492,6 +497,7 @@ def plot( data, **kwargs ): else: panels = _build_panels(fig, config) axA1 = panels.at[config['main_panel'],'axes'][0] + warnings.warn(f"this type is {type(axA1)}") if config['volume']: if config['volume_panel'] == config['main_panel']: # ohlc and volume on same panel: move volume to secondary axes: @@ -525,10 +531,10 @@ def plot( data, **kwargs ): collections = None if ptype == 'line': lw = config['_width_config']['line_width'] - axA1.plot(xdates, closes, color=config['linecolor'], linewidth=lw) + pmarkevery = config['markevery'] + axA1.plot(xdates, closes, 'o', markevery=pmarkevery, ls='-', color=config['linecolor'], linewidth=lw, ) else: else: collections =_construct_mpf_collections(ptype,dates,xdates,opens,highs,lows,closes,volumes,config,style) - if ptype in VALID_PMOVE_TYPES: collections, calculated_values = collections volumes = calculated_values['volumes'] @@ -650,10 +656,35 @@ def plot( data, **kwargs ): tlines = [tlines,] for tline_item in tlines: line_collections.append(_construct_tline_collections(tline_item, dtix, dates, opens, highs, lows, closes)) - - for collection in line_collections: - if collection is not None: - axA1.add_collection(collection) + ### note + # if config['addplot'] is not None: + # for panid,row in panels.iterrows(): + # print(panid) + # print("the value of min and max is ",miny,maxy) + # print("type of row is ", type(row)) + # print("panid is ",panid) + # print("row key is ",row.keys()) + # ax = row['axes'] + # line_collections = [] + # if (panid == 0): + # line_collections.append(_construct_vline_collections(config['vlines'], dtix, miny, maxy)) + # else: + # line_collections.append(_construct_vline_collections(config['vlines'], dtix, 200, 600)) + + # for collection in line_collections: + # if collection is not None: + # ax[0].add_collection(collection) + if config['addplot'] is not None: + for panid,row in panels.iterrows(): + ax = row['axes'] + print(dtix) + print(config['vlines']) + # _date_to_iloc + ax[0].axvline(x=50.5,linestyle='--') + else: + for collection in line_collections: + if collection is not None: + axA1.add_collection(collection) datalen = len(xdates) if config['volume']: From 591c81c7255b69ee196b20d45a923ec356d65c7e Mon Sep 17 00:00:00 2001 From: Faisal Date: Sun, 13 Nov 2022 20:23:29 +0300 Subject: [PATCH 2/4] markevery - syntax issue fixed --- src/mplfinance/plotting.py | 2 +- tests/test.py | 9 +++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) create mode 100644 tests/test.py diff --git a/src/mplfinance/plotting.py b/src/mplfinance/plotting.py index 8abedf67..03bd8071 100644 --- a/src/mplfinance/plotting.py +++ b/src/mplfinance/plotting.py @@ -532,7 +532,7 @@ def plot( data, **kwargs ): if ptype == 'line': lw = config['_width_config']['line_width'] pmarkevery = config['markevery'] - axA1.plot(xdates, closes, 'o', markevery=pmarkevery, ls='-', color=config['linecolor'], linewidth=lw, ) else: + axA1.plot(xdates, closes, 'o', markevery=pmarkevery, ls='-', color=config['linecolor'], linewidth=lw, ) else: collections =_construct_mpf_collections(ptype,dates,xdates,opens,highs,lows,closes,volumes,config,style) if ptype in VALID_PMOVE_TYPES: diff --git a/tests/test.py b/tests/test.py new file mode 100644 index 00000000..1fe48eca --- /dev/null +++ b/tests/test.py @@ -0,0 +1,9 @@ +import pandas as pd +import mplfinance as mpf +df = pd.read_csv('data/SPY_20110701_20120630_Bollinger.csv',index_col=0,parse_dates=True) + +apdict = [mpf.make_addplot(df['LowerB']), + mpf.make_addplot(df['UpperB'],panel=1)] +vls = pd.date_range(df.index.min(), df.index.max(), freq='D').tolist() +kwargs = dict(type='candle', vlines=dict(vlines=vls[0], linewidths=0.5, colors=('r'))) +mpf.plot(df,volume=False,addplot=apdict,**kwargs) \ No newline at end of file From e5596e5997c3e25836223ccc913e84cf52bcc874 Mon Sep 17 00:00:00 2001 From: Jonathan Xu <790265863@qq.com> Date: Mon, 14 Nov 2022 00:04:52 +0300 Subject: [PATCH 3/4] change vline --- src/mplfinance/plotting.py | 47 ++++++++++++++++++-------------------- 1 file changed, 22 insertions(+), 25 deletions(-) diff --git a/src/mplfinance/plotting.py b/src/mplfinance/plotting.py index 03bd8071..fb4811ce 100644 --- a/src/mplfinance/plotting.py +++ b/src/mplfinance/plotting.py @@ -260,7 +260,7 @@ def _valid_plot_kwargs(): 'Validator' : lambda value: _hlines_validator(value) }, 'vlines' : { 'Default' : None, - 'Description' : 'Draw one or more VERTICAL LINES across entire plot, by'+ + 'Description' : 'Draw VERTICAL LINES across one plot, by'+ ' specifying a date[time], or sequence of date[time]. May also'+ ' be a dict with key `vlines` specifying a date[time] or sequence'+ ' of date[time], plus one or more of the following keys:'+ @@ -532,7 +532,7 @@ def plot( data, **kwargs ): if ptype == 'line': lw = config['_width_config']['line_width'] pmarkevery = config['markevery'] - axA1.plot(xdates, closes, 'o', markevery=pmarkevery, ls='-', color=config['linecolor'], linewidth=lw, ) + axA1.plot(xdates, closes, 'o', markevery=pmarkevery, ls='-', color=config['linecolor'], linewidth=lw ) else: collections =_construct_mpf_collections(ptype,dates,xdates,opens,highs,lows,closes,volumes,config,style) if ptype in VALID_PMOVE_TYPES: @@ -656,31 +656,28 @@ def plot( data, **kwargs ): tlines = [tlines,] for tline_item in tlines: line_collections.append(_construct_tline_collections(tline_item, dtix, dates, opens, highs, lows, closes)) - ### note - # if config['addplot'] is not None: - # for panid,row in panels.iterrows(): - # print(panid) - # print("the value of min and max is ",miny,maxy) - # print("type of row is ", type(row)) - # print("panid is ",panid) - # print("row key is ",row.keys()) - # ax = row['axes'] - # line_collections = [] - # if (panid == 0): - # line_collections.append(_construct_vline_collections(config['vlines'], dtix, miny, maxy)) - # else: - # line_collections.append(_construct_vline_collections(config['vlines'], dtix, 200, 600)) - - # for collection in line_collections: - # if collection is not None: - # ax[0].add_collection(collection) - if config['addplot'] is not None: + + vlinelist = config['vlines'] + + if config['addplot'] is not None and vlinelist is not None: for panid,row in panels.iterrows(): ax = row['axes'] - print(dtix) - print(config['vlines']) - # _date_to_iloc - ax[0].axvline(x=50.5,linestyle='--') + + # print("the type of vlinelist is ",type(vlinelist)) + + # print(vlinelist) + # print(vlinelist['vlines']) + + # for line in vlinelist['vlines']: + # print("the line is ",line) + # print("the line is type ",type(line)) + + # print("the data type is ",type(data)) + # print("the data type is ",data.columns) + # ax[0].axvline(x=data.loc[str(line)],linestyle='--') + ax[0].axvline(x=50,linestyle='--') + + else: for collection in line_collections: if collection is not None: From cc523382c94e32fff008c96fdc2b627475f8ad2b Mon Sep 17 00:00:00 2001 From: Jonathan Xu <790265863@qq.com> Date: Mon, 14 Nov 2022 00:53:13 +0300 Subject: [PATCH 4/4] Revert "change vline" This reverts commit e5596e5997c3e25836223ccc913e84cf52bcc874. --- src/mplfinance/plotting.py | 47 ++++++++++++++++++++------------------ 1 file changed, 25 insertions(+), 22 deletions(-) diff --git a/src/mplfinance/plotting.py b/src/mplfinance/plotting.py index fb4811ce..03bd8071 100644 --- a/src/mplfinance/plotting.py +++ b/src/mplfinance/plotting.py @@ -260,7 +260,7 @@ def _valid_plot_kwargs(): 'Validator' : lambda value: _hlines_validator(value) }, 'vlines' : { 'Default' : None, - 'Description' : 'Draw VERTICAL LINES across one plot, by'+ + 'Description' : 'Draw one or more VERTICAL LINES across entire plot, by'+ ' specifying a date[time], or sequence of date[time]. May also'+ ' be a dict with key `vlines` specifying a date[time] or sequence'+ ' of date[time], plus one or more of the following keys:'+ @@ -532,7 +532,7 @@ def plot( data, **kwargs ): if ptype == 'line': lw = config['_width_config']['line_width'] pmarkevery = config['markevery'] - axA1.plot(xdates, closes, 'o', markevery=pmarkevery, ls='-', color=config['linecolor'], linewidth=lw ) + axA1.plot(xdates, closes, 'o', markevery=pmarkevery, ls='-', color=config['linecolor'], linewidth=lw, ) else: collections =_construct_mpf_collections(ptype,dates,xdates,opens,highs,lows,closes,volumes,config,style) if ptype in VALID_PMOVE_TYPES: @@ -656,28 +656,31 @@ def plot( data, **kwargs ): tlines = [tlines,] for tline_item in tlines: line_collections.append(_construct_tline_collections(tline_item, dtix, dates, opens, highs, lows, closes)) - - vlinelist = config['vlines'] - - if config['addplot'] is not None and vlinelist is not None: + ### note + # if config['addplot'] is not None: + # for panid,row in panels.iterrows(): + # print(panid) + # print("the value of min and max is ",miny,maxy) + # print("type of row is ", type(row)) + # print("panid is ",panid) + # print("row key is ",row.keys()) + # ax = row['axes'] + # line_collections = [] + # if (panid == 0): + # line_collections.append(_construct_vline_collections(config['vlines'], dtix, miny, maxy)) + # else: + # line_collections.append(_construct_vline_collections(config['vlines'], dtix, 200, 600)) + + # for collection in line_collections: + # if collection is not None: + # ax[0].add_collection(collection) + if config['addplot'] is not None: for panid,row in panels.iterrows(): ax = row['axes'] - - # print("the type of vlinelist is ",type(vlinelist)) - - # print(vlinelist) - # print(vlinelist['vlines']) - - # for line in vlinelist['vlines']: - # print("the line is ",line) - # print("the line is type ",type(line)) - - # print("the data type is ",type(data)) - # print("the data type is ",data.columns) - # ax[0].axvline(x=data.loc[str(line)],linestyle='--') - ax[0].axvline(x=50,linestyle='--') - - + print(dtix) + print(config['vlines']) + # _date_to_iloc + ax[0].axvline(x=50.5,linestyle='--') else: for collection in line_collections: if collection is not None: