niXman / binapi

Binance API C++ implementation
Apache License 2.0
263 stars 88 forks source link

Guidance for binance futures #16

Closed bashman closed 3 years ago

bashman commented 3 years ago

I need some guidance on how to use binapi with binance futures. I am not very handy in C++, I have been using python most of the time.

I saw the order book, ticker and others with binance futures, binapi it works fine, but now, I need to connect binapi to binance futures websocket, for user data.

I got this error: cpp_agent: /home/lucy/cpp_agent/src/types.cpp:1467: static binapi::rest::start_user_data_stream_t binapi::rest::start_user_data_stream_t::parse(const char*, std::size_t): the statement 'json.is_valid()' is not valid. The program has unexpectedly finished.

I know, that the program expects something that the exchange is not sending, or in a different format than the one it expects. I know, that we have to change the endpoints and some urls, and I know more needs to be done... So, I need your guidance to make the necessary modifications, I can share my experience with the community.

niXman commented 3 years ago

hi,

please provide me the content of str with length len first: https://github.com/niXman/binapi/blob/master/src/types.cpp#L1465

bashman commented 3 years ago

hi,

please provide me the content of str with length len first: https://github.com/niXman/binapi/blob/master/src/types.cpp#L1465

Well, I made some changes: Url and some endpoints and I have this code:

    auto start_uds = api.start_user_data_stream();
    BREAK_IF_ERROR(start_uds);
    std::cout << "start_uds=" << start_uds.v << std::endl << std::endl;

    binapi::ws::websockets_pool wsp(ioctx, "fstream.binance.com", "443");

    auto user_data_stream = wsp.userdata(start_uds.v.listenKey.c_str(),
         [](const char *fl, int ec, std::string errmsg, binapi::userdata::order_update_t msg) -> bool {
             if ( ec ) {
                 std::cout << "order update: fl=" << fl << ", ec=" << ec << ", errmsg: " << errmsg << ", msg: " << msg << std::endl;
                 return false;
             }
             std::cout << "order update:\n" << msg << std::endl;
             return true;
         }
        ,[](const char *fl, int ec, std::string errmsg, binapi::userdata::account_update_t msg) -> bool {
             if ( ec ) {
                 std::cout << "account update: fl=" << fl << ", ec=" << ec << ", errmsg: " << errmsg << ", msg: " << msg << std::endl;
                 return false;
             }
             std::cout << "account update:\n" << msg << std::endl;
             return true;
         }
    );

    auto ping_uds = api.ping_user_data_stream(start_uds.v.listenKey);
    BREAK_IF_ERROR(ping_uds);
    std::cout << "ping_uds=" << ping_uds.v << std::endl << std::endl;

    ioctx.run();

    return a.exec();
}

When build and run the program, the out is:

Binapi listen key output start_uds={"listenKey":"mNMyz4atf7Boni6yReixv5zsls35XhDrgitRwQAfiPyOzs2LWnMELdNqgE2jAc2l"}

ping_uds={"ok":true}

I have compared the output with python, and it is the same listenkey

CCXT Pro listen key url output: wss://fstream.binance.com/ws/mNMyz4atf7Boni6yReixv5zsls35XhDrgitRwQAfiPyOzs2LWnMELdNqgE2jAc2l

Then when a message arrives, this message is displayed and the program terminates:

/home/lucy/cpp_agent/src/websocket.cpp(270): /home/lucy/cpp_agent/include/binapi/flatjson.hpp(1397): key not found

niXman commented 3 years ago

please add the new line after this line and type std::cout.write(ptr, size); std::cout << std::endl;

rebuild (with uncommented line 32) and show me the output.

thanks.

bashman commented 3 years ago

please add the new line after this line and type std::cout.write(ptr, size); std::cout << std::endl;

rebuild (with uncommented line 32) and show me the output.

thanks.

This is te output: {"e":"ACCOUNT_UPDATE","T":1614621239216,"E":1614621239221,"a":{"B":[{"a":"USDT","wb":"83.43031407","cw":"47.15123794"}],"P":[{"s":"XRPUSDT","pa":"247","ep":"0.44092","cr":"-21.38228539","up":"-1.21277000","mt":"isolated","iw":"36.27907613","ps":"BOTH","ma":"USDT"}],"m":"ORDER"}}

niXman commented 3 years ago

very strange... can you provide me the complete example please so I can check it myself?

bashman commented 3 years ago

very strange... can you provide me the complete example please so I can check it myself?

Yes!.

niXman commented 3 years ago

can you provide me the complete example please so I can check it myself?

any update?