Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

In LiveBot, eventReceiver always timeout #131

Closed
lli839 opened this issue Aug 26, 2024 · 5 comments
Closed

In LiveBot, eventReceiver always timeout #131

lli839 opened this issue Aug 26, 2024 · 5 comments

Comments

@lli839
Copy link

lli839 commented Aug 26, 2024

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

@nkaz001
Copy link
Owner

nkaz001 commented Aug 26, 2024

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.

@lli839
Copy link
Author

lli839 commented Aug 26, 2024

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?

@nkaz001
Copy link
Owner

nkaz001 commented Aug 26, 2024

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();

@lli839
Copy link
Author

lli839 commented Aug 26, 2024 via email

@lli839 lli839 closed this as completed Aug 27, 2024
@nkaz001
Copy link
Owner

nkaz001 commented Aug 27, 2024

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

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

No branches or pull requests

4 participants
@nkaz001 @lli839 and others