def get_contract(client): contract = client.ib.reqContractDetails( ContFuture(symbol=config.SYMBOL, exchange=config.EXCHANGE)) if not contract: log.error("Failed to Grab Continuous Future {}".format(config.SYMBOL)) sysexit() return contract[0].contract, contract[0].tradingHours
def get_contract(client): contract = client.ib.reqContractDetails( ContFuture(symbol=config.SYMBOL, exchange=config.EXCHANGE) ) if not contract: percentb = (bars[-1].close - low[-1]) / (up[-1] - low[-1]) * 100 widthprior = (up[-2] - low[-2]) / sma[-2] * 100 percentbprior = (bars[-2].close - low[-2]) / (up[-2] - low[-2]) * 100 return width, percentb, widthprior, percentbprior
def modifySTPOrder(ib, modBuyStopLossPrice, modSellStopLossPrice, closePrice): contContract = ib.reqContractDetails( ContFuture(symbol=config.SYMBOL, exchange=config.EXCHANGE)) tradeContract = contContract[0].contract openOrdersList = ib.openOrders() x = 0 while x < len(openOrdersList): log.info( "----------------------- modify stop orders ---------------: ") log.info("Action: {a} orderType: {ot} auxPrice: {ap} modBuyStopLossPrice: {mbslp} modSellStopLossPrice: {msslp} # of orders: {no} close: {c}" \ .format(a=openOrdersList[x].action,ot=openOrdersList[x].orderType,ap=openOrdersList[x].auxPrice,mbslp=modBuyStopLossPrice,msslp=modSellStopLossPrice,no=len(openOrdersList),c=closePrice)) # for debugging log.info("openOrdersList: {ool}".format(ool=openOrdersList[x])) if openOrdersList[x].action.upper( ) == "BUY" and openOrdersList[x].orderType == "STP" and openOrdersList[ x].auxPrice > modSellStopLossPrice: log.info( "new auxPrice buy: {ap} from: {pp} new lmtprice {lt}".format( ap=modBuyStopLossPrice, pp=openOrdersList[x].auxPrice, lt=modSellStopLossPrice - 1)) openOrdersList[x].auxPrice = modSellStopLossPrice #openOrdersList[x].lmtPrice = modSellStopLossPrice - 10 #openOrder = openOrdersList[x] log.info("openOrdersList: {oo}".format(oo=openOrdersList[x])) ib.placeOrder(tradeContract, openOrdersList[x]) elif openOrdersList[x].action.upper() == "SELL" and openOrdersList[ x].orderType == "STP" and openOrdersList[ x].auxPrice < modBuyStopLossPrice: log.info( "new auxPrice buy: {ap} from: {pp} new lmtprice {lt}".format( ap=modSellStopLossPrice, pp=openOrdersList[x].auxPrice, lt=modBuyStopLossPrice + 1)) openOrdersList[x].auxPrice = modBuyStopLossPrice #openOrdersList[x].lmtPrice = modBuyStopLossPrice + 10 #openOrder = openOrdersList[x] log.info("openOrdersList: {oo}".format(oo=openOrdersList[x])) ib.placeOrder(tradeContract, openOrdersList[x]) else: log.info( "modifySTPOrder:: we have open orders but either they were not better stops or not STP orders. order action: {oa} type: {t} aux price: {ap} mod pricebuy: {mpb} mod price sell: {mps}" .format(oa=openOrdersList[x].action, t=openOrdersList[x].orderType, ap=openOrdersList[x].auxPrice, mpb=modBuyStopLossPrice, mps=modSellStopLossPrice)) x += 1 return True
import asyncio import time import categories import orders import config import logger import logic from indicator import Indicator ib = IB() ib.connect(config.HOST, config.PORT, clientId=config.CLIENTID) buyPrice = '2770.75' contContract = ib.reqContractDetails( ContFuture(symbol=config.SYMBOL, exchange=config.EXCHANGE)) tradeContract = contContract[0].contract print("tradecontract ", tradeContract) print("") #contract = ib.Future(symbol = contract.symbol) #print("contract for symbol",contract) print("") openOrdersList = ib.openOrders() #print("contract",contract) print("open orders", openOrdersList) print("open orders", ib.openTrades()) print("open trades ", ib.trades()) print("orders ", ib.orders()) print("length of orders ", len(openOrdersList)) x = 0 # not sure we need to differentiate between buy or sell stop orders below
def lookup_continuous_contracts(symbols): return [ContFuture(**s) for s in symbols]
def validate_contract(self, contract): if isinstance(contract, ContFuture): return contract elif isinstance(contract, Future): return ContFuture(**contract.dict()).update(secType='CONTFUT')