#pragma once #include "lib/api/services/OrderService.pb.h" #include "lib/domain_model/Security.pb.h" #include #include #include #include #include #include #include #include namespace bts::orderman { const std::string &getm( const std::map &map, char const *key_name); bts::api::orderman::OrderStatus convert_ccapi_status(const std::string &status); // 1. // Event [ // type = RESPONSE, // messageList = [ // Message [ // type = CREATE_ORDER, // recapType = UNKNOWN, // time = 1970-01-01T00:00:00.000000000Z, // timeReceived = 2023-05-04T20:13:15.425177524Z, // elementList = [ // Element [ // nameValueMap = { // CUMULATIVE_FILLED_PRICE_TIMES_QUANTITY = 0.000000, // CUMULATIVE_FILLED_QUANTITY = 0.0, // INSTRUMENT = BTC-PERPETUAL, // LIMIT_PRICE = 28860.0, // ORDER_ID = 16257763986, // QUANTITY = 10.0, // SIDE = BUY, // STATUS = openis_position_cb // } // ] // ], // correlationIdList = [ NewOrder:605003:BTC-PERPETUAL ] // ] // ] // ] // This is a Response message when a request to send an order is sent struct CCAPICreateOrderMsg { std::string order_id; std::string instrument; api::Side side; double price; double qty; double cumm_filled_price; double cumm_filled_qty; bts::api::orderman::OrderStatus status; std::string corr_id; CCAPICreateOrderMsg( const std::map &map, const std::string &corr_id) : order_id(getm(map, CCAPI_EM_ORDER_ID)), instrument(getm(map, CCAPI_INSTRUMENT)), side( getm(map, CCAPI_EM_ORDER_SIDE) == CCAPI_EM_ORDER_SIDE_BUY ? api::SIDE_BUY : api::SIDE_SELL), // TODO, market orders? price(std::stod(getm(map, CCAPI_EM_ORDER_LIMIT_PRICE))), qty(std::stod(getm(map, CCAPI_EM_ORDER_QUANTITY))), cumm_filled_price(std::stod( getm(map, CCAPI_EM_ORDER_CUMULATIVE_FILLED_PRICE_TIMES_QUANTITY))), cumm_filled_qty( std::stod(getm(map, CCAPI_EM_ORDER_CUMULATIVE_FILLED_QUANTITY))), status(convert_ccapi_status(getm(map, CCAPI_EM_ORDER_STATUS))), corr_id(corr_id) {} }; // Event [ // type = RESPONSE, // messageList = [ // Message [ // type = RESPONSE_ERROR, // recapType = UNKNOWN, // time = 1970-01-01T00:00:00.000000000Z, // timeReceived = 2023-05-19T14:16:37.229712920Z, // elementList = [ // Element [ // nameValueMap = { // ERROR_MESSAGE = {"jsonrpc":"2.0","id":1684505797121182092,"error":{"message":"Invalid params","data":{"reason": // "positive float required","param":"price"},"code":-32602},"usIn":1684505797179014,"usOut":1684505797179346,"usDiff":332,"test // net":true}, // HTTP_STATUS_CODE = 400 // } // ] // ], // correlationIdList = [ NewOrder:655008:BTC-20MAY23-24500-P ] // ] // ] // ] struct CCAPICreateOrderErrorMsg { std::string corrid; std::string error; static std::optional parse( const ccapi::Element &element, const std::string &corrid) { const auto &nv_map = element.getNameValueMap(); auto it = nv_map.find(CCAPI_ERROR_MESSAGE); if (it == nv_map.end()) return std::nullopt; nlohmann::json main_pl = nlohmann::json::parse(it->second); const auto &error_msg = main_pl.at("error"); CCAPICreateOrderErrorMsg rv = {corrid, error_msg.dump()}; return rv; } }; // 2. // Event [ // type = SUBSCRIPTION_DATA, // messageList = [ // Message [ // type = EXECUTION_MANAGEMENT_EVENTS_ORDER_UPDATE, // recapType = UNKNOWN, // time = 2023-05-04T20:13:18.559000000Z, // timeReceived = 2023-05-04T20:13:18.608831966Z, // elementList = [ // Element [ // nameValueMap = { // CUMULATIVE_FILLED_PRICE_TIMES_QUANTITY = 288635.300000, // CUMULATIVE_FILLED_QUANTITY = 10.0, // LIMIT_PRICE = 28865.5, // ORDER_ID = 16257763986, // QUANTITY = 10.0, // SIDE = BUY, // STATUS = filled // } // ] // ], // correlationIdList = [ OrderSub:BTC-PERPETUAL ] // ] // ] // ] // This is a Subscription message as result of an orders subscription struct CCAPIOrderUpdateMsg { std::string order_id; api::Side side; double price; double qty; double cumm_filled_price; double cumm_filled_qty; api::orderman::OrderStatus status; CCAPIOrderUpdateMsg(const std::map &map) : order_id(getm(map, CCAPI_EM_ORDER_ID)), side( getm(map, CCAPI_EM_ORDER_SIDE) == CCAPI_EM_ORDER_SIDE_BUY ? api::SIDE_BUY : api::SIDE_SELL), // TODO, market orders? price(std::stod(getm(map, CCAPI_EM_ORDER_LIMIT_PRICE))), qty(std::stod(getm(map, CCAPI_EM_ORDER_QUANTITY))), cumm_filled_price(std::stod( getm(map, CCAPI_EM_ORDER_CUMULATIVE_FILLED_PRICE_TIMES_QUANTITY))), cumm_filled_qty( std::stod(getm(map, CCAPI_EM_ORDER_CUMULATIVE_FILLED_QUANTITY))), status(convert_ccapi_status(getm(map, CCAPI_EM_ORDER_STATUS))) {} }; // 3. // Event [ // type = SUBSCRIPTION_DATA, // messageList = [ // Message [ // type = EXECUTION_MANAGEMENT_EVENTS_PRIVATE_TRADE, // recapType = UNKNOWN, // time = 2023-05-04T20:13:18.559000000Z, // timeReceived = 2023-05-04T20:13:18.610572197Z, // elementList = [ // Element [ // nameValueMap = { // FEE_ASSET = BTC, // FEE_QUANTITY = 1.7e-7, // INSTRUMENT = BTC-PERPETUAL, // IS_MAKER = 0, // LAST_EXECUTED_PRICE = 28863.5, // LAST_EXECUTED_SIZE = 10.0, // ORDER_ID = 16257763986, // SEQUENCE_NUMBER = 99252800, // SIDE = SELL, // TRADE_ID = 161565953 // } // ] // ], // correlationIdList = [ TradeSub:BTC-PERPETUAL ] // ] // ] // ] // Trade message struct CCAPITradeMsg { std::string order_id; api::Side side; double last_exec_price; double last_exec_qty; std::string trade_id; CCAPITradeMsg(const std::map &map) : order_id(getm(map, CCAPI_EM_ORDER_ID)), side( getm(map, CCAPI_EM_ORDER_SIDE) == CCAPI_EM_ORDER_SIDE_BUY ? api::SIDE_BUY : api::SIDE_SELL), last_exec_price( std::stod(getm(map, CCAPI_EM_ORDER_LAST_EXECUTED_PRICE))), last_exec_qty(std::stod(getm(map, CCAPI_EM_ORDER_LAST_EXECUTED_SIZE))), trade_id(getm(map, CCAPI_TRADE_ID)) {} }; struct CCAPISubscriptionStatusMsg { bool is_success; std::string corr_id; // maybe add info message here }; class DeribitOpenPosition { public: double total_profit_loss; double size_currency; double size; double settlement_price; double realized_profit_loss; double realized_funding; double open_orders_margin; double mark_price; double maintenance_margin; double leverage; std::string kind; double interest_value; std::string instrument_name; double initial_margin; double index_price; double floating_profit_loss; double estimated_liquidation_price; std::string direction; double delta; double average_price; static void parse(const nlohmann::json &j, DeribitOpenPosition &dop); }; void from_json(const nlohmann::json &j, DeribitOpenPosition &dop); void to_json(nlohmann::json &j, const DeribitOpenPosition &dop); std::ostream &operator<<( std::ostream &out, const DeribitOpenPosition &dob) noexcept; } // namespace bts::orderman