Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OnTradeUpdate not stable #170

Closed
pratttrader opened this issue Feb 6, 2020 · 10 comments
Closed

OnTradeUpdate not stable #170

pratttrader opened this issue Feb 6, 2020 · 10 comments
Assignees
Labels
bug Something isn't working question Further information is requested

Comments

@pratttrader
Copy link

I'm posting this here only because I'm using the C# API. I'm not sure if my issues are with the ALPACA backend or with the AlpacaStreamingClient.

I've been having many instances where I place on order....PostOrderAsync returns an accepted order, the order shows up in the ALPACA web interface(so it's active), but nothing gets returned ever through OnTradeUpdate. I have no way to debug this..so this is just an FYI for you. There also is no pattern to it. If I re-initialize the object, it's fine for awhile, then it stops and once it stops working it's dead. Some days it works fine all day and then others it just bombs. Should I open/close the streaming connection after every PostOrderAsync?

But as a result I need to strip out all use of AlpacaStreamingClient for now and move to ListOrdersAsync or GetOrderAsync..to verify the status of an active order.

Using 3.5.0.0

@zhaizhe111
Copy link

I have the same issue

@OlegRa OlegRa self-assigned this Feb 7, 2020
@OlegRa OlegRa added the bug Something isn't working label Feb 7, 2020
@OlegRa OlegRa added this to the SDK 3.6.0 Release milestone Feb 7, 2020
@OlegRa
Copy link
Collaborator

OlegRa commented Feb 7, 2020

@pratttrader @zhaizhe111 - I've tried to reproduce this problem on my side but (as expected) without any success. Could you please provide me more information about this problem?

  1. What is your target application is - is it a console app, Windows application, or web app (back-end)?
  2. Which version of .NET do you use for your application? I need the exact version of the used runtime and the exact version of SDK.
  3. Is it 32- or 64-bit application? I don't ask about OS bitness - it's about application bitness.
  4. What is your target OS - Windows, Linux, or maybe MacOS? Again, I need an exact version of your OS for creating a test environment.
  5. Have you subscribed on SocketClosed and OnError events of AlpacaStreamingClient class? If not, subscribe first one and do reconnect in case of the first one. SDK does not provide reconnect logic for streaming clients (but now I'm thinking about this optional feature).

@OlegRa OlegRa added the question Further information is requested label Feb 7, 2020
@pratttrader
Copy link
Author

pratttrader commented Feb 7, 2020

This is a Windows Desktop app, .NET 4.5 x86 running on Windows 64 bit.
I have not attached to those events...I was thinking about that though. I will try that.
Earlier today, for about an hour, I couldn't even enter an order through PostOrderAsync...it would return nothing...it must have been on their end, because it started working again. I posted a comment on their forum.
I'll wire up a handler for SocketClosed today and try it next week.

@OlegRa
Copy link
Collaborator

OlegRa commented Feb 7, 2020

@pratttrader Of course, the main question is - that Alpaca environment do you use? Paper-only environment updated much often and as a result, you can experience 'strange' API behavior often.

@pratttrader
Copy link
Author

yes, this is paper only..

@pratttrader
Copy link
Author

So should the DisconnectAsync() method raise the SocketClosed Event? To test my change, what should I use to "close" the socket? The only thing I see is DisconnectAsync, and that's not raising the SocketClosed Event.

@OlegRa
Copy link
Collaborator

OlegRa commented Feb 8, 2020

The DisconnectAsync call will not trigger the SocketClosed event - this event will be triggered by the underlying web socket component only in case of 'unexpected' disconnections. For example, if a web socket connection will be interrupted by a server or as a result of generic network failure. If you close connection from the client you know that it's closed and there is no reason to report it via this event.

@pratttrader
Copy link
Author

I'm trying to add my own auto-reconnect function in my application, using what is available to me from Alpaca.Markets. A streaming connection is useless if doesn't at least try to self-correct when the socket is broken for whatever reason. The idea I have is like other brokers do it where it tries to reconnect, say 10x, every 4 seconds and then just stop...assuming that the socket/network connection is just dead.

The problem I'm facing is this: if I'm connected with the AlpacaStreamingClient, and manually break the network connection...it raises the SocketClosed event. Good. But when I go back to ConnectAsync with the connection still broken, it raises "The socket is connected, you needn't connect again!" exception.....but it's not actually connected.

Is that just an incorrect error message? This is all being triggered from the SocketClosed event.

Thanks

@OlegRa
Copy link
Collaborator

OlegRa commented Feb 10, 2020

@pratttrader I'm sorry for your disappointments but unfortunately, the problem is not in Alpaca SDK itself and I don't know how to fix it easily right now.

We use WebSocket4Net library for all web socket communications in Alpaca SDK and according to their issue tracker, this library has a problem with re-connection logic (see details here).

I see the next possibilities for you (from simplest to hardest from my perspective):

  1. Re-create the whole AlpacaStreamingClient object on each connection lost. Yes, it's expensive but in this case, you will have a new fresh connection with a new underlying web socket object.
  2. Try to implement your own IWebSocketFactory interface using the existing WebSocket4NetFactory class as an example. In this custom interface implementation, you can re-create only the underlying WebSocket object and resolve this issue.
  3. Try another web socket client available in Alpaca SDK - WebSocketSharp. This client available only in .NET 4.5 builds of SDK and you need more work for enabling it but it can be an option for you.
  4. Try to push WebSocket4Net maintainers and force problem fixing on their side. On my side, I can re-integrate the updated version of WebSocket4Net library into Alpaca SDK as soon as problem will be fixed.

Implementing re-connection logic inside SDK is a breaking change and I'm not ready to do it right now (need more time for designing this feature properly).

@pratttrader
Copy link
Author

Thanks for the reply. I'm not disappointed because this is what I do.

I currently use another broker and before I move everything to ALPACA I just need to test everything from a trading perspective and a technology perspective.

I'm not asking for an implementation of logic because in my view, that's my responsibliity. I was pointing out that I'm getting "Socket Connected", when it's not...which is hard to use on my end. All I have to work with are exceptions, object states and properties, and if something is inaccurate, it makes it hard.

Right now I have it set to attemptt a reconnect, and if that fails (as in, my provider's connection has dropped), it releases the ALPACA objects and alerts me. From there, I might cycle every 10 seconds to try again. I'm also using another broker's connection for real-time prices, so I handle their exceptions and tie that it into my ALPACA code..but I need to plan on the assumption that that wont always be there.

Additionally I'm building a seperate "Network Listener" which monitors my own network connection.

I think have a plan going forward...thanks for the help.

OlegRa added a commit that referenced this issue Oct 26, 2022
* docs: add OlegRa as a contributor for code, doc, and 7 more (#121)
* docs: add ttt733 as a contributor for code, example, and tutorial (#124)
* docs: add ooples as a contributor for code, example, and bug (#125)
* docs: add ElektroMech as a contributor for code (#126)
* docs: add biyimaks as a contributor for code (#127)
* docs: add gjtorikian as a contributor for code (#128)
* docs: add ajit-kolathur as a contributor for code (#129)
* docs: add natehitze as a contributor for code (#130)
* docs: add Hard-Coder05 as a contributor for code (#131)
* docs: add amwsis as a contributor for code (#132)
* docs: add Dav-id as a contributor for doc (#133)
* docs: add shlomikushchi as a contributor for doc (#134)
* docs: add PrometheusUno as a contributor for doc (#135)
* docs: add schmich as a contributor for doc (#136)
* docs: add Ronmenator as a contributor for doc (#137)
* docs: add codebeaulieu as a contributor for code (#138)
* docs: add Xallen79 as a contributor for code (#139)
* docs: add jcjurevis as a contributor for code (#140)
* docs: add koolinoor as a contributor for code, and bug (#141)
* docs: add umitanuki as a contributor for bug, ideas, and 2 more (#142)
* docs: add smartchris84 as a contributor for bug (#143)
* docs: add codebeaulieu as a contributor for bug (#144)
* docs: add mercalli as a contributor for bug (#145)
* docs: add thedudebond as a contributor for bug (#146)
* docs: add biyimaks as a contributor for bug (#147)
* docs: add xutao650 as a contributor for bug (#148)
* docs: add ttt733 as a contributor for bug (#149)
* docs: add Ronmenator as a contributor for bug (#150)
* docs: add sghitch as a contributor for bug (#151)
* docs: add Wingspear as a contributor for bug (#152)
* docs: add jameswhollister as a contributor for bug (#153)
* docs: add DragonMastery as a contributor for bug (#154)
* docs: add apfitzge as a contributor for bug, and ideas (#155)
* docs: add apasumarthi1999 as a contributor for bug (#156)
* docs: add pratttrader as a contributor for bug (#157)
* docs: add tclzcja as a contributor for bug (#158)
* docs: add SavageShade as a contributor for bug (#159)
* docs: add nkoehler as a contributor for bug (#160)
* docs: add camerontbelt as a contributor for bug (#161)
* docs: add adrianbegi as a contributor for bug (#162)
* docs: add FranMaher as a contributor for bug (#163)
* docs: add softwaresalt as a contributor for bug, and ideas (#164)
* docs: add kwcoffee1 as a contributor for bug (#165)
* docs: add gsalaz98 as a contributor for bug (#166)
* docs: add jcjurevis as a contributor for bug, and ideas (#167)
* docs: add thi517 as a contributor for bug (#168)
* docs: add SteveHaudegen as a contributor for bug (#169)
* docs: add samueldonovan1701 as a contributor for bug (#170)
* docs: add jzderadicka as a contributor for bug (#171)
* docs: add aliasgharrajabi as a contributor for bug (#172)
* docs: add Huios as a contributor for bug (#173)
* docs: add rudeGit as a contributor for bug (#174)
* docs: add lgreenlee as a contributor for bug (#175)
* docs: add r-ulak as a contributor for bug (#176)
* docs: add zhorakiev as a contributor for bug, and ideas (#177)
* docs: add shlomikushchi as a contributor for ideas (#178)
* docs: add liftee as a contributor for bug (#179)
* docs: add TonyC1Ward as a contributor for bug (#180)
* docs: add jeffwlandry as a contributor for bug (#181)
* docs: add nbmrao as a contributor for bug (#182)
* docs: add mehtadharmesh as a contributor for bug (#183)
* docs: add Ricbun16 as a contributor for bug (#184)
* docs: add ayushguptacoder as a contributor for bug (#185)
* docs: add opened as a contributor for bug (#186)
* docs: add bizjaya as a contributor for bug (#187)
* docs: add raduistrate as a contributor for bug (#188)
* docs: add woaksie as a contributor for bug (#189)
* docs: add ktrauberman as a contributor for bug (#190)
* docs: add gchudublin as a contributor for bug (#191)
* docs: add mattrobinson3 as a contributor for bug (#192)
* docs: add Tifoid as a contributor for bug (#193)
* docs: add AdsophicSolutions as a contributor for bug, and ideas (#194)
* docs: add waymanapps as a contributor for ideas, and question (#195)
* docs: add tstraus13 as a contributor for bug (#196)
* docs: add matt953 as a contributor for bug, and ideas (#197)
* docs: add YongliangLi as a contributor for bug, and ideas (#198)
* docs: add polymorphicshade as a contributor for bug, and question (#199)
* docs: add nathan-datusarator as a contributor for bug (#200)
* docs: add ManfredLange as a contributor for bug, and ideas (#201)
* docs: add JCloarec as a contributor for bug (#202)
* docs: add jeonnagroup2 as a contributor for bug (#203)
* docs: add IsaackRasmussen as a contributor for bug (#204)
* docs: add matsydoodles as a contributor for bug (#205)
* docs: add natehitze as a contributor for doc (#206)
* docs: add logmd as a contributor for code (#207)
* docs: add tdhatcher as a contributor for bug (#208)
* docs: add TheHumanWithAPlan as a contributor for bug (#209)
* docs: add tcj2001 as a contributor for bug (#210)
* docs: add JasonAr936 as a contributor for bug, and ideas (#211)
* docs: add markns as a contributor for bug (#213)
* docs: add Ilya171 as a contributor for bug (#214)
* docs: add waymanapps as a contributor for financial (#215)

Co-authored-by: allcontributors[bot] <46447321+allcontributors[bot]@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working question Further information is requested
Projects
None yet
Development

No branches or pull requests

3 participants