Esempio n. 1
0
    def get_market_state(self):
        T = 2e3  # EURUSD
        T2 = 2e3

        if False:
            x_p = np.stack([
                np.frombuffer(self.channel_dO.get(size=self.time_dim)),
                np.frombuffer(self.channel_dH.get(size=self.time_dim)),
                np.frombuffer(self.channel_dL.get(size=self.time_dim)),
            ],
                           axis=-1)
            x_p = tanh(x_p * T)

        x_sma = np.stack([
            np.frombuffer(self.data.sma_16.get(size=self.time_dim)),
            np.frombuffer(self.data.sma_32.get(size=self.time_dim)),
            np.frombuffer(self.data.sma_64.get(size=self.time_dim)),
            np.frombuffer(self.data.sma_128.get(size=self.time_dim)),
        ],
                         axis=-1)
        # Gradient along features axis:
        x_sma = np.gradient(x_sma, axis=1) * T2

        # Log-scale:
        #x_sma = log_transform(x_sma)
        x_sma = tanh(x_sma)

        #x = np.concatenate([x_p, x_sma], axis=-1)
        x = x_sma

        return x[:, None, :]
Esempio n. 2
0
    def get_external_state(self):
        x_sma = np.stack([
            np.frombuffer(self.data.sma_16.get(size=self.time_dim)),
            np.frombuffer(self.data.sma_32.get(size=self.time_dim)),
            np.frombuffer(self.data.sma_64.get(size=self.time_dim)),
            np.frombuffer(self.data.sma_128.get(size=self.time_dim)),
            np.frombuffer(self.data.sma_256.get(size=self.time_dim)),
        ],
                         axis=-1)
        # Gradient along features axis:
        diff = np.gradient(x_sma, axis=-1) * self.p.state_ext_scale
        diff = tanh(diff)
        avg = np.gradient(x_sma, axis=0) * self.p.state_ext_scale
        avg = tanh(avg)

        return {'avg': avg[:, None, :], 'diff': diff[:, None, :]}
Esempio n. 3
0
    def get_single_external_state(self, key):
        # Use Hi-Low median as signal:
        x = (np.frombuffer(self.data_streams[key].high.get(size=self.time_dim))
             + np.frombuffer(
                 self.data_streams[key].low.get(size=self.time_dim))) / 2

        # Differences along time dimension:
        d_x = np.gradient(x, axis=0) * self.p.cwt_signal_scale

        # Compute continuous wavelet transform using Ricker wavelet:
        cwt_x = signal.cwt(d_x, signal.ricker, self.cwt_width).T

        norm_x = cwt_x

        # Note: differences taken once again along channels axis,
        # apply weighted scaling to normalize channels
        # norm_x = np.gradient(cwt_x, axis=-1)
        # norm_x = zscore(norm_x, axis=0) * self.p.state_ext_scale
        norm_x *= self.p.state_ext_scale[key]

        out_x = tanh(norm_x)

        # out_x = np.clip(norm_x, -10, 10)

        # return out_x[:, None, :]
        return out_x[:, None, :]
Esempio n. 4
0
    def get_external_2_state(self):
        x = np.stack([
            np.frombuffer(self.data.high.get(size=self.time_dim)),
            np.frombuffer(self.data.open.get(size=self.time_dim)),
            np.frombuffer(self.data.low.get(size=self.time_dim)),
            np.frombuffer(self.data.close.get(size=self.time_dim)),
        ],
                     axis=-1)
        # # Differences along features dimension:
        d_x = np.gradient(x, axis=-1) * self.p.cwt_signal_scale

        # Compute continuous wavelet transform using Ricker wavelet:
        # cwt_x = signal.cwt(d_x, signal.ricker, self.cwt_width).T

        norm_x = d_x

        # Note: differences taken once again along channels axis,
        # apply weighted scaling to normalize channels
        # norm_x = np.gradient(cwt_x, axis=-1)
        # norm_x = zscore(norm_x, axis=0) * self.p.state_ext_scale
        # norm_x *= self.p.state_ext_scale

        out_x = tanh(norm_x)

        # out_x = np.clip(norm_x, -10, 10)

        return out_x[:, None, :]
Esempio n. 5
0
    def get_external_state(self):

        x_sma = np.stack([
            feature.get(size=self.time_dim) for feature in self.data.features
        ],
                         axis=-1)
        # Gradient along features axis:
        dx = np.gradient(x_sma, axis=-1) * self.p.state_ext_scale

        # In [-1,1]:
        x = tanh(dx)
        return x[:, None, :]
Esempio n. 6
0
 def get_broker_state(self):
     T_b = 1
     x_broker = np.concatenate([
         np.asarray(self.sliding_stat['broker_value'])[..., None],
         np.asarray(self.sliding_stat['unrealized_pnl'])[..., None],
         np.asarray(self.sliding_stat['realized_pnl'])[..., None],
         np.asarray(self.sliding_stat['broker_cash'])[..., None],
         np.asarray(self.sliding_stat['exposure'])[..., None],
     ],
                               axis=-1)
     x_broker = tanh(np.gradient(x_broker, axis=-1) * T_b)
     return x_broker[..., None]
Esempio n. 7
0
 def get_internal_state(self):
     x_broker = np.concatenate([
         np.asarray(self.sliding_stat['broker_value'])[..., None],
         np.asarray(self.sliding_stat['unrealized_pnl'])[..., None],
         np.asarray(self.sliding_stat['realized_pnl'])[..., None],
         np.asarray(self.sliding_stat['broker_cash'])[..., None],
         np.asarray(self.sliding_stat['exposure'])[..., None],
     ],
                               axis=-1)
     x_broker = tanh(
         np.gradient(x_broker, axis=-1) * self.p.state_int_scale)
     return x_broker[:, None, :]
Esempio n. 8
0
    def get_external_state(self):

        x = np.stack([
            np.frombuffer(self.channel_O.get(size=self.time_dim)),
            np.frombuffer(self.channel_H.get(size=self.time_dim)),
            np.frombuffer(self.channel_L.get(size=self.time_dim)),
        ],
                     axis=-1)
        # Amplify and squash in [-1,1], seems to be best option as of 4.10.17:
        # `self.p.state_ext_scale` param is supposed to keep most of the signal
        # in 'linear' part of tanh while squashing spikes.
        x_market = tanh(x * self.p.state_ext_scale)

        return x_market[:, None, :]
Esempio n. 9
0
    def get_external_state(self):

        x_sma = np.stack([
            np.frombuffer(self.data.sma_16.get(size=self.time_dim)),
            np.frombuffer(self.data.sma_32.get(size=self.time_dim)),
            np.frombuffer(self.data.sma_64.get(size=self.time_dim)),
            np.frombuffer(self.data.sma_128.get(size=self.time_dim)),
            np.frombuffer(self.data.sma_256.get(size=self.time_dim)),
        ],
                         axis=-1)
        # Gradient along features axis:
        dx = np.gradient(x_sma, axis=-1) * self.p.state_ext_scale

        x = tanh(dx)

        return x[:, None, :]
Esempio n. 10
0
    def get_state(self):

        T = 2e3  # EURUSD
        # T = 1e2 # EURUSD, Z-norm
        # T = 1 # BTCUSD

        x = np.stack(
            [
                np.frombuffer(self.channel_O.get(size=self.dim_time)),
                np.frombuffer(self.channel_H.get(size=self.dim_time)),
                np.frombuffer(self.channel_L.get(size=self.dim_time)),
            ],
            axis=-1
        )
        # Log-scale: NOT used. Seems to hurt performance.
        # x = log_transform(x)

        # Amplify and squash in [-1,1], seems to be best option as of 4.10.17:
        # T param is supposed to keep most of the signal in 'linear' part of tanh while squashing spikes.
        x_market = tanh(x * T)

        # Update inner state statistic and compose state:
        self.update_sliding_stat()

        x_broker = np.concatenate(
            [
                np.asarray(self.sliding_stat['unrealized_pnl'])[..., None],
                # max_unrealized_pnl[..., None],
                # min_unrealized_pnl[..., None],
                np.asarray(self.sliding_stat['realized_pnl'])[..., None],
                np.asarray(self.sliding_stat['broker_value'])[..., None],
                np.asarray(self.sliding_stat['broker_cash'])[..., None],
                np.asarray(self.sliding_stat['exposure'])[..., None],
                # norm_episode_duration, gamma=5)[...,None],
                # norm_position_duration, gamma=2)[...,None],
            ],
            axis=-1
        )

        self.state['external'] = x_market[:, None, :]
        self.state['internal'] = x_broker[:, None, :]

        return self.state
Esempio n. 11
0
    def get_market_state(self):
        T = 2e3  # EURUSD
        # T = 1e2 # EURUSD, Z-norm
        # T = 1 # BTCUSD

        x = np.stack([
            np.frombuffer(self.channel_O.get(size=self.time_dim)),
            np.frombuffer(self.channel_H.get(size=self.time_dim)),
            np.frombuffer(self.channel_L.get(size=self.time_dim)),
        ],
                     axis=-1)
        # Log-scale: NOT used. Seems to hurt performance.
        # x = log_transform(x)

        # Amplify and squash in [-1,1], seems to be best option as of 4.10.17:
        # T param is supposed to keep most of the signal in 'linear' part of tanh while squashing spikes.
        x_market = tanh(x * T)

        return x_market[:, None, :]
Esempio n. 12
0
    def get_internal_state(self):

        x_broker = np.concatenate(
            [
                np.asarray(self.broker_stat['value'])[..., None],
                np.asarray(self.broker_stat['unrealized_pnl'])[..., None],
                np.asarray(self.broker_stat['realized_pnl'])[..., None],
                np.asarray(self.broker_stat['cash'])[..., None],
                np.asarray(self.broker_stat['exposure'])[..., None],
                np.asarray(self.broker_stat['pos_direction'])[..., None],

                # np.asarray(self.broker_stat['value'])[-self.p.skip_frame:, None],
                # np.asarray(self.broker_stat['unrealized_pnl'])[-self.p.skip_frame:, None],
                # np.asarray(self.broker_stat['realized_pnl'])[-self.p.skip_frame:, None],
                # np.asarray(self.broker_stat['cash'])[-self.p.skip_frame:, None],
                # np.asarray(self.broker_stat['exposure'])[-self.p.skip_frame:, None],
                # np.asarray(self.broker_stat['pos_direction'])[-self.p.skip_frame:, None],
            ],
            axis=-1)
        x_broker = tanh(
            np.gradient(x_broker, axis=-1) * self.p.state_int_scale)
        # return x_broker[:, None, :]
        return np.clip(x_broker[:, None, :], -2, 2)