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