bmello4688 / TDAmeritradeApi

Unofficial TD Ameritrade CSharp API Library
Apache License 2.0
12 stars 5 forks source link

Application architecture guidance - Multiple clients? #4

Closed Buriska closed 2 years ago

Buriska commented 2 years ago

Hello,

I'm in the process of building a small WinForms trading system using this project and ran into a few issues regarding architecture and was hoping you could provide some guidance.

I have 2 modules so far, RiskManager and OrderManager. Each 'Manager' currently adds it's own event to a global TDAmeritradeClient that I initialize during "Login" (form where use enters credentials and connects to the API)

My thinking here was to keep my operations separate, the event under RiskManager is solely used in that module (updating prices on current positions) and isn't touched by any other part of the system. The issue I've run into is that sharing a global client across the application leads to the events running over each other when data is received (for example, a new order coming in triggers both the event for AccountActivity streaming and MarketData streaming).

Should I be initializing a new client per module? Is there some better way to handle receiving data with multiple events across the application?

bmello4688 commented 2 years ago

Hi Boris,

You really just want one client. You could wrap the client in a new class and split the events out into different event types. You could define multiple interfaces on your new class to reduce the scope down for the classes using them. I’m contributing to QuantConnect. You can check out my current draft pull request here.

https://github.com/QuantConnect/Lean.Brokerages.TDAmeritrade/pull/1/files

For example,

Public class TDBrokerage: IOrderActivity, IRiskActivity { }

Public interface IOrderActivity { Public event Action OrderChangedEvent; }

Brian

Sent from Mailhttps://go.microsoft.com/fwlink/?LinkId=550986 for Windows

From: @.> Sent: Monday, November 1, 2021 11:33 AM To: @.> Cc: @.***> Subject: [bmello4688/TDAmeritradeApi] Application architecture guidance - Multiple clients? (Issue #4)

Hello,

I'm in the process of building a small WinForms trading system using this project and ran into a few issues regarding architecture and was hoping you could provide some guidance.

I have 2 modules so far, RiskManager and OrderManager. Each 'Manager' currently adds it's own event to a global TDAmeritradeClient that I initialize during "Login" (form where use enters credentials and connects to the API)

My thinking here was to keep my operations separate, the event under RiskManager is solely used in that module (updating prices on current positions) and isn't touched by any other part of the system. The issue I've run into is that sharing a global client across the application leads to the events running over each other when data is received (for example, a new order coming in triggers both the event for AccountActivity streaming and MarketData streaming).

Should I be initializing a new client per module? Is there some better way to handle receiving data with multiple events across the application?

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHubhttps://github.com/bmello4688/TDAmeritradeApi/issues/4, or unsubscribehttps://github.com/notifications/unsubscribe-auth/AB3WTQXNEVFH5IXVTXVXD5TUJ2XMHANCNFSM5HEJTNEA. Triage notifications on the go with GitHub Mobile for iOShttps://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Androidhttps://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub.

Buriska commented 2 years ago

Well, this opened up a new rabbit hole to go down.... Lean looks like it has everything I need and was working on building for myself.

Thanks for this & answering the original question

Buriska commented 2 years ago

LEAN / Quantconnect is really something else, thank you so much for the suggestion. Also, great work so far on the TD brokerage integration!