| Conditions | 5 |
| Total Lines | 107 |
| 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:
| 1 | from zipline.errors import ( |
||
| 65 | def _initialize_precalculated_series(self, sid, env, trading_days, |
||
| 66 | data_portal): |
||
| 67 | """ |
||
| 68 | Internal method that precalculates the benchmark return series for |
||
| 69 | use in the simulation. |
||
| 70 | |||
| 71 | Parameters |
||
| 72 | ---------- |
||
| 73 | sid: (int) Asset to use |
||
| 74 | |||
| 75 | env: TradingEnvironment |
||
| 76 | |||
| 77 | trading_days: pd.DateTimeIndex |
||
| 78 | |||
| 79 | data_portal: DataPortal |
||
| 80 | |||
| 81 | Notes |
||
| 82 | ----- |
||
| 83 | If the benchmark asset started trading after the simulation start, |
||
| 84 | or finished trading before the simulation end, exceptions are raised. |
||
| 85 | |||
| 86 | If the benchmark asset started trading the same day as the simulation |
||
| 87 | start, the first available minute price on that day is used instead |
||
| 88 | of the previous close. |
||
| 89 | |||
| 90 | We use history to get an adjusted price history for each day's close, |
||
| 91 | as of the look-back date (the last day of the simulation). Prices are |
||
| 92 | fully adjusted for dividends, splits, and mergers. |
||
| 93 | |||
| 94 | Returns |
||
| 95 | ------- |
||
| 96 | A pd.Series, indexed by trading day, whose values represent the % |
||
| 97 | change from close to close. |
||
| 98 | """ |
||
| 99 | if sid is None: |
||
| 100 | # get benchmark info from trading environment, which defaults to |
||
| 101 | # downloading data from Yahoo. |
||
| 102 | daily_series = \ |
||
| 103 | env.benchmark_returns[trading_days[0]:trading_days[-1]] |
||
| 104 | |||
| 105 | if self.emission_rate == "minute": |
||
| 106 | # we need to take the env's benchmark returns, which are daily, |
||
| 107 | # and resample them to minute |
||
| 108 | minutes = env.minutes_for_days_in_range( |
||
| 109 | start=trading_days[0], |
||
| 110 | end=trading_days[-1] |
||
| 111 | ) |
||
| 112 | |||
| 113 | minute_series = daily_series.reindex( |
||
| 114 | index=minutes, |
||
| 115 | method="ffill" |
||
| 116 | ) |
||
| 117 | |||
| 118 | return minute_series |
||
| 119 | else: |
||
| 120 | return daily_series |
||
| 121 | elif self.emission_rate == "minute": |
||
| 122 | minutes = env.minutes_for_days_in_range(self.trading_days[0], |
||
| 123 | self.trading_days[-1]) |
||
| 124 | benchmark_series = data_portal.get_history_window( |
||
| 125 | [sid], |
||
| 126 | minutes[-1], |
||
| 127 | bar_count=len(minutes) + 1, |
||
| 128 | frequency="1m", |
||
| 129 | field="price", |
||
| 130 | ffill=True |
||
| 131 | ) |
||
| 132 | |||
| 133 | return benchmark_series.pct_change()[1:] |
||
| 134 | else: |
||
| 135 | try: |
||
| 136 | # get the window of close prices for benchmark_sid from the |
||
| 137 | # last trading day of the simulation, going up to one day |
||
| 138 | # before the simulation start day (so that we can get the % |
||
| 139 | # change on day 1) |
||
| 140 | benchmark_series = data_portal.get_history_window( |
||
| 141 | [sid], |
||
| 142 | trading_days[-1], |
||
| 143 | bar_count=len(trading_days) + 1, |
||
| 144 | frequency="1d", |
||
| 145 | field="price", |
||
| 146 | ffill=True |
||
| 147 | )[sid] |
||
| 148 | return benchmark_series.pct_change()[1:] |
||
| 149 | except NoDataOnDate: |
||
| 150 | # Attempt to handle case where stock data starts on first |
||
| 151 | # day, in this case use the open to close return. |
||
| 152 | benchmark_series = data_portal.get_history_window( |
||
| 153 | [sid], |
||
| 154 | trading_days[-1], |
||
| 155 | bar_count=len(trading_days), |
||
| 156 | frequency="1d", |
||
| 157 | field="price", |
||
| 158 | ffill=True |
||
| 159 | )[sid] |
||
| 160 | |||
| 161 | # get a minute history window of the first day |
||
| 162 | first_open = data_portal.get_spot_value( |
||
| 163 | sid, 'open', trading_days[0]) |
||
| 164 | first_close = data_portal.get_spot_value( |
||
| 165 | sid, 'close', trading_days[0]) |
||
| 166 | |||
| 167 | first_day_return = (first_close - first_open) / first_open |
||
| 168 | |||
| 169 | returns = benchmark_series.pct_change()[:] |
||
| 170 | returns[0] = first_day_return |
||
| 171 | return returns |
||
| 172 |