INNER CODE UNIT · Python
lookback
RichardS0268/CNN-for-Trading · dataset.py:46
lookback = image_size[1]//3
for d in range(lookback-1, len(tabular_df)):
# random skip some trading dates
if np.random.rand(1) > sample_rate:
continue
# skip dates before start_date
if tabular_df.iloc[d]['date'] < start_date:
continue
price_slice = tabular_df[d-(lookback-1):d+1][['open', 'high', 'low', 'close']+ind_names].reset_index(drop=True)
volume_slice = tabular_df[d-(lookback-1):d+1][['volume']].reset_index(drop=True)
# number of no transactions days > 0.2*look back days
if (1.0*(price_slice[['open', 'high', 'low', 'close']].sum(axis=1)/price_slice['open'] == 4)).sum() > lookback//5:
continue
valid_dates.append(tabular_df.iloc[d]['date']) # trading dates surviving the validation