def __core_dict(self): pos_stats = calc_position_stats(self.position_tracker) period_stats = calc_period_stats(pos_stats, self.ending_cash) rval = { 'ending_value': self.ending_value, 'ending_exposure': self.ending_exposure, # this field is renamed to capital_used for backward # compatibility. 'capital_used': self.period_cash_flow, 'starting_value': self.starting_value, 'starting_exposure': self.starting_exposure, 'starting_cash': self.starting_cash, 'ending_cash': self.ending_cash, 'portfolio_value': self.ending_cash + self.ending_value, 'pnl': self.pnl, 'returns': self.returns, 'period_open': self.period_open, 'period_close': self.period_close, 'gross_leverage': period_stats.gross_leverage, 'net_leverage': period_stats.net_leverage, 'short_exposure': pos_stats.short_exposure, 'long_exposure': pos_stats.long_exposure, 'short_value': pos_stats.short_value, 'long_value': pos_stats.long_value, 'longs_count': pos_stats.longs_count, 'shorts_count': pos_stats.shorts_count, } return rval
def as_account(self): account = self._account_store pt = self.position_tracker pos_stats = calc_position_stats(pt) period_stats = calc_period_stats(pos_stats, self.ending_cash) # If no attribute is found on the PerformancePeriod resort to the # following default values. If an attribute is found use the existing # value. For instance, a broker may provide updates to these # attributes. In this case we do not want to over write the broker # values with the default values. account.settled_cash = \ getattr(self, 'settled_cash', self.ending_cash) account.accrued_interest = \ getattr(self, 'accrued_interest', 0.0) account.buying_power = \ getattr(self, 'buying_power', float('inf')) account.equity_with_loan = \ getattr(self, 'equity_with_loan', self.ending_cash + self.ending_value) account.total_positions_value = \ getattr(self, 'total_positions_value', self.ending_value) account.total_positions_value = \ getattr(self, 'total_positions_exposure', self.ending_exposure) account.regt_equity = \ getattr(self, 'regt_equity', self.ending_cash) account.regt_margin = \ getattr(self, 'regt_margin', float('inf')) account.initial_margin_requirement = \ getattr(self, 'initial_margin_requirement', 0.0) account.maintenance_margin_requirement = \ getattr(self, 'maintenance_margin_requirement', 0.0) account.available_funds = \ getattr(self, 'available_funds', self.ending_cash) account.excess_liquidity = \ getattr(self, 'excess_liquidity', self.ending_cash) account.cushion = \ getattr(self, 'cushion', self.ending_cash / (self.ending_cash + self.ending_value)) account.day_trades_remaining = \ getattr(self, 'day_trades_remaining', float('inf')) account.leverage = getattr(self, 'leverage', period_stats.gross_leverage) account.net_leverage = period_stats.net_leverage account.net_liquidation = getattr(self, 'net_liquidation', period_stats.net_liquidation) return account
def calculate_performance(self): pt = self.position_tracker pos_stats = calc_position_stats(pt) self.ending_value = pos_stats.net_value self.ending_exposure = pos_stats.net_exposure total_at_start = self.starting_cash + self.starting_value self.ending_cash = self.starting_cash + self.period_cash_flow total_at_end = self.ending_cash + self.ending_value self.pnl = total_at_end - total_at_start if total_at_start != 0: self.returns = self.pnl / total_at_start else: self.returns = 0.0