def profit_on_close(strat, records): last_record = records[-1] value, position = last_record[1] last_update = strat.series[-1] last_order = base.Order(quantity=abs(position), limit_price=last_update, transmit=0, open_close='C') close = last_order.cost_long() * (abs(position) / position) return value + close
def ib_open_order(self, evt): """ open_order(...) -> message the account supervisor with the details of a new order This method converts the contract and order objects supplied by Ib into the contract and order types from the base module. """ order_id, contract, order = evt.order_id, evt.contract, evt.order co, oo = base.Contract(), base.Order() co.__dict__.update(contract.__dict__) oo.__dict__.update(order.__dict__) self.account.existing_order(order_id, co, oo)
def emitOrder(self): """ emitOrder() -> emit an order and a contract """ symbol = '%s' % self.symbolCombo.currentText() action = self.actions['%s' % self.actionCombo.currentText()] orty = self.types['%s' % self.typeCombo.currentText()] quan = self.quantitySpin.value() lmt = self.priceEdit.value() aux = self.auxEdit.value() contract = base.Contract.stock_factory(symbol) order = base.Order(quantity=quan, limit_price=lmt, aux_price=aux, open_close='O', action=action, order_type=orty) self.emit(util.sigSubmitOrder, (contract, order))
def handle(self, parent, ticker, guidance): symb = ticker.symbol csize, cdir = 0, 0 try: ## match size and direction to current position if any csize = parent.positions[symb].position cdir = csize / abs(csize) except (KeyError, ZeroDivisionError, ): pass if guidance.direction == base.Directions.Long: ## long guidance; determine buy/open or close/sell if cdir == 0 or cdir == base.Directions.Long: size = self.default_size ooc = base.OrderPositions.Open oact = base.OrderActions.Buy elif cdir == base.Directions.Short: size = csize ooc = base.OrderPositions.Close oact = base.OrderActions.Buy elif guidance.direction == base.Directions.Short: ## short guidance; determine ssell/open or close/sell if cdir == 0 or cdir == base.Directions.Short: size = self.default_size ooc = base.OrderPositions.Open oact = base.OrderActions.ShortSell elif cdir == base.Directions.Long: size = csize ooc = base.OrderPositions.Close oact = base.OrderActions.Sell elif guidance.direction == base.Directions.NoDirection: ## guidance was neither long or short; nothing to do return 0 else: ## unknown direction return 0 ## add 'contract' and 'order' attributes to the guidance for other ## handlers to reference guidance.contract = base.Contract.stock_factory(symb) guidance.order = base.Order(quantity=abs(size), limit_price=0, aux_price=0, open_close=ooc, origin=0, transmit=1, tif="DAY", action=oact, order_type="LMT") return 1
def gauge(self): """ gauge() -> a generator for strategy performance measurement """ running_pnl = 0 running_pos = 0 opencloselookup = {NoReverse: 'O', Reverse: 'C'} for record in self.history: index, price, direction, reverse, movesize = record[0:5] if not direction: continue order = base.Order(quantity=movesize, limit_price=price, transmit=0, open_close=opencloselookup[reverse]) cost = base.Order.cost_long(order) running_pnl += -cost running_pos += movesize yield ( (index, price), (running_pnl, running_pos), order, )