nkaz001 / hftbacktest

A high-frequency trading and market-making backtesting and trading bot in Python and Rust, which accounts for limit orders, queue positions, and latencies, utilizing full tick data for trades and order books, with real-world crypto market-making examples for Binance Futures
MIT License
2.02k stars 397 forks source link

In LiveBot, eventReceiver always timeout #131

Closed lli839 closed 3 months ago

lli839 commented 3 months ago

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: https://github.com/nkaz001/hftbacktest/blob/c7767c86a47d4e82121aa9504ecca8a6fd82249e/hftbacktest/src/live/bot.rs#L444-L446

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 commented 3 months ago

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 commented 3 months ago

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 commented 3 months ago

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 commented 3 months ago

hmm, the strange thing is that if putting a testnet, the code works fine. Even though I replaced Public with the exact URLs you mentioned above, the event receiver still returns timeout error.


From: nkaz001 @.> Sent: Monday, August 26, 2024 7:56 PM To: nkaz001/hftbacktest @.> Cc: lli839 @.>; Author @.> Subject: Re: [nkaz001/hftbacktest] In LiveBot, eventReceiver always timeout (Issue #131)

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

— Reply to this email directly, view it on GitHubhttps://github.com/nkaz001/hftbacktest/issues/131#issuecomment-2310027460, or unsubscribehttps://github.com/notifications/unsubscribe-auth/AYZ5CAH4SLA4SHWMB2B4OZLZTMJWRAVCNFSM6AAAAABNDQBY7CVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDGMJQGAZDONBWGA. You are receiving this because you authored the thread.Message ID: @.***>

nkaz001 commented 3 months ago

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