Petersoj / alpaca-java

A Java API for Alpaca, the commission free, algo friendly, stock trading broker. https://alpaca.markets
https://petersoj.github.io/alpaca-java/
MIT License
198 stars 84 forks source link

Ability to add or remove Websocket ticker listeners #68

Closed jetonbacaj closed 3 years ago

jetonbacaj commented 4 years ago

If we need to add or remove a specific ticker from the alpaca and/or polygon, we have to remove the whole listener, reinitialize it with the updated ticker list, and then listen for it again.

Any way we can just add or remove tickers from the listener itself? @Petersoj

Petersoj commented 4 years ago

I'm thinking of using a javafx.beans.property.SimpleListProperty and whenever the underlying list changes within a Listener, subscriptions will be re-evaluated and updated accordingly. I'll start a PR soon.

jetonbacaj commented 3 years ago

I'm thinking of using a javafx.beans.property.SimpleListProperty and whenever the underlying list changes within a Listener, subscriptions will be re-evaluated and updated accordingly. I'll start a PR soon.

Wouldn't adding a "removeTicker" method, which in turn calls Polygon with a {"action":"unsubscribe","params":"{channel}.{ticker}"} (and the appropriate method+call for subscribing) be enough of a functionality, instead of having a List listener? The latter would be cool, but I would personally prefer explicit calls for sub/unsub, and not expose the actual list in any way.

Petersoj commented 3 years ago

I think it's a better design to keep the Listener interface that contains a list getter for the tickers and their associated channels (e.g. AlpacaStreamListener#getStreamMessageTypes). Perhaps what we could do is have some method in AlpacaAPI/PolygonAPI like refreshListener that will send the subscribing/unsubscribing messages based on the underlying updated list in the Listener interface. What do you think?

Petersoj commented 3 years ago

@jetonbacaj any thoughts on my previous comment?

jetonbacaj commented 3 years ago

I think that that would work, though depends on the implementation of the refreshListener. I'd avoid doing a unsubscribe then subscribe for all of the tickers in the list after the add/remove, as this could result in a ticker information data loss in the interim (especially for second bars, and even more so for trade and quote updates).

Unless I am misunderstanding your idea... which is completely possible, and quite probable. :-)

Petersoj commented 3 years ago

Version 8.0.0 no longer uses listeners with individual ticker lists so you can now (un)subscribe to symbols without adding/remove listeners.