Ejemplo n.º 1
0
 def __init__(self,
              name,
              timestamps,
              values,
              plot_type='line',
              line_type='solid',
              line_width=None,
              color=None,
              marker=None,
              marker_size=50,
              marker_color='red'):
     '''Args:
         name: Name to show in plot legend
         timestamps: pandas Series or numpy array of datetime64
         values: pandas Series or numpy array of floats
         plot_type: 'line' or 'scatter'
         marker: If set, show a marker at each value in values.  See matplotlib marker types
     '''
     self.name = name
     self.timestamps = series_to_array(timestamps)
     self.values = series_to_array(values)
     self.plot_type = plot_type
     self.line_type = line_type
     self.color = color
     self.line_width = line_width
     if plot_type == 'scatter' and marker is None:
         raise Exception('marker must be set for plot_type scatter')
     self.marker = marker
     self.marker_size = marker_size
     self.marker_color = marker_color
     self.time_plot = True
Ejemplo n.º 2
0
 def __init__(self,
              name,
              x,
              y,
              plot_type='line',
              line_type='solid',
              line_width=None,
              color=None,
              marker=None,
              marker_size=50,
              marker_color='red'):
     '''
     Args:
         x: pandas series or numpy array of floats
         y: pandas series or numpy arry of floats
     '''
     self.name = name
     self.x = series_to_array(x)
     self.y = series_to_array(y)
     self.plot_type = plot_type
     if plot_type == 'scatter' and marker is None: marker = 'X'
     self.line_type = line_type
     self.line_width = line_width
     self.color = color
     self.marker = marker
     self.marker_size = marker_size
     self.marker_color = marker_color
     self.time_plot = False
Ejemplo n.º 3
0
 def __init__(self, 
              name: str,
              x: Union[np.ndarray, pd.Series],
              y: Union[np.ndarray, pd.Series],
              display_attributes: DisplayAttributes = None) -> None:
     self.name = name
     self.x = series_to_array(x)
     self.y = series_to_array(y)
     if display_attributes is None: display_attributes = LinePlotAttributes()
     self.display_attributes = display_attributes
Ejemplo n.º 4
0
 def __init__(self, 
              name: str, 
              timestamps: Union[pd.Series, np.ndarray],
              values: Union[pd.Series, np.ndarray],
              display_attributes: DisplayAttributes = None) -> None:
     '''
     Args:
         name: Name to show in plot legend
     '''
     self.name = name
     self.timestamps = series_to_array(timestamps)
     self.values = series_to_array(values)
     if display_attributes is None: display_attributes = LinePlotAttributes()
     self.display_attributes = display_attributes
Ejemplo n.º 5
0
 def __init__(self,
              name,
              bucket_names,
              bucket_values,
              proportional_widths=True,
              show_means=True,
              show_all=True,
              show_outliers=False,
              notched=False):
     '''
     Args:
         name: name used for this data in a plot legend
         bucket_names: list of strings used on x axis labels
         bucket_values: list of numpy arrays that are summarized in this plot
         proportional_widths: if set to True, the width each box in the boxplot will be proportional to the number of items in its corresponding array
         show_means: Whether to display a marker where the mean is for each array
         show_outliers: Whether to show markers for outliers that are outside the whiskers.  
           Box is at Q1 = 25%, Q3 = 75% quantiles, whiskers are at Q1 - 1.5 * (Q3 - Q1), Q3 + 1.5 * (Q3 - Q1)
         notched: Whether to show notches indicating the confidence interval around the median
     '''
     assert isinstance(bucket_names, list) and isinstance(
         bucket_values, list) and len(bucket_names) == len(bucket_values)
     self.name = name
     self.bucket_names = bucket_names
     self.bucket_values = series_to_array(bucket_values)
     self.plot_type = 'boxplot'
     self.proportional_widths = proportional_widths
     self.show_means = show_means
     self.show_all = show_all
     self.show_outliers = show_outliers
     self.notched = notched
     self.time_plot = False
Ejemplo n.º 6
0
    def run_indicators(self, 
                       indicator_names: Sequence[str] = None, 
                       contract_groups: Sequence[ContractGroup] = None, 
                       clear_all: bool = False) -> None:
        '''Calculate values of the indicators specified and store them.
        
        Args:
            indicator_names: List of indicator names.  If None (default) run all indicators
            contract_groups: Contract group to run this indicator for.  If None (default), we run it for all contract groups.
            clear_all: If set, clears all indicator values before running.  Default False.
        '''
        
        if indicator_names is None: indicator_names = list(self.indicators.keys())
        if contract_groups is None: contract_groups = self.contract_groups
            
        if clear_all: self.indicator_values = defaultdict(types.SimpleNamespace)
            
        ind_names = []
            
        for ind_name, cgroup_list in self.indicator_cgroups.items():
            if len(set(contract_groups).intersection(cgroup_list)): ind_names.append(ind_name)
                
        indicator_names = list(set(ind_names).intersection(indicator_names))
         
        for cgroup in contract_groups:
            cgroup_ind_namespace = self.indicator_values[cgroup]
            for indicator_name in indicator_names:
                # First run all parents
                parent_names = self.indicator_deps[indicator_name]
                for parent_name in parent_names:
                    if cgroup in self.indicator_values and hasattr(cgroup_ind_namespace, parent_name): continue
                    self.run_indicators([parent_name], [cgroup])
                    
                # Now run the actual indicator
                if cgroup in self.indicator_values and hasattr(cgroup_ind_namespace, indicator_name): continue
                indicator_function = self.indicators[indicator_name]
                     
                parent_values = types.SimpleNamespace()

                for parent_name in parent_names:
                    setattr(parent_values, parent_name, getattr(cgroup_ind_namespace, parent_name))
                    
                if isinstance(indicator_function, np.ndarray) or isinstance(indicator_function, pd.Series):
                    indicator_values = indicator_function
                else:
                    indicator_values = indicator_function(cgroup, self.timestamps, parent_values, self.strategy_context)

                setattr(cgroup_ind_namespace, indicator_name, series_to_array(indicator_values))
Ejemplo n.º 7
0
 def run_signals(self, 
                 signal_names: Sequence[str] = None, 
                 contract_groups: Sequence[ContractGroup] = None, 
                 clear_all: bool = False) -> None:
     '''Calculate values of the signals specified and store them.
     
     Args:
         signal_names: List of signal names.  If None (default) run all signals
         contract_groups: Contract groups to run this signal for. If None (default), we run it for all contract groups.
         clear_all: If set, clears all signal values before running.  Default False.
     '''
     if signal_names is None: signal_names = list(self.signals.keys())
     if contract_groups is None: contract_groups = self.contract_groups
         
     if clear_all: self.signal_values = defaultdict(types.SimpleNamespace)
         
     sig_names = []
     
     for sig_name, cgroup_list in self.signal_cgroups.items():
         if len(set(contract_groups).intersection(cgroup_list)): 
             sig_names.append(sig_name)
             
     signal_names = list(set(sig_names).intersection(signal_names))
     
     for cgroup in contract_groups:
         for signal_name in signal_names:
             if cgroup not in self.signal_cgroups[signal_name]: continue
             # First run all parent signals
             parent_names = self.signal_deps[signal_name]
             for parent_name in parent_names:
                 if cgroup in self.signal_values and hasattr(self.signal_values[cgroup], parent_name): continue
                 self.run_signals([parent_name], [cgroup])
             # Now run the actual signal
             if cgroup in self.signal_values and hasattr(self.signal_values[cgroup], signal_name): continue
             signal_function = self.signals[signal_name]
             parent_values = types.SimpleNamespace()
             for parent_name in parent_names:
                 sig_vals = getattr(self.signal_values[cgroup], parent_name)
                 setattr(parent_values, parent_name, sig_vals)
                 
             # Get indicators needed for this signal
             indicator_values = types.SimpleNamespace()
             for indicator_name in self.signal_indicator_deps[signal_name]:
                 setattr(indicator_values, indicator_name, getattr(self.indicator_values[cgroup], indicator_name))
                 
             signal_output = signal_function(cgroup, self.timestamps, indicator_values, parent_values, self.strategy_context)
             setattr(self.signal_values[cgroup], signal_name, series_to_array(signal_output))
Ejemplo n.º 8
0
 def __init__(self, name: str, 
              bucket_names: Sequence[str],
              bucket_values: Sequence[np.ndarray], 
              display_attributes: DisplayAttributes = None) -> None:
     '''
     Args:
         name: name used for this data in a plot legend
         bucket_names: list of strings used on x axis labels
         bucket_values: list of numpy arrays that are summarized in this plot
     '''
     assert isinstance(bucket_names, list) and isinstance(bucket_values, list) and len(bucket_names) == len(bucket_values)
     self.display_attributes = BoxPlotAttributes()
     self.name = name
     self.bucket_names = bucket_names
     self.bucket_values = series_to_array(bucket_values)
     if display_attributes is None: display_attributes = BoxPlotAttributes()
     self.display_attributes = display_attributes
Ejemplo n.º 9
0
 def add_indicator(self, 
                   name: str, 
                   indicator: IndicatorType, 
                   contract_groups: Sequence[ContractGroup] = None, 
                   depends_on: Sequence[str] = None) -> None:
     '''
     Args:
         name: Name of the indicator
         indicator:  A function that takes strategy timestamps and other indicators and returns a numpy array
           containing indicator values.  The return array must have the same length as the timestamps object.
           Can also be a numpy array or a pandas Series in which case we just store the values.
         contract_groups: Contract groups that this indicator applies to.  If not set, it applies to all contract groups. Default None.
         depends_on: Names of other indicators that we need to compute this indicator. Default None.
     '''
     self.indicators[name] = indicator
     self.indicator_deps[name] = [] if depends_on is None else list(depends_on)
     if contract_groups is None: contract_groups = self.contract_groups
     if isinstance(indicator, np.ndarray) or isinstance(indicator, pd.Series):
         indicator_values = series_to_array(indicator)
         for contract_group in contract_groups:
             setattr(self.indicator_values[contract_group], name, indicator_values)
     self.indicator_cgroups[name] = list(contract_groups)