| Conditions | 18 |
| Total Lines | 108 |
| Lines | 0 |
| Ratio | 0 % |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like zipline.utils.run_pipeline() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
| 1 | # |
||
| 146 | def run_pipeline(print_algo=True, **kwargs): |
||
| 147 | """Runs a full zipline pipeline given configuration keyword |
||
| 148 | arguments. |
||
| 149 | |||
| 150 | 1. Load data (start and end dates can be provided a strings as |
||
| 151 | well as the source and symobls). |
||
| 152 | |||
| 153 | 2. Instantiate algorithm (supply either algo_text or algofile |
||
| 154 | kwargs containing initialize() and handle_data() functions). If |
||
| 155 | algofile is supplied, will try to look for algofile_analyze.py and |
||
| 156 | append it. |
||
| 157 | |||
| 158 | 3. Run algorithm (supply capital_base as float). |
||
| 159 | |||
| 160 | 4. Return performance dataframe. |
||
| 161 | |||
| 162 | :Arguments: |
||
| 163 | * print_algo : bool <default=True> |
||
| 164 | Whether to print the algorithm to command line. Will use |
||
| 165 | pygments syntax coloring if pygments is found. |
||
| 166 | |||
| 167 | """ |
||
| 168 | start = kwargs['start'] |
||
| 169 | end = kwargs['end'] |
||
| 170 | # Compare against None because strings/timestamps may have been given |
||
| 171 | if start is not None: |
||
| 172 | start = pd.Timestamp(start, tz='UTC') |
||
| 173 | if end is not None: |
||
| 174 | end = pd.Timestamp(end, tz='UTC') |
||
| 175 | |||
| 176 | # Fail out if only one bound is provided |
||
| 177 | if ((start is None) or (end is None)) and (start != end): |
||
| 178 | raise PipelineDateError(start=start, end=end) |
||
| 179 | |||
| 180 | # Check if start and end are provided, and if the sim_params need to read |
||
| 181 | # a start and end from the DataSource |
||
| 182 | if start is None: |
||
| 183 | overwrite_sim_params = True |
||
| 184 | else: |
||
| 185 | overwrite_sim_params = False |
||
| 186 | |||
| 187 | symbols = kwargs['symbols'].split(',') |
||
| 188 | asset_identifier = kwargs['metadata_index'] |
||
| 189 | |||
| 190 | # Pull asset metadata |
||
| 191 | asset_metadata = kwargs.get('asset_metadata', None) |
||
| 192 | asset_metadata_path = kwargs['metadata_path'] |
||
| 193 | # Read in a CSV file, if applicable |
||
| 194 | if asset_metadata_path is not None: |
||
| 195 | if os.path.isfile(asset_metadata_path): |
||
| 196 | asset_metadata = pd.read_csv(asset_metadata_path, |
||
| 197 | index_col=asset_identifier) |
||
| 198 | |||
| 199 | source_arg = kwargs['source'] |
||
| 200 | source_time_column = kwargs['source_time_column'] |
||
| 201 | |||
| 202 | if source_arg is None: |
||
| 203 | raise NoSourceError() |
||
| 204 | |||
| 205 | elif source_arg == 'yahoo': |
||
| 206 | source = zipline.data.load_bars_from_yahoo( |
||
| 207 | stocks=symbols, start=start, end=end) |
||
| 208 | |||
| 209 | elif os.path.isfile(source_arg): |
||
| 210 | source = zipline.data.load_prices_from_csv( |
||
| 211 | filepath=source_arg, |
||
| 212 | identifier_col=source_time_column |
||
| 213 | ) |
||
| 214 | |||
| 215 | elif os.path.isdir(source_arg): |
||
| 216 | source = zipline.data.load_prices_from_csv_folder( |
||
| 217 | folderpath=source_arg, |
||
| 218 | identifier_col=source_time_column |
||
| 219 | ) |
||
| 220 | |||
| 221 | else: |
||
| 222 | raise NotImplementedError( |
||
| 223 | 'Source %s not implemented.' % kwargs['source']) |
||
| 224 | |||
| 225 | algo_text = kwargs.get('algo_text', None) |
||
| 226 | if algo_text is None: |
||
| 227 | # Expect algofile to be set |
||
| 228 | algo_fname = kwargs['algofile'] |
||
| 229 | with open(algo_fname, 'r') as fd: |
||
| 230 | algo_text = fd.read() |
||
| 231 | |||
| 232 | if print_algo: |
||
| 233 | if PYGMENTS: |
||
| 234 | highlight(algo_text, PythonLexer(), TerminalFormatter(), |
||
| 235 | outfile=sys.stdout) |
||
| 236 | else: |
||
| 237 | print_(algo_text) |
||
| 238 | |||
| 239 | algo = zipline.TradingAlgorithm(script=algo_text, |
||
| 240 | namespace=kwargs.get('namespace', {}), |
||
| 241 | capital_base=float(kwargs['capital_base']), |
||
| 242 | algo_filename=kwargs.get('algofile'), |
||
| 243 | equities_metadata=asset_metadata, |
||
| 244 | start=start, |
||
| 245 | end=end) |
||
| 246 | |||
| 247 | perf = algo.run(source, overwrite_sim_params=overwrite_sim_params) |
||
| 248 | |||
| 249 | output_fname = kwargs.get('output', None) |
||
| 250 | if output_fname is not None: |
||
| 251 | perf.to_pickle(output_fname) |
||
| 252 | |||
| 253 | return perf |
||
| 254 |