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