Skip to content

Commit

Permalink
WIP Bitfinex, added human readable for long values
Browse files Browse the repository at this point in the history
  • Loading branch information
michelelacorte committed Dec 8, 2020
1 parent 8be766b commit 1e5e723
Show file tree
Hide file tree
Showing 5 changed files with 147 additions and 6 deletions.
26 changes: 22 additions & 4 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ import 'package:flutter_trading_volume/models/supported_pairs.dart';
import 'package:flutter_trading_volume/models/trades/bitmex_trade.dart';
import 'package:flutter_trading_volume/models/trades/bybit_trade.dart';
import 'package:flutter_trading_volume/routes/data_logs_route.dart';
import 'package:flutter_trading_volume/utils/utils.dart';
import 'package:flutter_trading_volume/websockets/binance_socket.dart';
import 'package:flutter_trading_volume/websockets/bitfinex_socket.dart';
import 'package:flutter_trading_volume/websockets/bitmex_socket.dart';
import 'package:flutter_trading_volume/websockets/bybit_socket.dart';
import 'package:flutter_trading_volume/websockets/ftx_socket.dart';
Expand Down Expand Up @@ -62,6 +64,7 @@ class _TradeHomePageState extends State<TradeHomePage> {
FtxSocket _ftxSocket;
ByBitSocket _byBitSocket;
BitmexSocket _bitmexSocket;
BitfinexSocket _bitfinexSocket;

AudioPlayer audioPlayer = AudioPlayer();

Expand Down Expand Up @@ -96,6 +99,7 @@ class _TradeHomePageState extends State<TradeHomePage> {
_ftxSocket = new FtxSocket(pair: _currentPair);
_byBitSocket = new ByBitSocket(pair: _currentPair);
_bitmexSocket = new BitmexSocket(pair: _currentPair);
_bitfinexSocket = new BitfinexSocket(pair: _currentPair);
}

void _connectToSocket() {
Expand All @@ -113,6 +117,9 @@ class _TradeHomePageState extends State<TradeHomePage> {
if(_bitmexSocket.socket == null){
_bitmexSocket.connect();
}
if(_bitfinexSocket.socket == null){
_bitfinexSocket.connect();
}
_listenForDataUpdate();
}

Expand All @@ -121,6 +128,7 @@ class _TradeHomePageState extends State<TradeHomePage> {
_ftxSocket.closeConnection();
_byBitSocket.closeConnection();
_bitmexSocket.closeConnection();
_bitfinexSocket.closeConnection();
}

void _listenForDataUpdate() {
Expand Down Expand Up @@ -152,6 +160,15 @@ class _TradeHomePageState extends State<TradeHomePage> {
if(trade != null) _prices[BITMEX_PRICE_ID] = trade.price;
});
});
//TODO: WIP
_bitfinexSocket.socket.stream.listen((event) {
setState(() {
print(event);
//var trade = BitmexTrade.fromJson(event.toString());
//_updateData(trade);
//if(trade != null) _prices[BITMEX_PRICE_ID] = trade.price;
});
});
}

void _startStopSocket() {
Expand Down Expand Up @@ -377,6 +394,7 @@ class _TradeHomePageState extends State<TradeHomePage> {
_ftxSocket.pair = newValue;
_byBitSocket.pair = newValue;
_bitmexSocket.pair = newValue;
_bitfinexSocket.pair = newValue;
}
});
},
Expand Down Expand Up @@ -462,7 +480,7 @@ class _TradeHomePageState extends State<TradeHomePage> {
fontSize: 24,
color: Colors.white)),
subtitle: Text(
'This website is under development!',
'This website is under development!\n\nCurrent supported exchange: Binance, FTX, ByBit, BitMEX',
style: TextStyle(color: Colors.white))),
],
),
Expand All @@ -481,13 +499,13 @@ class _TradeHomePageState extends State<TradeHomePage> {
),
ListTile(
title: Text(
'Price: ${_averagePrice() ?? 0}\$'),
'Price: ${_averagePrice().toStringAsFixed(4) ?? 0}\$'),
subtitle: Text(
'Quantity executed: ${_cumulativeQuantity.toStringAsFixed(4)}'),
'Quantity executed: ${humanReadableNumberGenerator(_cumulativeQuantity)}'),
),
ListTile(
title: Text(
'Cumulative Value: ${_cumulativePrice.toStringAsFixed(4)}\$'),
'Cumulative Value: ${humanReadableNumberGenerator(_cumulativePrice)}\$'),
),
ListTile(
title: Text(
Expand Down
68 changes: 68 additions & 0 deletions lib/models/trades/bitfinex_trade.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import 'dart:convert';
import 'package:flutter_trading_volume/models/order_type.dart';
import 'base_trade.dart';

class BitfinexData {
final String symbol;
final double price;
final double size;
final String side;
final String time;

BitfinexData(
{this.symbol, this.price, this.size, this.side, this.time});

factory BitfinexData.fromJson(dynamic jsonData) {
return BitfinexData(
symbol: jsonData['symbol'] as String,
price: jsonData['price'] ?? 0,
size: jsonData['size'] ?? 0,
side: jsonData['side'] as String,
time: jsonData['timestamp'] as String
);
}
}

class BitfinexTrade extends BaseTrade{

BitfinexTrade({
symbol,
price,
quantity,
orderType,
tradeTime}) : super(market: 'Bitfinex', symbol: symbol, price: price,
quantity: quantity, tradeTime: tradeTime, orderType: orderType);

// ignore: slash_for_doc_comments
/**
{
"data":[
{
"timestamp":"2020-12-08T16:06:13.580Z",
"symbol":"XBTUSD",
"side":"Buy",
"size":1500,
"price":18813,
"tickDirection":"PlusTick",
"trdMatchID":"3539a9fa-ef88-4c25-6634-92997c69ec31",
"grossValue":7972500,
"homeNotional":0.079725,
"foreignNotional":1500
}
]
}
*/
factory BitfinexTrade.fromJson(String jsonAsString) {
if(jsonAsString == null || jsonAsString.contains('subscribe') || jsonAsString.contains('info')) return null;
final jsonData = json.decode(jsonAsString);
var dataJson = jsonData['data'] as List;
List<BitfinexData> tradeData = dataJson.map((bitmexJson) => BitfinexData.fromJson(bitmexJson)).toList();
return BitfinexTrade(
symbol: tradeData != null ? tradeData[0].symbol : '',
price: tradeData != null ? tradeData[0].price ?? 0 : 0,
quantity: tradeData != null ? tradeData[0].size ?? 0 : 0,
orderType: tradeData != null ? (tradeData[0].side.contains('Buy') ? OrderType.BUY : OrderType.SELL) : OrderType.ALL,
tradeTime: tradeData != null ? tradeData[0].time : '0',
);
}
}
4 changes: 2 additions & 2 deletions lib/routes/data_logs_route.dart
Original file line number Diff line number Diff line change
Expand Up @@ -255,8 +255,8 @@ class DataLogsRouteState extends State<DataLogsRoute> {
? Colors.red
: Colors.green))),
DataCell(Text(element.price.toString())),
DataCell(Text(element.quantity.toString())),
DataCell(Text(element.value.toString())),
DataCell(Text(humanReadableNumberGenerator(element.quantity))),
DataCell(Text(humanReadableNumberGenerator(element.value))),
DataCell(Text(element.tradeTime)),
],
)
Expand Down
14 changes: 14 additions & 0 deletions lib/utils/utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,18 @@ void downloadStringAsCsv(String toExport) {
anchor.click();
html.document.body.children.remove(anchor);
html.Url.revokeObjectUrl(url);
}

String humanReadableNumberGenerator(double num) {
if (num > 999 && num < 99999) {
return "${(num / 1000).toStringAsFixed(1)}K ";
} else if (num > 99999 && num < 999999) {
return "${(num / 1000).toStringAsFixed(0)}K ";
} else if (num > 999999 && num < 999999999) {
return "${(num / 1000000).toStringAsFixed(1)}M ";
} else if (num > 999999999) {
return "${(num / 1000000000).toStringAsFixed(1)}B ";
} else {
return num.toString();
}
}
41 changes: 41 additions & 0 deletions lib/websockets/bitfinex_socket.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import 'package:flutter/material.dart';
import 'package:flutter_trading_volume/models/supported_pairs.dart';
import 'package:flutter_trading_volume/websockets/base_socket.dart';
import 'package:web_socket_channel/html.dart';

class BitfinexSocket implements BaseSocket {
SupportedPairs pair;
HtmlWebSocketChannel socket;

BitfinexSocket({@required this.pair});

@override
HtmlWebSocketChannel connect() {
if(socket == null) {
socket = HtmlWebSocketChannel.connect(wsUrl());
}
if(socket != null && socket.sink != null) {
socket.sink.add(wsSubscribeMessage());
}
return socket;
}

@override
void closeConnection() {
if(socket != null && socket.sink != null) {
socket.sink.close();
}
socket = null;
}

@override
String wsUrl() {
return 'wss://api-pub.bitfinex.com/ws/2';
}

@override
String wsSubscribeMessage() {
return """{"event": "subscribe", "channel": "trades", "symbol": "${pair.toStringUSD()}"}""";
}

}

0 comments on commit 1e5e723

Please sign in to comment.