Coder Social home page Coder Social logo

marfusios / bitfinex-client-websocket Goto Github PK

View Code? Open in Web Editor NEW
55.0 12.0 39.0 5.63 MB

πŸ› οΈ C# client for Bitfinex & Ethfinex websocket API version 2.0

License: Apache License 2.0

C# 100.00%
bitfinex websockets cryptocurrency api-client bitcoin trade dotnet-core ethfinex bitfinex-websocket-api bitfinex-wss

bitfinex-client-websocket's Introduction

Logo

Bitfinex & Ethfinex websocket API client

Build Status NuGet version NuGet downloads

This is a C# implementation of the Bitfinex & Ethfinex websocket API version 2.0 found here:

https://bitfinex.readme.io/v2/docs (Ethfinex)

You can do almost everything with provided websocket API. Using REST API is unnecessary! As a benefit, you will get real-time data and fast execution of your commands.

Releases and breaking changes

License:

Apache License 2.0

Features

  • installation via NuGet (Bitfinex.Client.Websocket)
  • public and authenticated API
  • targeting .NET Standard 2.0 (.NET Core, Linux/MacOS compatible)
  • reactive extensions (Rx.NET)
  • integrated logging abstraction (LibLog)

Usage

var exitEvent = new ManualResetEvent(false);
var url = BitfinexValues.ApiWebsocketUrl;

using (var communicator = new BitfinexWebsocketCommunicator(url))
{
    using (var client = new BitfinexWebsocketClient(communicator))
    {
        client.Streams.InfoStream.Subscribe(info =>
        {
            Log.Information($"Info received, reconnection happened, resubscribing to streams");
            
            await client.Send(new PingRequest() {Cid = 123456});
            //await client.Send(new TickerSubscribeRequest("BTC/USD"));
        });

        client.Streams.PongStream.Subscribe(pong =>
        {
            Console.WriteLine($"Pong received! Id: {pong.Cid}") // Pong received! Id: 123456
            exitEvent.Set();
        });

        await communicator.Start();

        exitEvent.WaitOne(TimeSpan.FromSeconds(30));
    }
}

More usage examples:

  • integration tests (link)
  • console sample (link)
  • desktop sample (link)

API coverage

PUBLIC Covered
Info βœ”
Ping-Pong βœ”
Errors βœ”
Configuration βœ”
Channel subscribing βœ”
Channel unsubscribing βœ”
Ticker βœ”
Ticker - funding
Trades βœ”
Trades - funding βœ”
Books βœ”
Books - funding βœ”
Raw books βœ”
Raw books - funding βœ”
Candles βœ”
Funding βœ”
Sequencing βœ”
Server timestamp βœ”
Book checksum βœ”
AUTHENTICATED Covered
Account info βœ”
Orders βœ”
Positions βœ”
Trades βœ”
Funding
Wallets βœ”
Balance βœ”
Notifications βœ”
AUTHENTICATED - INPUT Covered
New order βœ”
Update order βœ”
Cancel order βœ”
Cancel order multi βœ”
Order multi-op
New offer
Cancel offer
Calc βœ”

Pull Requests are welcome!

Other websocket libraries


Extensions
All order books together, etc.

Bitmex

Binance

Coinbase

Placing orders

Bitfinex supports input authenticated API via websockets. You are able to place, update, cancel orders. Also via multi batch. Usage:

// placing buy
client.Send(new NewOrderRequest(gid: 33, cid: 100, "ETH/USD", OrderType.Limit, 0.2, 163) {Flags = OrderFlag.PostOnly});

// palcing sell
client.Send(new NewOrderRequest(gid: 33, cid: 200, "ETH/USD", OrderType.Limit, -0.2, 188) { Flags = OrderFlag.PostOnly });

// updating by client id
client.Send(new UpdateOrderRequest(new CidPair(100, DateTime.UtcNow)) { Amount = 0.3, Price = 161});

// canceling by client id
client.Send(new CancelOrderRequest(new CidPair(100, DateTime.UtcNow)));

// other canceling options
client.Send(CancelMultiOrderRequest.CancelEverything());
client.Send(CancelMultiOrderRequest.CancelGroup(33));
client.Send(new CancelMultiOrderRequest(new[]
{
    new CidPair(100, DateTime.UtcNow),
    new CidPair(200, DateTime.UtcNow)
}));

Reconnecting

There is a built-in reconnection which invokes after 1 minute (default) of not receiving any messages from the server. It is possible to configure that timeout via communicator.ReconnectTimeoutMs. Also, there is a stream ReconnectionHappened which sends information about a type of reconnection. However, if you are subscribed to low rate channels, it is very likely that you will encounter that timeout - higher the timeout to a few minutes or call PingRequest by your own every few seconds.

In the case of Bitfinex outage, there is a built-in functionality which slows down reconnection requests (could be configured via communicator.ErrorReconnectTimeoutMs, the default is 1 minute).

Beware that you need to resubscribe to channels after reconnection happens. You should subscribe to Streams.InfoStream, Streams.AuthenticationStream and send subscriptions requests (see #12 for example).

Backtesting

The library is prepared for backtesting. The dependency between Client and Communicator is via abstraction IBitfinexCommunicator. There are two communicator implementations:

  • BitfinexWebsocketCommunicator - a realtime communication with Bitfinex via websocket API.
  • BitfinexFileCommunicator - a simulated communication, raw data are loaded from files and streamed. If you are interested in buying historical raw data (trades, order book events), contact me.

Feel free to implement IBitfinexCommunicator on your own, for example, load raw data from database, cache, etc.

Usage:

var communicator = new BitfinexFileCommunicator();
communicator.FileNames = new[]
{
    "data/bitfinex_raw_2018-11-12.txt"
};
communicator.Delimiter = ";;";

var client = new BitfinexWebsocketClient(communicator);
client.Streams.TradesStream.Subscribe(trade =>
{
    // do something with trade
});

await communicator.Start();

Multi-threading

Observables from Reactive Extensions are single threaded by default. It means that your code inside subscriptions is called synchronously and as soon as the message comes from websocket API. It brings a great advantage of not to worry about synchronization, but if your code takes a longer time to execute it will block the receiving method, buffer the messages and may end up losing messages. For that reason consider to handle messages on the other thread and unblock receiving thread as soon as possible. I've prepared a few examples for you:

Default behavior

Every subscription code is called on a main websocket thread. Every subscription is synchronized together. No parallel execution. It will block the receiving thread.

client
    .Streams
    .TradesStream
    .Subscribe(trade => { code1 });

client
    .Streams
    .BookStream
    .Subscribe(book => { code2 });

// 'code1' and 'code2' are called in a correct order, according to websocket flow
// ----- code1 ----- code1 ----- ----- code1
// ----- ----- code2 ----- code2 code2 -----

Parallel subscriptions

Every single subscription code is called on a separate thread. Every single subscription is synchronized, but different subscriptions are called in parallel.

client
    .Streams
    .TradesStream
    .ObserveOn(TaskPoolScheduler.Default)
    .Subscribe(trade => { code1 });

client
    .Streams
    .BookStream
    .ObserveOn(TaskPoolScheduler.Default)
    .Subscribe(book => { code2 });

// 'code1' and 'code2' are called in parallel, do not follow websocket flow
// ----- code1 ----- code1 ----- code1 -----
// ----- code2 code2 ----- code2 code2 code2

Parallel subscriptions with synchronization

In case you want to run your subscription code on the separate thread but still want to follow websocket flow through every subscription, use synchronization with gates:

private static readonly object GATE1 = new object();
client
    .Streams
    .TradesStream
    .ObserveOn(TaskPoolScheduler.Default)
    .Synchronize(GATE1)
    .Subscribe(trade => { code1 });

client
    .Streams
    .BookStream
    .ObserveOn(TaskPoolScheduler.Default)
    .Synchronize(GATE1)
    .Subscribe(book => { code2 });

// 'code1' and 'code2' are called concurrently and follow websocket flow
// ----- code1 ----- code1 ----- ----- code1
// ----- ----- code2 ----- code2 code2 ----

Async/Await integration

Using async/await in your subscribe methods is a bit tricky. Subscribe from Rx.NET doesn't await tasks, so it won't block stream execution and cause sometimes undesired concurrency. For example:

client
    .Streams
    .TradesStream
    .Subscribe(async trade => {
        // do smth 1
        await Task.Delay(5000); // waits 5 sec, could be HTTP call or something else
        // do smth 2
    });

That await Task.Delay won't block stream and subscribe method will be called multiple times concurrently. If you want to buffer messages and process them one-by-one, then use this:

client
    .Streams
    .TradesStream
    .Select(trade => Observable.FromAsync(async () => {
        // do smth 1
        await Task.Delay(5000); // waits 5 sec, could be HTTP call or something else
        // do smth 2
    }))
    .Concat() // executes sequentially
    .Subscribe();

If you want to process them concurrently (avoid synchronization), then use this

client
    .Streams
    .TradesStream
    .Select(trade => Observable.FromAsync(async () => {
        // do smth 1
        await Task.Delay(5000); // waits 5 sec, could be HTTP call or something else
        // do smth 2
    }))
    .Merge() // executes concurrently
    // .Merge(4) you can limit concurrency with a parameter
    // .Merge(1) is same as .Concat()
    // .Merge(0) is invalid (throws exception)
    .Subscribe();

More info on Github issue.

Don't worry about websocket connection, those sequential execution via .Concat() or .Merge(1) has no effect on receiving messages. It won't affect receiving thread, only buffers messages inside TradesStream.

But beware of producer-consumer problem when the consumer will be too slow. Here is a StackOverflow issue with an example how to ignore/discard buffered messages and always process only the last one.

Desktop application (WinForms or WPF)

Due to the large amount of questions about integration of this library into a desktop application (old full .NET Framework), I've prepared WinForms example (link).

WinForms example screen

Available for help

I do consulting, please don't hesitate to contact me if you have a custom solution you would like me to implement (web, [email protected])

bitfinex-client-websocket's People

Contributors

aquisio avatar axiomaticcrab avatar danielboba avatar guido112 avatar marfusios avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

bitfinex-client-websocket's Issues

No quotes

I'm going
bitfinexAPI.Send(new TickerSubscribeRequest("BTC/USD"));
with
bitfinexAPI.Streams.TickerStream.Subscribe(ticker =>
{
btcusdBid = Convert.ToDecimal(ticker.Bid);
btcusdAsk = Convert.ToDecimal(ticker.Ask);
});
but no quotes are streaming
By the way Ping pong is okay

Expose Symbol property on Ticker and Trade classes

This is an excellent library. Thank you for creating it.

I have modified my local clone to expose a Symbol property on the Ticker and Trade classes, implemented exactly as you have done in Book and RawBook.

It would be great if this could be added to your codebase, so that I can go back to consuming it from the Nuget package.

Thanks again,
Tim

Orderbook crosses in book stream

Not an issue, but a question regarding the data quality from Bitfinex: I've noticed that sometimes the raw orderbook stream will send bids that cross the best ask at that point, and vice versa. I'm wondering if you had observed the same issues and how you go about remediating them. For now I'm just removing all crossed quotes from the book when the situation occurs, but it feels like I might not have an accurate representation of the real book.

I've checked that there was indeed no level removal (book.Count == 0) at the crossed price beforehand, so my raw data confirms the issue is happening. So I'm wondering if I might have missed some updates, ie. dropped messages. Would you suggest to always use the server sequence number? In some code comment you write that it should not be required due to TCP transport guarantees. I've also seen the checksum option but it seems heavy-handed and would increase traffic quite a bit.

Thanks for your insights, and really appreciate the high-quality library you're providing!

Funding ticker?

Hi,

I would like to access funding ticker for USD. I tried following:

 client.Send(new TickerSubscribeRequest("fUSD")); //Not working
 client.Send(new TickerSubscribeRequest("USD"));  //Not working

 client.Send(new TickerSubscribeRequest("BTC/USD"));//WORKS

Any ideas what might be wrong? :-)

Tried to use the console example to place trade, API responds auth: invalid, code: 10100

Output on screeen:

|=======================|
| BITFINEX CLIENT |
|=======================|

[17:26:06.558 DBG] ====================================
[17:26:06.680 DBG] STARTING
[17:26:06.680 DBG] ====================================
[17:26:06.972 DBG] [WEBSOCKET Bitfinex-1] Starting..
[17:26:08.491 INF] Reconnection happened, type: Websocket.Client.Models.ReconnectionInfo
[17:26:08.593 INF] Info received version: 2, reconnection happened, resubscribing to streams
[17:26:08.606 DBG] Authenticating
[17:26:15.490 DBG] Sending sell request
[17:26:15.851 ERR] [BFX WEBSOCKET CLIENT] Error received - message: auth: invalid, code: 10100
[17:26:15.858 INF] Pong received! Id: 123456
[17:26:15.877 INF] Authenticated: True
[17:26:15.950 INF] Wallet BTC balance: 0.000478960589130397 type: Exchange

20051 Info message handling

Hello,

First of all, I'm working with your lib and it seems pretty nice to me, so thanks for your job.

I'm listening to candles stream and sometimes it disconnects. According to Bitfinex documentation, I've realized that I have to subscribe to Info stream as well, so I get a notification when server status changed.

20051 : Stop/Restart Websocket Server (please try to reconnect)

Now I have a question. What should I do when I receive 20051? Is there any option to reconnect? Or what's the best way to do so?

Install via NuGet: Target Framework 2.0 Error

Hello,

I have the following error when trying to install via NuGet:


Severity Code Description Project File Line Suppression State
Error Could not install package 'Bitfinex.Client.Websocket 3.0.91'. You are trying to install this package into a project that targets '.NETFramework,Version=v2.0', but the package does not contain any assembly references or content files that are compatible with that framework. For more information, contact the package author.

Even though I have choose the .NET 2.0 Version (see screenshot):

grafik

Im a newbie what I' doing wrong?

communicator.Start().Wait(); hangs

Hello!

Here is part of code from WPF app:

BitfinexWebsocketCommunicator communicator = new BitfinexWebsocketCommunicator(BitfinexValues.ApiWebsocketUrl);
bitfinexAPI = new BitfinexWebsocketClient(communicator);
ticksProcess = TickerProcess;
bitfinexAPI.Streams.TickerStream.Subscribe(ticksProcess);
bitfinexAPI.Send(new TickerSubscribeRequest("BTC/USD"));
communicator.Start().Wait();
bitfinexAPI.Authenticate(apiKey, apiSecret);

Specifications

If you specified a bit more details on NewOrderRequest, I think a lot of people would appreciate it very much.
Also you wrote "Price (Not required for market orders)" there, but you didn't make constructor, that doesn't need to receive price as one of parameters, not counting empty constructor. Which leaves me to either use empty one, which is quite amusing, or write 0 in price section ?

Implement Orders Stream

Hi,
Great work on this.

Would you be able to complete the Orders Stream?
Need to get status of order.
Thanks :)

Can't install

"Install-Package : Unable to resolve dependencies. 'Newtonsoft.Json 11.0.1' is not compatible with 'Bitfinex.Client.Websocket 0.1.1 constraint: Newtonsoft.Json (>= 10.0.3)'"

Followed manual step by step, still got the issue. (tried 10.0.3 as well)
Would appreciate any advice.

UPD : I'm a dumbass, sorry :)

Read NOTIFY_INFO from notification message

Hi!
Could you add an example how to read the NOTIFY_INFO object from the notification?
I found the document here but not sure if we could parse it to every specified type.
See here: https://docs.bitfinex.com/reference#ws-auth-notifications
This is easy one with raw text:

[
  CHAN_ID, 
  TYPE, //'n' 
  [
    MTS, 
    TYPE, 
    MESSAGE_ID, 
    null, 
      NOTIFY_INFO // NOTIFY_INFO comes as an array or object and differs per notification  
    CODE, 
    STATUS, 
    TEXT,
    ...
  ]
]
  
[0,"n",[1575282446099,"fon-req",null,null,[41238905,null,null,null,-1000,null,null,null,null,null,null,null,null,null,0.002,2,null,null,null,null,null],null,"SUCCESS","Submitting funding bid of 1000.0 USD at 0.2000 for 2 days."]]

but how about the array/object?

notify_info =  [ 
  ID, 
  GID,
  CID,
  SYMBOL, 
  MTS_CREATE, 
  MTS_UPDATE, 
  AMOUNT, 
  AMOUNT_ORIG, 
  TYPE,
  TYPE_PREV,
  _PLACEHOLDER,
  _PLACEHOLDER,
  FLAGS,
  STATUS,
  _PLACEHOLDER,
  _PLACEHOLDER,
  PRICE,
  PRICE_AVG,
  PRICE_TRAILING,
  PRICE_AUX_LIMIT,
  _PLACEHOLDER,
  _PLACEHOLDER,
  _PLACEHOLDER,
  NOTIFY, 
  HIDDEN, 
  PLACED_ID,
  ...
]

Thanks in advanced!

Open / Start the projekt.

Hello,

I have a problem when I try to open the Projekt with the Visual Studio Express 2017:
grafik

The following message appears:
grafik
I confirm the message with "OK".

After that I have the following structure in "Solution Explorer":
grafik
I see that are many file are missing??

And the following Warnings:
grafik

There's nothing I can do now. No NuGet installation, no .cs files and so on. Can someone help?

NEA8

Anyone get cut off after about 50 minutes?

Hello All,

I am using this library in a console app, and am using the sample.

I keep getting the "No message received after 10 minutes, hard reset" message after about 50 minutes of activity.

Anyone else seeing this?

Cheers,
Matt

Support margin info for symbols

Hi Marfusios!
Thank you for sharing the library.
Please support margin symbol info as well.
I found the document here: https://docs.bitfinex.com/reference#ws-auth-margin-info

// margin symbol calc
[
  CHAN_ID,
  TYPE, // 'miu'
  [
    'sym',
    SYMBOL,
    [
      TRADABLE_BALANCE,
      GROSS_BALANCE,
      BUY,
      SELL,
      ...
    ]
   ]
]
  
[0,"miu",["sym","tETHUSD",[149361.09689202666,149639.26293509,830.0182168075556,895.0658432466332,null,null,null,null]]]

Candles

Hello, when you complete work with Candles?

Aborted State

Hello,

In some cases the socket goes to Abort state and then nothing works again, cant reconnect...

Here full log
https://pastebin.com/nM9GQ1ZE

All start in
[08:27:00 ERR] [BFX WEBSOCKET COMMUNICATOR] Error while listening to websocket stream

I'm still no catch where this happen...

Updating an order

Not really an issue, more of a question.
Is there any way I can update my sent order to buy/sell, without using new CancelOrderR... and new NewOrderR... ? My main problem right now is that if I sent and order with my program to buy smth, price went up and I want to move my order upwards as well - I don't get this order in the OrdersStream, therefore I don't have it's real ID, therefore I can't cancel it using my program. Any suggestions ?

Cancellation

When I'm subbed to walletstream, it only gives me info on first use and when wallets actually change, but I'd like to get this info in exact moment I need it. Could you please explain me how can I unsub from streams after I've used their contents, so that I can sub again later (and receive contents again) ?
I'd really appreciate your help

Reconnect not working

Hi,

Thanks for all your good work.
I've compiled your console sample, and reconnecting is not working.
Please take a look at screenshot
https://ibb.co/gYDdUc

I've run the app, disconnected internet cable, waited 30 sec. and reconnected the cable.
Internet is working, but the app didn't reconnect.

API Update – September 1st, 2018 - Rate limiting

http://blog.bitfinex.com/api/api-update-september-2018/

"As of September 1st, every WebSocket connection will have a limit of 50 subscriptions to market data feed channels (tickers, book, candles, trades, …)."

They are already rolling this out, as I have spent 2 day debugging thining I have a threading issue, but its actually this.

How can we fix this?

Split the pairs into groups?

Here is a log with the errors i'm receiving
https://pastebin.com/vcLy9SpL (log dump with errors)

Chart Marks feature request

[https://docs.bitfinex.com/v2/reference#ui-goodies](Bitfinex API)

Thanks for adding checksum, its critical.

Order filled not received

Subscribed to bitfinexAPI.Streams.OrderCreatedStream and bitfinexAPI.Streams.OrderUpdatedStream
After placing order I get into OrderCreatedStream but after order filled I do not get into OrderUpdatedStream. Is it right that I'm waiting info about order filling or any other changes in order after it was accepted by bitfinex in OrderUpdatedStream?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.