Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
eynzhang committed Apr 19, 2020
2 parents 3c6d6c4 + f407ad9 commit 996cc01
Show file tree
Hide file tree
Showing 9 changed files with 275 additions and 1 deletion.
2 changes: 1 addition & 1 deletion config.h
Original file line number Diff line number Diff line change
@@ -1 +1 @@
#define CURRENT_VERSION 1.0.13
#define CURRENT_VERSION 1.0.14
131 changes: 131 additions & 0 deletions examples/OrderV2example/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
cmake_minimum_required(VERSION 2.8)

#设置编译器
IF (CMAKE_COMPILER_TYPE MATCHES "GCC")
SET(CMAKE_C_COMPILER "gcc")
SET(CMAKE_CXX_COMPILER "g++")
ELSEIF (CMAKE_COMPILER_TYPE MATCHES "CLANG")
SET(CMAKE_C_COMPILER "clang")
SET(CMAKE_CXX_COMPILER "clang++")
ENDIF ()

SET(CMAKE_C_FLAGS "-Wall -std=c99")
SET(CMAKE_C_FLAGS_DEBUG "-g")
SET(CMAKE_C_FLAGS_MINSIZEREL "-Os -DNDEBUG")
SET(CMAKE_C_FLAGS_RELEASE "-O3 -DNDEBUG")
SET(CMAKE_C_FLAGS_RELWITHDEBINFO "-O2 -g")

SET(CMAKE_CXX_FLAGS "-Wall -std=c++11")
SET(CMAKE_CXX_FLAGS_DEBUG "-g")
SET(CMAKE_CXX_FLAGS_MINSIZEREL "-Os -DNDEBUG")
SET(CMAKE_CXX_FLAGS_RELEASE "-O3 -DNDEBUG")
SET(CMAKE_CXX_FLAGS_RELWITHDEBINFO "-O2 -g")


#判断操作系统
IF (WIN32)
MESSAGE(STATUS "Now is windows")
ELSEIF (APPLE)
MESSAGE(STATUS "Now is Apple systens.")
ELSEIF (UNIX)
MESSAGE(STATUS "Now is UNIX-like OS's. Including aPPLE os x and CygWin")
ENDIF ()

if (UNIX AND NOT APPLE)
set(platform x64)
set(LINUX TRUE)
endif ()

if (APPLE)
endif ()



if (LINUX)
message(STATUS "当前操作系统: Linux")
endif ()

if (APPLE)
message(STATUS "当前操作系统: MacOS")
endif ()

MESSAGE(STATUS "operation system is ${CMAKE_SYSTEM}")

PROJECT(example)

AUX_SOURCE_DIRECTORY(. DIR_SRCS)
ADD_EXECUTABLE(${PROJECT_NAME} ${DIR_SRCS})

IF (CMAKE_BUILD_TYPE MATCHES "Release")
SET(CMAKE_BUILD_POSTFIX ${CMAKE_BUILD_TYPE})
ELSEIF(CMAKE_BUILD_TYPE MATCHES "Debug")
SET(CMAKE_BUILD_POSTFIX ${CMAKE_BUILD_TYPE})
ELSE()
MESSAGE(FATAL_ERROR "The CMAKE_BUILD_TYPE only supports Release or Debug")
ENDIF()


find_library(LIBHUOBICLIENT_PATH HuobiClient)
IF(NOT LIBHUOBICLIENT_PATH)
message(${HUOBI_CLIENT_DIR}/lib/${CMAKE_BUILD_POSTFIX})
MESSAGE(FATAL_ERROR "HuobiClient not found")
ENDIF(NOT LIBHUOBICLIENT_PATH)
MESSAGE(STATUS ${LIBHUOBICLIENT_PATH} " found")


find_library(LIBWEBSOCKET_PATH websockets)
IF(NOT LIBWEBSOCKET_PATH)
MESSAGE(FATAL_ERROR "libwebsockets not found")
ENDIF(NOT LIBWEBSOCKET_PATH)
MESSAGE(STATUS ${LIBWEBSOCKET_PATH} " found")

find_library(LIBSSL_PATH ssl)
IF(NOT LIBSSL_PATH)
MESSAGE(FATAL_ERROR "libssl not found")
ENDIF(NOT LIBSSL_PATH)
MESSAGE(STATUS ${LIBSSL_PATH} " found")

find_library(LIBCRYPTO_PATH crypto)
IF(NOT LIBCRYPTO_PATH)
MESSAGE(FATAL_ERROR "libcrypto not found")
ENDIF(NOT LIBCRYPTO_PATH)
MESSAGE(STATUS ${LIBCRYPTO_PATH} " found")

find_library(LIBCURL_PATH curl)
IF(NOT LIBCURL_PATH)
MESSAGE(FATAL_ERROR "curl not found")
ENDIF(NOT LIBCURL_PATH)
MESSAGE(STATUS ${LIBCURL_PATH} " found")

find_library(LIBZ_PATH z)
IF(NOT LIBZ_PATH)
MESSAGE(FATAL_ERROR "z not found")
ENDIF(NOT LIBZ_PATH)
MESSAGE(STATUS ${LIBZ_PATH} " found")

find_library(LIBPTHREAD_PATH pthread)
IF(NOT LIBPTHREAD_PATH)
MESSAGE(FATAL_ERROR "pthread not found")
ENDIF(NOT LIBPTHREAD_PATH)
MESSAGE(STATUS ${LIBPTHREAD_PATH} " found")

find_library(LIBDECNUMBER_PATH decnumber)
IF(NOT LIBDECNUMBER_PATH)
MESSAGE(FATAL_ERROR "decnumber not found")
ENDIF(NOT LIBDECNUMBER_PATH)
MESSAGE(STATUS ${LIBDECNUMBER_PATH} " found")

FIND_PATH(
LIBDECNUMBER_INCLUDE_DIR
decNumber.h
/usr/include/decnumber/
/usr/local/include/decnumber/
)
include_directories(${LIBDECNUMBER_INCLUDE_DIR})
include_directories(/usr/local/include)

add_definitions(-std=c++11)

TARGET_LINK_LIBRARIES(${PROJECT_NAME} ${LIBHUOBICLIENT_PATH} ${LIBDECNUMBER_PATH} ${LIBSSL_PATH} ${LIBCRYPTO_PATH} ${LIBWEBSOCKET_PATH} ${LIBCURL_PATH} ${LIBZ_PATH} ${LIBPTHREAD_PATH})


23 changes: 23 additions & 0 deletions examples/OrderV2example/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#include<iostream>
#include "Huobi/HuobiClient.h"
#include "../key.h"
using namespace Huobi;
using namespace std;

int main(int argc, char** argv) {

SubscriptionClient* subscriptionClient = createSubscriptionClient(
Key::apiKey, Key::secretKey);
subscriptionClient->subscribeOrderUpdateV2("htusdt", [](OrderUpdateV2Event event) {
cout << "---- aggressor: " << event.aggressor << " ----" << endl;
cout << "---- orderId: " << event.orderId << " ----" << endl;
cout << "---- symbol: " << event.symbol << " ----" << endl;
cout << "---- tradeId: " << event.tradeId << " ----" << endl;
cout << "---- tradePrice: " << event.tradePrice << " ----" << endl;
cout << "---- tradeTime: " << event.tradeTime << " ----" << endl;
cout << "---- tradeVolume: " << event.tradeVolume << " ----" << endl;

});
subscriptionClient->startService();

}
40 changes: 40 additions & 0 deletions include/Huobi/OrderUpdateV2Event.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/

/*
* File: OrderUpdateV2Event.h
* Author: yuanxueqi
*
* Created on 2020年4月15日, 下午2:52
*/

#ifndef ORDERUPDATEV2EVENT_H
#define ORDERUPDATEV2EVENT_H
namespace Huobi {

struct OrderUpdateV2Event {
std::string eventType;
std::string symbol;
long orderId;
std::string clientOrderId;
std::string orderPrice;
std::string orderSize;
std::string type;
std::string orderStatus;
long orderCreateTime;
std::string tradePrice;
std::string tradeVolume;
long tradeId;
long tradeTime;
bool aggressor;
std::string remainAmt;
long lastActTime;
};
}


#endif /* ORDERUPDATEV2EVENT_H */

5 changes: 5 additions & 0 deletions include/Huobi/SubscriptionClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include "Huobi/MarketDepthMBPEvent.h"
#include "Huobi/TradeClearingEvent.h"
#include "Huobi/AccountUpdateEvent.h"
#include "Huobi/OrderUpdateV2Event.h"
#include "SubscriptionOptions.h"


Expand Down Expand Up @@ -191,6 +192,10 @@ namespace Huobi {
const std::function<void(const MarketDepthMBPEvent&) >& callback,
const std::function<void(HuobiApiException&)>& errorHandler = std::function<void(HuobiApiException&)>()) = 0;

virtual void subscribeOrderUpdateV2(
const char* symbol,
const std::function<void(const OrderUpdateV2Event&) >& callback,
const std::function<void(HuobiApiException&)>& errorHandler = std::function<void(HuobiApiException&)>()) = 0;

/*
* start sub,must excute after sub-function.
Expand Down
7 changes: 7 additions & 0 deletions src/SubscriptionClientImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,13 @@ namespace Huobi {
const std::function<void(const MarketDepthMBPEvent&) >& callback,
const std::function<void(HuobiApiException&)>& errorHandler = std::function<void(HuobiApiException&)>()) override;

void subscribeOrderUpdateV2(
const char* symbol,
const std::function<void(const OrderUpdateV2Event&) >& callback,
const std::function<void(HuobiApiException&)>& errorHandler = std::function<void(HuobiApiException&)>()) override;



WebSocketRequest* requestCandlestickEvent(
bool autoClose,
const char* symbols,
Expand Down
7 changes: 7 additions & 0 deletions src/Utils/Channels.h
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,13 @@ namespace Huobi {
return writer.toJsonString();
}

static std::string OrderUpdateV2(char*& op, const std::string& symbol) {
JsonWriter writer;
writer.put("action", op);
writer.put("ch", "orders#" + symbol);
return writer.toJsonString();
}


};
char* Channels::OP_SUB = "sub";
Expand Down
54 changes: 54 additions & 0 deletions src/WebSocketApiImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -496,6 +496,60 @@ namespace Huobi {

}

WebSocketRequest* WebSocketApiImpl::subscribeOrderUpdateV2(
const std::list<std::string>& symbols,
const std::function<void(const OrderUpdateV2Event&) >& callback,
const std::function<void(HuobiApiException&)>& errorHandler) {
InputChecker::checker()->checkCallback(callback);
auto req = new WebSocketRequestImpl<OrderUpdateV2Event>();
req->connectionHandler = [symbols](WebSocketConnection * connection) {
for (std::string symbol : symbols) {
connection->send(Channels::OrderUpdateV2(Channels::OP_SUB, symbol));
std::this_thread::sleep_for(std::chrono::milliseconds(1));
}
};
req->JsonParser = [this](const JsonWrapper & json) {
OrderUpdateV2Event orderUpdateV2Event;
JsonWrapper data = json.getJsonObjectOrArray("data");
orderUpdateV2Event.eventType = data.getString("eventType");
orderUpdateV2Event.symbol = data.getString("symbol");
orderUpdateV2Event.orderId = data.getLong("orderId");
if (data.containKey("clientOrderId"))
orderUpdateV2Event.clientOrderId = data.getString("clientOrderId");
if (data.containKey("orderPrice"))
orderUpdateV2Event.orderPrice = data.getString("orderPrice");
if (data.containKey("orderSize"))
orderUpdateV2Event.orderSize = data.getString("orderSize");
orderUpdateV2Event.orderStatus = data.getString("orderStatus");
if (data.containKey("orderCreateTime"))
orderUpdateV2Event.orderCreateTime = data.getLong("orderCreateTime");
if (data.containKey("tradePrice"))
orderUpdateV2Event.tradePrice = data.getString("tradePrice");
if (data.containKey("tradeVolume"))
orderUpdateV2Event.tradeVolume = data.getString("tradeVolume");
if (data.containKey("tradeId"))
orderUpdateV2Event.tradeId = data.getLong("tradeId");
if (data.containKey("tradeTime"))
orderUpdateV2Event.tradeTime = data.getLong("tradeTime");
if (data.containKey("aggressor"))
orderUpdateV2Event.aggressor = data.getBool("aggressor");
if (data.containKey("orderStatus"))
orderUpdateV2Event.orderStatus = data.getString("orderStatus");
if (data.containKey("remainAmt"))
orderUpdateV2Event.remainAmt = data.getString("remainAmt");
if (data.containKey("lastActTime"))
orderUpdateV2Event.lastActTime = data.getLong("lastActTime");

return orderUpdateV2Event;
};
req->isNeedSignature = true;
req->Callback = callback;
req->errorHandler = errorHandler;
req->isV2=true;
return req;


}

WebSocketRequest* WebSocketApiImpl::requestCandlestickEvent(
bool autoClose,
Expand Down
7 changes: 7 additions & 0 deletions src/WebSocketApiImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include "Huobi/MarketDepthMBPEvent.h"
#include "Huobi/TradeClearingEvent.h"
#include "Huobi/AccountUpdateEvent.h"
#include "Huobi/OrderUpdateV2Event.h"
#include "Huobi/Enums.h"

namespace Huobi {
Expand Down Expand Up @@ -106,6 +107,12 @@ namespace Huobi {
const std::function<void(const MarketDepthMBPEvent&) >& callback,
const std::function<void(HuobiApiException&)>& errorHandler);

WebSocketRequest* subscribeOrderUpdateV2(
const std::list<std::string>& symbols,
const std::function<void(const OrderUpdateV2Event&) >& callback,
const std::function<void(HuobiApiException&)>& errorHandler = std::function<void(HuobiApiException&)>());


WebSocketRequest * requestCandlestickEvent(
bool autoClose,
const std::list<std::string>& symbols,
Expand Down

0 comments on commit 996cc01

Please sign in to comment.