softwarespartan / IB4m

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

Recommended connection method for multiple classes #17

Closed bondtrade closed 6 years ago

bondtrade commented 6 years ago

Hi Abel,

I've written two classes that try and connect to TWS - 1) a "datahandler" class that receives streaming data (that then in turn feeds it into a "strategy" class), and 2) an "exectionhandler" class which acts as a interface to TWS for orders generated by a "portfolio" class.

I am having difficulty making both classes connect to TWS if they attempt to create connections within the class. Should a connection/session object be created outside of the classes and passed into them or do you have another recommended approach?

Thanks again. Cheers, Paul.

softwarespartan commented 6 years ago

Hi Paul

Good to hear from you.

hm … what do you mean by “problem making both classes connect to TWS”?

do you have error or stack trace?

The TWS.Session object is a singleton

What “singleton” means is that there is only ever a single instance of TWS.Session object

All objects/classes/codes/whatever can share the same session object

When you call TWS.Session.getInstance() what you are effectively asking for is “hey give me the single instance of session please”

On the first call TWS.Session.getInstance() sees that session does not exist and so creates the instance

All subsequent calls to TWS.Session.getInstance() returns that same instance

There is no perf penalty for calling TWS.Session.getInstance()

You can check if session is connected with session.eClientSocket.isConnected()

But also, you can call connect on an already connected session with no adverse effects.

So any time you need to make an API call any where in your codes, just call TWS.Session.getInstance() to get the session handle.

At any rate, let me know if this helps and send more details if you can.

Happy Holidays!

-abel

On Dec 18, 2017, at 7:57 PM, bondtrade notifications@github.com wrote:

Hi Abel,

I've written two classes that try and connect to TWS - 1) a "datahandler" class that receives streaming data (that then in turn feeds it into a "strategy" class), and 2) an "exectionhandler" class which acts as a interface to TWS for orders generated by a "portfolio" class.

I am having difficulty making both classes connect to TWS if they attempt to create connections within the class. Should a connection/session object be created outside of the classes and passed into them or do you have another recommended approach?

Thanks again. Cheers, Paul.

— 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/17, or mute the thread https://github.com/notifications/unsubscribe-auth/AEWq1PXuyrsAuMeYqZx1PAaNBXUzDgbOks5tBwnngaJpZM4RGS8t.

bondtrade commented 6 years ago

Hi Abel,

No error was generated as such but only one connection would “stick” between Matlab and TWS. In the Connections window in TWS two connections appeared at first and one disconnects straightaway leaving only one classdef with a connection.

At any rate, the singleton explanation is what I was after thanks. Now I just save the session object to a variable outside of the classes, connect, and then pass the session object into the classes. Works fine.

Thanks again and happy holidays to you too!

Cheers, Paul.

From: Abel Brown [mailto:notifications@github.com] Sent: Tuesday, 19 December 2017 12:43 PM To: softwarespartan/IB4m IB4m@noreply.github.com Cc: bondtrade pgrant71@outlook.com; Author author@noreply.github.com Subject: Re: [softwarespartan/IB4m] Recommended connection method for multiple classes (#17)

Hi Paul

Good to hear from you.

hm … what do you mean by “problem making both classes connect to TWS”?

do you have error or stack trace?

The TWS.Session object is a singleton

What “singleton” means is that there is only ever a single instance of TWS.Session object

All objects/classes/codes/whatever can share the same session object

When you call TWS.Session.getInstance() what you are effectively asking for is “hey give me the single instance of session please”

On the first call TWS.Session.getInstance() sees that session does not exist and so creates the instance

All subsequent calls to TWS.Session.getInstance() returns that same instance

There is no perf penalty for calling TWS.Session.getInstance()

You can check if session is connected with session.eClientSocket.isConnected()

But also, you can call connect on an already connected session with no adverse effects.

So any time you need to make an API call any where in your codes, just call TWS.Session.getInstance() to get the session handle.

At any rate, let me know if this helps and send more details if you can.

Happy Holidays!

-abel

On Dec 18, 2017, at 7:57 PM, bondtrade notifications@github.com wrote:

Hi Abel,

I've written two classes that try and connect to TWS - 1) a "datahandler" class that receives streaming data (that then in turn feeds it into a "strategy" class), and 2) an "exectionhandler" class which acts as a interface to TWS for orders generated by a "portfolio" class.

I am having difficulty making both classes connect to TWS if they attempt to create connections within the class. Should a connection/session object be created outside of the classes and passed into them or do you have another recommended approach?

Thanks again. Cheers, Paul.

— 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/17, or mute the thread https://github.com/notifications/unsubscribe-auth/AEWq1PXuyrsAuMeYqZx1PAaNBXUzDgbOks5tBwnngaJpZM4RGS8t.

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