def FillArrivalPriceParams(baseOrder: Order, maxPctVol: float, riskAversion: str, startTime: str, endTime: str, forceCompletion: bool, allowPastTime: bool, monetaryValue: float): baseOrder.algoStrategy = "ArrivalPx" baseOrder.algoParams = [] baseOrder.algoParams.append(TagValue("maxPctVol", maxPctVol)) baseOrder.algoParams.append(TagValue("riskAversion", riskAversion)) baseOrder.algoParams.append(TagValue("startTime", startTime)) baseOrder.algoParams.append(TagValue("endTime", endTime)) baseOrder.algoParams.append( TagValue("forceCompletion", int(forceCompletion))) baseOrder.algoParams.append( TagValue("allowPastEndTime", int(allowPastTime))) baseOrder.algoParams.append(TagValue("monetaryValue", monetaryValue))
def FillPriceVariantPctVolParams(baseOrder: Order, pctVol: float, deltaPctVol: float, minPctVol4Px: float, maxPctVol4Px: float, startTime: str, endTime: str, noTakeLiq: bool, monetaryValue: float): baseOrder.algoStrategy = "PctVolPx" baseOrder.algoParams = [] baseOrder.algoParams.append(TagValue("pctVol", pctVol)) baseOrder.algoParams.append(TagValue("deltaPctVol", deltaPctVol)) baseOrder.algoParams.append(TagValue("minPctVol4Px", minPctVol4Px)) baseOrder.algoParams.append(TagValue("maxPctVol4Px", maxPctVol4Px)) baseOrder.algoParams.append(TagValue("startTime", startTime)) baseOrder.algoParams.append(TagValue("endTime", endTime)) baseOrder.algoParams.append(TagValue("noTakeLiq", int(noTakeLiq))) baseOrder.algoParams.append(TagValue("monetaryValue", monetaryValue))
def openOrder(self, orderId: OrderId, contract: Contract, order: Order, orderState: OrderState): super().openOrder(orderId, contract, order, orderState) print(current_fn_name(), vars()) order.contract = contract self.permId2ord[order.permId] = order
def sell_limit_price(volume, price, tp='OG'): order = Order() order.action = 'SELL' order.orderType = 'LMT' order.totalQuantity = volume order.lmtPrice = price order.transmit = True order.tif = 'DAY' if tp == 'OG' else 'GTC' return order
def _get_lmt_parent(self) -> Order: _order = Order() _order.action = "BUY" if self.market_is_rising else "SELL" _order.totalQuantity = self.order_size _order.orderType = "LMT" _order.lmtPrice = self.parent_price _order.tif = "GTC" _order.transmit = False return _order
def create_order(sAction, iTotalQuantity, sOrderType, sLmtPrice): order = Order() order.action = sAction order.totalQuantity = iTotalQuantity order.orderType = sOrderType order.lmtPrice = sLmtPrice order.faGroup = GROUP_NAME order.faMethod = "NetLiq" return order
def create_order(self, action, quantity, order_type, price=None): order = Order() if order_type == 'LMT': order.action = action order.orderType = 'LMT' order.totalQuantity = quantity order.lmtPrice = price elif order_type == 'MKT': order.action = action order.orderType = 'MKT' order.totalQuantity = quantity return order
def FillAccumulateDistributeParams(baseOrder: Order, componentSize: int, timeBetweenOrders: int, randomizeTime20: bool, randomizeSize55: bool, giveUp: int, catchUp: bool, waitForFill: bool, startTime: str, endTime: str): baseOrder.algoStrategy = "AD" baseOrder.algoParams = [] baseOrder.algoParams.append(TagValue("componentSize", componentSize)) baseOrder.algoParams.append(TagValue("timeBetweenOrders", timeBetweenOrders)) baseOrder.algoParams.append(TagValue("randomizeTime20", int(randomizeTime20))) baseOrder.algoParams.append(TagValue("randomizeSize55", int(randomizeSize55))) baseOrder.algoParams.append(TagValue("giveUp", giveUp)) baseOrder.algoParams.append(TagValue("catchUp", int(catchUp))) baseOrder.algoParams.append(TagValue("waitForFill", int(waitForFill))) baseOrder.algoParams.append(TagValue("activeTimeStart", startTime)) baseOrder.algoParams.append(TagValue("activeTimeEnd", endTime))
def FillScaleParams(baseOrder: Order, scaleInitLevelSize: int, scaleSubsLevelSize: int, scaleRandomPercent: bool, scalePriceIncrement: float, scalePriceAdjustValue: float, scalePriceAdjustInterval: int, scaleProfitOffset: float, scaleAutoReset: bool, scaleInitPosition: int, scaleInitFillQty: int): baseOrder.scaleInitLevelSize = scaleInitLevelSize # Initial Component Size baseOrder.scaleSubsLevelSize = scaleSubsLevelSize # Subsequent Comp. Size baseOrder.scaleRandomPercent = scaleRandomPercent # Randomize size by +/-55% baseOrder.scalePriceIncrement = scalePriceIncrement # Price Increment # Auto Price adjustment baseOrder.scalePriceAdjustValue = scalePriceAdjustValue # starting price by baseOrder.scalePriceAdjustInterval = scalePriceAdjustInterval # in seconds # Profit Orders baseOrder.scaleProfitOffset = scaleProfitOffset # Create profit taking order Profit Offset baseOrder.scaleAutoReset = scaleAutoReset # Restore size after taking profit baseOrder.scaleInitPosition = scaleInitPosition # Initial Position baseOrder.scaleInitFillQty = scaleInitFillQty # Filled initial Component Size
def make_order(action='BUY', quantity=1, limitPrice=0, orderType='LMT', parentId=0, transmit=False): order = Order() order.action = action order.totalQuantity = quantity order.lmtPrice = limitPrice order.orderType = orderType order.parentId = parentId order.transmit = transmit return order
def replace_order(self, order_id: int, new_lmt_price, contract: Contract): parent = Order() parent.orderId = order_id parent.action = "buy" parent.orderType = "LMT" parent.totalQuantity = 1 parent.lmtPrice = new_lmt_price parent.transmit = True self.placeOrder(order_id, contract, parent)
def TrailingStopLimit(action: str, quantity: float, lmtPriceOffset: float, trailingAmount: float, trailStopPrice: float): order = Order() order.action = action order.orderType = "TRAIL LIMIT" order.totalQuantity = quantity order.trailStopPrice = trailStopPrice order.lmtPriceOffset = lmtPriceOffset order.auxPrice = trailingAmount return order
def PeggedToStock(action: str, quantity: float, delta: float, stockReferencePrice: float, startingPrice: float): order = Order() order.action = action order.orderType = "PEG STK" order.totalQuantity = quantity order.delta = delta order.lmtPrice = stockReferencePrice order.startingPrice = startingPrice return order
def _take_profit_order_builder(self, order: Order): if order is None: take_profit = Order() else: take_profit = order take_profit.action = self.close_order_action take_profit.orderType = ORDER_TYPE_LMT take_profit.totalQuantity = self.parent_order.totalQuantity take_profit.lmtPrice = self.parent_order.lmtPrice * ( 100 + app.config.PP33_LIMITS[self.symbol]['take_profit']) / 100 take_profit.lmtPrice = normalize(self.symbol, take_profit.lmtPrice) take_profit.parentId = self.parent_order.orderId take_profit.transmit = False return take_profit
def put_order_with_condition(self, order_id: int): order1 = Order() order1.orderId = order_id order1.action = "buy" order1.orderType = "LMT" order1.totalQuantity = 1 order1.lmtPrice = 160 current_time = datetime.today() cancel_time = current_time + timedelta(minutes=3) cancel_time_str = cancel_time.strftime("%Y%m%d %H:%M:%S") order1.conditions.append(self.time_condition(False, cancel_time_str)) order1.conditions.append( self.price_condition( order_condition.PriceCondition.TriggerMethodEnum.Last, 265598, "SMART", 150, False, True)) contract_aapl = self.contract_aapl() # order2 = Order() # order2.orderId = order_id # order2.action = "buy" # order2.orderType = "MKT" # order2.totalQuantity = 1 # order2.conditions.append(self.time_condition(False, "20190226 12:18:00")) # contract_es = self.contract_es() self.placeOrder(order_id, contract_aapl, order1)
def send_order(self, req: OrderRequest): """ Send a new order. """ if not self.status: return "" if req.exchange not in EXCHANGE_VT2IB: self.gateway.write_log(f"不支持的交易所:{req.exchange}") return "" if req.type not in ORDERTYPE_VT2IB: self.gateway.write_log(f"不支持的价格类型:{req.type}") return "" self.orderid += 1 ib_contract = Contract() ib_contract.conId = str(req.symbol) ib_contract.exchange = EXCHANGE_VT2IB[req.exchange] ib_order = Order() ib_order.orderId = self.orderid ib_order.clientId = self.clientid ib_order.action = DIRECTION_VT2IB[req.direction] ib_order.orderType = ORDERTYPE_VT2IB[req.type] ib_order.lmtPrice = req.price ib_order.totalQuantity = req.volume self.client.placeOrder(self.orderid, ib_contract, ib_order) self.client.reqIds(1) order = req.create_order_data(str(self.orderid), self.gateway_name) self.gateway.on_order(order) return order.vt_orderid
def create_order_pct(sAction="SELL", iPercent="-100", sOrderType="MKT", iTotalQuantity=0.0, bTransmit=True): order = Order() order.action = sAction order.faGroup = GROUP_NAME order.faMethod = "PctChange" order.faPercentage = iPercent order.orderType = sOrderType order.totalQuantity = iTotalQuantity order.transmit = bTransmit return order
def send_buy_order(self, sec_instance, price): buy_vol = self.determine_buy_volume(price) cost = buy_vol * price + buy_vol * IB_TRADE_COST # m57 if self.buying_power < cost: print( f"Total spent limit reached, can't buy {sec_instance.contract.symbol} at {price}." ) return False elif self.account_buying_power_usd < cost: print( f"Account's buying power is insufficient for buying {sec_instance.contract.symbol} at {price}." ) return False else: self.buying_power -= cost sec_instance.buy_vol = buy_vol order = Order() order.action = "BUY" order.totalQuantity = buy_vol # take the floor order.account = ACCOUNT order.orderType = "LMT" order.lmtPrice = price self.placeOrder(self.nextValidOrderId, sec_instance, order) self.buying_orders[sec_instance.order_id] = datetime.datetime.now() print( f"ID: {sec_instance.order_id} -- Buying {sec_instance.contract.symbol} at {order.lmtPrice}" f" for {order.totalQuantity}. Buying power available: {self.buying_power}" ) return True
def _to_ib_order(self, order: Order): ib_order = IBOrder() if order.quantity > 0: ib_order.action = 'BUY' else: ib_order.action = 'SELL' ib_order.totalQuantity = abs(order.quantity) execution_style = order.execution_style self._set_execution_style(ib_order, execution_style) time_in_force = order.time_in_force tif_str = self._map_to_tif_str(time_in_force) ib_order.tif = tif_str return ib_order
def put_single_order(self, order_id: int): contract_opt = self.contract_spx() parent = Order() parent.orderId = order_id parent.action = "buy" parent.orderType = "LMT" parent.totalQuantity = 1 parent.lmtPrice = 32.5 parent.transmit = True self.placeOrder(order_id, contract_opt, parent)
def stop(action: str, quantity: float, stopPrice: float): # ! [stop] order = Order() order.action = action order.orderType = "STP LMT" order.lmtPrice = stopPrice order.auxPrice = stopPrice order.totalQuantity = quantity order.tif = 'GTC' # ! [stop] return order
def create_order(self, action, quantity, type, price=0): order = Order() order.orderType = type if type != 'MKT': order.lmtPrice = price order.tif = 'GTC' order.action = action order.totalQuantity = quantity order.account = self.account print('Parent Order Quantity ' + str(quantity)) return order
def stop_order(action: str, quantity: float, stop_price: float): order = Order() order.action = action order.orderType = "STP" order.totalQuantity = quantity order.tif = "GTC" order.auxPrice = stop_price return order
def order_limit(symbol, action, qty, lim_price, dis_lim_price): order = Order() order.action = action order.orderType = "LMT" order.totalQuantity = qty order.lmtPrice = lim_price order.discretionaryAmt = dis_lim_price return order
def LimitOnOpen(action: str, quantity: float, limitPrice: float): order = Order() order.action = action order.tif = "OPG" order.orderType = "LMT" order.totalQuantity = quantity order.lmtPrice = limitPrice return order
def AtAuction(action: str, quantity: float, price: float): order = Order() order.action = action order.tif = "AUC" order.orderType = "MTL" order.totalQuantity = quantity order.lmtPrice = price return order
def Block(action: str, quantity: float, price: float): order = Order() order.action = action order.orderType = "LMT" order.totalQuantity = quantity # Large volumes! order.lmtPrice = price order.blockOrder = True return order
def SweepToFill(action: str, quantity: float, price: float): order = Order() order.action = action order.orderType = "LMT" order.totalQuantity = quantity order.lmtPrice = price order.sweepToFill = True return order
def create_order(action,secType,quantity,price,transmit): order = Order() order.action = action order.orderType = secType order.totalQuantity = quantity order.lmtPrice = price order.Transmit= transmit return order
def _create_order(action, qty, order_type, limit_price, stop_price): order = Order() order.action = action order.totalQuantity = qty order.auxPrice = stop_price order.lmtPrice = limit_price order.orderType = order_type return order
def create_order(action, total_quantity, method, group_name): order = Order() order.action = action order.totalQuantity = total_quantity order.faGroup = group_name order.faMethod = method order.transmit = True return order