Beispiel #1
0
def _construct_aline_collections(alines, dtix=None):
    """construct arbitrary line collections

    Parameters
    ----------
    alines : sequence
        sequences of segments, which are sequences of lines,
        which are sequences of two or more points ( date[time], price ) or (x,y) 

        date[time] may be (a) pandas.to_datetime parseable string,
                          (b) pandas Timestamp, or
                          (c) python datetime.datetime or datetime.date

    alines may also be a dict, containing
    the following keys:

        'alines'     : the same as defined above: sequence of price, or dates, or segments
        'colors'     : colors for the above alines
        'linestyle'  : line types for the above alines
        'linewidths' : line types for the above alines

    dtix:  date index for the x-axis, used for converting the dates when
           x-values are 'evenly spaced integers' (as when skipping non-trading days)

    Returns
    -------
    ret : list
        lines collections
    """
    if alines is None:
        return None

    if isinstance(alines, dict):
        aconfig = _process_kwargs(alines, _valid_lines_kwargs())
        alines = aconfig['alines']
    else:
        aconfig = _process_kwargs({}, _valid_lines_kwargs())

    #print('aconfig=',aconfig)
    #print('alines=',alines)

    alines = _alines_validator(alines, returnStandardizedValue=True)
    if alines is None:
        raise ValueError('Unable to standardize alines value: ' + str(alines))

    alines = _convert_segment_dates(alines, dtix)

    lw = aconfig['linewidths']
    co = aconfig['colors']
    ls = aconfig['linestyle']
    al = aconfig['alpha']
    lcollection = LineCollection(alines,
                                 colors=co,
                                 linewidths=lw,
                                 linestyles=ls,
                                 antialiaseds=(0, ),
                                 alpha=al)
    return lcollection
def _valid_plot_kwargs():
    '''
    Construct and return the "valid kwargs table" for the mplfinance.plot() function.
    A valid kwargs table is a `dict` of `dict`s.  The keys of the outer dict are the
    valid key-words for the function.  The value for each key is a dict containing
    2 specific keys: "Default", and "Validator" with the following values:
        "Default"      - The default value for the kwarg if none is specified.
        "Validator"    - A function that takes the caller specified value for the kwarg,
                         and validates that it is the correct type, and (for kwargs with 
                         a limited set of allowed values) may also validate that the
                         kwarg value is one of the allowed values.
    '''

    vkwargs = {
        'columns'                   : { 'Default'     : None, # use default names: ('Open', 'High', 'Low', 'Close', 'Volume')
                                        'Validator'   : lambda value: isinstance(value, (tuple, list))
                                                                   and len(value) == 5
                                                                   and all(isinstance(c, str) for c in value) },
        'type'                      : { 'Default'     : 'ohlc',
                                        'Validator'   : lambda value: value in _get_valid_plot_types() },
 
        'style'                     : { 'Default'     : None,
                                        'Validator'   : _styles._valid_mpf_style },
 
        'volume'                    : { 'Default'     : False,
                                        'Validator'   : lambda value: isinstance(value,bool) or isinstance(value,mpl_axes.Axes) },
 
        'mav'                       : { 'Default'     : None,
                                        'Validator'   : _mav_validator },
        
        'renko_params'              : { 'Default'     : dict(),
                                        'Validator'   : lambda value: isinstance(value,dict) },

        'pnf_params'                : { 'Default'     : dict(),
                                        'Validator'   : lambda value: isinstance(value,dict) },
 
        'study'                     : { 'Default'     : None,
                                        'Validator'   : lambda value: _kwarg_not_implemented(value) }, 
 
        'marketcolors'              : { 'Default'     : None, # use 'style' for default, instead.
                                        'Validator'   : lambda value: isinstance(value,dict) },
 
        'no_xgaps'                  : { 'Default'     : True,  # None means follow default logic below:
                                        'Validator'   : lambda value: _warn_no_xgaps_deprecated(value) },
 
        'show_nontrading'           : { 'Default'     : False, 
                                        'Validator'   : lambda value: isinstance(value,bool) },
 
        'figscale'                  : { 'Default'     : None, # scale base figure size up or down.
                                        'Validator'   : lambda value: isinstance(value,float) or isinstance(value,int) },
 
        'figratio'                  : { 'Default'     : None, # aspect ratio; scaled to 8.0 height
                                        'Validator'   : lambda value: isinstance(value,(tuple,list))
                                                                      and len(value) == 2
                                                                      and isinstance(value[0],(float,int))
                                                                      and isinstance(value[1],(float,int)) },
 
        'figsize'                   : { 'Default'     : None,  # figure size; overrides figratio and figscale
                                        'Validator'   : lambda value: isinstance(value,(tuple,list))
                                                                      and len(value) == 2
                                                                      and isinstance(value[0],(float,int))
                                                                      and isinstance(value[1],(float,int)) },
 
        'linecolor'                 : { 'Default'     : None, # line color in line plot
                                        'Validator'   : lambda value: mcolors.is_color_like(value) },

        'title'                     : { 'Default'     : None, # Figure Title
                                        'Validator'   : lambda value: isinstance(value,(str,dict)) },
 
        'axtitle'                   : { 'Default'     : None, # Axes Title (subplot title)
                                        'Validator'   : lambda value: isinstance(value,str) },
 
        'ylabel'                    : { 'Default'     : 'Price', # y-axis label
                                        'Validator'   : lambda value: isinstance(value,str) },
 
        'ylabel_lower'              : { 'Default'     : None, # y-axis label default logic below
                                        'Validator'   : lambda value: isinstance(value,str) },
 
        'addplot'                   : { 'Default'     : None, 
                                        'Validator'   : lambda value: isinstance(value,dict) or (isinstance(value,list) and all([isinstance(d,dict) for d in value])) },
 
        'savefig'                   : { 'Default'     : None, 
                                        'Validator'   : lambda value: isinstance(value,dict) or isinstance(value,str) or isinstance(value, io.BytesIO) },
 
        'block'                     : { 'Default'     : None, 
                                        'Validator'   : lambda value: isinstance(value,bool) },
 
        'returnfig'                 : { 'Default'     : False, 
                                        'Validator'   : lambda value: isinstance(value,bool) },

        'return_calculated_values'  : {'Default'      : None,
                                       'Validator'    : lambda value: isinstance(value, dict) and len(value) == 0},

        'set_ylim'                  : {'Default'      : None,
                                       'Validator'    : lambda value: _warn_set_ylim_deprecated(value) },
 
        'ylim'                      : {'Default'      : None,
                                       'Validator'    : lambda value: isinstance(value, (list,tuple)) and len(value) == 2 
                                                                      and all([isinstance(v,(int,float)) for v in value])},
 
        'xlim'                      : {'Default'      : None,
                                       'Validator'    : lambda value: isinstance(value, (list,tuple)) and len(value) == 2 
                                                                      and all([isinstance(v,(int,float)) for v in value])},
 
        'set_ylim_panelB'           : {'Default'      : None,
                                       'Validator'    : lambda value: _warn_set_ylim_deprecated(value) },
 
        'hlines'                    : { 'Default'     : None, 
                                        'Validator'   : lambda value: _hlines_validator(value) },
 
        'vlines'                    : { 'Default'     : None, 
                                        'Validator'   : lambda value: _vlines_validator(value) },

        'alines'                    : { 'Default'     : None, 
                                        'Validator'   : lambda value: _alines_validator(value) },
 
        'tlines'                    : { 'Default'     : None, 
                                        'Validator'   : lambda value: _tlines_validator(value) },
       
        'panel_ratios'              : { 'Default'     : None,
                                        'Validator'   : lambda value: isinstance(value,(tuple,list)) and len(value) <= 10 and
                                                                      all([isinstance(v,(int,float)) for v in value]) },

        'main_panel'                : { 'Default'     : 0,
                                        'Validator'   : lambda value: _valid_panel_id(value) },

        'volume_panel'              : { 'Default'     : 1,
                                        'Validator'   : lambda value: _valid_panel_id(value) },

        'num_panels'                : { 'Default'     : None,
                                        'Validator'   : lambda value: isinstance(value,int) and value in range(1,10+1) },

        'datetime_format'           : { 'Default'     : None,
                                        'Validator'   : lambda value: isinstance(value,str) },

        'xrotation'                 : { 'Default'     : 45,
                                        'Validator'   : lambda value: isinstance(value,(int,float)) },

        'axisoff'                   : { 'Default'     : False,
                                        'Validator'   : lambda value: isinstance(value,bool) },

        'closefig'                  : { 'Default'     : 'auto',
                                        'Validator'   : lambda value: isinstance(value,bool) },

        'fill_between'              : { 'Default'     : None,
                                        'Validator'   : lambda value: _num_or_seq_of_num(value) or 
                                                                     (isinstance(value,dict) and 'y1' in value and
                                                                       _num_or_seq_of_num(value['y1'])) },

        'tight_layout'              : { 'Default'     : False,
                                        'Validator'   : lambda value: isinstance(value,bool) },

        'width_adjuster_version'    : { 'Default'     : 'v1',
                                        'Validator'   : lambda value: value in ('v0', 'v1') },

        'scale_width_adjustment'    : { 'Default'     : None,
                                        'Validator'   : lambda value: isinstance(value,dict) and len(value) > 0 },

        'update_width_config'       : { 'Default'     : None,
                                        'Validator'   : lambda value: isinstance(value,dict) and len(value) > 0 },

        'return_width_config'       : { 'Default'     : None,
                                        'Validator'   : lambda value: isinstance(value,dict) and len(value)==0 },

        'saxbelow'                  : { 'Default'     : True,  # Issue#115 Comment#639446764
                                        'Validator'   : lambda value: isinstance(value,bool) },
        
        'scale_padding'             : { 'Default'     : 1.0,   # Issue#193 
                                        'Validator'   : lambda value: _scale_padding_validator(value) },

        'ax'                        : { 'Default'     : None,
                                        'Validator'   : lambda value: isinstance(value,mpl_axes.Axes) },

        'volume_exponent'           : { 'Default'     : None,
                                        'Validator'   : lambda value: isinstance(value,int) or value == 'legacy'},

        'tz_localize'               : { 'Default'     : True,
                                        'Validator'   : lambda value: isinstance(value,bool) },
    }

    _validate_vkwargs_dict(vkwargs)

    return vkwargs
Beispiel #3
0
def _valid_plot_kwargs():
    '''
    Construct and return the "valid kwargs table" for the mplfinance.plot() function.
    A valid kwargs table is a `dict` of `dict`s.  The keys of the outer dict are the
    valid key-words for the function.  The value for each key is a dict containing
    2 specific keys: "Default", and "Validator" with the following values:
        "Default"      - The default value for the kwarg if none is specified.
        "Validator"    - A function that takes the caller specified value for the kwarg,
                         and validates that it is the correct type, and (for kwargs with 
                         a limited set of allowed values) may also validate that the
                         kwarg value is one of the allowed values.
    '''

    vkwargs = {
        'columns': {
            'Default': ('Open', 'High', 'Low', 'Close', 'Volume'),
            'Validator':
            lambda value: isinstance(value, (tuple, list)) and len(value) == 5
            and all(isinstance(c, str) for c in value)
        },
        'type': {
            'Default':
            'ohlc',
            'Validator':
            lambda value: value in [
                'candle', 'candlestick', 'ohlc', 'bars', 'ohlc_bars', 'line',
                'renko', 'pnf'
            ]
        },
        'style': {
            'Default':
            'default',
            'Validator':
            lambda value: value in _styles.available_styles() or isinstance(
                value, dict)
        },
        'volume': {
            'Default': False,
            'Validator': lambda value: isinstance(value, bool)
        },
        'mav': {
            'Default': None,
            'Validator': _mav_validator
        },
        'renko_params': {
            'Default': dict(),
            'Validator': lambda value: isinstance(value, dict)
        },
        'pnf_params': {
            'Default': dict(),
            'Validator': lambda value: isinstance(value, dict)
        },
        'study': {
            'Default': None,
            'Validator': lambda value: _kwarg_not_implemented(value)
        },
        'marketcolors': {
            'Default': None,  # use 'style' for default, instead.
            'Validator': lambda value: isinstance(value, dict)
        },
        'no_xgaps': {
            'Default': True,  # None means follow default logic below:
            'Validator': lambda value: _warn_no_xgaps_deprecated(value)
        },
        'show_nontrading': {
            'Default': False,
            'Validator': lambda value: isinstance(value, bool)
        },
        'figscale': {
            'Default':
            1.0,  # scale base figure size up or down.
            'Validator':
            lambda value: isinstance(value, float) or isinstance(value, int)
        },
        'figratio': {
            'Default':
            (8.00,
             5.75),  # aspect ratio; will equal fig size when figscale=1.0
            'Validator':
            lambda value: isinstance(value,
                                     (tuple, list)) and len(value) == 2 and
            isinstance(value[0],
                       (float, int)) and isinstance(value[1], (float, int))
        },
        'linecolor': {
            'Default': 'k',  # line color in line plot
            'Validator': lambda value: mcolors.is_color_like(value)
        },
        'title': {
            'Default': None,  # Plot Title
            'Validator': lambda value: isinstance(value, str)
        },
        'ylabel': {
            'Default': 'Price',  # y-axis label
            'Validator': lambda value: isinstance(value, str)
        },
        'ylabel_lower': {
            'Default': None,  # y-axis label default logic below
            'Validator': lambda value: isinstance(value, str)
        },

        #'xlabel'                    : { 'Default'     : None,  # x-axis label, default is None because obvious it's time or date
        #                                'Validator'   : lambda value: isinstance(value,str) },
        'addplot': {
            'Default':
            None,
            'Validator':
            lambda value: isinstance(value, dict) or (isinstance(
                value, list) and all([isinstance(d, dict) for d in value]))
        },
        'savefig': {
            'Default':
            None,
            'Validator':
            lambda value: isinstance(value, dict) or isinstance(value, str) or
            isinstance(value, io.BytesIO)
        },
        'block': {
            'Default': True,
            'Validator': lambda value: isinstance(value, bool)
        },
        'returnfig': {
            'Default': False,
            'Validator': lambda value: isinstance(value, bool)
        },
        'return_calculated_values': {
            'Default': None,
            'Validator':
            lambda value: isinstance(value, dict) and len(value) == 0
        },
        'set_ylim': {
            'Default':
            None,
            'Validator':
            lambda value: isinstance(value, (list, tuple)) and len(value) == 2
            and all([isinstance(v, (int, float)) for v in value])
        },
        'set_ylim_panelB': {
            'Default':
            None,
            'Validator':
            lambda value: isinstance(value, (list, tuple)) and len(value) == 2
            and all([isinstance(v, (int, float)) for v in value])
        },
        'hlines': {
            'Default': None,
            'Validator': lambda value: _hlines_validator(value)
        },
        'vlines': {
            'Default': None,
            'Validator': lambda value: _vlines_validator(value)
        },
        'alines': {
            'Default': None,
            'Validator': lambda value: _alines_validator(value)
        },
        'tlines': {
            'Default': None,
            'Validator': lambda value: _tlines_validator(value)
        },
    }

    _validate_vkwargs_dict(vkwargs)

    return vkwargs