|
| 1 | +import mplfinance as mpf |
| 2 | +import pandas as pd |
| 3 | +import textwrap |
| 4 | + |
| 5 | +def df_wrapcols(df,wrap_columns=None): |
| 6 | + |
| 7 | + if wrap_columns is None: return df |
| 8 | + if not isinstance(wrap_columns,dict): |
| 9 | + raise TypeError('wrap_columns must be a dict of column_names and wrap_lengths') |
| 10 | + |
| 11 | + for col in wrap_columns: |
| 12 | + if col not in df.columns: |
| 13 | + raise ValueError('column "'+str(col)+'" not found in df.columns') |
| 14 | + |
| 15 | + index = [] |
| 16 | + column_data = {} |
| 17 | + for col in df.columns: |
| 18 | + column_data[col] = [] |
| 19 | + |
| 20 | + for ix in df.index: |
| 21 | + row = df.loc[ix,] |
| 22 | + |
| 23 | + row_data = {} |
| 24 | + for col in row.index: |
| 25 | + cstr = str(row[col]) |
| 26 | + if col in wrap_columns: |
| 27 | + wlen = wrap_columns[col] |
| 28 | + tw = textwrap.wrap(cstr,wlen) if not cstr.isspace() else [' '] |
| 29 | + else: |
| 30 | + tw = [cstr] |
| 31 | + row_data[col] = tw |
| 32 | + |
| 33 | + cmax = max(row_data,key=lambda k: len(row_data[k])) |
| 34 | + rlen = len(row_data[cmax]) |
| 35 | + for r in range(rlen): |
| 36 | + for col in row.index: |
| 37 | + extension = [' ']*(rlen - len(row_data[col])) |
| 38 | + row_data[col].extend(extension) |
| 39 | + column_data[col].append(row_data[col][r]) |
| 40 | + ixstr = str(ix)+'.'+str(r) if r > 0 else str(ix) |
| 41 | + index.append(ixstr) |
| 42 | + |
| 43 | + return pd.DataFrame(column_data,index=index) |
| 44 | + |
| 45 | +def make_left_formatter(maxwidth): |
| 46 | + wm3 = maxwidth-3 |
| 47 | + w = maxwidth |
| 48 | + def left_formatter(value): |
| 49 | + if not isinstance(value,str): |
| 50 | + return f'{value:<}' |
| 51 | + elif value[0:maxwidth] == '-'*maxwidth: |
| 52 | + return f'{value:<{w}.{w}s}' |
| 53 | + elif len(value) > maxwidth: |
| 54 | + return f'{value:<{wm3}.{wm3}s}...' |
| 55 | + else: |
| 56 | + return f'{value:<{w}.{w}s}' |
| 57 | + return left_formatter |
| 58 | + |
| 59 | + |
| 60 | +def kwarg_help( func_name=None, kwarg_names=None ): |
| 61 | + |
| 62 | + func_kwarg_map = { |
| 63 | + 'plot' : mpf.plotting._valid_plot_kwargs, |
| 64 | + 'make_addplot' : mpf.plotting._valid_addplot_kwargs, |
| 65 | + 'make_marketcolors' : mpf._styles._valid_make_marketcolors_kwargs, |
| 66 | + 'make_mpf_style' : mpf._styles._valid_make_mpf_style_kwargs, |
| 67 | + 'renko_params' : mpf._utils._valid_renko_kwargs, |
| 68 | + 'pnf_params' : mpf._utils._valid_pnf_kwargs, |
| 69 | + 'lines' : mpf._utils._valid_lines_kwargs, |
| 70 | + 'scale_width_adjustment': mpf._widths._valid_scale_width_kwargs, |
| 71 | + 'update_width_config': mpf._widths._valid_update_width_kwargs, |
| 72 | + } |
| 73 | + |
| 74 | + func_kwarg_aliases = { |
| 75 | + 'addplot' : mpf.plotting._valid_addplot_kwargs, |
| 76 | + 'marketcolors' : mpf._styles._valid_make_marketcolors_kwargs, |
| 77 | + 'mpf_style' : mpf._styles._valid_make_mpf_style_kwargs, |
| 78 | + 'style' : mpf._styles._valid_make_mpf_style_kwargs, |
| 79 | + 'renko' : mpf._utils._valid_renko_kwargs, |
| 80 | + 'pnf' : mpf._utils._valid_pnf_kwargs, |
| 81 | + 'hlines' : mpf._utils._valid_lines_kwargs, |
| 82 | + 'alines' : mpf._utils._valid_lines_kwargs, |
| 83 | + 'tlines' : mpf._utils._valid_lines_kwargs, |
| 84 | + 'vlines' : mpf._utils._valid_lines_kwargs, |
| 85 | + } |
| 86 | + |
| 87 | + if func_name is None: |
| 88 | + print('\nUsage: `kwarg_help(func_name)` or `kwarg_help(func_name,kwarg_names)`') |
| 89 | + print(' kwarg_help is available for the following func_names:') |
| 90 | + s = str(list(func_kwarg_map.keys())) |
| 91 | + text = textwrap.wrap(s,68) |
| 92 | + for t in text: |
| 93 | + print(' ',t) |
| 94 | + print() |
| 95 | + return |
| 96 | + |
| 97 | + fkmap = {**func_kwarg_map, **func_kwarg_aliases} |
| 98 | + |
| 99 | + if func_name not in fkmap: |
| 100 | + raise ValueError('Function name "'+func_name+'" NOT a valid function name') |
| 101 | + |
| 102 | + if kwarg_names is not None and isinstance(kwarg_names,str): |
| 103 | + kwarg_names = [ kwarg_names, ] |
| 104 | + |
| 105 | + if ( kwarg_names is not None |
| 106 | + and (not isinstance(kwarg_names,(list,tuple)) |
| 107 | + or not all([isinstance(k,str) for k in kwarg_names]) |
| 108 | + ) |
| 109 | + ): |
| 110 | + raise ValueError('kwarg_names must be a sequence (list,tuple) of strings') |
| 111 | + |
| 112 | + vks = fkmap[func_name]() |
| 113 | + |
| 114 | + df = (pd.DataFrame(vks).T).drop('Validator',axis=1) |
| 115 | + df.index.name = 'Kwarg' |
| 116 | + df.reset_index(inplace=True) |
| 117 | + |
| 118 | + if kwarg_names is not None: |
| 119 | + for k in kwarg_names: |
| 120 | + if k not in df['Kwarg'].values: |
| 121 | + print(' Warning: "'+k+'" is not a valid `kwarg_name` for `func_name` "'+func_name,'"') |
| 122 | + df = df[ df['Kwarg'].isin(kwarg_names) ] |
| 123 | + if len(df) < 1: |
| 124 | + raise ValueError(' None of specified `kwarg_names` are valid for `func_name` "'+func_name,'"') |
| 125 | + |
| 126 | + df['Default'] = ["'"+d+"'" if isinstance(d,str) else str(d) for d in df['Default']] |
| 127 | + |
| 128 | + klen = df['Kwarg'].str.len().max()+1 |
| 129 | + dlen = df['Default'].str.len().max()+1 |
| 130 | + |
| 131 | + wraplen = max( 40, 80-(klen+dlen) ) |
| 132 | + df = df_wrapcols(df,wrap_columns={'Description':wraplen}) |
| 133 | + |
| 134 | + dividers = [] |
| 135 | + for col in df.columns: |
| 136 | + dividers.append('-'*int(df[col].str.len().max())) |
| 137 | + dfd = pd.DataFrame(dividers).T |
| 138 | + dfd.columns = df.columns |
| 139 | + dfd.index = pd.Index(['---']) |
| 140 | + |
| 141 | + df = dfd.append(df) |
| 142 | + |
| 143 | + formatters = { 'Kwarg' : make_left_formatter( klen ), |
| 144 | + 'Default' : make_left_formatter( dlen ), |
| 145 | + 'Description' : make_left_formatter( wraplen ), |
| 146 | + } |
| 147 | + |
| 148 | + print('\n ','-'*78) |
| 149 | + print(' Kwargs for func_name "'+func_name+'":') |
| 150 | + |
| 151 | + s = df.to_string(formatters=formatters,index=False,justify='left') |
| 152 | + |
| 153 | + print('\n ',s.replace('\n','\n ')) |
0 commit comments