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])
def __init__(self): self.ib = IB() self.ib.connect('127.0.0.1', 7497, clientId=np.random.randint(10, 1000)) self.tickers_ret = {} self.endDateTime = '' self.No_days = '43200 S' self.interval = '30 secs' self.tickers_signal = "Hold" self.ES = Future(symbol='ES', lastTradeDateOrContractMonth='20200619', exchange='GLOBEX', currency='USD') self.ib.qualifyContracts(self.ES) self.ES_df = self.ib.reqHistoricalData(contract=self.ES, endDateTime=self.endDateTime, durationStr=self.No_days, barSizeSetting=self.interval, whatToShow='TRADES', useRTH=False, keepUpToDate=True) self.tickers_ret = [] self.options_ret = [] self.option = {'call': FuturesOption, 'put': FuturesOption} self.options_history = {} self.trade_options = {'call': [], 'put': []} self.price = 0 self.i = -1 self.ES_df.updateEvent += self.make_clean_df self.Buy = True self.Sell = False self.ib.positionEvent += self.order_verify self.waitTimeInSeconds = 120 self.tradeTime = 0
def start(self): self._logger.info('Starting mywatchdog') self.controller.start() IB.sleep(self.appStartupTime) try: self.ib.connect(self.host, self.port, self.clientId, self.connectTimeout) self.ib.setTimeout(self.appTimeout) except: # a connection failure will be handled by the apiError callback pass self._logger.info('Restarted mywatchdog') self._logger.info('Start Scheduler List for mywatchdog') for scheduler, schedules in self.schedulerList: self._logger.info( f'mywatchdog: scheduler: {scheduler}, {type(scheduler)}') if isinstance(schedules, list) and isinstance( scheduler, AsyncIOScheduler) and scheduler.running: for schedule in schedules: scheduler.add_job(**schedule) self._logger.info(f'mywatchdog: schedule: {schedule}') pass scheduler.start() pass pass self._logger.info('End Scheduler List for mywatchdog')
def test_ibgw_port(host): account = os.environ['IB_ACCOUNT'] password = os.environ['IB_PASSWORD'] trade_mode = os.environ['TRADE_MODE'] # build local ./Dockerfile subprocess.check_call(['docker', 'build', '-t', IMAGE_NAME, '.']) # run a container docker_id = subprocess.check_output( ['docker', 'run', '--env', 'IB_ACCOUNT={}'.format(account), '--env', 'IB_PASSWORD={}'.format(password), '--env', 'TRADE_MODE={}'.format(trade_mode), '-p', '4002:4002', '-d', IMAGE_NAME, "tail", "-f", "/dev/null"]).decode().strip() time.sleep(60) ib = IB() ib.connect('localhost', 4002, clientId=1) contract = Forex('EURUSD') bars = ib.reqHistoricalData( contract, endDateTime='', durationStr='30 D', barSizeSetting='1 hour', whatToShow='MIDPOINT', useRTH=True) # convert to pandas dataframe: df = util.df(bars) print(df) # at the end of the test suite, destroy the container subprocess.check_call(['docker', 'rm', '-f', docker_id])
def start_and_connect(self): """ Starts the IB gateway with IBC and connects to it. """ logging.info('Starting IBC...') self.ibc.start() wait = self.connection_timeout try: while not self.isConnected(): # retry until connection is established or timeout is reached IB.sleep(1) wait -= 1 logging.info('Connecting to IB gateway...') try: self.connect(**self.ib_config) except ConnectionRefusedError: if not wait: logging.warning('Timeout reached') raise TimeoutError('Could not connect to IB gateway') except Exception as e: logging.error(e) # write the launch log to logging (of limited use though as only the first # phase of the gateway startup process is logged in this non-encrypted log) try: with open(self.ibc_config['twsPath'] + '/launcher.log', 'r') as fp: logging.info(fp.read()) except FileNotFoundError: logging.warning(self.ibc_config['twsPath'] + '/launcher.log not found') raise e
def init_app(client_id_=int(config.get_config()['IB']['IBClientId'])): app = IB() app.errorEvent += __on_app_error host = config.get_config()['IB']['IBHost'] port = int(config.get_config()['IB']['IBPort']) client_id = client_id_ app.connect(host, port, clientId=client_id, readonly=True) return app
def test_request_realtime_bars(ib: IB): contract = Forex('EURUSD') bars = ib.reqRealTimeBars(contract, 5, 'MIDPOINT', False) bars.updateEvent += on_bar_update ib.sleep(100000) # 开盘到收盘时间 ib.cancelRealTimeBars(bars) return bars
def get_contracts(params: List[Dict[str, Any]], ib: IB, ) -> List[Dict[str, Any]]: contract_list = [] for candle in params: for contract in candle.contract_fields: contract_list.append(getattr(candle, contract)) ib.qualifyContracts(*contract_list) log.debug(f'contracts qualified: {contract_list}') return params
def __init__(self): self.log = log(__name__, 'logs/basem.log') self.db = Basedb() self.empty = [] self.total = 0 self.i = 0 self.ib = IB() self.ib.connect(Config.ib_host, Config.ib_port, Config.ib_client_id)
def __init__(self, host, port, clientId): qt.QWidget.__init__(self) self.setWindowTitle("Giulio's App") self.canvas = MplCanvas() # self.edit = qt.QLineEdit('', self) # self.edit.editingFinished.connect(self.add) self.table = HistoricalTable() self.MAList = [] self.MADict = {} self.connectButton = qt.QPushButton('Connect') self.connectButton.setStyleSheet( "border: 1px solid black; background: white") self.connectButton.resize(100, 32) self.connectButton.setGeometry(200, 150, 100, 40) self.connectButton.clicked.connect(self.onConnectButtonClicked) self.displayButton = qt.QPushButton('Display values') self.displayButton.setStyleSheet( "border: 1px solid black; background: white") self.displayButton.resize(100, 32) self.displayButton.clicked.connect(self.onDisplayButtonClicked) self.cancelAllButton = qt.QPushButton('CancelAll') self.cancelAllButton.setStyleSheet( "border: 1px solid black; background: white") self.cancelAllButton.resize(100, 32) self.cancelAllButton.setGeometry(200, 150, 100, 40) self.cancelAllButton.clicked.connect(self.onCancelAllButtonClicked) layout = qt.QVBoxLayout(self) # layout.addWidget(self.edit) layout.addWidget(self.table) #layout.addWidget(self.canvas) layout.addWidget(self.connectButton) layout.addWidget(self.cancelAllButton) # layout.addStretch(1) # self.fig = plt.figure() # self.ax = self.fig.add_subplot(1, 1, 1) self.xs = [] self.ys = [] # layout.addWidget(self.fig) self.connectInfo = (host, port, clientId) self.ib = IB() self.headers = [ 'symbol', 'bidSize', 'bid', 'ask', 'askSize', 'last', 'lastSize', 'close' ] self.id = 1 self.firstSignal = True self.isConnectionBroken = False self.firstma50 = 0 self.firstma200 = 0 self.availableCash = 0 self.ib.orderStatusEvent += self.order_status_cb self.ib.execDetailsEvent += self.exec_details_cb self.ib.errorEvent += self.error_cb self.ib.accountSummaryEvent += self.accountSummary self.ib.pendingTickersEvent += self.onPendingTickers
def main(ib: IB): try: logger.getLogger().info("Connecting...") ib.connect(config.HOST, config.PORT, clientId=config.CLIENTID) ib.reqMarketDataType(config.DATATYPE.value) except OSError: logger.getLogger().error("Connection Failed.") sys_exit() app = App(ib) app.run()
def get_contracts( candles: List[Candle], ib: IB, ) -> List[Candle]: contract_list = [] for candle in candles: for contract in candle.contract_fields: contract_list.append(getattr(candle, contract)) ib.qualifyContracts(*contract_list) log.debug(f'contracts qualified: {contract_list}') return candles
def __init__(self, strat_name, client_id, runtime_tm, debugging, manager_, heartbeat_q, traded_instr): self.strat_name = strat_name self.client_id = client_id self.runtime_tm = runtime_tm self.debugging = debugging self.manager_ = manager_ self.heartbeat_q = heartbeat_q self.traded_instr = traded_instr self.account_curr = ACCOUNT_CURR self.tax_rate_account_country = TAX_RATE_ACCOUNT_COUNTRY self.simult_reqs_interval = SIMULT_REQS_INTERVAL self.potential_mkt_data_lines_already_in_use = POTENTIAL_MKT_DATA_LINES_ALREADY_IN_USE self.ib = IB() # self.ib.setCallback('error', self.on_ib_error) self.ib.errorEvent += self.on_ib_error self.err_ = None self.report = Reporting(self) self.cash = CashManagement(debugging) self.hlprs = ClientSharedMemory() self.access_type = 'tws' if self.access_type == 'gateway': self.__port = 4001 elif self.access_type == 'tws': self.__port = 7497 self.__host = '127.0.0.1' self.ib.connect(self.__host, self.__port, clientId=self.client_id) self.portfolio = [] self.portfolio_instr = [] self.cnt_trades_per_instr_per_day = {} self.req_tracker = { 'total': 0, 'open_market_data_reqs': 0, 'open_market_data_lines': 0, 'hist_data_prior_time': None } self.start_cet = datetime.datetime.now() self.manager_[ 'active_mkt_data_lines'] = POTENTIAL_MKT_DATA_LINES_ALREADY_IN_USE self.funda_data_req_max = IB_PACING['funda_data_reqs']['reqs'] if self.debugging.get_meths: self.print_meths(str(self.__class__.__name__), dir(self)) _inst_ = self self.fin_ratios = FinRatios(_inst_)
async def main(options): ib = IB() ib.disconnectedEvent += onDisconnected await ib.connectAsync(options.ib_host, options.ib_port) server = Server([IbBridgeServer(ib)]) with graceful_exit([server]): await server.start(options.http_host, options.http_port) print(f"Serving on {options.http_host}:{options.http_port}") await server.wait_closed() ib.disconnectedEvent -= onDisconnected ib.disconnect()
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 determine_PnL(ib): account = ib.managedAccounts()[0] ib.reqPnL(account, '') IB.sleep(8) data = ib.pnl() print(data) ib.cancelPnL(account, '') dailyPnL = 0.0 unrealizedPnL = 0.0 realizedPnL = 0.0 return dailyPnL, unrealizedPnL, realizedPnL
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 test_ibgw_restart(ib_docker): subprocess.check_output( ['docker', 'container', 'stop', ib_docker]).decode().strip() subprocess.check_output( ['docker', 'container', 'start', ib_docker]).decode().strip() ib = IB() wait = 60 while not ib.isConnected(): try: IB.sleep(1) ib.connect('localhost', 4002, clientId=999) except: pass wait -= 1 if wait <= 0: break contract = Forex('EURUSD') bars = ib.reqHistoricalData( contract, endDateTime='', durationStr='30 D', barSizeSetting='1 hour', whatToShow='MIDPOINT', useRTH=True) # convert to pandas dataframe: df = util.df(bars) print(df)
def __init__(self, host, port, clientId): QWidget.__init__(self) self.edit = QLineEdit('', self) self.edit.editingFinished.connect(self.add) self.connectButton = QPushButton('Connect') self.connectButton.clicked.connect(self.onConnectButtonClicked) layout = QVBoxLayout(self) layout.addWidget(self.edit) layout.addWidget(self.connectButton) self.graph = TickerGraph(self, width=5, height=4) self.graph.move(0, 0) layout.addWidget(self.graph) self.connectInfo = (host, port, clientId) self.ib = IB()
async def main(): print('Running testbed.py') ib = IB() with await ib.connectAsync(host=os.getenv('IB_GATEWAY_URLNAME', 'tws'), port=int(os.getenv('IB_GATEWAY_URLPORT', '4004')), clientId=int( os.getenv('EFP_CLIENT_ID', (5 + random.randint(0, 4)))), timeout=15, readonly=True): portfolio = util.tree(ib.portfolio()) tg.outbound("Test successful.") return portfolio
def __init__(self, db=False, qtrade=False, ib=False): self.sql = SqlMapper() self.ib = IB() self.binance = Client() if qtrade: self.qtrade = Questrade( token_yaml= 'C:/Users/haseab/Desktop/Python/PycharmProjects/FAB/local/Workers/access_token.yml', save_yaml=True) print('Connected to Questrade API') if db: self.conn = self.sql.connect_psql() if ib: print(self.ib.connect('127.0.0.1', 7496, 104))
def main(ib: IB): logger.getLogger().info("Connecting...") ib.disconnect() ib.waitOnUpdate(timeout=0.5) #added to handle is client already in use https://github.com/erdewit/ib_insync/issues/76 randomClientID = random.randint(1,250) log.info("random client ID: {CI}".format(CI=randomClientID)) ib.connect(config.HOST, config.PORT, clientId=randomClientID,timeout=0) #ib.connect(config.HOST, config.PORT, clientId=config.CLIENTID,timeout=0) ib.reqMarketDataType(config.DATATYPE.value) # try: # logger.getLogger().info("Connecting...") # ib.connect(config.HOST, config.PORT, clientId=config.CLIENTID,timeout=0) # #ib.connect(config.HOST, config.PORT, clientId=config.CLIENTID,timeout=0) # ib.reqMarketDataType(config.DATATYPE.value) # except NameError: # got this block from https://groups.io/g/insync/message/4045 # #self.num_disconnects += 1 # print(datetime.datetime.now(), 'Connection error exception', self.num_disconnects) # #self.ib.cancelHistoricalData(bars) # log.info('Sleeping for 10sec...') # ib.disconnect # self.ib.sleep(10) # ib.connect(config.HOST, config.PORT, clientId=config.CLIENTID,timeout=0) # except OSError: # log.info("main try except OS errror > Connection Failed.") # sys_exit() app = App(ib) app.run()
def __init__(self): self.analyze = AnalyzeData() access_type = 'tws' client_id = 0 if access_type == 'gateway': __port = 4001 elif access_type == 'tws': __port = 7497 __host = '127.0.0.1' self.ib = IB() self.ib.connect(__host, __port, clientId=client_id) self.db = Database() self.conn = self.db.connect_() self.limit = 50
def __init__(self, host, port, clientId): qt.QWidget.__init__(self) self.edit = qt.QLineEdit('', self) self.edit.editingFinished.connect(self.add) self.table = TickerTable() self.connectButton = qt.QPushButton('Connect') self.connectButton.clicked.connect(self.onConnectButtonClicked) layout = qt.QVBoxLayout(self) layout.addWidget(self.edit) layout.addWidget(self.table) layout.addWidget(self.connectButton) self.connectInfo = (host, port, clientId) self.ib = IB() self.ib.pendingTickersEvent += self.table.onPendingTickers
def __init__(self, client_id=30): """Wrapper function for Interactive Broker API connection""" self.ib = IB() nest_asyncio.apply() try: # IB Gateway self.ib.connect('127.0.0.1', port=4001, clientId=client_id) except ConnectionRefusedError: # TWS try: self.ib.connect('127.0.0.1', port=7496, clientId=client_id) # TWS Paper except ConnectionRefusedError: print('Warning- Connected to Paper Portfolio - Account Values are hypothetical') self.ib.connect('127.0.0.1', port=7497, clientId=client_id)
class IBConnection: """ # usage with IBConnection.client() as ib: ... """ lock = threading.RLock() ib = IB() nest_asyncio.apply(ib.client._loop) @classmethod @contextmanager def client(cls): try: asyncio.get_event_loop() except RuntimeError: asyncio.set_event_loop(cls.ib.client._loop) cls.lock.acquire() if not cls.ib.isConnected(): from config import Config port = Config.get('broker.ibkr.port') ip_address = Config.get('broker.ibkr.ipaddress') cls.ib.connect(host=ip_address, port=port, clientId=10, readonly=False) cls.lock.release() yield cls.ib
def __init__(self, json_path=None, key_name=None, table_name=None, use_IB=False): self.json_path = json_path self.key_name = key_name if table_name: self.table_name = table_name SpectreDB.instantiate_engine(self) if use_IB: from ib_insync import IB, Forex, util # Need to have IB Gateway open to execute # util.startLoop() # uncomment this line when in a notebook self.ib = IB() self.ib.connect('127.0.0.1', 7497, clientId=1)
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 plot_contracts_bars(ib: IB, contracts: List[Contract], end: Optional[datetime.datetime] = None, bars_settings: Optional[List[Tuple[str, str]]] = None, bar_type: Optional[str] = "TRADES"): if end is None: end = datetime.datetime.now().replace(tzinfo=datetime.timezone.utc) if bars_settings is None or len(bars_settings) < 1: bars_settings = [ ("2 Y", "1 day"), ("6 M", "4 hours"), ("1 W", "10 mins"), ("1 D", "1 min"), ] for durationStr, barSizeSetting in bars_settings: fig, ax = plt.subplots(figsize=(24, 14)) for contract in contracts: bars = util.df( ib.reqHistoricalData( contract, endDateTime=end, durationStr=durationStr, barSizeSetting=barSizeSetting, whatToShow=bar_type, useRTH=True, formatDate=2 # UTC )) ys = np.cumprod(np.insert(1 + np.diff(np.log(bars["close"])), 0, 1)) xs = bars["date"].values ax.plot(xs, ys, label=contract.symbol) ax.set_title = f"{durationStr} - {barSizeSetting} bars" plt.xticks(rotation=45) plt.legend()
def __init__(self, client=None, ipaddress=None, port=None, log=logtoscreen("connectionIB"), mongo_db=arg_not_supplied): """ :param client: client id. If not passed then will get from database specified by mongo_db :param ipaddress: IP address of machine running IB Gateway or TWS. If not passed then will get from private config file, or defaults :param port: Port listened to by IB Gateway or TWS :param log: logging object :param mongo_db: mongoDB connection """ # resolve defaults ipaddress, port, idoffset = ib_defaults(ipaddress=ipaddress, port=port) # The client id is pulled from a mongo database # If for example you want to use a different database you could do something like: # connectionIB(mongo_ib_tracker = mongoIBclientIDtracker(database_name="another") # You can pass a client id yourself, or let IB find one self.db_id_tracker = mongoIBclientIDtracker(mongo_db=mongo_db, log=log, idoffset=idoffset) client = self.db_id_tracker.return_valid_client_id(client) # If you copy for another broker include this line log.label(broker="IB", clientid=client) self._ib_connection_config = dict(ipaddress=ipaddress, port=port, client=client) #if you copy for another broker, don't forget the logs ibServer.__init__(self, log=log) ibClient.__init__(self, log=log) # this is all very IB specific ib = IB() ib.connect(ipaddress, port, clientId=client) ## Add handlers, from ibServer methods ib.errorEvent += self.error_handler self.ib = ib