src/environment/portfolio.py
def _step(self, action):
        """
        Step the env.
        Actions should be portfolio [w0...]
        - Where wn is a portfolio weight from 0 to 1. The first is cash_bias
        - cn is the portfolio conversion weights see PortioSim._step for description
        """
        np.testing.assert_almost_equal(
            action.shape,
            (len(self.sim.asset_names) + 1,)
        )

        # normalise just in case
        action = np.clip(action, 0, 1)

        weights = action  # np.array([cash_bias] + list(action))  # [w0, w1...]
        weights /= (weights.sum() + eps)
        weights[0] += np.clip(1 - weights.sum(), 0, 1)  # so if weights are all zeros we normalise to [1,0...]

        assert ((action >= 0) * (action <= 1)).all(), 'all action values should be between 0 and 1. Not %s' % action
        np.testing.assert_almost_equal(
            np.sum(weights), 1.0, 3, err_msg='weights should sum to 1. action="%s"' % weights)

        observation, done1, ground_truth_obs = self.src._step()

        # concatenate observation with ones
        cash_observation = np.ones((1, self.window_length, observation.shape[2]))
        observation = np.concatenate((cash_observation, observation), axis=0)

        cash_ground_truth = np.ones((1, 1, ground_truth_obs.shape[2]))
        ground_truth_obs = np.concatenate((cash_ground_truth, ground_truth_obs), axis=0)

        # relative price vector of last observation day (close/open)
        close_price_vector = observation[:, -1, 3]
        open_price_vector = observation[:, -1, 0]
        y1 = close_price_vector / open_price_vector
        reward, info, done2 = self.sim._step(weights, y1)

        # calculate return for buy and hold a bit of each asset
        info['market_value'] = np.cumprod([inf["return"] for inf in self.infos + [info]])[-1]
        # add dates
        info['date'] = index_to_date(self.start_idx + self.src.idx + self.src.step)
        info['steps'] = self.src.step
        info['next_obs'] = ground_truth_obs

        self.infos.append(info)

        return observation, reward, done1 or done2, info

        Step the env.
        Actions should be portfolio [w0...]
        - Where wn is a portfolio weight from 0 to 1. The first is cash_bias
        - cn is the portfolio conversion weights see PortioSim._step for description
        
Step the env.
Actions should be portfolio [w0...]
Where wn is a portfolio weight from 0 to 1. The first is cash_bias
cn is the portfolio conversion weights see PortioSim._step for description
Step the env . Actions should be portfolio [ w0 ... ] Where wn is a portfolio weight from 0 to 1 . The first is cash_bias cn is the portfolio conversion weights see PortioSim . _step for description
def _step(self, action):
        np.testing.assert_almost_equal(
            action.shape,
            (len(self.sim.asset_names) + 1,)
        )
        action = np.clip(action, 0, 1)
        weights = action  
        weights /= (weights.sum() + eps)
        weights[0] += np.clip(1 - weights.sum(), 0, 1)  
        assert ((action >= 0) * (action <= 1)).all(), 'all action values should be between 0 and 1. Not %s' % action
        np.testing.assert_almost_equal(
            np.sum(weights), 1.0, 3, err_msg='weights should sum to 1. action="%s"' % weights)
        observation, done1, ground_truth_obs = self.src._step()
        cash_observation = np.ones((1, self.window_length, observation.shape[2]))
        observation = np.concatenate((cash_observation, observation), axis=0)
        cash_ground_truth = np.ones((1, 1, ground_truth_obs.shape[2]))
        ground_truth_obs = np.concatenate((cash_ground_truth, ground_truth_obs), axis=0)
        close_price_vector = observation[:, -1, 3]
        open_price_vector = observation[:, -1, 0]
        y1 = close_price_vector / open_price_vector
        reward, info, done2 = self.sim._step(weights, y1)
        info['market_value'] = np.cumprod([inf["return"] for inf in self.infos + [info]])[-1]
        info['date'] = index_to_date(self.start_idx + self.src.idx + self.src.step)
        info['steps'] = self.src.step
        info['next_obs'] = ground_truth_obs
        self.infos.append(info)
        return observation, reward, done1 or done2, info
def _step ( self , action ) : np . testing . assert_almost_equal ( action . shape , ( len ( self . sim . asset_names ) + 1 , ) ) action = np . clip ( action , 0 , 1 ) weights = action weights /= ( weights . sum ( ) + eps ) weights [ 0 ] += np . clip ( 1 - weights . sum ( ) , 0 , 1 ) assert ( ( action >= 0 ) * ( action <= 1 ) ) . all ( ) , 'all action values should be between 0 and 1. Not %s' % action np . testing . assert_almost_equal ( np . sum ( weights ) , 1.0 , 3 , err_msg = 'weights should sum to 1. action="%s"' % weights ) observation , done1 , ground_truth_obs = self . src . _step ( ) cash_observation = np . ones ( ( 1 , self . window_length , observation . shape [ 2 ] ) ) observation = np . concatenate ( ( cash_observation , observation ) , axis = 0 ) cash_ground_truth = np . ones ( ( 1 , 1 , ground_truth_obs . shape [ 2 ] ) ) ground_truth_obs = np . concatenate ( ( cash_ground_truth , ground_truth_obs ) , axis = 0 ) close_price_vector = observation [ : , - 1 , 3 ] open_price_vector = observation [ : , - 1 , 0 ] y1 = close_price_vector / open_price_vector reward , info , done2 = self . sim . _step ( weights , y1 ) info [ 'market_value' ] = np . cumprod ( [ inf [ "return" ] for inf in self . infos + [ info ] ] ) [ - 1 ] info [ 'date' ] = index_to_date ( self . start_idx + self . src . idx + self . src . step ) info [ 'steps' ] = self . src . step info [ 'next_obs' ] = ground_truth_obs self . infos . append ( info ) return observation , reward , done1 or done2 , info
Step the env.
Step the env .
"""
        Step the env.
        Actions should be portfolio [w0...]
        - Where wn is a portfolio weight from 0 to 1. The first is cash_bias
        - cn is the portfolio conversion weights see PortioSim._step for description
        """ # normalise just in case # np.array([cash_bias] + list(action))  # [w0, w1...] # so if weights are all zeros we normalise to [1,0...] # concatenate observation with ones # relative price vector of last observation day (close/open) # calculate return for buy and hold a bit of each asset # add dates
self
action