def __init__(self): super(trading_vix_env, self).__init__() self.action_space = spaces.box.Box( low=0, #no position high=1, #all in stock shape=(1, ), dtype=np.float32) high_observation = np.asarray([999, 999, 999, 999, 999, 999]) self.observation_space = spaces.box.Box(low=-1 * high_observation, high=high_observation, dtype=np.float32) self.seed() self.max_trajectory_length = 201 #load data index_data = pd.read_csv("^VIX.csv") index_data = index_data.rename(columns = {"Date":"Date",\ "Open":"index_open",\ "High":'index_high',\ 'Low':'index_low',\ 'Close':'index_close',\ 'Adj Close':'index_adj_close',\ 'Volume':'index_volume'}) vix_price_data = pd.read_csv('VIXY.csv') vix_price_data = vix_price_data.rename(columns = {"Date":"Date",\ "Open":"vix_price_open",\ "High":'vix_price_high',\ 'Low':'vix_price_low',\ 'Close':'vix_price_close',\ 'Adj Close':'vix_price_adj_close',\ 'Volume':'vix_price_volume'}) total_data = pd.merge(index_data, vix_price_data, on="Date", how='inner') #build features #compute the exponential moving average mv_10 = total_data['index_adj_close'].ewm(span=10).mean() mv_20 = total_data['index_adj_close'].ewm(span=20).mean() mv_30 = total_data['index_adj_close'].ewm(span=30).mean() mv_50 = total_data['index_adj_close'].ewm(span=50).mean() mv_100 = total_data['index_adj_close'].ewm(span=100).mean() spot_to_mv_10 = total_data['index_adj_close'] / mv_10 spot_to_mv_20 = total_data['index_adj_close'] / mv_20 spot_to_mv_30 = total_data['index_adj_close'] / mv_30 spot_to_mv_50 = total_data['index_adj_close'] / mv_50 spot_to_mv_100 = total_data['index_adj_close'] / mv_100 vix_measure = spot_to_mv_10 + spot_to_mv_20 + spot_to_mv_30 + spot_to_mv_50 + spot_to_mv_100 vix_measure_list = vix_measure.tolist() index_feature_dataframe = pd.DataFrame() index_feature_dataframe['vix_price_adj_close'] = total_data[ 'vix_price_adj_close'][1:] #[1:] for matching counting_days index_feature_dataframe['vix_adj_close'] = total_data[ 'index_adj_close'][1:] index_feature_dataframe['mv_ratio'] = vix_measure_list[1:] threshold_list = [5, 6, 7] for threshold in threshold_list: counting_days = trading_vix_utils.day_counter_helper( vix_measure_list, threshold) index_feature_dataframe['days_since_' + str(threshold)] = counting_days index_feature_dataframe = index_feature_dataframe.iloc[ -1000:] #there may be a vix regime change in 2018/1?? index_feature_dataframe = index_feature_dataframe.reset_index( drop=True) self.index_feature_dataframe = index_feature_dataframe #other variables self.current_time_index = None self.quantity = None self.cash = None self.min_transaction_value = None self.buy_and_hold_stock_quantity = None self.current_portfolio_value = None self.current_trajectory_length = None self.has_at_least_one_sell = None
def __init__(self, threshold_list=[5, 6, 7]): seed_index = 0 np.random.seed(seed_index) random.seed(seed_index) #load data index_data = pd.read_csv("^VIX.csv") index_data = index_data.rename(columns = {"Date":"Date",\ "Open":"index_open",\ "High":'index_high',\ 'Low':'index_low',\ 'Close':'index_close',\ 'Adj Close':'index_adj_close',\ 'Volume':'index_volume'}) vix_price_data = pd.read_csv('VIXY.csv') vix_price_data = vix_price_data.rename(columns = {"Date":"Date",\ "Open":"vix_price_open",\ "High":'vix_price_high',\ 'Low':'vix_price_low',\ 'Close':'vix_price_close',\ 'Adj Close':'vix_price_adj_close',\ 'Volume':'vix_price_volume'}) total_data = pd.merge(index_data, vix_price_data, on="Date", how='inner') #build features #compute the exponential moving average mv_10 = total_data['index_adj_close'].ewm(span=10).mean() mv_20 = total_data['index_adj_close'].ewm(span=20).mean() mv_30 = total_data['index_adj_close'].ewm(span=30).mean() mv_50 = total_data['index_adj_close'].ewm(span=50).mean() mv_100 = total_data['index_adj_close'].ewm(span=100).mean() spot_to_mv_10 = total_data['index_adj_close'] / mv_10 spot_to_mv_20 = total_data['index_adj_close'] / mv_20 spot_to_mv_30 = total_data['index_adj_close'] / mv_30 spot_to_mv_50 = total_data['index_adj_close'] / mv_50 spot_to_mv_100 = total_data['index_adj_close'] / mv_100 vix_measure = spot_to_mv_10 + spot_to_mv_20 + spot_to_mv_30 + spot_to_mv_50 + spot_to_mv_100 vix_measure_list = vix_measure.tolist() index_feature_dataframe = pd.DataFrame() index_feature_dataframe['vix_price_adj_close'] = total_data[ 'vix_price_adj_close'][1:] #[1:] for matching counting_days index_feature_dataframe['vix_adj_close'] = total_data[ 'index_adj_close'][1:] index_feature_dataframe['mv_ratio'] = vix_measure_list[1:] threshold_list = [5, 6, 7] for threshold in threshold_list: counting_days = trading_vix_utils.day_counter_helper( vix_measure_list, threshold) index_feature_dataframe['days_since_' + str(threshold)] = counting_days index_feature_dataframe = index_feature_dataframe.iloc[ -1000:] #there may be a vix regime change in 2018/1?? index_feature_dataframe = index_feature_dataframe.reset_index( drop=True) self.index_feature_dataframe = index_feature_dataframe #other variables self.current_time_index = None self.quantity = None self.cash = None self.min_transaction_value = None self.buy_and_hold_stock_quantity = None self.current_portfolio_value = None