Skip to content
This repository has been archived by the owner on Jun 8, 2020. It is now read-only.

[bitfinex] Added some methods of authenticated api #209

Closed
wants to merge 8 commits into from

Conversation

Mikelle
Copy link
Contributor

@Mikelle Mikelle commented Jul 10, 2018

Added web socket support for getting orders, pre trades and trades, balaces updates

@badgerwithagun
Copy link
Collaborator

Nice, @Mikelle! 👍

I'm quite excited by this development; my own application really struggles due to poll limits on exchanges for private authenticated data, so adding streaming support would make a big difference.

Two questions I think we need to answer:

  1. It's a significant decision in principle to extend the scope of xchange-stream to cover authenticated streaming data, so we need @dozd's thoughts on this.
  2. Your changes are very BFX focused, rather than introducing a generic API we can extend to cover other exchanges. That's fine, but I think we need to introduce a generic API so that we can introduce other exchanges later. Assuming @dozd is in favour in principle, would you be willing to work together to define a suitable API?

@badgerwithagun
Copy link
Collaborator

Hmmm, crossover with #160.

@bryantharris - your thought here?
@dozd - can we get #160 merged?

@bryantharris
Copy link
Contributor

@badgerwithagun I think the right approach is a two layered approach the same as the REST XChange project that this streaming project is based on.

Basically, layer 0 is exchange specific supporting 1:1 streaming events as to what is supported on the exchange ws API.
Then a layer 1, which would represent generic support, which is implemented using layer 0. This would be the consistent API to the extent there is commonality shared across all streaming implementations.

You'd typically use the common API but could always drop to the lower level if there were specific event messages you wanted to follow.

With respect to authenticated streaming, I give a resounding thumbs up that it should be supported. To me the primary advantage of authenticated streaming is real time updates to order status changes without polling.

I get viewing tickers (non authenticated), but the real power seems to be in having the stream be authenticated and receiving private events IMHO.

@Mikelle
Copy link
Contributor Author

Mikelle commented Aug 6, 2018

@badgerwithagun @bryantharris So what should I do to make you accept the pull request? Develop new generic API for authenticated streaming?

@badgerwithagun
Copy link
Collaborator

@Mikelle: thanks for sticking with this. It would make sense to follow the same approach as @bryantharris has followed on #160 for trades: that is, if we are authenticated, then subscribe to the private trades stream and supply both Trades and UserTrades in the existing trades stream API.

However, when it comes to orders, we currently lack any generic API, so you will need to define a new one. I think the Orderbook stream API is the closest pattern to follow (maintain a snapshot of the user's order list in memory and keep that updated from the exchange stream). The level 0 API underlying that would be the actual stream of changes.

I don't have the power to approve change requests though; just helping out. There are others on here who are closer to the code who would have clearer ideas.

@Mikelle Mikelle changed the title [bitfinex] Added some method of authenticated api [bitfinex] Added some methods of authenticated api Aug 15, 2018
@pchertalev
Copy link
Contributor

So we are waiting for approve pool request of #160 for using the same approach for Bitfinex, right?

Copy link
Collaborator

@badgerwithagun badgerwithagun left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a really nice-looking PR. Great work.

If it could just be adjusted to fit into the existing patterns for authentication and L1 APIs, I reckon it's good for a merge.

Do you have time to follow this up?

@@ -57,4 +59,28 @@ public StreamingMarketDataService getStreamingMarketDataService() {
@Override
public void useCompressedMessages(boolean compressedMessages) { streamingService.useCompressedMessages(compressedMessages); }

public Completable connectToAuthenticated() {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it possible to make this connection/authentication transparent, so it occurs automatically if authentication details are available, as we do for GDAX?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done on my uplift fork


public BitfinexStreamingExchange() {
this.streamingService = new BitfinexStreamingService(API_URI);
this.streamingAuthenticatedDataService = new BitfinexStreamingRawService(API_URI);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need to have a second connection to BFX for the authenticated stream, or is it possible to just authenticate the existing connection?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to the BFX documentation, it very much looks like this is the case, so I'm going to try it.

}
}

public Observable<BitfinexWebSocketAuthOrder> getAuthenticatedOrders() {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's an existing pattern for exposing L1 (exchange specific) methods. Example: BinanceStreamingMarketDataService.getRawTicker(). Could we move these to the StreamingMarketDataService? e.g. getRawAuthenticatedOrders()?

return subjectPreTrade.share();
}

public Observable<BitfinexWebSocketAuthTrade> getAuthenticatedTrades() {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd like it if we could do as we do for GDAX, and merge the authenticated trades (as UserTrade) with the public trades (as Trades) and return them in the getTrades() stream. However, just implementing the L1 API is valuable on its own, so if you don't fancy doing this, that's fine. We can raise an issue for it and see if someone else picks it up (I might).

@@ -0,0 +1,303 @@
package info.bitrich.xchangestream.bitfinex;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've not reviewed this code - I suspect it might move around quite a lot based on my other comments.

Will review properly once the structure is right.

exchange.setCredentials("API-KEY", "API-SECRET");

exchange.connectToAuthenticated().blockingAwait();
exchange.getStreamingAuthenticatedDataService().getAuthenticatedTrades().subscribe(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SO, to clarify my earlier comments, this would be more "standard" like this:

ExchangeSpecification specification = StreamingExchangeFactory.INSTANCE.createExchange(BitfinexStreamingExchange.class.getName()).getDefaultExchangeSpecification();
specification.setApiKey("API-KEY");
specification.setSecretKey("API-SECRET");
BitfinexStreamingExchange exchange = (BitfinexStreamingExchange)StreamingExchangeFactory.INSTANCE.createExchange(specification);

exchange.getStreamingMarketDataService().getRawAuthenticatedTrades().subscribe(
 ...
);

So, use the existing authentication details provided by the exchange specification, authenticate by default if the authentication details are available, and use L1 "raw" methods on the StreamingMarketDataService itself.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done on my uplift fork

@davidjirovec
Copy link
Contributor

Any plans on merging this?

@Flemingjp
Copy link
Collaborator

@davidjirocev If it's tested and reviewed, then most likely.

@davidjirovec
Copy link
Contributor

I'm running this branch a few months :)

@Flemingjp
Copy link
Collaborator

@Mikelle any chance you could work on the comments @badgerwithagun made?

@badgerwithagun
Copy link
Collaborator

I'm going to fork this PR and bring it in line with the work I'm doing to commonise support for authenticated streams across multiple exchanges (see the thread on #246 for details).

Once #246 is merged I'll be able to resubmit.

@badgerwithagun
Copy link
Collaborator

OK, the uplift of this PR to resolve conflicts, answer the review comments and bring it in line with other exchanges is now underway and can be tracked on https://github.com/badgerwithagun/xchange-stream/tree/bitfinex-authenticated.

@badgerwithagun
Copy link
Collaborator

@Mikelle, is there a reason why you used a separate NettyStreamingService for the authenticated stream, rather than just sending an AUTH on the existing stream?

@badgerwithagun
Copy link
Collaborator

OK, I have created my modified version as #273. This covers all my review issues.

I guess this one can be closed if everyone's happy with the modifications.

@badgerwithagun
Copy link
Collaborator

Closing this in the absence of any complaints. Can re-open if necessary.

Please head over to #273.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants