softwarespartan / IB4m

Interactive Brokers API for Matlab
GNU General Public License v2.0
62 stars 21 forks source link

how to get real-time prices with a self-defined structure? #7

Closed NewbieAlex closed 7 years ago

NewbieAlex commented 7 years ago

Hi Abel, Thank you for sharing this great work here. I'm using the "Market Data (Level 1)" example to monitor real-time price information. I need to get real-time bid/ask price, bid/ask size, and volume. In the tutorial, all the real-time data are contained in a buffer, and can be printed out. How can I extract the data in the buffer and get the bid/ask information explicitly? For example, ultimately I would like to have a numerical vector that has most recent bid/ask info, and keep updating the vector.

Thanks in advance, Alex

softwarespartan commented 7 years ago

Hi Alex

Great question.

The real-time data is a little bit different than something like realtime bars (5 second intervals)

essentially the big/ask size, vol etc can come any any freq the TWS wants to send it (i.e. whenever it gets updates from IB servers)

Therefore, TWS never “waits” to make a bar or anything like that. You just get latest update for whatever quantity has changes.

Do note that IB servers consolidate at like 150 ms so it’s not true real-time. But if you really hit an issue with 150 ms consolidation then that is an awesome problem to have ;)

Anyway, to your question, you just have to use the switch statement in the example

for i = 1:min(numel(mktDataEvents),10)

e = mktDataEvents{i};

switch e.data.tickId

    case 0
        % bid size 
    case 1
        % bid price 
    case 2 
        % ask price 
    case 3
        % ask size 
    case 4
        % last sale price
    case 5
        % last sale size
    case 8 
        % total volume
    otherwise
        % no op
end

end

Notice that the tickID for each market event changes for based on message/event type.

You can read more about tick types here:

https://www.interactivebrokers.com/en/software/api/apiguide/tables/tick_types.htm https://www.interactivebrokers.com/en/software/api/apiguide/tables/tick_types.htm

But in general, IB4m normalizes the market events into key-value pairs. so that event.data returns a com.tws.MarketData object.

If you look at the fields for com.tws.MarketData you will see “key”, “tickID”, and “value”.

Therefore, you know if tickID == 0 that the event has update for bid size where event.data.value is that new bid size.

Likewise if tickId == 2 you know that the market data event is for ask price where event.data.value is that new ask price.

Notice that the “key” field returns a string of the tick type but strings take a lot longer to compare so use tickId.

So finally, if you use that switch statement it will process the market data events appropriately. You just have to fill in what you want done for each tickId in the body of the cases.

Don’t hesitate to reach out again with additional questions or for more clarification.

Cheers -abel

On May 15, 2017, at 7:51 PM, NewbieAlex notifications@github.com wrote:

Hi Abel, Thank you for sharing this great work here. I'm using the "Market Data (Level 1)" example to monitor real-time price information. I need to get real-time bid/ask price, bid/ask size, and volume. In the tutorial, all the real-time data are contained in a buffer, and can be printed out. How can I extract the data in the buffer and get the bid/ask information explicitly? For example, ultimately I would like to have a numerical vector that has most recent bid/ask info, and keep updating the vector.

Thanks in advance, Alex

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/softwarespartan/IB4m/issues/7, or mute the thread https://github.com/notifications/unsubscribe-auth/AEWq1HrlLYvBbuyqGPO2VjLWIzooGxf1ks5r6JCwgaJpZM4NbeDH.

NewbieAlex commented 7 years ago

It worked out nicely. Thank you, Abel! Again, great work and sharing here!