nkaz001/hftbacktest

In LiveBot, eventReceiver always timeout

Closed this issue · 5 comments

Version: 2.0.0

Hi, I'm writing simple code to listen live events. However, this line in bot.rs always get a timeout error:

Err(RecvTimeoutError::Timeout) => {
return Ok(true);
}

Any idea?

Here is my code:

use std::{fmt::Debug};
use hftbacktest::connector::binancefutures::{BinanceFutures, Endpoint};
use hftbacktest::live::{LiveBot, LoggingRecorder};
use hftbacktest::prelude::*;

pub fn listen_orders<MD, I, R>(
    hbt: &mut I,
    recorder: &mut R,
) -> Result<(), i64>
where
    MD: MarketDepth,
    I: Bot<MD>,
    <I as Bot<MD>>::Error:Debug,
    R: Recorder,
    <R as Recorder>::Error:Debug,
{
    while hbt.elapse(1000_000_000).unwrap() {
        let depth = hbt.depth(0);
        println!("best bid={}, best_ask={}", depth.best_bid(), depth.best_ask());
    }
    println!("all done");
    Ok(())
}

const ORDER_PREFIX: &str = "";
// mainnet
const API_KEY: &str = "<MAINNET_API_KEY>";
const SECRET: &str = "<MAINNET_SECRET>";


fn prepare_live() -> LiveBot<HashMapMarketDepth> {
    let binance_futures = BinanceFutures::builder()
        .endpoint(Endpoint::Public)
        .api_key(API_KEY)
        .secret(SECRET)
        .order_prefix(ORDER_PREFIX)
        .build()
        .unwrap();

    let mut hbt = LiveBot::builder()
        .register("binancefutures", binance_futures)
        .add("binancefutures", "BTCUSDT", 0.1, 0.001)
        .depth(|asset| HashMapMarketDepth::new(asset.tick_size, asset.lot_size))
        .build()
        .unwrap();

    hbt.run().unwrap();
    hbt
}

fn main() {
    tracing_subscriber::fmt::init();
    let mut hbt = prepare_live();
    let mut recorder = LoggingRecorder::new();
    listen_orders(
        &mut hbt,
        &mut recorder,
    )
        .unwrap();
    hbt.close().unwrap();
}

As a result, the println!() only prints NaN.

best bid=NaN, best_ask=NaN
best bid=NaN, best_ask=NaN
best bid=NaN, best_ask=NaN
best bid=NaN, best_ask=NaN

The Timeout error just indicates that there is no market feed during the given elapsed duration. Currently, the Connector doesn't have a feature to retrieve a market depth snapshot, so no bid/ask data is retrieved until the market is updated.

The Timeout error just indicates that there is no market feed during the given elapsed duration. Currently, the Connector doesn't have a feature to retrieve a market depth snapshot, so no bid/ask data is retrieved until the market is updated.

hi @nkaz001 , thanks for the reply. The code keeps running for couple of minutes. It always returns time out error. Is it normal?

I ran your code on the testnet(Endpoint::Testnet), and it works as expected. Perhaps the Endpoint::Public might be incorrect. Could you try testing it by manually inputting the endpoints

The following endpoint is what Endpoint::Public refers to.

let binance_futures = BinanceFutures::builder()
    .api_url("https://fapi.binance.com")
    .stream_url("wss://fstream.binance.com")
    .api_key(API_KEY)
    .secret(SECRET)
    .order_prefix(ORDER_PREFIX)
    .build()
    .unwrap();

I couldn't open the file. Please let me know which part needs to be fixed or PR.