| Conditions | 7 |
| Total Lines | 132 |
| 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 | import os |
||
| 145 | def test_adjust_forward_fill_minute(self): |
||
| 146 | tempdir = TempDirectory() |
||
| 147 | try: |
||
| 148 | start_day = pd.Timestamp("2013-06-21", tz='UTC') |
||
| 149 | end_day = pd.Timestamp("2013-06-24", tz='UTC') |
||
| 150 | |||
| 151 | env = TradingEnvironment() |
||
| 152 | env.write_data( |
||
| 153 | equities_data={ |
||
| 154 | 0: { |
||
| 155 | 'start_date': start_day, |
||
| 156 | 'end_date': env.next_trading_day(end_day) |
||
| 157 | } |
||
| 158 | } |
||
| 159 | ) |
||
| 160 | |||
| 161 | minutes = env.minutes_for_days_in_range( |
||
| 162 | start=start_day, |
||
| 163 | end=end_day |
||
| 164 | ) |
||
| 165 | |||
| 166 | df = pd.DataFrame({ |
||
| 167 | # 390 bars of real data, then 100 missing bars, then 290 |
||
| 168 | # bars of data again |
||
| 169 | "open": np.array(list(range(0, 390)) + [0] * 100 + |
||
| 170 | list(range(390, 680))) * 1000, |
||
| 171 | "high": np.array(list(range(1000, 1390)) + [0] * 100 + |
||
| 172 | list(range(1390, 1680))) * 1000, |
||
| 173 | "low": np.array(list(range(2000, 2390)) + [0] * 100 + |
||
| 174 | list(range(2390, 2680))) * 1000, |
||
| 175 | "close": np.array(list(range(3000, 3390)) + [0] * 100 + |
||
| 176 | list(range(3390, 3680))) * 1000, |
||
| 177 | "volume": np.array(list(range(4000, 4390)) + [0] * 100 + |
||
| 178 | list(range(4390, 4680))), |
||
| 179 | "minute": minutes |
||
| 180 | }) |
||
| 181 | |||
| 182 | MinuteBarWriterFromDataFrames().write(tempdir.path, {0: df}) |
||
| 183 | |||
| 184 | sim_params = SimulationParameters( |
||
| 185 | period_start=minutes[0], |
||
| 186 | period_end=minutes[-1], |
||
| 187 | data_frequency="minute" |
||
| 188 | ) |
||
| 189 | |||
| 190 | # create a split for 6/24 |
||
| 191 | adjustments_path = os.path.join(tempdir.path, "adjustments.db") |
||
| 192 | writer = SQLiteAdjustmentWriter(adjustments_path, |
||
| 193 | pd.date_range(start=start_day, |
||
| 194 | end=end_day), |
||
| 195 | None) |
||
| 196 | |||
| 197 | splits = pd.DataFrame([{ |
||
| 198 | 'effective_date': int(end_day.value / 1e9), |
||
| 199 | 'ratio': 0.5, |
||
| 200 | 'sid': 0 |
||
| 201 | }]) |
||
| 202 | |||
| 203 | dividend_data = { |
||
| 204 | # Hackery to make the dtypes correct on an empty frame. |
||
| 205 | 'ex_date': np.array([], dtype='datetime64[ns]'), |
||
| 206 | 'pay_date': np.array([], dtype='datetime64[ns]'), |
||
| 207 | 'record_date': np.array([], dtype='datetime64[ns]'), |
||
| 208 | 'declared_date': np.array([], dtype='datetime64[ns]'), |
||
| 209 | 'amount': np.array([], dtype=float), |
||
| 210 | 'sid': np.array([], dtype=int), |
||
| 211 | } |
||
| 212 | dividends = pd.DataFrame( |
||
| 213 | dividend_data, |
||
| 214 | index=pd.DatetimeIndex([], tz='UTC'), |
||
| 215 | columns=['ex_date', |
||
| 216 | 'pay_date', |
||
| 217 | 'record_date', |
||
| 218 | 'declared_date', |
||
| 219 | 'amount', |
||
| 220 | 'sid'] |
||
| 221 | ) |
||
| 222 | |||
| 223 | merger_data = { |
||
| 224 | # Hackery to make the dtypes correct on an empty frame. |
||
| 225 | 'effective_date': np.array([], dtype=int), |
||
| 226 | 'ratio': np.array([], dtype=float), |
||
| 227 | 'sid': np.array([], dtype=int), |
||
| 228 | } |
||
| 229 | mergers = pd.DataFrame( |
||
| 230 | merger_data, |
||
| 231 | index=pd.DatetimeIndex([], tz='UTC') |
||
| 232 | ) |
||
| 233 | |||
| 234 | writer.write(splits, mergers, dividends) |
||
| 235 | |||
| 236 | dp = DataPortal( |
||
| 237 | env, |
||
| 238 | minutes_equities_path=tempdir.path, |
||
| 239 | sim_params=sim_params, |
||
| 240 | adjustment_reader=SQLiteAdjustmentReader(adjustments_path) |
||
| 241 | ) |
||
| 242 | |||
| 243 | # phew, finally ready to start testing. |
||
| 244 | for idx, minute in enumerate(minutes[:390]): |
||
| 245 | for field_idx, field in enumerate(["open", "high", "low", |
||
| 246 | "close", "volume"]): |
||
| 247 | self.assertEqual( |
||
| 248 | dp.get_spot_value(0, field, dt=minute), |
||
| 249 | idx + (1000 * field_idx) |
||
| 250 | ) |
||
| 251 | |||
| 252 | for idx, minute in enumerate(minutes[390:490]): |
||
| 253 | # no actual data for this part, so we'll forward-fill. |
||
| 254 | # make sure the forward-filled values are adjusted. |
||
| 255 | for field_idx, field in enumerate(["open", "high", "low", |
||
| 256 | "close"]): |
||
| 257 | self.assertEqual( |
||
| 258 | dp.get_spot_value(0, field, dt=minute), |
||
| 259 | (389 + (1000 * field_idx)) / 2.0 |
||
| 260 | ) |
||
| 261 | |||
| 262 | self.assertEqual( |
||
| 263 | dp.get_spot_value(0, "volume", dt=minute), |
||
| 264 | 8778 # 4389 * 2 |
||
| 265 | ) |
||
| 266 | |||
| 267 | for idx, minute in enumerate(minutes[490:]): |
||
| 268 | # back to real data |
||
| 269 | for field_idx, field in enumerate(["open", "high", "low", |
||
| 270 | "close", "volume"]): |
||
| 271 | self.assertEqual( |
||
| 272 | dp.get_spot_value(0, field, dt=minute), |
||
| 273 | (390 + idx + (1000 * field_idx)) |
||
| 274 | ) |
||
| 275 | finally: |
||
| 276 | tempdir.cleanup() |
||
| 277 | |||
| 337 |