Beispiel #1
0
    def __init__(self,
                 df,
                 initial_balance=10000,
                 commission=0.0025,
                 reward_func='sortino',
                 **kwargs):
        super(BitcoinTradingEnv, self).__init__()

        self.initial_balance = initial_balance
        self.commission = commission
        self.reward_func = reward_func

        self.df = df.fillna(method='bfill').reset_index()
        self.stationary_df = log_and_difference(
            self.df,
            ['Open', 'High', 'Low', 'Close', 'Volume BTC', 'Volume USD'])

        benchmarks = kwargs.get('benchmarks', [])
        self.benchmarks = [
            {
                'label':
                'Buy and HODL',
                'values':
                buy_and_hodl(self.df['Close'], initial_balance, commission)
            },
            {
                'label':
                'RSI Divergence',
                'values':
                rsi_divergence(self.df['Close'], initial_balance, commission)
            },
            {
                'label':
                'SMA Crossover',
                'values':
                sma_crossover(self.df['Close'], initial_balance, commission)
            },
            *benchmarks,
        ]

        self.forecast_len = kwargs.get('forecast_len', 10)
        self.confidence_interval = kwargs.get('confidence_interval', 0.95)
        self.obs_shape = (1, 5 + len(self.df.columns) - 2 +
                          (self.forecast_len * 3))

        # Actions of the format Buy 1/4, Sell 3/4, Hold (amount ignored), etc.
        self.action_space = spaces.Discrete(12)

        # Observes the price action, indicators, account action, price forecasts
        self.observation_space = spaces.Box(low=0,
                                            high=1,
                                            shape=self.obs_shape,
                                            dtype=np.float16)
Beispiel #2
0
from util.benchmarks import buy_and_hodl, rsi_divergence, sma_crossover

df = pd.read_csv('./data/wdo_small.csv')
# df = df.drop(['Symbol'], axis=1)
df = df.sort_values(['Date'])

test_len = int(len(df) * 0.2)
train_len = int(len(df)) - test_len

test_df = df[train_len:]
test_df = test_df.fillna(method='bfill').reset_index()

initial_balance = 10000
commission = 0.0025

buy_and_hodl_net_worths = buy_and_hodl(test_df['Close'], initial_balance,
                                       commission)
rsi_divergence_net_worths = rsi_divergence(test_df['Close'], initial_balance,
                                           commission)
sma_crossover_net_worths = sma_crossover(test_df['Close'], initial_balance,
                                         commission)

with open('./research/results/buy_and_hodl_net_worths.pkl', 'wb') as handle:
    pickle.dump(buy_and_hodl_net_worths, handle)

with open('./research/results/rsi_divergence_net_worths.pkl', 'wb') as handle:
    pickle.dump(rsi_divergence_net_worths, handle)

with open('./research/results/sma_crossover_net_worths.pkl', 'wb') as handle:
    pickle.dump(sma_crossover_net_worths, handle)
    def __init__(self,
                 df,
                 initial_balance=1,
                 commission=0.002,
                 close_key='close_price',
                 date_key='timestamp_ms',
                 annualization=365 * 24 * 12,
                 max_leverage=4,
                 training=True,
                 max_steps=None,
                 is_cnn=True,
                 verbose=False,
                 **kwargs):
        super(MarginTradingEnv, self).__init__()

        self.initial_balance = initial_balance
        self.commission = commission
        self.annualization = annualization
        self.max_leverage = max_leverage
        self.close_key = close_key
        self.date_key = date_key
        self.position = 0.1
        self.training = training
        self.max_steps = max_steps
        self.is_cnn = is_cnn
        self.verbose = verbose

        self.reward_func = kwargs.get('reward_func', 'sortino')
        self.window_size = kwargs.get('window_size', 20)
        self.account_history_size = kwargs.get('account_history_size', 3)
        self.action_type = kwargs.get('action_type', 'discrete_nl_3')
        self.obs_type = kwargs.get('obs_type', 2)

        # Actions of the format -1=Short 1=Long
        if self.action_type == "continuous":
            self.action_space = spaces.Box(low=-1,
                                           high=1,
                                           shape=(1, ),
                                           dtype=np.float32)
        elif self.action_type == "discrete_20":
            self.action_space = spaces.Discrete(20)
        elif self.action_type == "discrete_5":
            self.action_space = spaces.Discrete(5)
        elif self.action_type == "discrete_3":
            self.action_space = spaces.Discrete(3)
        elif self.action_type == "discrete_nl_3":
            self.action_space = spaces.Discrete(3)

        try:
            self.df = df[['close_price', 'high_price',
                          'all_volume']].reset_index()
            self.stationary_df = df.drop(
                columns=['close_price', 'high_price',
                         'all_volume'], axis=1).reset_index()
        except Exception as e:
            print(df.columns)
            print(e)

        benchmarks = kwargs.get('benchmarks', [])
        self.benchmarks = [
            {
                'label':
                'Buy and HODL',
                'values':
                buy_and_hodl(self.df[self.close_key], initial_balance,
                             commission)
            },
            {
                'label':
                'RSI Divergence',
                'values':
                rsi_divergence(self.df[self.close_key], initial_balance,
                               commission)
            },
            {
                'label':
                'SMA Crossover',
                'values':
                sma_crossover(self.df[self.close_key], initial_balance,
                              commission)
            },
            *benchmarks,
        ]

        self.obs_shape = (1, (8 * self.account_history_size) +
                          (len(self.stationary_df.columns) * self.window_size))

        # Observes the price action, indicators, account action, price forecasts
        self.observation_space = spaces.Box(low=-1,
                                            high=1,
                                            shape=self.obs_shape,
                                            dtype=np.float16)
        self.scaler = preprocessing.MinMaxScaler(feature_range=(-1, 1))