def get_valid_spy_contract(idx) -> OptionContract: from ib_insync import IB, Stock ib = IB() ib.connect(clientId=idx + 1) ib_stk_con = Stock(symbol="SPY", exchange="SMART", currency="USD") ib_details = ib.reqContractDetails(ib_stk_con)[0] ib.reqMarketDataType(4) tick = ib.reqMktData(contract=ib_stk_con, snapshot=True) while np.isnan(tick.ask): ib.sleep() ask = tick.ask ib_con_id = ib_details.contract.conId ib_chains = ib.reqSecDefOptParams( underlyingSymbol="SPY", futFopExchange="", underlyingSecType="STK", underlyingConId=ib_con_id, ) ib_chain = ib_chains[0] ib_chain.strikes.sort(key=lambda s: abs(s - ask)) strike = ib_chain.strikes[0] expiration_str = ib_chain.expirations[idx] expiration_date = datetime.strptime(expiration_str, "%Y%m%d") spy_contract = OptionContract( symbol="SPY", strike=strike, right=Right.CALL, multiplier=int(ib_chain.multiplier), last_trade_date=expiration_date, ) ib.disconnect() return spy_contract
class TkApp: """ Example of integrating with Tkinter. """ def __init__(self): self.ib = IB().connect() self.root = tk.Tk() self.root.protocol('WM_DELETE_WINDOW', self._onDeleteWindow) self.entry = tk.Entry(self.root, width=50) self.entry.insert(0, "Stock('TSLA', 'SMART', 'USD')") self.entry.grid() self.button = tk.Button(self.root, text='Get details', command=self.onButtonClick) self.button.grid() self.text = tk.Text(self.root) self.text.grid() self.loop = asyncio.get_event_loop() def onButtonClick(self): contract = eval(self.entry.get()) cds = self.ib.reqContractDetails(contract) self.text.delete(1.0, tk.END) self.text.insert(tk.END, str(cds)) def run(self): self._onTimeout() self.loop.run_forever() def _onTimeout(self): self.root.update() self.loop.call_later(0.03, self._onTimeout) def _onDeleteWindow(self): self.loop.stop()
def update_details(ib: IB, store: AbstractBaseStore, keys: Optional[Union[str, List[str]]] = None) -> None: """ Pull contract details from ib and update metadata in store. Args: ib: connected IB instance store: datastore instance, for which data will be updated keys (Optional): keys in datastore, for which data is to be updated, if not given, update all keys """ if keys is None: keys = store.keys() elif isinstance(keys, str): keys = [keys] contracts = {} for key in keys: try: contract = eval(store.read_metadata(key)['repr']) except TypeError: log.error(f'Metadata missing for {key}') continue contract.update(includeExpired=True) contracts[key] = contract ib.qualifyContracts(*contracts.values()) details = {} for k, v in contracts.copy().items(): try: details[k] = ib.reqContractDetails(v)[0] except IndexError: log.error(f'Contract unavailable: {k}') del contracts[k] # get commission levels order = MarketOrder('BUY', 1) commissions = {} for k, v in contracts.items(): try: commissions[k] = ib.whatIfOrder(v, order).commission except AttributeError: log.error(f'Commission unavailable for: {k}') commissions[k] = np.nan for c, d in details.items(): _d = {'name': d.longName, 'min_tick': d.minTick, 'commission': commissions[c] } store.write_metadata(c, _d) log.info('Data written to store.')
def back_fill_catalog( ib: IB, catalog: DataCatalog, contracts: List[Contract], start_date: datetime.date, end_date: datetime.date, tz_name="Asia/Hong_Kong", kinds=("BID_ASK", "TRADES"), ): """ Back fill the data catalog with market data from Interactive Brokers. Parameters ---------- ib : IB The ib_insync client. catalog : DataCatalog DataCatalog to write the data to contracts : List[Contract] The list of IB Contracts to collect data for start_date : datetime.date The start_date for the back fill. end_date : datetime.date The end_date for the back fill. tz_name : str The timezone of the contracts kinds : tuple[str] (default: ('BID_ASK', 'TRADES') The kinds to query data for """ for date in pd.bdate_range(start_date, end_date): for kind in kinds: for contract in contracts: [details] = ib.reqContractDetails(contract=contract) instrument = parse_instrument(contract_details=details) raw = fetch_market_data(contract=contract, date=date.to_pydatetime(), kind=kind, tz_name=tz_name, ib=ib) if kind == "TRADES": ticks = parse_historic_trade_ticks( historic_ticks=raw, instrument_id=instrument.id) elif kind == "BID_ASK": ticks = parse_historic_quote_ticks( historic_ticks=raw, instrument_id=instrument.id) else: raise RuntimeError() write_objects(catalog=catalog, chunk=ticks)
"FB", "AAPL", "NFLX", "MSFT", "BABA", "INTC", "TSLA", ] FUTURES = [ "ES", "NQ", "RTY", "CL", "NG", "ZB", "ZN", "GC", "MXP", "EUR", "JPY", "GBP" ] stockContracts = [Stock(s, "SMART", "USD") for s in STOCK] ib.qualifyContracts(*stockContracts) futures = [ib.reqContractDetails(Future(f)) for f in FUTURES] futuresContracts = [c.contract for f in futures for c in f] futuresContracts = [ c for c in futuresContracts if c.tradingClass == c.symbol and c.lastTradeDateOrContractMonth.startswith("2019") ] for contract in stockContracts + futuresContracts: ib.reqMktData(contract, "", False, False) def onPendingTickers(tickers): ticks = [] for t in tickers: encodedTick = json.dumps(util.tree(t)) ticks.append({"Data": encodedTick})
class IBStore(with_metaclass(MetaSingleton, object)): '''Singleton class wrapping an ibpy ibConnection instance. The parameters can also be specified in the classes which use this store, like ``IBData`` and ``IBBroker`` Params: - ``host`` (default:``127.0.0.1``): where IB TWS or IB Gateway are actually running. And although this will usually be the localhost, it must not be - ``port`` (default: ``7496``): port to connect to. The demo system uses ``7497`` - ``clientId`` (default: ``None``): which clientId to use to connect to TWS. ``None``: generates a random id between 1 and 65535 An ``integer``: will be passed as the value to use. - ``notifyall`` (default: ``False``) If ``False`` only ``error`` messages will be sent to the ``notify_store`` methods of ``Cerebro`` and ``Strategy``. If ``True``, each and every message received from TWS will be notified - ``_debug`` (default: ``False``) Print all messages received from TWS to standard output - ``reconnect`` (default: ``3``) Number of attempts to try to reconnect after the 1st connection attempt fails Set it to a ``-1`` value to keep on reconnecting forever - ``timeout`` (default: ``3.0``) Time in seconds between reconnection attemps - ``timeoffset`` (default: ``True``) If True, the time obtained from ``reqCurrentTime`` (IB Server time) will be used to calculate the offset to localtime and this offset will be used for the price notifications (tickPrice events, for example for CASH markets) to modify the locally calculated timestamp. The time offset will propagate to other parts of the ``backtrader`` ecosystem like the **resampling** to align resampling timestamps using the calculated offset. - ``timerefresh`` (default: ``60.0``) Time in seconds: how often the time offset has to be refreshed - ``indcash`` (default: ``True``) Manage IND codes as if they were cash for price retrieval ''' # Set a base for the data requests (historical/realtime) to distinguish the # id in the error notifications from orders, where the basis (usually # starting at 1) is set by TWS REQIDBASE = 0x01000000 BrokerCls = None #getattr(sys.modules["cerebro.strategies." +classname.split('.')[0]], classname.split('.')[1])IBBroker #None # broker class will autoregister DataCls = None # data class will auto register params = ( ('host', '127.0.0.1'), ('port', 7496), ('clientId', None), # None generates a random clientid 1 -> 2^16 ('notifyall', False), # NOT IMPLEMENTED ('_debug', False), ('reconnect', 3), # -1 forever, 0 No, > 0 number of retries ('timeout', 3.0), # timeout between reconnections ('timeoffset', True), # Use offset to server for timestamps if needed ('timerefresh', 60.0), # How often to refresh the timeoffset ('indcash', True), # Treat IND codes as CASH elements ('readonly', False), # Set to True when IB API is in read-only mode ('account', ''), # Main account to receive updates for ) @classmethod def getdata(cls, *args, **kwargs): '''Returns ``DataCls`` with args, kwargs''' return cls.DataCls(*args, **kwargs) @classmethod def getbroker(cls, *args, **kwargs): '''Returns broker with *args, **kwargs from registered ``BrokerCls``''' return cls.BrokerCls(*args, **kwargs) def __init__(self): super(IBStore, self).__init__() self._env = None # reference to cerebro for general notifications self.broker = None # broker instance self.datas = list() # datas that have registered over start # self.ccount = 0 # requests to start (from cerebro or datas) # self._lock_tmoffset = threading.Lock() # self.tmoffset = timedelta() # to control time difference with server # # Structures to hold datas requests # self.qs = collections.OrderedDict() # key: tickerId -> queues # self.ts = collections.OrderedDict() # key: queue -> tickerId self.iscash = dict() # tickerIds from cash products (for ex: EUR.JPY) self.acc_cash = AutoDict() # current total cash per account self.acc_value = AutoDict() # current total value per account self.acc_upds = AutoDict() # current account valueinfos per account self.positions = collections.defaultdict(Position) # actual positions self.orderid = None # next possible orderid (will be itertools.count) self.managed_accounts = list() # received via managedAccounts self.notifs = queue.Queue() # store notifications for cerebro self.orders = collections.OrderedDict() # orders by order ided self.opending = collections.defaultdict(list) # pending transmission self.brackets = dict() # confirmed brackets self.last_tick = None # Use the provided clientId or a random one if self.p.clientId is None: self.clientId = random.randint(1, pow(2, 16) - 1) else: self.clientId = self.p.clientId if self.p.timeout is None: self.timeout = 2 else: self.timeout = self.p.timeout if self.p.readonly is None: self.readonly = False else: self.readonly = self.p.readonly if self.p.account is None: self.account = "" else: self.account = self.p.account if self.p._debug: util.logToConsole(level=logging.DEBUG) util.patchAsyncio() util.startLoop() self.ib = IB() self.ib.connect( host=self.p.host, port=self.p.port, clientId=self.clientId, timeout=self.timeout, readonly=self.readonly, account=self.account, ) # This utility key function transforms a barsize into a: # (Timeframe, Compression) tuple which can be sorted def keyfn(x): n, t = x.split() tf, comp = self._sizes[t] return (tf, int(n) * comp) # This utility key function transforms a duration into a: # (Timeframe, Compression) tuple which can be sorted def key2fn(x): n, d = x.split() tf = self._dur2tf[d] return (tf, int(n)) # Generate a table of reverse durations self.revdur = collections.defaultdict(list) # The table (dict) is a ONE to MANY relation of # duration -> barsizes # Here it is reversed to get a ONE to MANY relation of # barsize -> durations for duration, barsizes in self._durations.items(): for barsize in barsizes: self.revdur[keyfn(barsize)].append(duration) # Once managed, sort the durations according to real duration and not # to the text form using the utility key above for barsize in self.revdur: self.revdur[barsize].sort(key=key2fn) def start(self, data=None, broker=None): #self.reconnect(fromstart=True) # reconnect should be an invariant # Datas require some processing to kickstart data reception if data is not None: self._env = data._env # For datas simulate a queue with None to kickstart co self.datas.append(data) # if connection fails, get a fakeation that will force the # datas to try to reconnect or else bail out return self.getTickerQueue(start=True) elif broker is not None: self.broker = broker def stop(self): try: self.ib.disconnect() # disconnect should be an invariant except AttributeError: pass # conn may have never been connected and lack "disconnect" def get_notifications(self): '''Return the pending "store" notifications''' # The background thread could keep on adding notifications. The None # mark allows to identify which is the last notification to deliver self.notifs.put(None) # put a mark notifs = list() while True: notif = self.notifs.get() if notif is None: # mark is reached break notifs.append(notif) return notifs def managedAccounts(self): # 1st message in the stream self.managed_accounts = self.ib.managedAccounts() # Request time to avoid synchronization issues self.reqCurrentTime() def currentTime(self,msg): if not self.p.timeoffset: # only if requested ... apply timeoffset return curtime = datetime.fromtimestamp(float(msg.time)) with self._lock_tmoffset: self.tmoffset = curtime - datetime.now() threading.Timer(self.p.timerefresh, self.reqCurrentTime).start() def timeoffset(self): with self._lock_tmoffset: return self.tmoffset def reqCurrentTime(self): self.ib.reqCurrentTime() def nextOrderId(self): # Get the next ticker using a new request value from TWS self.orderid = self.ib.client.getReqId() return self.orderid def getTickerQueue(self, start=False): '''Creates ticker/Queue for data delivery to a data feed''' q = queue.Queue() if start: q.put(None) return q return q def getContractDetails(self, contract, maxcount=None): #cds = list() cds = self.ib.reqContractDetails(contract) #cds.append(cd) if not cds or (maxcount and len(cds) > maxcount): err = 'Ambiguous contract: none/multiple answers received' self.notifs.put((err, cds, {})) return None return cds def reqHistoricalDataEx(self, contract, enddate, begindate, timeframe, compression, what=None, useRTH=False, tz='', sessionend=None, #tickerId=None ): ''' Extension of the raw reqHistoricalData proxy, which takes two dates rather than a duration, barsize and date It uses the IB published valid duration/barsizes to make a mapping and spread a historical request over several historical requests if needed ''' # Keep a copy for error reporting purposes kwargs = locals().copy() kwargs.pop('self', None) # remove self, no need to report it if timeframe < TimeFrame.Seconds: # Ticks are not supported return self.getTickerQueue(start=True) if enddate is None: enddate = datetime.now() if begindate is None: duration = self.getmaxduration(timeframe, compression) if duration is None: err = ('No duration for historical data request for ' 'timeframe/compresison') self.notifs.put((err, (), kwargs)) return self.getTickerQueue(start=True) barsize = self.tfcomp_to_size(timeframe, compression) if barsize is None: err = ('No supported barsize for historical data request for ' 'timeframe/compresison') self.notifs.put((err, (), kwargs)) return self.getTickerQueue(start=True) return self.reqHistoricalData(contract=contract, enddate=enddate, duration=duration, barsize=barsize, what=what, useRTH=useRTH, tz=tz, sessionend=sessionend) # Check if the requested timeframe/compression is supported by IB durations = self.getdurations(timeframe, compression) # if not durations: # return a queue and put a None in it # return self.getTickerQueue(start=True) # Get or reuse a queue # if tickerId is None: # tickerId, q = self.getTickerQueue() # else: # tickerId, q = self.reuseQueue(tickerId) # reuse q for old tickerId # Get the best possible duration to reduce number of requests duration = None # for dur in durations: # intdate = self.dt_plus_duration(begindate, dur) # if intdate >= enddate: # intdate = enddate # duration = dur # begin -> end fits in single request # break intdate = begindate if duration is None: # no duration large enough to fit the request duration = durations[-1] # Store the calculated data # self.histexreq[tickerId] = dict( # contract=contract, enddate=enddate, begindate=intdate, # timeframe=timeframe, compression=compression, # what=what, useRTH=useRTH, tz=tz, sessionend=sessionend) barsize = self.tfcomp_to_size(timeframe, compression) if contract.secType in ['CASH', 'CFD']: #self.iscash[tickerId] = 1 # msg.field code if not what: what = 'BID' # default for cash unless otherwise specified elif contract.secType in ['IND'] and self.p.indcash: #self.iscash[tickerId] = 4 # msg.field code pass what = what or 'TRADES' q = self.getTickerQueue() histdata = self.ib.reqHistoricalData( contract, intdate.strftime('%Y%m%d %H:%M:%S') + ' GMT', duration, barsize, what, useRTH, 2) # dateformat 1 for string, 2 for unix time in seconds for msg in histdata: q.put(msg) return q def reqHistoricalData(self, contract, enddate, duration, barsize, what=None, useRTH=False, tz='', sessionend=None): '''Proxy to reqHistorical Data''' # get a ticker/queue for identification/data delivery q = self.getTickerQueue() if contract.secType in ['CASH', 'CFD']: #self.iscash[tickerId] = True if not what: what = 'BID' # TRADES doesn't work elif what == 'ASK': #self.iscash[tickerId] = 2 pass else: what = what or 'TRADES' # split barsize "x time", look in sizes for (tf, comp) get tf #tframe = self._sizes[barsize.split()[1]][0] # self.histfmt[tickerId] = tframe >= TimeFrame.Days # self.histsend[tickerId] = sessionend # self.histtz[tickerId] = tz histdata = self.ib.reqHistoricalData( contract, enddate.strftime('%Y%m%d %H:%M:%S') + ' GMT', duration, barsize, what, useRTH, 2) # dateformat 1 for string, 2 for unix time in seconds for msg in histdata: q.put(msg) return q def reqRealTimeBars(self, contract, useRTH=False, duration=5): '''Creates a request for (5 seconds) Real Time Bars Params: - contract: a ib.ext.Contract.Contract intance - useRTH: (default: False) passed to TWS - duration: (default: 5) passed to TWS Returns: - a Queue the client can wait on to receive a RTVolume instance ''' # get a ticker/queue for identification/data delivery q = self.getTickerQueue() rtb = self.ib.reqRealTimeBars(contract, duration, 'MIDPOINT', useRTH=useRTH) self.ib.sleep(duration) for bar in rtb: q.put(bar) return q def reqMktData(self, contract, what=None): '''Creates a MarketData subscription Params: - contract: a ib.ext.Contract.Contract intance Returns: - a Queue the client can wait on to receive a RTVolume instance ''' # get a ticker/queue for identification/data delivery q = self.getTickerQueue() ticks = '233' # request RTVOLUME tick delivered over tickString if contract.secType in ['CASH', 'CFD']: #self.iscash[tickerId] = True ticks = '' # cash markets do not get RTVOLUME if what == 'ASK': #self.iscash[tickerId] = 2 pass # q.put(None) # to kickstart backfilling # Can request 233 also for cash ... nothing will arrive md = MktData() q_ticks = queue.Queue() util.run(md.update_ticks(self.ib, contract, ticks, q_ticks)) while not q_ticks.empty(): ticker = q_ticks.get() for tick in ticker.ticks: # https://interactivebrokers.github.io/tws-api/tick_types.html if tick != self.last_tick: #last price #print(str(tick.time) +" >> " + str(tick.price)) self.last_tick = tick q.put(tick) return q # The _durations are meant to calculate the needed historical data to # perform backfilling at the start of a connetion or a connection is lost. # Using a timedelta as a key allows to quickly find out which bar size # bar size (values in the tuples int the dict) can be used. _durations = dict([ # 60 seconds - 1 min ('60 S', ('1 secs', '5 secs', '10 secs', '15 secs', '30 secs', '1 min')), # 120 seconds - 2 mins ('120 S', ('1 secs', '5 secs', '10 secs', '15 secs', '30 secs', '1 min', '2 mins')), # 180 seconds - 3 mins ('180 S', ('1 secs', '5 secs', '10 secs', '15 secs', '30 secs', '1 min', '2 mins', '3 mins')), # 300 seconds - 5 mins ('300 S', ('1 secs', '5 secs', '10 secs', '15 secs', '30 secs', '1 min', '2 mins', '3 mins', '5 mins')), # 600 seconds - 10 mins ('600 S', ('1 secs', '5 secs', '10 secs', '15 secs', '30 secs', '1 min', '2 mins', '3 mins', '5 mins', '10 mins')), # 900 seconds - 15 mins ('900 S', ('1 secs', '5 secs', '10 secs', '15 secs', '30 secs', '1 min', '2 mins', '3 mins', '5 mins', '10 mins', '15 mins')), # 1200 seconds - 20 mins ('1200 S', ('1 secs', '5 secs', '10 secs', '15 secs', '30 secs', '1 min', '2 mins', '3 mins', '5 mins', '10 mins', '15 mins', '20 mins')), # 1800 seconds - 30 mins ('1800 S', ('1 secs', '5 secs', '10 secs', '15 secs', '30 secs', '1 min', '2 mins', '3 mins', '5 mins', '10 mins', '15 mins', '20 mins', '30 mins')), # 3600 seconds - 1 hour ('3600 S', ('5 secs', '10 secs', '15 secs', '30 secs', '1 min', '2 mins', '3 mins', '5 mins', '10 mins', '15 mins', '20 mins', '30 mins', '1 hour')), # 7200 seconds - 2 hours ('7200 S', ('5 secs', '10 secs', '15 secs', '30 secs', '1 min', '2 mins', '3 mins', '5 mins', '10 mins', '15 mins', '20 mins', '30 mins', '1 hour', '2 hours')), # 10800 seconds - 3 hours ('10800 S', ('10 secs', '15 secs', '30 secs', '1 min', '2 mins', '3 mins', '5 mins', '10 mins', '15 mins', '20 mins', '30 mins', '1 hour', '2 hours', '3 hours')), # 14400 seconds - 4 hours ('14400 S', ('15 secs', '30 secs', '1 min', '2 mins', '3 mins', '5 mins', '10 mins', '15 mins', '20 mins', '30 mins', '1 hour', '2 hours', '3 hours', '4 hours')), # 28800 seconds - 8 hours ('28800 S', ('30 secs', '1 min', '2 mins', '3 mins', '5 mins', '10 mins', '15 mins', '20 mins', '30 mins', '1 hour', '2 hours', '3 hours', '4 hours', '8 hours')), # 1 days ('1 D', ('1 min', '2 mins', '3 mins', '5 mins', '10 mins', '15 mins', '20 mins', '30 mins', '1 hour', '2 hours', '3 hours', '4 hours', '8 hours', '1 day')), # 2 days ('2 D', ('2 mins', '3 mins', '5 mins', '10 mins', '15 mins', '20 mins', '30 mins', '1 hour', '2 hours', '3 hours', '4 hours', '8 hours', '1 day')), # 1 weeks ('1 W', ('3 mins', '5 mins', '10 mins', '15 mins', '20 mins', '30 mins', '1 hour', '2 hours', '3 hours', '4 hours', '8 hours', '1 day', '1 W')), # 2 weeks ('2 W', ('15 mins', '20 mins', '30 mins', '1 hour', '2 hours', '3 hours', '4 hours', '8 hours', '1 day', '1 W')), # 1 months ('1 M', ('30 mins', '1 hour', '2 hours', '3 hours', '4 hours', '8 hours', '1 day', '1 W', '1 M')), # 2+ months ('2 M', ('1 day', '1 W', '1 M')), ('3 M', ('1 day', '1 W', '1 M')), ('4 M', ('1 day', '1 W', '1 M')), ('5 M', ('1 day', '1 W', '1 M')), ('6 M', ('1 day', '1 W', '1 M')), ('7 M', ('1 day', '1 W', '1 M')), ('8 M', ('1 day', '1 W', '1 M')), ('9 M', ('1 day', '1 W', '1 M')), ('10 M', ('1 day', '1 W', '1 M')), ('11 M', ('1 day', '1 W', '1 M')), # 1+ years ('1 Y', ('1 day', '1 W', '1 M')), ]) # Sizes allow for quick translation from bar sizes above to actual # timeframes to make a comparison with the actual data _sizes = { 'secs': (TimeFrame.Seconds, 1), 'min': (TimeFrame.Minutes, 1), 'mins': (TimeFrame.Minutes, 1), 'hour': (TimeFrame.Minutes, 60), 'hours': (TimeFrame.Minutes, 60), 'day': (TimeFrame.Days, 1), 'W': (TimeFrame.Weeks, 1), 'M': (TimeFrame.Months, 1), } _dur2tf = { 'S': TimeFrame.Seconds, 'D': TimeFrame.Days, 'W': TimeFrame.Weeks, 'M': TimeFrame.Months, 'Y': TimeFrame.Years, } def getdurations(self, timeframe, compression): key = (timeframe, compression) if key not in self.revdur: return [] return self.revdur[key] def getmaxduration(self, timeframe, compression): key = (timeframe, compression) try: return self.revdur[key][-1] except (KeyError, IndexError): pass return None def tfcomp_to_size(self, timeframe, compression): if timeframe == TimeFrame.Months: return '{} M'.format(compression) if timeframe == TimeFrame.Weeks: return '{} W'.format(compression) if timeframe == TimeFrame.Days: if not compression % 7: return '{} W'.format(compression // 7) return '{} day'.format(compression) if timeframe == TimeFrame.Minutes: if not compression % 60: hours = compression // 60 return ('{} hour'.format(hours)) + ('s' * (hours > 1)) return ('{} min'.format(compression)) + ('s' * (compression > 1)) if timeframe == TimeFrame.Seconds: return '{} secs'.format(compression) # Microseconds or ticks return None def dt_plus_duration(self, dt, duration): size, dim = duration.split() size = int(size) if dim == 'S': return dt + timedelta(seconds=size) if dim == 'D': return dt + timedelta(days=size) if dim == 'W': return dt + timedelta(days=size * 7) if dim == 'M': month = dt.month - 1 + size # -1 to make it 0 based, readd below years, month = divmod(month, 12) return dt.replace(year=dt.year + years, month=month + 1) if dim == 'Y': return dt.replace(year=dt.year + size) return dt # could do nothing with it ... return it intact # def histduration(self, dt1, dt2): # # Given two dates calculates the smallest possible duration according # # to the table from the Historical Data API limitations provided by IB # # # # Seconds: 'x S' (x: [60, 120, 180, 300, 600, 900, 1200, 1800, 3600, # # 7200, 10800, 14400, 28800]) # # Days: 'x D' (x: [1, 2] # # Weeks: 'x W' (x: [1, 2]) # # Months: 'x M' (x: [1, 11]) # # Years: 'x Y' (x: [1]) # td = dt2 - dt1 # get a timedelta for calculations # # First: array of secs # tsecs = td.total_seconds() # secs = [60, 120, 180, 300, 600, 900, 1200, 1800, 3600, 7200, 10800, # 14400, 28800] # idxsec = bisect.bisect_left(secs, tsecs) # if idxsec < len(secs): # return '{} S'.format(secs[idxsec]) # tdextra = bool(td.seconds or td.microseconds) # over days/weeks # # Next: 1 or 2 days # days = td.days + tdextra # if td.days <= 2: # return '{} D'.format(days) # # Next: 1 or 2 weeks # weeks, d = divmod(td.days, 7) # weeks += bool(d or tdextra) # if weeks <= 2: # return '{} W'.format(weeks) # # Get references to dt components # y2, m2, d2 = dt2.year, dt2.month, dt2.day # y1, m1, d1 = dt1.year, dt1.month, dt2.day # H2, M2, S2, US2 = dt2.hour, dt2.minute, dt2.second, dt2.microsecond # H1, M1, S1, US1 = dt1.hour, dt1.minute, dt1.second, dt1.microsecond # # Next: 1 -> 11 months (11 incl) # months = (y2 * 12 + m2) - (y1 * 12 + m1) + ( # (d2, H2, M2, S2, US2) > (d1, H1, M1, S1, US1)) # if months <= 1: # months <= 11 # return '1 M' # return '{} M'.format(months) # elif months <= 11: # return '2 M' # cap at 2 months to keep the table clean # # Next: years # # y = y2 - y1 + (m2, d2, H2, M2, S2, US2) > (m1, d1, H1, M1, S1, US1) # # return '{} Y'.format(y) # return '1 Y' # to keep the table clean def makecontract(self, symbol, sectype, exch, curr, expiry='', strike=0.0, right='', mult=1): '''returns a contract from the parameters without check''' contract = Contract() contract.symbol = symbol contract.secType = sectype contract.exchange = exch if curr: contract.currency = curr if sectype in ['FUT', 'OPT', 'FOP']: contract.lastTradeDateOrContractMonth = expiry if sectype in ['OPT', 'FOP']: contract.strike = strike contract.right = right if mult: contract.multiplier = mult return contract def cancelOrder(self, orderid): '''Proxy to cancelOrder''' self.ib.cancelOrder(orderid) def placeOrder(self, orderid, contract, order): '''Proxy to placeOrder''' trade = self.ib.placeOrder(contract, order) while not trade.isDone(): self.ib.waitOnUpdate() return trade def reqTrades(self): '''Proxy to Trades''' return self.ib.trades() def reqPositions(self): '''Proxy to reqPositions''' return self.ib.reqPositions() def getposition(self, contract, clone=False): # Lock access to the position dicts. This is called from main thread # and updates could be happening in the background #with self._lock_pos: position = self.positions[contract.conId] if clone: return copy(position) return position def reqAccountUpdates(self, subscribe=True, account=None): '''Proxy to reqAccountUpdates If ``account`` is ``None``, wait for the ``managedAccounts`` message to set the account codes ''' if account is None: #self._event_managed_accounts.wait() self.managedAccounts() account = self.managed_accounts[0] #self.ib.reqAccountUpdates(subscribe, bytes(account)) self.updateAccountValue() def updateAccountValue(self): # Lock access to the dicts where values are updated. This happens in a # sub-thread and could kick it at anytime #with self._lock_accupd: #if self.connected(): ret = self.ib.accountValues() for msg in ret: try: value = float(msg.value) except ValueError: value = msg.value self.acc_upds[msg.account][msg.tag][msg.currency] = value if msg.tag == 'NetLiquidation': # NetLiquidationByCurrency and currency == 'BASE' is the same self.acc_value[msg.account] = value elif msg.tag == 'TotalCashBalance' and msg.currency == 'BASE': self.acc_cash[msg.account] = value def get_acc_values(self, account=None): '''Returns all account value infos sent by TWS during regular updates Waits for at least 1 successful download If ``account`` is ``None`` then a dictionary with accounts as keys will be returned containing all accounts If account is specified or the system has only 1 account the dictionary corresponding to that account is returned ''' # Wait for at least 1 account update download to have been finished # before the account infos can be returned to the calling client # if self.connected(): # self._event_accdownload.wait() # Lock access to acc_cash to avoid an event intefering #with self._updacclock: if account is None: # wait for the managedAccount Messages # if self.connected(): # self._event_managed_accounts.wait() if not self.managed_accounts: return self.acc_upds.copy() elif len(self.managed_accounts) > 1: return self.acc_upds.copy() # Only 1 account, fall through to return only 1 account = self.managed_accounts[0] try: return self.acc_upds[account].copy() except KeyError: pass return self.acc_upds.copy() def get_acc_value(self, account=None): '''Returns the net liquidation value sent by TWS during regular updates Waits for at least 1 successful download If ``account`` is ``None`` then a dictionary with accounts as keys will be returned containing all accounts If account is specified or the system has only 1 account the dictionary corresponding to that account is returned ''' # Wait for at least 1 account update download to have been finished # before the value can be returned to the calling client # if self.connected(): # self._event_accdownload.wait() # Lock access to acc_cash to avoid an event intefering #with self._lock_accupd: if account is None: # wait for the managedAccount Messages # if self.connected(): # self._event_managed_accounts.wait() if not self.managed_accounts: return float() elif len(self.managed_accounts) > 1: return sum(self.acc_value.values()) # Only 1 account, fall through to return only 1 account = self.managed_accounts[0] try: return self.acc_value[account] except KeyError: pass return float() def get_acc_cash(self, account=None): '''Returns the total cash value sent by TWS during regular updates Waits for at least 1 successful download If ``account`` is ``None`` then a dictionary with accounts as keys will be returned containing all accounts If account is specified or the system has only 1 account the dictionary corresponding to that account is returned ''' # Wait for at least 1 account update download to have been finished # before the cash can be returned to the calling client' # if self.connected(): # self._event_accdownload.wait() # result = [v for v in self.ib.accountValues() \ # if v.tag == 'TotalCashBalance' and v.currency == 'BASE'] # Lock access to acc_cash to avoid an event intefering #with self._lock_accupd: if account is None: #wait for the managedAccount Messages # if self.connected(): # self._event_managed_accounts.wait() if not self.managed_accounts: return float() elif len(self.managed_accounts) > 1: return sum(self.acc_cash.values()) # Only 1 account, fall through to return only 1 account = self.managed_accounts[0] try: return self.acc_cash[account] except KeyError: pass
import datetime 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
class Live(IB): def __init__(self, symbol, temp, client, verbose=False, notification=False): self.symbol = symbol instruments = pd.read_csv('instruments.csv').set_index('symbol') params = instruments.loc[self.symbol] self.market = str(params.market) self.exchange = str(params.exchange) self.temp = temp self.tick_size = float(params.tick_size) self.digits = int(params.digits) self.leverage = int(params.leverage) self.client = client self.verbose = verbose self.notification = notification self.ib = IB() print(self.ib.connect('127.0.0.1', 7497, client)) self.get_contract() self.data = self.download_data(tempo=self.temp, duration='1 D') self.current_date() self.pool = pd.DataFrame(columns=[ 'date', 'id', 'type', 'lots', 'price', 'S/L', 'T/P', 'commission', 'comment', 'profit' ]) self.history = pd.DataFrame(columns=[ 'date', 'id', 'type', 'lots', 'price', 'S/L', 'T/P', 'commission', 'comment', 'profit' ]) self.pending = pd.DataFrame(columns=[ 'date', 'id', 'type', 'lots', 'price', 'S/L', 'T/P', 'commission', 'comment', 'profit' ]) self.position = 0 self.number = 0 def get_contract(self): if self.market == 'futures': expiration = self.ib.reqContractDetails( Future(self.symbol, self.exchange))[0].contract.lastTradeDateOrContractMonth self.contract = Future(symbol=self.symbol, exchange=self.exchange, lastTradeDateOrContractMonth=expiration) elif self.market == 'forex': self.contract = Forex(self.symbol) elif self.market == 'stocks': self.contract = Stock(symbol=self.symbol, exchange=self.exchange, currency='USD') def download_data(self, tempo, duration): pr = (lambda mark: 'TRADES' if mark == 'futures' else ('TRADES' if mark == 'stocks' else 'MIDPOINT'))(self.market) historical = self.ib.reqHistoricalData(self.contract, endDateTime='', durationStr=duration, barSizeSetting=tempo, whatToShow=pr, useRTH=True, keepUpToDate=True) return historical def data_to_df(self, data): df = util.df(data)[['date', 'open', 'high', 'low', 'close', 'volume']].set_index('date') df.index = pd.to_datetime(df.index) return df def send_telegram_message(self, msg): '''Sends a telegram message ''' requests.post( 'https://api.telegram.org/bot804823606:AAFq-YMKr4hIjQ4N5M8GYCGa5w9JJ1kIunk/sendMessage', data={ 'chat_id': '@ranguito_channel', 'text': msg }) def current_date(self): self.date = datetime.now().strftime('%Y-%m-%d') self.weekday = datetime.now().weekday() self.hour = datetime.now().strftime('%H:%M:%S') def pool_check(self): '''Check pool trades''' if self.position == 0: self.pool = pd.DataFrame(columns=[ 'date', 'type', 'lots', 'price', 'S/L', 'T/P', 'commission', 'comment', 'profit' ]) def calculate_profit(self, type, price, lots): '''Calculates profit''' if type == 'BUY': profit = (lambda pos: 0 if pos >= 0 else (self.pool[self.pool.type == 'SELL'])[ 'price'].iloc[0] - price)(self.position) else: profit = (lambda pos: 0 if pos <= 0 else price - (self.pool[self.pool.type == 'BUY'])['price'].iloc[0])( self.position) return profit * self.leverage * lots def order_values(self, order_id): price = 0 commission = 0 if len(self.ib.fills()) > 0: for trade in util.tree(self.ib.fills()): if ('OrderId' and 'clientId') in trade[1]['Execution']: if ((nested_lookup('orderId', trade)[0] == order_id) and (nested_lookup('clientId', trade)[0] == self.client)): commission = nested_lookup('commission', trade)[0] price = nested_lookup('price', trade)[0] return (price, commission) def order_send(self, type, lots, sl=0, tp=0, comment=''): market_order = MarketOrder(type, lots) #initial_margin, maintenance_margin = self.get_margins(market_order) self.ib.placeOrder(self.contract, market_order) id = market_order.orderId self.number += 1 price = 0 while price == 0: self.ib.sleep(1) price, commission = self.order_values(id) profit = self.calculate_profit(type, price, lots) trade = { 'date': str(self.date) + ' ' + str(self.hour), 'id': id, 'type': type, 'lots': lots, 'price': price, 'S/L': sl, 'T/P': tp, 'commission': commission, 'comment': comment, 'profit': profit } self.save_trade(trade) self.pool = pd.concat( [self.pool, pd.DataFrame(trade, index=[self.number])], sort=False) self.history = pd.concat( [self.history, pd.DataFrame(trade, index=[self.number])], sort=False) mult = (lambda dir: 1 if dir == 'BUY' else -1)(type) self.position += (mult * lots) self.pool_check() if self.verbose: print('%s %s | %sING %d units at %5.2f in %s' % (str( self.date), str(self.hour), type, lots, price, self.symbol)) if self.notification: if self.position != 0: self.send_message_in(type, price, sl, tp, lots) else: self.send_message_out(type, price, lots, profit, commission, commission) def bracket_stop_order(self, type, lots, entry_price, sl=0, tp=0, comment=''): bracket_order = self.ib.bracketStopOrder(type, lots, entry_price, tp, sl) #initial_margin, maintenance_margin = self.get_margins(bracket_order[0]) for order in bracket_order: self.ib.placeOrder(self.contract, order) id_entry = bracket_order[0].orderId id_tp = bracket_order[1].orderId id_sl = bracket_order[2].orderId trade = { 'date': str(self.date) + ' ' + str(self.hour), 'id': id_entry, 'type': bracket_order[0].action, 'lots': lots, 'price': entry_price, 'S/L': sl, 'T/P': tp, 'commission': 0, 'comment': comment, 'profit': 0 } self.pending = pd.concat( [self.pending, pd.DataFrame(trade, index=[id_entry])], sort=False) trade = { 'date': str(self.date) + ' ' + str(self.hour), 'id': id_tp, 'type': bracket_order[1].action, 'lots': lots, 'price': tp, 'S/L': 0, 'T/P': 0, 'commission': 0, 'comment': comment, 'profit': 0 } self.pending = pd.concat( [self.pending, pd.DataFrame(trade, index=[id_entry])], sort=False) trade = { 'date': str(self.date) + ' ' + str(self.hour), 'id': id_sl, 'type': bracket_order[2].action, 'lots': lots, 'price': sl, 'S/L': 0, 'T/P': 0, 'commission': 0, 'comment': comment, 'profit': 0 } self.pending = pd.concat( [self.pending, pd.DataFrame(trade, index=[id_entry])], sort=False) return (bracket_order[0], bracket_order[1], bracket_order[2]) def pending_check(self, order): id = order.orderId if len(self.pending) > 0: price, commission = self.order_values(id) if price > 0: self.number += 1 order_select = self.pending[self.pending.id == id] profit = self.calculate_profit(order_select.type.iloc[0], price, order_select.lots.iloc[0]) trade = { 'date': str(self.date) + ' ' + str(self.hour), 'id': id, 'type': order_select.type.iloc[0], 'lots': order_select.lots.iloc[0], 'price': price, 'S/L': order_select['S/L'].iloc[0], 'T/P': order_select['T/P'].iloc[0], 'commission': commission, 'comment': '', 'profit': profit } self.save_trade(trade) self.pool = pd.concat( [self.pool, pd.DataFrame(trade, index=[self.number])], sort=False) self.history = pd.concat( [self.history, pd.DataFrame(trade, index=[self.number])], sort=False) mult = (lambda dir: 1 if dir == 'BUY' else -1)(order_select.type.iloc[0]) self.position += (mult * order_select.lots.iloc[0]) self.pool_check() if self.verbose: print('%s %s | %sING %d units at %5.2f in %s' % (str(self.date), str( self.hour), order_select.type.iloc[0], order_select.lots.iloc[0], price, self.symbol)) if self.notification: if self.position != 0: self.send_message_in(order_select.type.iloc[0], price, order_select['S/L'].iloc[0], order_select['T/P'].iloc[0], order_select.lots.iloc[0]) else: self.send_message_out(order_select.type.iloc[0], price, order_select.lots.iloc[0], profit, commission, commission) return True else: return False def get_margins(self, order): init_margin = float( self.ib.whatIfOrder(self.contract, order).initMarginChange) maint_margin = float( self.ib.whatIfOrder(self.contract, order).maintMarginChange) return (init_margin, maint_margin) def send_message_in(self, type, price_in, sl, tp, lots): msg_in = '%s Opened in %s \nPrice: %5.2f \nS/L: %5.2f \nT/P: %5.2f \nLots: %d \nAt: %s' % ( type, self.symbol, price_in, sl, tp, lots, self.hour) self.send_telegram_message(msg_in) def send_message_out(self, type, price_out, lots, profit, comm_in, comm_out): msg_out = '%s Closed in %s \nPrice: %5.2f \nProfit(USD): %5.2f \nCommissions(USD): %5.2f \nAt: %s' % \ (type, self.symbol, price_out, profit, (comm_in+comm_out),self.hour) self.send_telegram_message(msg_out) def save_trade(self, trade): if not path.exists('history_trades_%s.csv' % self.symbol): initial = pd.DataFrame(columns=[ 'date', 'id', 'type', 'lots', 'price', 'S/L', 'T/P', 'commission', 'comment', 'profit' ]).set_index('date') initial.to_csv('history_trades_%s.csv' % self.symbol) history = pd.read_csv('history_trades_%s.csv' % self.symbol) trade = pd.DataFrame(trade, index=[0]) history = pd.concat([history, trade], sort=False) history['net profit'] = history['profit'] - history['commission'] history['accumulated profit'] = history['net profit'].cumsum() history['max profit'] = history['accumulated profit'].cummax() history.set_index('date').to_csv('history_trades_%s.csv' % self.symbol)
class IBDataService: ip = "127.0.0.1" port = 4002 # 4001 for real trading def __init__(self): self.uid = random.randint(1000, 10000) print(f"init - UID: {str(self.uid)}") self.ib = IB() self.connect() def connect(self, *args): print(f"connectToIB - UID: {str(self.uid)}") if self.ib.isConnected() is False: print("CONNECTING ...") self.ib.connect("127.0.0.1", 4002, clientId=self.uid) print("CONNECTED") def disconnect(self, *args): print(f"connectToIB - UID: {str(self.uid)}") if self.ib.isConnected(): print("DISCONNECTING ...") self.ib.disconnect() print("DISCONNECTED ...") def getContractDetail(self, contract): print(f"getContractDetail - UID: {str(self.uid)}") data = self.ib.reqContractDetails(contract) # print(data) if len(data) > 0: return data[0] else: return None def getFuturesContractDetail(self, contract): print(f"getFuturesContractDetail - UID: {str(self.uid)}") data = self.ib.reqContractDetails(contract) if len(data) > 0: return data else: return None def getHistoricalData(self, contract, endDate="", duration="1 Y", barSize="1 day", price="MIDPOINT"): print(f"getHistoricalData - UID: {str(self.uid)}") data = self.ib.reqHistoricalData(contract, endDate, duration, barSize, price, 1, 1, False, []) return data async def startRealtimeData(self, contract, method): print(f"startRealtimeData - UID: {str(self.uid)}") self.ib.reqMktData(contract, "233", False, False) ticker = self.ib.reqTickByTickData(contract, TickDataType.LAST.value) ticker.updateEvent += method print(f"ENDS - startRealtimeData - UID: {str(self.uid)}") def stopRealtimeData(self, contract): print(f"stopRealtimeData - UID: {str(self.uid)}") self.ib.cancelMktData(contract) self.ib.cancelTickByTickData(contract, TickDataType.LAST.value) print(f"ENDS - stopRealtimeData - UID: {str(self.uid)}")
conexionBBDD.execute( "UPDATE CLIENT_ID SET CLIENT_ID = %s WHERE USUARIO = %s", (clientId, usuario)) return clientId engine = c.engine conexionBBDD = engine.connect() clientID = getClientId() conexionBBDD.close() ib = IB() ib.connect(host="127.0.0.1", port=7496, clientId=clientID) '''FUTURO 1''' '''Descargamos todos los datos del futuro continuo''' details = ib.reqContractDetails(Future(futuro1, exchange1, includeExpired=True)) '''Cogemos solo los contratos, y filtramos los que tengan fecha superior a hoy''' contracts = [ d.contract for d in details if d.contract.lastTradeDateOrContractMonth > fDesde and d.contract.exchange == exchange1 ] '''Necesitamos hacer esta chapu para poder ordenar y quedarnos con los N primeros''' '''Creamos una lista de listas, que luego sí podemos ordenar''' lista1 = [] for contract in contracts: lista2 = [] lista2.append(contract.conId) lista2.append(contract.localSymbol) lista2.append(contract.lastTradeDateOrContractMonth)
logger.debug("Hit take profit") logger.debug(f"{self.take_profit}") logger.debug( "----------------------------------------------") self.cancel_order(self.stop_loss.order) if __name__ == "__main__": import logging import sys logger = logging.getLogger(__name__) logger.setLevel(logging.DEBUG) logger.addHandler(logging.StreamHandler(sys.stdout)) logger.addHandler(logging.FileHandler("trades.log")) ib = IB() ib.connect("127.0.0.1", 4002, clientId=1) # TWS=7496, PAPER=7497, GTW=4001 ib.reqMarketDataType(1) # this isn't a proper way to roll the f*****g futures ES = ib.reqContractDetails(Future(symbol="ES", exchange="GLOBEX"))[0].contract MES = ib.reqContractDetails(Future(symbol="MES", exchange="GLOBEX"))[0].contract ds = DocStrat(ib=ib, es=ES, mes=MES) logger.debug("Doc Strategy instantiated") ds.run()
from kafka import KafkaProducer import json from time import sleep def json_serializer(data): return json.dumps(data).encode("utf-8") ib = IB() print(ib.connect('127.0.0.1', 7497, 5)) symbol = 'MNQ' exchange = 'GLOBEX' expiration = ib.reqContractDetails(Future( symbol, exchange))[0].contract.lastTradeDateOrContractMonth contract = Future(symbol=symbol, exchange=exchange, lastTradeDateOrContractMonth=expiration) sleep(20) bars = ib.reqRealTimeBars(contract, 5, 'MIDPOINT', False) print(bars) producer = KafkaProducer(bootstrap_servers=['localhost:9092'], value_serializer=json_serializer) while True: print(bars[-1]) producer.send('first_topic', bars[-1])
def __call__(self, ib: IB): log.debug( f'Candle {self.contract.localSymbol} initializing data stream...') self.details = ib.reqContractDetails(self.contract)[0] self.streamer(ib, self.contract)
class ArbitrageOnSymbol(): def __init__(self, args): super(ArbitrageOnSymbol, self).__init__() print('ArbitrageOnSymbol__init__: ', args[0], args[1], args[2]) # time.sleep(1) self.symbol = args[0] self.exchange = args[1] self.clientId = args[2] self.ib_server = args[3] self.ib_port = args[4] self.file_path = args[5] # self.is_refresh_source = False self.is_refresh = False # self.Storage = self.file_path + '/Storage_' + self.symbol self.Storage_ = self.file_path + '/Storage__' + self.symbol #self.data = self.get_data() # self.start_time = time.perf_counter() self.amount_of_arbitrages = 0 self.test_counter = 0 #asyncio.set_event_loop(asyncio.new_event_loop()) loop = asyncio.get_event_loop_policy().new_event_loop() asyncio.set_event_loop(loop) self.ib_ = IB() self.data = None print('End ArbitrageOnSymbol__init__: ', args[0], args[1], args[2]) # pull data and store locally def fetch_possible_contract(self, right='C'): print('fetch_possible_contract------ 1') print(right, ' ', self.symbol) o = Option(symbol=self.symbol, right=right, exchange=self.exchange) # print('fetch_possible_contract 1: ', self.symbol, ' ', right) o_cd = self.ib_.reqContractDetails(o) # print('fetch_possible_contract 2: ', self.symbol, ' ', right) cs = [j.contract for j in o_cd] print('fetch_possible_contract------ 2') print('Done: ', right, ' ', self.symbol) print('fetch_possible_contract------ 3') return cs def fetch_possible_contracts(self): # loop = asyncio.get_event_loop() #loop = asyncio.get_event_loop_policy().new_event_loop() #asyncio.set_event_loop(loop) try: print('fetch_possible_contracts 1') self.ib_.connect(self.ib_server, self.ib_port, clientId=self.clientId) print('fetch_possible_contracts 2') print(self.ib_) ##### print('fetch_possible_contracts 3') c = self.fetch_possible_contract('C') p = self.fetch_possible_contract('P') print('fetch_possible_contracts 4') possible_contracts = {'C': c, 'P': p} if os.path.exists(self.Storage): os.remove(self.Storage) with open(self.Storage, 'wb') as f: pickle.dump(possible_contracts, f) print('fetch_possible_contracts 5') return possible_contracts ##### except KeyboardInterrupt: pass finally: print("ib_.disconnect") # self.ib_.disconnect() print("Closing Loop") #loop.close() # fetching the data def get_possible_contracts(self): print('get_possible_contracts 1') with open(self.Storage, 'rb') as f: return pickle.load(f) def convert_data(self): if os.path.exists(self.Storage_): os.remove(self.Storage_) data = {} data_ = self.get_possible_contracts( ) # for every right (C or P) we have list of contract print('convert_data - 1') data['C'] = self.get_data_('C', data_['C']) data['P'] = self.get_data_('P', data_['P']) with open(self.Storage_, 'wb') as f: pickle.dump(data, f) print('convert_data - 2') return data # Create three dictionaries # cs = strikes for each contract period # cc_c = contracts for each contract period # bf = possible butterfly def get_data_(self, right, data): print('---------------------') print('-----get_data_--------', right) print('---------------------') cs = {} # strikes for each contract period cs0 = {} # strikes for each contract period for i in data: h = i.lastTradeDateOrContractMonth if h not in cs: cs[h] = { 'strikes': { str(i.strike): { 'event': None, 'bid': 0, 'bidSize': 0, 'ask': 0, 'askSize': 0, 'close': 0, 'price': 0, 'undPrice': 0, 'contract': i } }, 'strategies': [] } cs0[h] = [i.strike] else: cs[h]['strikes'][str(i.strike)] = { 'event': None, 'bid': 0, 'bidSize': 0, 'ask': 0, 'askSize': 0, 'close': 0, 'price': 0, 'undPrice': 0, 'contract': i } cs0[h].append(i.strike) for h in cs0: cs0[h] = sorted(cs0[h]) oo = cs0[h] pp = [] for v in range(1, len(oo)): pp.append(float(oo[v]) - float(oo[v - 1])) dd1 = [] for pi in pp: if pi not in dd1: dd1.append(pi) dd1 = sorted(dd1) df = int(max(oo) / min(dd1)) dd = [] for d1 in dd1: for f in range(1, df): dk = d1 * f if dk not in dd: dd.append(dk) for d in dd: for v in range(1, len(oo)): if ((oo[v] + d) in oo) and ((oo[v] - d) in oo): k = ((oo[v] - d), oo[v], (oo[v] + d)) cs[h]['strategies'].append(k) print('End ---------------------') print('-----get_data_--------', right) print('End ---------------------') return cs # end fetching the data # run the algo trading def run(self): print("run 1: Process for {}".format(self.symbol)) self.data = self.get_data() print("run 2: Process for {}".format(self.symbol)) #loop = asyncio.get_event_loop() #loop = asyncio.get_event_loop_policy().new_event_loop() #asyncio.set_event_loop(loop) loop = asyncio.get_event_loop() print(loop) print("run 3: Process for {}".format(self.symbol)) while True: print("run 1 While Loop", ' ', self.symbol) try: print("run 2 While Loop", ' ', self.symbol) self.ib_.connect(self.ib_server, self.ib_port, clientId=self.clientId) print("run 3 While Loop", ' ', self.symbol) print(self.ib_) ##### # kk = '20190802' # self.objects_.append(ArbitrageOnContract('C', kk, self.data['C'][kk], loop, self)) ##### loop.create_task(self.main()) loop.run_forever() except KeyboardInterrupt: pass finally: print("finally Closing Loop", ' ', self.symbol) self.ib_.disconnect() loop.close() async def main(self): print('main 1 ', self.symbol) for contracts_right in self.data: # print('main ', contracts_right) for contract_date in self.data[contracts_right]: await asyncio.sleep(0) contracts_data = self.data[contracts_right][contract_date] print(contract_date) s = asyncio.ensure_future( self.running(contracts_right, contract_date, contracts_data, self.exchange)) def get_data(self): with open(self.Storage_, 'rb') as f: return pickle.load(f) async def running(self, contracts_right, contract_date, contracts_data, exchange): # print(self.symbol, ': ', contracts_right, ' - running - ', contract_date, '\n',contracts_data,'\n',exchange) # await asyncio.sleep(1) start_inner_time = time.perf_counter() price_tasks = [] for s in contracts_data['strikes']: contracts_data['strikes'][s]['event'] = asyncio.Event() contracts_data['strikes'][s]['price'] = 0 c = contracts_data['strikes'][s]['contract'] o_price_fut = asyncio.ensure_future(self.ib_.reqTickersAsync(c)) price_task = asyncio.ensure_future( self.add_success_callback(o_price_fut, self.put_price_in_array, contracts_data)) price_tasks.append(price_task) self.get_legs_arrived_tasks(contracts_right, contract_date, contracts_data, exchange) results = await asyncio.gather(*price_tasks) end_time = time.perf_counter() #print('End of running ', contracts_right, ' ', contract_date, ' Time of running: ', # end_time-start_inner_time, ' ', ' Total time:', end_time-self.start_time, 'Prices:\n', results) await self.running(contracts_right, contract_date, contracts_data, exchange) async def add_success_callback(self, fut, callback, *args, **kwargs): result = await fut result = callback(result, *args, **kwargs) return result async def get_legs_arrived_task(self, strategy, contracts_right, contract_date, contracts_data, exchange): await contracts_data['strikes'][str(strategy[0])]['event'].wait() await contracts_data['strikes'][str(strategy[1])]['event'].wait() await contracts_data['strikes'][str(strategy[2])]['event'].wait() await self.calculate_butterfly(strategy, contracts_right, contract_date, contracts_data, exchange) return strategy def get_legs_arrived_tasks(self, contracts_right, contract_date, contracts_data, exchange): legs_arrived_tasks = [] # print(contracts_data) # await asyncio.sleep(1) for strategy in contracts_data['strategies']: task = asyncio.ensure_future( self.get_legs_arrived_task(strategy, contracts_right, contract_date, contracts_data, exchange)) legs_arrived_tasks.append(task) return legs_arrived_tasks # From here need to fix def put_price_in_array(self, ticker, contracts_data): try: t = ticker[0] #print('t0----') #print(t) #print('--0000---') #print(t.bidGreeks) #print('---11111---') #print(t.bidGreeks.undPrice) #print('t2222----') p = (t.ask + t.bid) / 2 contracts_data['strikes'][str(t.contract.strike)]['bid'] = t.bid contracts_data['strikes'][str( t.contract.strike)]['bidSize'] = t.bidSize contracts_data['strikes'][str(t.contract.strike)]['ask'] = t.ask contracts_data['strikes'][str( t.contract.strike)]['askSize'] = t.askSize contracts_data['strikes'][str( t.contract.strike)]['close'] = t.close contracts_data['strikes'][str(t.contract.strike)]['price'] = p # print(contracts_data['strikes'][str(t.contract.strike)]) contracts_data['strikes'][str( t.contract.strike)]['undPrice'] = t.bidGreeks.undPrice contracts_data['strikes'][str(t.contract.strike)]['event'].set() return p except Exception as e: print('=======ERRORR==============="') print(e) print('=======End ERRORR==============="') # need to improve. async def calculate_butterfly(self, strategy, contracts_right, contract_date, contracts_data, exchange): l = contracts_data['strikes'][str(strategy[0])]['ask'] m = contracts_data['strikes'][str(strategy[1])]['bid'] r = contracts_data['strikes'][str(strategy[2])]['ask'] u0 = contracts_data['strikes'][str(strategy[0])]['undPrice'] u1 = contracts_data['strikes'][str(strategy[1])]['undPrice'] u2 = contracts_data['strikes'][str(strategy[2])]['undPrice'] # print('u0: ', u0, 'u1: ', u1,'u2: ', u2) strategy_price = 999 if l > -1 and m > -1 and r > -1: strategy_price = (l + r) - (2 * m) #if strategy_price < 100: # await self.place_arbitrage(strategy, strategy_price, contracts_data, exchange) # self.amount_of_arbitrages += 1 if strategy_price < 0.09: await self.log_to_db(strategy, contracts_right, contract_date, contracts_data, strategy_price) return strategy_price # need to fix async def place_arbitrage(self, strategy, strategy_price, contracts_data, exchange): for s in contracts_data['strikes']: c = contracts_data['strikes'][s]['contract'] if c.strike == strategy[0]: lc = c elif c.strike == strategy[1]: mc = c elif c.strike == strategy[2]: rc = c # x = [lc, mc, rc] # x1 = self.ib_.qualifyContracts(x) #self.ib_.qualifyContracts(mc) #self.ib_.qualifyContracts(rc) combo_legs = [ ComboLeg(conId=lc.conId, ratio=1, action='BUY', exchange=exchange), ComboLeg(conId=mc.conId, ratio=2, action='SELL', exchange=exchange), ComboLeg(conId=rc.conId, ratio=1, action='BUY', exchange=exchange), ] c = Contract(symbol=self.parent.symbol, secType='BAG', exchange=exchange, currency='USD', comboLegs=combo_legs) o = MarketOrder(action='BUY', totalQuantity=1000) # lo = LimitOrder(action='BUY', totalQuantity=1, lmtPrice=strategy_price) if self.test_counter < 3: # t = self.ib_.placeOrder(contract=c, order=o) print('------100') print('place_arbitrage: order issued ', strategy, strategy_price) print(c) print('------100') # print(t) self.test_counter += 1 async def log_to_db(self, strategy, contracts_right, contract_date, contracts_data, strategy_price): # print('log_to_db: ', contracts_right, contract_date, ' ', strategy, ' ', strategy_price) lstrike = round(float(strategy[0]), 2) lb = round(contracts_data['strikes'][str(strategy[0])]['bid'], 2) lbs = contracts_data['strikes'][str(strategy[0])]['bidSize'] la = round(contracts_data['strikes'][str(strategy[0])]['ask'], 2) las = contracts_data['strikes'][str(strategy[0])]['askSize'] lc = round(contracts_data['strikes'][str(strategy[0])]['close'], 2) lp = round(contracts_data['strikes'][str(strategy[0])]['price'], 2) lu = round(contracts_data['strikes'][str(strategy[0])]['undPrice'], 2) rstrike = float(strategy[2]) rb = round(contracts_data['strikes'][str(strategy[2])]['bid'], 2) rbs = contracts_data['strikes'][str(strategy[2])]['bidSize'] ra = round(contracts_data['strikes'][str(strategy[2])]['ask'], 2) ras = contracts_data['strikes'][str(strategy[2])]['askSize'] rc = round(contracts_data['strikes'][str(strategy[2])]['close'], 2) rp = round(contracts_data['strikes'][str(strategy[2])]['price'], 2) ru = round(contracts_data['strikes'][str(strategy[2])]['undPrice'], 2) mstrike = float(strategy[1]) mb = round(contracts_data['strikes'][str(strategy[1])]['bid'], 2) mbs = contracts_data['strikes'][str(strategy[1])]['bidSize'] ma = round(contracts_data['strikes'][str(strategy[1])]['ask'], 2) mas = contracts_data['strikes'][str(strategy[1])]['askSize'] mc = round(contracts_data['strikes'][str(strategy[1])]['close'], 2) mp = round(contracts_data['strikes'][str(strategy[1])]['price'], 2) mu = round(contracts_data['strikes'][str(strategy[1])]['undPrice'], 2) #print('==========================================') #print('lb: ', lb, ' lbs: ', lbs, ' la: ', la, ' las: ', las, ' lc: ', lc , ' lp: ', lp , ' lu: ', lu) #print('rb: ', rb, ' rbs: ', rbs, ' ra: ', ra, ' ras: ', ras, ' rc: ', rc , ' rp: ', rp , ' ru: ', ru) #print('mb: ', mb, ' mbs: ', mbs, ' ma: ', ma, ' mas: ', mas, ' mc: ', mc , ' mp: ', mp , ' mu: ', mu) #print('-----------------+++++++++++++++++++++++++++++----------------------------------') try: PlacedOrders.objects.create(Right=contracts_right, Ticker=self.symbol, ContractDate=contract_date, LeftStrike=lstrike, LeftOrderAskPrice=la, LeftOrderBidPrice=lb, LeftOrderAveragePrice=lp, LeftOrderClose=lc, LeftOrderBidSize=lbs, LeftOrderAskSize=las, LeftActualPrice=0, LeftActualUndPrice=lu, MidStrike=mstrike, MidOrderAskPrice=ma, MidOrderBidPrice=mb, MidOrderAveragePrice=mp, MidOrderClose=mc, MidOrderBidSize=mbs, MidOrderAskSize=mas, MidActualPrice=0, MidActualUndPrice=mu, RightStrike=rstrike, RightOrderAskPrice=ra, RightOrderBidPrice=rb, RightOrderAveragePrice=rp, RightOrderClose=rc, RightOrderBidSize=rbs, RightOrderAskSize=ras, RightActualPrice=0, RightActualUndPrice=ru, StrategyPrice=strategy_price) except Exception as e: print("======ERROR-DB---------") print(e) print("======End ERROR-DB---------")
class request(IB): def __init__(self, symbol, temp, client): self.symbol = symbol self.temp = temp instruments = pd.read_csv('instruments.csv').set_index('symbol') self.params = instruments.loc[self.symbol] self.market = str(self.params.market) self.exchange = str(self.params.exchange) self.tick_size = float(self.params.tick_size) self.digits = int(self.params.digits) self.leverage = int(self.params.leverage) self.client = client self.current_date() self._sundays_activation() self.ib = IB() print(self.ib.connect('127.0.0.1', 7497, self.client)) self.connected = self.ib.isConnected() ####### self.get_contract() self.interrumption = False #self.data = self.download_data(tempo=temp, duration='1 D') #self.ib.reqMktData(self.contract, '', False, False); self.ticker = self.ib.ticker(self.contract) ######### #self.ticker = self.ib.reqTickByTickData(self.contract, 'Last', 0) self.bars = self.ib.reqRealTimeBars(self.contract, 5, 'MIDPOINT', False) self.operable = True def operable_schedule(self): if self.weekday == 4 and pd.to_datetime( self.hour).time() > pd.to_datetime('18:00:00').time(): print('%s %s | Today is Friday and Market has Closed!' % (self.date, self.hour)) self.operable = False elif self.weekday == 5: print('%s %s | Today is Saturday and market is not Opened' % (self.date, self.hour)) self.operable = False else: self.operable = True def current_date(self): self.date = datetime.now().strftime('%Y-%m-%d') self.weekday = datetime.now().weekday() self.hour = datetime.now().strftime('%H:%M:%S') def _sundays_activation(self): hour = '18:00:05' if self.weekday == 6: if pd.to_datetime(self.hour).time() < pd.to_datetime(hour).time(): print('Today is Sunday. Bot activation is at 18:00:00') while True: self.current_date() if pd.to_datetime( self.hour).time() >= pd.to_datetime(hour).time(): print('Activation Done') self.send_telegram_message( '%s %s | Bot Activation Done' % (self.date, self.hour)) break def continuous_check_message(self, message): if datetime.now().minute == 0 and datetime.now().second == 0: self.send_telegram_message(message, type='info') def reconnection(self): if self.hour == '23:44:30' or self.hour == '16:59:30': self.interrumption = True self.ib.disconnect() self.connected = self.ib.isConnected() print('%s %s | Ib disconnection' % (self.date, self.hour)) print('Connected: %s' % self.connected) if self.hour == '23:46:00' or self.hour == '18:00:05': self.interrumption = False print('%s %s | Reconnecting...' % (self.date, self.hour)) while not self.connected: try: self.ib.connect('127.0.0.1', 7497, self.client) self.connected = self.ib.isConnected() if self.connected: print('%s %s | Connection reestablished!' % (self.date, self.hour)) print('Requesting Market Data...') self.bars = self.ib.reqRealTimeBars( self.contract, 5, 'MIDPOINT', False) print('Last Close of %s: %.2f' % (self.symbol, self.bars[-1].close)) print('%s Data has been Updated!' % self.symbol) except: print( '%s %s | Connection Failed! Trying to reconnect in 10 seconds...' % (self.date, self.hour)) self.ib.sleep(10) print('%s %s | %s Data has been Updated!' % (self.date, self.hour, self.symbol)) def _local_symbol_selection(self): '''Selects local symbol according to symbol and current date''' current_date = datetime.now().date() # csv file selection according to symbol if self.symbol in ['ES', 'RTY', 'NQ', 'MES', 'MNQ', 'M2K']: contract_dates = pd.read_csv( 'D:/Archivos/futuro/Algorithmics/Codes/My_Bots/Hermes/contract_dates/indexes_globex.txt', parse_dates=True) elif self.symbol in ['YM', 'MYM', 'DAX']: contract_dates = pd.read_csv( 'D:/Archivos/futuro/Algorithmics/Codes/My_Bots/Hermes/contract_dates/indexes_ecbot_dtb.txt', parse_dates=True) elif self.symbol in ['QO', 'MGC']: contract_dates = pd.read_csv( 'D:/Archivos/futuro/Algorithmics/Codes/My_Bots/Hermes/contract_dates/QO_MGC.txt', parse_dates=True) elif self.symbol in ['CL', 'QM']: contract_dates = pd.read_csv( 'D:/Archivos/futuro/Algorithmics/Codes/My_Bots/Hermes/contract_dates/CL_QM.txt', parse_dates=True) else: contract_dates = pd.read_csv( 'D:/Archivos/futuro/Algorithmics/Codes/My_Bots/Hermes/contract_dates/%s.txt' % symbol, parse_dates=True) # Current contract selection according to current date for i in range(len(contract_dates)): initial_date = pd.to_datetime( contract_dates.iloc[i].initial_date).date() final_date = pd.to_datetime( contract_dates.iloc[i].final_date).date() if initial_date <= current_date <= final_date: current_contract = contract_dates.iloc[i].contract break # local symbol selection local = current_contract if self.symbol in [ 'ES', 'RTY', 'NQ', 'MES', 'MNQ', 'M2K', 'QO', 'CL', 'MGC', 'QM' ]: local = '%s%s' % (self.symbol, current_contract) if self.symbol in ['YM', 'ZS']: local = '%s %s' % (self.symbol, current_contract) if self.symbol == 'MYM': local = '%s %s' % (self.symbol, current_contract) if self.symbol == 'DAX': local = 'FDAX %s' % current_contract return local def get_contract(self): if self.market == 'futures': local = self._local_symbol_selection() self.contract = Future(symbol=self.symbol, exchange=self.exchange, localSymbol=local) print( self.ib.reqContractDetails( self.contract)[0].contract.lastTradeDateOrContractMonth) '''expiration = self.ib.reqContractDetails(Future(self.symbol,self.exchange))[0].contract.lastTradeDateOrContractMonth self.contract = Future(symbol=self.symbol, exchange=self.exchange, lastTradeDateOrContractMonth=expiration)''' elif self.market == 'forex': self.contract = Forex(self.symbol) elif self.market == 'stocks': self.contract = Stock(symbol=self.symbol, exchange=self.exchange, currency='USD') def download_data(self, tempo, duration): pr = (lambda market: 'MIDPOINT' if market == 'forex' else 'TRADES')(self.market) historical = self.ib.reqHistoricalData(self.contract, endDateTime='', durationStr=duration, barSizeSetting=tempo, whatToShow=pr, useRTH=False, keepUpToDate=False) return historical def send_telegram_message(self, message, type='trades'): bot_token = '1204313430:AAGonra1LaFhyI1gCVOHsz8yAohJUeFgplo' bot_chatID = '-499850995' if type == 'trades' else '-252750334' url = 'https://api.telegram.org/bot%s/sendMessage?chat_id=%s&text=%s' % ( bot_token, bot_chatID, message) requests.get(url)
from ib_insync import IB, Stock, Option import datetime from typing import List import pandas as pd import pytz """ A sale's also a buy. How the f**k do I distinguish that shit? Focus only on opts transacted at the ask. Probably Δ hedged by the writer. What about Γ? EOD? """ ib = IB() ib.connect("127.0.0.1", 4002, clientId=2) # TWS=7496, GTW=4001, # PAPER=7497 cs = ib.reqContractDetails(Stock(symbol="SPY", exchange="ARCA")) x = cs[0].contract # 1) prendi tutti gli strikes e tutte le exp chains = ib.reqSecDefOptParams( underlyingSymbol=x.symbol, futFopExchange="", underlyingSecType=x.secType, underlyingConId=x.conId, ) chain = next(c for c in chains if c.tradingClass == "SPY" and c.exchange == "SMART") [ticker] = ib.reqTickers(x) xValue = ticker.marketPrice()