def updatePositionsByFill(self, fill_evevt): posClosed = 0 pnl = 0. symbol = fill_evevt.symbol currDT = fill_evevt.timeindex.date() quantity = fill_evevt.quantity direction = convertDirection(fill_evevt.direction) value = fill_evevt.nominal / quantity / direction if symbol not in self._allPositions: lag = self._lags[symbol] short = self._shortable[symbol] self._allPositions[symbol] = SymbolPositionsHistory(symbol, lag, short, currDT, quantity, 0, direction, value) posOpened = quantity else: symbolPositionsHistory = self._allPositions[symbol] posClosed, posOpened, pnl = \ symbolPositionsHistory.updatePositionsByFill(currDT, quantity, direction, value) if symbolPositionsHistory.empty(): del self._allPositions[symbol] self._avaliableForTrade(symbol, currDT) return -posClosed * direction, posOpened * direction, pnl
def updatePositionsByCancelOrder(self, symbol, currDT, quantity, direction): direction = convertDirection(direction) if symbol in self._allPositions: symbolPositionsHistory = self._allPositions[symbol] symbolPositionsHistory.updatePositionsByCancelOrder(currDT, quantity, direction) # update cache for future usage self._avaliableForTrade(symbol, currDT)
def updatePositionsByFill(self, currDT, quantity, direction, value): posClosed = 0 posOpened = 0 pnl = 0. toFinish = quantity direction = convertDirection(direction) for i, d in enumerate(self.existDirections): if d != direction: amount = self.positions[i] if amount >= toFinish: self.positions[i] -= toFinish self.locked[i] -= toFinish posClosed += toFinish pnl += (value - self.existValues[i]) * d * toFinish toFinish = 0 break else: toFinish -= amount self.positions[i] = 0 self.locked[i] = 0 posClosed += amount pnl += (value - self.existValues[i]) * d * amount if toFinish: if len(self.dates) >= 1 and self.existDirections[ -1] == direction and self.dates[-1] == currDT: self.existValues[-1] = \ (self.positions[-1] * self.existValues[-1] + toFinish * value) / (self.positions[-1] + toFinish) self.positions[-1] += toFinish toFinish = 0 if toFinish and len(self.dates) >= 2 and self.existDirections[ -2] == direction and self.dates[-2] == currDT: self.existValues[-2] = \ (self.positions[-2] * self.existValues[-2] + toFinish * value) / (self.positions[-2] + toFinish) self.positions[-2] += toFinish toFinish = 0 if toFinish: self.dates.append(currDT) self.positions.append(toFinish) self.locked.append(0) self.existDirections.append(direction) self.existValues.append(value) posOpened = toFinish deleted = 0 for k in range(i + 1): if self.positions[k - deleted] == 0: del self.dates[k - deleted] del self.positions[k - deleted] del self.locked[k - deleted] del self.existDirections[k - deleted] del self.existValues[k - deleted] deleted += 1 return posClosed, posOpened, pnl
def updatePositionsByFill(self, currDT, quantity, direction, value): posClosed = 0 posOpened = 0 pnl = 0. toFinish = quantity direction = convertDirection(direction) for i, d in enumerate(self.existDirections): if d != direction: amount = self.positions[i] if amount >= toFinish: self.positions[i] -= toFinish self.locked[i] -= toFinish posClosed += toFinish pnl += (value - self.existValues[i]) * d * toFinish toFinish = 0 break else: toFinish -= amount self.positions[i] = 0 self.locked[i] = 0 posClosed += amount pnl += (value - self.existValues[i]) * d * amount if toFinish: if len(self.dates) >= 1 and self.existDirections[-1] == direction and self.dates[-1] == currDT: self.existValues[-1] = \ (self.positions[-1] * self.existValues[-1] + toFinish * value) / (self.positions[-1] + toFinish) self.positions[-1] += toFinish toFinish = 0 if toFinish and len(self.dates) >= 2 and self.existDirections[-2] == direction and self.dates[-2] == currDT: self.existValues[-2] = \ (self.positions[-2] * self.existValues[-2] + toFinish * value) / (self.positions[-2] + toFinish) self.positions[-2] += toFinish toFinish = 0 if toFinish: self.dates.append(currDT) self.positions.append(toFinish) self.locked.append(0) self.existDirections.append(direction) self.existValues.append(value) posOpened = toFinish deleted = 0 for k in range(i+1): if self.positions[k - deleted] == 0: del self.dates[k - deleted] del self.positions[k - deleted] del self.locked[k - deleted] del self.existDirections[k - deleted] del self.existValues[k - deleted] deleted += 1 return posClosed, posOpened, pnl
def updatePositionsByOrder(self, symbol, currDT, quantity, direction): direction = convertDirection(direction) if symbol not in self._allPositions: if not self._shortable[symbol] and direction == -1: raise ValueError("Short sell is not allowed for {0}".format(symbol)) else: symbolPositionsHistory = self._allPositions[symbol] symbolPositionsHistory.updatePositionsByOrder(currDT, quantity, direction) # update cache for future usage self._avaliableForTrade(symbol, currDT)