softwarespartan / IB4m

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

EventHandler Example #11

Closed bondtrade closed 6 years ago

bondtrade commented 6 years ago

Hi Abel,

I read though a closed thread where you give some great examples on how to subscribe to data and process it with callbacks.

I was wondering if you would be kind enough to provide a detailed example which extends the previous ones using EventHandler to efficiently subscribe to multiple symbols, assign listeners/callbacks, and process the results please?

Great work.

softwarespartan commented 6 years ago

Thanks for reaching out

Apologies for delay in reply. Have been on travel.

The listeners just have to subclass and implement “process” method then can just call subscribe method for the event handler.

How many simultaneous listeners are you anticipating?

Will try to send additional details this weekend.

Cheers -abel

On Sep 7, 2017, at 12:02 AM, bondtrade notifications@github.com wrote:

Hi Abel,

I read though a closed thread where you give some great examples on how to subscribe to data and process it with callbacks.

I was wondering if you would be kind enough to provide a detailed example which extends the previous ones using EventHandler to efficiently subscribe to multiple symbols, assign listeners/callbacks, and process the results please?

Great work.

— 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/11, or mute the thread https://github.com/notifications/unsubscribe-auth/AEWq1MJUpFDF6-18hpe9uEqGdkJQfmg7ks5sf2rggaJpZM4PPQlD.

bondtrade commented 6 years ago

Hi Abel,

Thanks very much. OO programming is not my strong suit and this is my first time writing an execution system.

I love your framework - it’s working great with 16 symbols so far using snapshots on a timer and without the cost/licensing hassles of IB Matlab. I suspect your framework is “closer to the metal” than IB Matlab also with much less of the API wrapped/hidden.

I’d like a robust/clean/elegant way of going to perhaps 50-100 symbols if possible…

Cheers, Paul.

From: Abel Brown [mailto:notifications@github.com] Sent: Sunday, 17 September 2017 10:44 AM To: softwarespartan/IB4m IB4m@noreply.github.com Cc: bondtrade pgrant71@outlook.com; Author author@noreply.github.com Subject: Re: [softwarespartan/IB4m] EventHandler Example (#11)

Thanks for reaching out

Apologies for delay in reply. Have been on travel.

The listeners just have to subclass and implement “process” method then can just call subscribe method for the event handler.

How many simultaneous listeners are you anticipating?

Will try to send additional details this weekend.

Cheers -abel

On Sep 7, 2017, at 12:02 AM, bondtrade notifications@github.com wrote:

Hi Abel,

I read though a closed thread where you give some great examples on how to subscribe to data and process it with callbacks.

I was wondering if you would be kind enough to provide a detailed example which extends the previous ones using EventHandler to efficiently subscribe to multiple symbols, assign listeners/callbacks, and process the results please?

Great work.

— 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/11, or mute the thread https://github.com/notifications/unsubscribe-auth/AEWq1MJUpFDF6-18hpe9uEqGdkJQfmg7ks5sf2rggaJpZM4PPQlD.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHubhttps://github.com/softwarespartan/IB4m/issues/11#issuecomment-330004178, or mute the threadhttps://github.com/notifications/unsubscribe-auth/AeP_FZY_83Xb03uJt37TazH7FoPjqNygks5sjGtggaJpZM4PPQlD.

softwarespartan commented 6 years ago

Hi Paul

First you’ll need to have a look at TWS.EventListener abstract class. All listeners must inherit from TWS.EventListener. This ensures that all listeners implement the method “process()”. That is, when the market data handler gets a new bar from TWS it has to “give” the bar to all the “listeners” who have registered to receive bars for particular asset. The handler just looks at the listeners in the list and one by one calls listener.process(newbar). Since all listeners have inherited from TWS.EventListener the handler is guaranteed that “listener.process(newbar)” will not result in a runtime error.

Simply create a new TWS.MarketData.EventHandler and use the "subscribe” method to add a listener for a contract.

There is a TWS.MarketData.Buffer object that should work as an example of how to create objects that inherit from TWS.EventListener.

Using these few objects you can create market data buffers (or your own objects) that will scale to many many events. I have scaled this to hundreds of simultaneous contracts and thousands of listeners (data buffers, gui objects etc). It works really well.

Give that a try and let me know if/when you have follow up questions.

Cheers -abel

On Sep 16, 2017, at 9:42 PM, bondtrade notifications@github.com wrote:

Hi Abel,

Thanks very much. OO programming is not my strong suit and this is my first time writing an execution system.

I love your framework - it’s working great with 16 symbols so far using snapshots on a timer and without the cost/licensing hassles of IB Matlab. I suspect your framework is “closer to the metal” than IB Matlab also with much less of the API wrapped/hidden.

I’d like a robust/clean/elegant way of going to perhaps 50-100 symbols if possible…

Cheers, Paul.

From: Abel Brown [mailto:notifications@github.com] Sent: Sunday, 17 September 2017 10:44 AM To: softwarespartan/IB4m IB4m@noreply.github.com Cc: bondtrade pgrant71@outlook.com; Author author@noreply.github.com Subject: Re: [softwarespartan/IB4m] EventHandler Example (#11)

Thanks for reaching out

Apologies for delay in reply. Have been on travel.

The listeners just have to subclass and implement “process” method then can just call subscribe method for the event handler.

How many simultaneous listeners are you anticipating?

Will try to send additional details this weekend.

Cheers -abel

On Sep 7, 2017, at 12:02 AM, bondtrade notifications@github.com wrote:

Hi Abel,

I read though a closed thread where you give some great examples on how to subscribe to data and process it with callbacks.

I was wondering if you would be kind enough to provide a detailed example which extends the previous ones using EventHandler to efficiently subscribe to multiple symbols, assign listeners/callbacks, and process the results please?

Great work.

— 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/11, or mute the thread https://github.com/notifications/unsubscribe-auth/AEWq1MJUpFDF6-18hpe9uEqGdkJQfmg7ks5sf2rggaJpZM4PPQlD.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHubhttps://github.com/softwarespartan/IB4m/issues/11#issuecomment-330004178, or mute the threadhttps://github.com/notifications/unsubscribe-auth/AeP_FZY_83Xb03uJt37TazH7FoPjqNygks5sjGtggaJpZM4PPQlD. — You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/softwarespartan/IB4m/issues/11#issuecomment-330006568, or mute the thread https://github.com/notifications/unsubscribe-auth/AEWq1H9DzTxFqSeGzhrGi1L6QMWo-n_xks5sjHj_gaJpZM4PPQlD.

bondtrade commented 6 years ago

Hi Abel,

Thanks again. Would a simple example of what you mean be asking too much? You have provided great examples for the non-OO aspects that are easy to follow and implement but I can't find any example on what you're describing that make sense to me - Java and OOP illiterate. Examples for the most compelling method of implementation seem to be missing from the package.

Here's a couple of embarrassing attempts to (barely) demonstrate that I've had a crack and am not just lazily soliciting a bit of free spoon-feeding:

priceSPY = TWS.MarketData.EventListener(); % Works but no subscribe functionality priceSPY = TWS.MarketData('SPY'); % Does not work priceSPY = this@TWS.MarketData.EventHandler(); % Does not work

Surely a new class definition doesn't have to be created for each symbol? If it does, then I'm stuffed and it's back to uni for a year or so to learn Java and OOP, unless... examples please...

Cheers, Paul.

softwarespartan commented 6 years ago

Hi Paul

Sure no problem. I am traveling (without my laptop) so can send something tomorrow when I return.

Keep in mind that you can just open the TWS.MarketData src file and look at the function signature for subscribe. I know that’s not a substitute for docs but it works.

Also, subscribe takes a contract object just like regular API market data request. There are many examples on the docs for creating contracts.

Buffer object I mentioned in last comment is good example. Def open that file to understand how to use the Event data via process method.

Create contract Create buffer Call subscribe(contract,buffer) to link data from contract and/into buffer

-abel

Sent from my iPhone

On Oct 18, 2017, at 12:26 AM, bondtrade notifications@github.com wrote:

Hi Abel,

Thanks again. Would a simple example of what you mean be asking too much? You have provided great examples for the non-OO aspects that are easy to follow and implement but I can't find any example on what you're describing that make sense to me - Java and OOP illiterate. Examples for the most compelling method of implementation seem to be missing from the package.

Here's a couple of embarrassing attempts to (barely) demonstrate that I've had a crack and am not just lazily soliciting a bit of free spoon-feeding:

priceSPY = TWS.MarketData.EventListener(); % Works but no subscribe functionality priceSPY = TWS.MarketData('SPY'); % Does not work priceSPY = this@TWS.MarketData.EventHandler(); % Does not work

Surely a new class definition doesn't have to be created for each symbol? If it does, then I'm stuffed and it's back to uni for a year or so to learn Java and OOP, unless... examples please...

Cheers, Paul.

— You are receiving this because you commented. Reply to this email directly, view it on GitHub, or mute the thread.

softwarespartan commented 6 years ago

Hi Paul

Below is an example for how to subscribe to market data events.

As advertised, create handler, contract, and listener, then link listener and contract data.

% get TWS session session = TWS.Session.getInstance();

% connect the session to TWS running instance session.eClientSocket.eConnect('127.0.0.1',7496,0);

% init handler for market data handler = TWS.MarketData.EventHandler.getInstance();

% create a generic contrac contract = com.tws.ContractFactory.GenericStockContract('FB');

% init some listener that implements the listener interface (i.e. process()) buf = TWS.MarketData.Buffer(1024);

% subscribe the listener to the market reqid = handler.subscribe(contract,buf,[]);

You will see market data events queuing up in the buffer.

That is, ‘handler' now calls 'buf.process(event)' every time gets new market data for contract('FB’)

Use the TWS.MarketData.Buffer as an example of how to create your own listeners. This is very generic design patter for event listeners.

Every application will have it’s own different set of listeners. This way, developers can create any functionality desired and register arbitrarily many listeners for the same contract efficiently.

Do not hesitate to reach out if you have additional questions.

Thanks -abel

p.s. While in the code this morning I fixed a few bugs. Please do a ‘git pull’ for the latest updates.

On Oct 18, 2017, at 12:26 AM, bondtrade notifications@github.com wrote:

Hi Abel,

Thanks again. Would a simple example of what you mean be asking too much? You have provided great examples for the non-OO aspects that are easy to follow and implement but I can't find any example on what you're describing that make sense to me - Java and OOP illiterate. Examples for the most compelling method of implementation seem to be missing from the package.

Here's a couple of embarrassing attempts to (barely) demonstrate that I've had a crack and am not just lazily soliciting a bit of free spoon-feeding:

priceSPY = TWS.MarketData.EventListener(); % Works but no subscribe functionality priceSPY = TWS.MarketData('SPY'); % Does not work priceSPY = this@TWS.MarketData.EventHandler mailto:this@TWS.MarketData.EventHandler(); % Does not work

Surely a new class definition doesn't have to be created for each symbol? If it does, then I'm stuffed and it's back to uni for a year or so to learn Java and OOP, unless... examples please...

Cheers, Paul.

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/softwarespartan/IB4m/issues/11#issuecomment-337457016, or mute the thread https://github.com/notifications/unsubscribe-auth/AEWq1Ky26_1oVu3MwQxjzYNfxvYji7u5ks5stX3-gaJpZM4PPQlD.

bondtrade commented 6 years ago

Thanks so much Abel! Much appreciated. Makes sense now having seen your example. I'll get busy.

Cheers, Paul.

softwarespartan commented 6 years ago

No problem. Apologies for delay in getting sample code to you.

Let me know if you hit any snags or need anything additional.

-abel

On Oct 20, 2017, at 10:28 AM, bondtrade notifications@github.com wrote:

Thanks so much Abel! Much appreciated. Makes sense now having seen your example. I'll get busy.

Cheers, Paul.

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/softwarespartan/IB4m/issues/11#issuecomment-338222646, or mute the thread https://github.com/notifications/unsubscribe-auth/AEWq1FtYQbTbfsyGZfTpEp7Or0k6_wKEks5suK3wgaJpZM4PPQlD.