Stream/send bar data to AmiBroker from python (IB API)


I have an IB account and I'd like to stream NIFTY data live to AmiBroker. At the moment, live streaming of NIFTY data does not work (discussion here: Chart not updating in real time of index data from IB) and I have not found any workaround for this.

I've worked with IB API for a while now and was wondering if it is possible to send data from python directly to AmiBroker (to solve the NIFTY not streaming live issue)? Is it possible to do something with OLE or something similar?

I use insync-ib api and the code to get 5-sec bars or tick data is very simple:

def onBarUpdate(bars, hasNewBar):
    if hasNewBar:
        last_bar = bars[-1]
        str_to_write = "NSE:NIFTY,%s,%.2f,%.2f,%.2f,%.2f" % (str(bars[-1].time).split('+')[0].replace(' ',','),,last_bar.high,last_bar.low,last_bar.close)
        #Example output: NSE:NIFTY,2019-01-03,08:16:05,10689.85,10689.85,10688.65,10688.70
        #how can I send this(str_to_write data) to AmiBroker?
nifty = Index('NIFTY50', 'NSE')
bars = ib.reqRealTimeBars(nifty, 5, 'TRADES', False)
bars.updateEvent += onBarUpdate

Can anyone suggest a way to send these quotes to AmiBroker?

1 Like

Search the forum. There are plenty of discussions.

Its possible, i've also done it before, you only need the import Fn to be called continuously to supply data.

Something working and being efficient is very different. Here is why its a bad idea.
If its a few scrips, i've never had a lag but its very crude.

For the solution part, just create an Amibroker object and call the Import() with proper path to data file and the import format.

import win32com.client

oAB = win32com.client.Dispatch("Broker.Application")

// Loop this
oAB.Import(0, "PATH\\TO\\DATA.txt", "default.format");

You might need to put the absolute path of import format in the 3rd argument.

Thanks for your reply.
That is what I'm doing atm, I understand it's not the right way to do it, but this is just temporary (I hope).

import win32com.client as wcl
ab = wcl.Dispatch( 'Broker.Application' )

def onBarUpdate(bars, hasNewBar):
    #this function is called for every 5-sec bar generated
    if hasNewBar:
        last_bar = bars[-1]
        str_to_write = "NSE:NIFTY,%s,%.2f,%.2f,%.2f,%.2f" % (str(bars[-1].time).split('+')[0].replace(' ',','),,last_bar.high,last_bar.low,last_bar.close) #this log call writes str_to_write string to NIFTY_LIVE.txt file
        ab.Import(0, 'C:\\Users\\<username>\\Desktop\\NIFTY_LIVE.txt', "nifty.format");
nifty = Index('NIFTY50', 'NSE')
bars = ib.reqRealTimeBars(nifty, 5, 'TRADES', False)
bars.updateEvent += onBarUpdate

The contents of the NIFTY_LIVE.txt files is something like this:


and the format file is like this (file in Formats folder in AmiBroker main directory):

$FORMAT Ticker, Date_YMD, Time, Open, High, Low, Close
$GROUP 255

I'm still not getting any live updates in AmiBroker charts. Do I need to configure anything in AmiBroker to make this work? I don't get any errors related to OLE and a test done using print ("Version", ab.Version) works fine.

see my post, I was adding data to it at that time.
For OLE to work, AB doesnt need any extra config.

Just make sure the Object is creating properly.

If you are in doubt, use the copy-paste JScript example from the manual and test it.

Also, make sure the symbol in the file matches whats in the DB.
Drop the semi-colon from the import line. Not erroneous but redundant :slight_smile:
outta habit from C/C++ styled languages.

UAC also causes problem, check all that and don't overlook anything & dozens of other things bcos this isn't in the scope of the Forum.

1 Like

No need, because AB handles those events.
Atleast, i've never needed to use it. Incase, you find the update not taking effect you can try it.

1 Like

Why would anyone want to write something that is already available directly and natively by means of IB plugin (data) and IBController (trading)

Just use existing tools.

Instead of just nifty.format try passing the format file with absolute path like the txt file.

Instead of checking just chart, open the quotations window in AB and check for changes.

Are you starting AB from within its current working directory ?

Try cmd, cd to path, then run Broker.exe.

Sometimes relative paths are messed up by the user.

Are you starting AB from within its current working directory?

Would this matter in any way?
The way I did it was:

  • I already had AB launched by executing the exe shortcut
  • I run the following code in one cmd:
	copyfile('C:\\Users\\user\\Desktop\\NIFTY_LIVE.txt', 'C:\\Users\\<username>\\Desktop\\NIFTY_LIVE_c.txt')
	ab.Import(0, 'C:\\Users\\user\\Desktop\\NIFTY_LIVE_c.txt', "nifty.format")
  • In another cmd, I run the insync-api which gets 5-sec bars from IB and writes them to NIFTY_LIVE.txt file.
    (But I think, now I can combine these 2 files since I figured that the problem from the previous code was due to read access violation)

Now, when I check the charts on AB, I see the bars being updated every 5 secs with a 1sec lag every 4-5 sec (probably from reading the file). So, my initial problem is now solved fully. But this still feels like an unnecessary and hacky workaround when the native AB-IB plugin works very well and it would be nice if it could just take care of this small edge case.

Flagged and deleted my previous 3 posts because I wanted to remove a name and I can't edit it.
Request to mods/admin: Please remove the username from the path in those posts and undo-delete and flag if possible.
Sorry the trouble.

Moderator comment: post are already deleted and you already posted new code without user names. And yes "hacky workaround" is not good idea. Instead complain to Interactive Brokers because it is their fault of not sending data in this single case the same way as for all other symbols

Replying to Moderator comment: (also tagging Owner: @Tomasz)
I already contacted IB. Here is the reply I got:

Dear ,

Please note Amibroker is an independent third party software. Though they provide support for IB and
utilize our APIs to allow clients to connect. They provide support for their software exclusively.
We do not have access nor do we monitor any such discussion forums.

With that being said the type of historical data requests made by AmiBroker is a static request.
They implement their own functions to backload the chart then stream updates with a separate streaming
L1 data request. You would need to contact AmiBroker directly if you have any issues with charts
loading but not streaming in real time as they would need to investigate further what the issue
is with the streaming request.

Should you have any further questions with IB in regards to any streaming data, please contact
us in real time when the market is open and we can help review accordingly. At this time we have
no issues with any streaming requests for Indices like NIFTY50 and should relay back just the
same in any third party as populating in TWS. For any real time support you can call IB or open
a chat when the NSE market is open.

Live Chat: LINK../livechat.htm Phone: LINK../index.php


Senior Specialist, Technical Assistance Center
Interactive Brokers

Is there anything I can tell them specifically or any evidence I can provide to them specifically to make them understand the problem and prove that it's an issue with IB and not AmiBroker?

Evidence? Type MSFT or SPY or whatever other symbol except this NIFTY index . Does it steam via IB plugin? Yes it does. End of evidence. The plugin treats every symbol the same.

I understand that, that is exactly what I told them when I opened the support ticket and that reply I attached above is basically the reply they have given to your question. Are there any logs from AmiBroker I can show them to prove that the data sent from IB is different for NIFTY compared to other tickers?

The problem with Interactive Brokers is that they are limiting access to certain markets. For example clients from E.U. (like us) simply are not allowed to trade NSE or even subscribe to NSE data. On attempt to subscribe to NSE data by client from E.U. you see the error message like this:

You are not eligible to subscribe to the market data services.

Their official stance from FAQ is:

Trading of products listed on the National Stock Exchange of India is limited to Indian residents and Non-Resident Indians (NRIs). Trading must be conducted via an account opened through IB India.

Trouble is that they block not only trading but even data.

Without market data subscription TWS does NOT allow to get any data (even delayed) via API. Since IB limits even ability to access data, it is impossible for us to check what is wrong with IB with regards to NIFTY50/ NSE.

Update: maybe just maybe there is some hope in newest API (will update later)