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