def down_contract(self): # Download contract from IB and save it to disk # Note: Already have conid from cache try: # Init API ib_cfg = ib_web_api.Configuration() ib_cfg.verify_ssl = False api = ContractApi(ib_web_api.ApiClient(ib_cfg)) # Get contract (name, industry, etc) contract = api.iserver_contract_conid_info_get(self.conid) # Remove unnecessary data contract = { 'conid': contract.con_id, 'category': contract.category, 'industry': contract.industry, } self.contract = contract except ApiException as e: raise e try: # Save contract to disk with open(self.cfg['dir_contracts'] + '/' + self.symbol + '.json', 'w') as f: f.write(json.dumps(contract)) except ApiException as e: raise 'Could not save contract: ' + e return self.contract
def run(self, symbols): symbols = [i.upper() for i in symbols] # Instantiate API class config = ib_web_api.Configuration() config.verify_ssl = False client = ib_web_api.ApiClient(config) api = MarketDataApi(client) for symbol in symbols: print('===> ' + symbol) # Get last 15 minutes data # TODO: Catch here points = api.iserver_marketdata_history_get( Company(symbol).get_conid(), '15min', bar='1min').data # Get just volume vol = [p.to_dict()['v'] for p in points] # Min/max/mean volume points, perc increase from mean to hi lo = min(i for i in vol if i > 0) hi = max(vol) m = mean(vol) perc = ((hi - m) / m) * 100 # Print stats print('LO: ' + str(lo)) print('HI: ' + str(hi)) print('ME: ' + str(round(m, 2))) print('PERC: ' + str(round(perc, 2)) + '%') print('CONID: ' + Company(symbol).get_conid())
def down_quote(self, conid, period, bar): ib_cfg = ib_web_api.Configuration() ib_cfg.verify_ssl = False api = MarketDataApi(ib_web_api.ApiClient(ib_cfg)) res = None for i in range(1, 6): try: # Download conid res = api.iserver_marketdata_history_get( conid, period=period, bar=bar ) except Exception as e: if i == 6: raise Exception('Could not get quote: %s\n' % e) else: continue # If three days, pick the last day # TODO: Do we need it like this? if period == '3d' and bar == '1d': if hasattr(res, 'points'): point = int(res.points) res = res.data[point].to_dict() else: raise Exception('Missing "points" for %s' % self.conid) else: if res is not None: res = [ i.to_dict() for i in res.data ] else: raise Exception('Could not get quote', self.conid) return res
def down_conid(self): # API: Get conid for symbol if self.conid is None: # Download conid config = ib_web_api.Configuration() config.verify_ssl = False api = ContractApi(ib_web_api.ApiClient(config)) # Main try: # Get conid conid = int response = api.iserver_secdef_search_post({ "symbol": self.symbol }) for item in response: if item.description == 'NASDAQ': self.conid = item.conid except ApiException as e: if self.conid is not None: detail = self.conid else: detail = self.symbol raise Exception('Could not download conid %s.' % detail) # Save conid to disk with open(self.cfg['dir_conids'] + '/' + self.symbol, 'w') as f: f.write(str(self.conid)) return self.conid
def check_ib_connectivity(): try: # Instantiate API class config = ib_web_api.Configuration() config.verify_ssl = False client = ib_web_api.ApiClient(config) api = SessionApi(client) res = api.iserver_auth_status_post() except Exception as e: raise ('No IB connectivity')
from ib_web_api.rest import ApiException # Parse args parser = argparse.ArgumentParser( description='Display last day data for ticker') parser.add_argument('tickers', metavar='TICKER', type=str, nargs='+', help='Ticker, e.g. AAPL') args = parser.parse_args() # Instantiate API class config = ib_web_api.Configuration() config.verify_ssl = False client = ib_web_api.ApiClient(config) try: # Get conids api = ContractApi(client) conids = {} for ticker in args.tickers: conid = int response = api.iserver_secdef_search_post({"symbol": ticker}) for item in response: if item.description == 'NASDAQ': conids[ticker] = item.conid except ApiException as e: print("Could not find conid: %s\n" % e) exit(1)
nargs='+', help='Tickers, e.g. AAPL AMZN') parser.add_argument('-v', dest='verbose', action='store_true', help='Verbose mode, print info messages') parser.add_argument('-s', dest='small', action='store_true', help='Print only result (category), without the ticker') args = parser.parse_args() # Instantiate API class config = ib_web_api.Configuration() config.verify_ssl = False client = ib_web_api.ApiClient(config) api = ContractApi(client) # Main try: # Get conids if args.verbose: print('Get conids') conids = {} for ticker in args.tickers: conid = int try: response = api.iserver_secdef_search_post({"symbol": ticker}) except Exception: # Could not get conid, so skip it continue
import ib_web_api as client from ib_web_api.rest import ApiException # Instantiate API class config = client.Configuration() config.verify_ssl = False api = client.MarketDataApi(client.ApiClient(config)) try: # Market data for Apple (AAPL) response = api.iserver_marketdata_history_get(265598, '1d') print(response.data) except ApiException as e: print("Exception: %s\n" % e)