def get_broker_realized_pnl(self, current_value, **kwargs): """ Args: current_value: current portfolio value Returns: normalized realized profit/loss for last closed trade (is zero if no pos. closures within last env. step) """ if self.trade_just_closed: pnl = decayed_result( self.trade_result, current_value, self.env.broker.startingcash, self.p.drawdown_call, self.p.target_call, gamma=1 ) # self.log.warning('get_broker_realized_pnl: got result: {} --> pnl: {}'.format(self.trade_result, pnl)) # Reset flag: # self.trade_just_closed = False # print('broker_realized_pnl: step {}, just closed.'.format(self.iteration)) else: pnl = 0.0 return pnl
def update_sliding_stat(self): """ Updates all sliding statistics deques with latest-step values: - normalized broker value - normalized broker cash - normalized exposure (position size) - position duration in steps, normalized wrt. max possible episode steps - exp. scaled episode duration in steps, normalized wrt. max possible episode steps - normalized decayed realized profit/loss for last closed trade (or zero if no trades been closed within last step); - normalized profit/loss for current opened trade (unrealized p/l); - normalized best possible up to present point unrealized result for current opened trade; - normalized worst possible up to present point unrealized result for current opened trade; - one hot encoding for actions received; - rewards received (based on self.reward variable values); """ stat = self.sliding_stat current_value = self.env.broker.get_value() stat['broker_value'].append( norm_value( current_value, self.env.broker.startingcash, self.p.drawdown_call, self.p.target_call, )) stat['broker_cash'].append( norm_value( self.env.broker.get_cash(), self.env.broker.startingcash, 99.0, self.p.target_call, )) stat['exposure'].append( self.position.size / (self.env.broker.startingcash * self.env.broker.get_leverage() + 1e-2)) stat['leverage'].append( self.env.broker.get_leverage()) # TODO: Do we need this? if self.trade_just_closed: stat['realized_pnl'].append( decayed_result(self.trade_result, current_value, self.env.broker.startingcash, self.p.drawdown_call, self.p.target_call, gamma=1)) # Reset flag: self.trade_just_closed = False # print('POS_OBS: step {}, just closed.'.format(self.iteration)) else: stat['realized_pnl'].append(0.0) if self.position.size == 0: self.current_pos_duration = 0 self.current_pos_min_value = current_value self.current_pos_max_value = current_value # print('ZERO_POSITION\n') else: self.current_pos_duration += 1 if self.current_pos_max_value < current_value: self.current_pos_max_value = current_value elif self.current_pos_min_value > current_value: self.current_pos_min_value = current_value stat['pos_duration'].append( self.current_pos_duration / (self.data.numrecords - self.inner_embedding)) stat['episode_step'].append( exp_scale(self.iteration / (self.data.numrecords - self.inner_embedding), gamma=3)) stat['max_unrealized_pnl'].append( (self.current_pos_max_value - self.realized_broker_value) * self.broker_value_normalizer) stat['min_unrealized_pnl'].append( (self.current_pos_min_value - self.realized_broker_value) * self.broker_value_normalizer) stat['unrealized_pnl'].append( (current_value - self.realized_broker_value) * self.broker_value_normalizer) stat['action'].append(self.action_norm(self.last_action)) stat['reward'].append(self.reward)