def _createFullNotionalEquityCurve(self, curve): rawpos = curve.drop(['cash', 'commission', 'total', 'margin', 'pnl'], axis=1) notionals = rawpos.abs().sum(axis=1).fillna(0) notional_directions = rawpos.sum(axis=1).fillna(0).apply(sign) pnldiffs = curve['pnl'].diff() # to handl the case when pnl is shown in null notional day cumPnL = 0. returns = [] for notional, pnldiff, direction in zip(notionals, pnldiffs, notional_directions): if not isClose(notional): r = direction * np.log( direction * notional / (direction * notional - pnldiff - cumPnL)) returns.append(r) cumPnL = 0. elif not np.isnan(pnldiff): returns.append(0.) cumPnL += pnldiff else: returns.append(0.) curve['return'] = returns curve.fillna(0., inplace=True) curve['equity_curve'] = np.exp(curve['return'].cumsum())
def updateHoldingsFromFill(self, fill, pnl): self.currentHoldings[fill.symbol] += fill.fillCost self.currentHoldings['commission'] += fill.commission if not isClose(fill.fillCost, 0.): self.currentHoldings['cash'] -= (fill.fillCost + fill.commission) else: self.currentHoldings['cash'] += (pnl - fill.commission) self.currentHoldings['pnl'] += pnl - fill.commission self.currentHoldings['total'] += pnl - fill.commission
def result(self): corr = self._correlationHolder.result() tmp = self._pReturnVar.result() if not isClose(tmp, 0.): pStd = math.sqrt(tmp) else: pStd = 0. tmp = self._mReturnVar.result() if not isClose(tmp, 0.): mStd = math.sqrt(tmp) else: mStd = 0. if not isClose(tmp, 0.): beta = corr * pStd / mStd else: beta = 0. alpha = self._pReturnMean.result() - beta * self._mReturnMean.result() return alpha, beta
def updateHoldingsFromFill(self, fill, pnl): self.currentHoldings[fill.symbol] += fill.fillCost self.currentHoldings['commission'] += fill.commission if not isClose(fill.fillCost, 0.): self.currentHoldings['cash'] -= (fill.fillCost + fill.commission) else: self.currentHoldings['cash'] += (pnl - fill.commission) self.currentHoldings['pnl'] += pnl - fill.commission self.currentHoldings['total'] += pnl - fill.commission fillDir = fill.direction if fillDir == OrderDirection.BUY_BACK or fillDir == OrderDirection.SELL_SHORT: self.currentHoldings['margin'] -= fill.fillCost
def result(self): n = self.size if n >= 2: n = self.size nominator = n * self._runningSumCrossSquare - self._runningSumLeft * self._runningSumRight denominator = (n * self._runningSumSquareLeft - self._runningSumLeft * self._runningSumLeft) \ * (n * self._runningSumSquareRight - self._runningSumRight * self._runningSumRight) if not isClose(denominator, 0.): denominator = math.sqrt(denominator) return nominator / denominator else: return 0.0 else: return np.nan
def result(self): n = self.size if n >= 2: n = self.size nominator = n * self._runningSumCrossSquare - self._runningSumLeft * self._runningSumRight denominator = (n * self._runningSumSquareLeft - self._runningSumLeft * self._runningSumLeft) \ * (n * self._runningSumSquareRight - self._runningSumRight * self._runningSumRight) if not isClose(denominator, 0.): denominator = math.sqrt(denominator) return nominator / denominator else: return 0.0 else: raise ZeroDivisionError("Container has less than 2 samples")
def _createFullNotionalEquityCurve(self, curve): rawpos = curve.drop(['cash', 'commission', 'total', 'margin', 'pnl'], axis=1) notionals = rawpos.abs().sum(axis=1).fillna(0) pnldiffs = curve['pnl'].diff() # to handl the case when pnl is shown in null notional day cumPnL = 0. returns = [] for notional, pnldiff in zip(notionals, pnldiffs): if not isClose(notional): r = np.log(notional / (notional - pnldiff - cumPnL)) returns.append(r) cumPnL = 0. elif not np.isnan(pnldiff): returns.append(0.) cumPnL += pnldiff else: returns.append(0.) curve['return'] = returns curve.fillna(0., inplace=True) curve['equity_curve'] = np.exp(curve['return'].cumsum())
def testIsClose(self): self.assertTrue(isClose(1.0, 1.000001, 1e-5)) self.assertTrue(isClose(1e9, 1. + 1e9, 1e-5, 1e-6)) self.assertFalse(isClose(1., 1. + 1e-5, 1e-6, 1e-6))
def result(self): tmp = self._var.result() if not isClose(tmp, 0.): return self._mean.result() / math.sqrt(self._var.result()) else: return np.nan