wisespace-io / binance-rs

Rust Library for the Binance API
Other
665 stars 297 forks source link

Getting prices that match the UI #23

Closed drbh closed 5 years ago

drbh commented 5 years ago

This is a great project and very useful in a project I'm working on.

However I can't seem to connect to the websocket correctly to get prices that match the web UI's price

I've followed the example code on the README at WEBSOCKETS - TRADES

let agg_trade: String = format!("!ticker@arr");
let mut btcusdt: f32 = "0".parse().unwrap();

let mut web_socket: WebSockets = WebSockets::new(|event: WebsocketEvent| {
    match event {
        WebsocketEvent::DayTicker(ticker_events) => {
            for tick_event in ticker_events {
                if tick_event.symbol == "BTCUSDT" {
                    btcusdt = tick_event.average_price.parse().unwrap();
                }
            }
        },
        _ => return,
    }
});

But for example - as I'm writing:

BTCUSDT @ $7967 on Binance.com 
BTCUSDT @ $7855.4326 from the websocket (above)

How should I change the websocket to get prices that match the UI?

wisespace-io commented 5 years ago

It depends what you want to match. Are you talking about the last price information in the UI? In that case, you should use current_close field.

let mut web_socket: WebSockets = WebSockets::new(|event: WebsocketEvent| {
    match event {
        WebsocketEvent::DayTicker(ticker_events) => {
            for tick_event in ticker_events {
                if tick_event.symbol == "BTCUSDT" {
                    btcusdt = tick_event.average_price.parse().unwrap();
                    let btcusdt_close: f32 = tick_event.current_close.parse().unwrap();
                    println!("{} - {}", btcusdt, btcusdt_close);
                }
            }
        },
        _ => return,
    }
});
drbh commented 5 years ago

ha silly mistake! I was just reading through the code sample average price looked strange. Thanks so much. current_close is exactly what I am looking for!