├── alpha_trade_python ├── __init__.py └── constants.py ├── utility.dll ├── stock_live_trade.exe ├── stock_live_trade ├── .DS_Store ├── hexin_stock_broker.cpp ├── hexin_stock_broker_guangfa.cpp ├── hexin_stock_broker_huatai.cpp ├── alpha_trade_constants.cpp ├── alpha_config_constants.cpp ├── main.cpp ├── GScopedLock.h ├── hexin_weituo.cpp ├── GSyncObject.h ├── alpha_trade_constants.h ├── alpha_config_constants.h ├── GWin32CriticalSection.cpp ├── GWin32CriticalSection.h ├── signature.h ├── GWin32Event.h ├── GBase64.h ├── GWin32Thread.h ├── lib.h ├── GWin32Thread.cpp ├── stock_live_trade.sln ├── license_manager.h ├── utility.h ├── broker_adapter.h ├── thread_broker_adapter.h ├── stock_broker.h ├── GBase64.cpp ├── md5.h ├── alpha_config_types.h ├── utility.cpp ├── hexin_stock_broker.h ├── hexin_stock_broker_guangfa.h ├── hexin_stock_broker_huatai.h ├── signature.cpp ├── license_manager.cpp ├── thread_broker_adapter.cpp ├── md5.cpp ├── alpha_config_types.cpp ├── PracticalSocket.h ├── PracticalSocket.cpp ├── stock_live_trade │ └── stock_live_trade.vcxproj ├── AlphaTrade_server.cpp ├── defs.h ├── AlphaConfig.h └── alpha_trade_types.h ├── example └── example.py ├── README.md ├── wine └── install-wine-i686-centos7.sh ├── alpha_trade.thrift ├── alpha_live_data.py ├── bbbstock_sim_trade.py └── alpha_live_trade.py /alpha_trade_python/__init__.py: -------------------------------------------------------------------------------- 1 | __all__ = ['ttypes', 'constants', 'AlphaTrade'] 2 | -------------------------------------------------------------------------------- /utility.dll: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/binhetech/alphaquant/master/utility.dll -------------------------------------------------------------------------------- /stock_live_trade.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/binhetech/alphaquant/master/stock_live_trade.exe -------------------------------------------------------------------------------- /stock_live_trade/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/binhetech/alphaquant/master/stock_live_trade/.DS_Store -------------------------------------------------------------------------------- /stock_live_trade/hexin_stock_broker.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/binhetech/alphaquant/master/stock_live_trade/hexin_stock_broker.cpp -------------------------------------------------------------------------------- /stock_live_trade/hexin_stock_broker_guangfa.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/binhetech/alphaquant/master/stock_live_trade/hexin_stock_broker_guangfa.cpp -------------------------------------------------------------------------------- /stock_live_trade/hexin_stock_broker_huatai.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/binhetech/alphaquant/master/stock_live_trade/hexin_stock_broker_huatai.cpp -------------------------------------------------------------------------------- /stock_live_trade/alpha_trade_constants.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Autogenerated by Thrift Compiler (0.10.0) 3 | * 4 | * DO NOT EDIT UNLESS YOU ARE SURE THAT YOU KNOW WHAT YOU ARE DOING 5 | * @generated 6 | */ 7 | #include "alpha_trade_constants.h" 8 | 9 | 10 | 11 | const alpha_tradeConstants g_alpha_trade_constants; 12 | 13 | alpha_tradeConstants::alpha_tradeConstants() { 14 | } 15 | 16 | 17 | 18 | -------------------------------------------------------------------------------- /alpha_trade_python/constants.py: -------------------------------------------------------------------------------- 1 | # 2 | # Autogenerated by Thrift Compiler (0.10.0) 3 | # 4 | # DO NOT EDIT UNLESS YOU ARE SURE THAT YOU KNOW WHAT YOU ARE DOING 5 | # 6 | # options string: py 7 | # 8 | 9 | from thrift.Thrift import TType, TMessageType, TFrozenDict, TException, TApplicationException 10 | from thrift.protocol.TProtocol import TProtocolException 11 | import sys 12 | from .ttypes import * 13 | -------------------------------------------------------------------------------- /stock_live_trade/alpha_config_constants.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Autogenerated by Thrift Compiler (0.10.0) 3 | * 4 | * DO NOT EDIT UNLESS YOU ARE SURE THAT YOU KNOW WHAT YOU ARE DOING 5 | * @generated 6 | */ 7 | #include "alpha_config_constants.h" 8 | 9 | 10 | 11 | const alpha_configConstants g_alpha_config_constants; 12 | 13 | alpha_configConstants::alpha_configConstants() { 14 | } 15 | 16 | 17 | 18 | -------------------------------------------------------------------------------- /stock_live_trade/main.cpp: -------------------------------------------------------------------------------- 1 | #include "PracticalSocket.h" 2 | #include "lib.h" 3 | #include 4 | #include "md5.h" 5 | 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | 16 | using namespace std; 17 | 18 | 19 | 20 | -------------------------------------------------------------------------------- /stock_live_trade/GScopedLock.h: -------------------------------------------------------------------------------- 1 | #ifndef GSCOPED_LOCK_H_HEADER_INCLUDED_B84DE627 2 | #define GSCOPED_LOCK_H_HEADER_INCLUDED_B84DE627 3 | 4 | #include "GSyncObject.h" 5 | 6 | class ScopedLock 7 | { 8 | public: 9 | ScopedLock(SyncObject& ref) : m_refSyncObj(ref) { m_refSyncObj.Lock(); } 10 | virtual ~ScopedLock() { m_refSyncObj.Unlock(); } 11 | private: 12 | SyncObject& m_refSyncObj; 13 | }; 14 | 15 | #endif -------------------------------------------------------------------------------- /stock_live_trade/hexin_weituo.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include "hexin_stock_broker_huatai.h" 4 | #include "hexin_stock_broker_guangfa.h" 5 | #include "thread_broker_adapter.h" 6 | #include "GWin32Thread.h" 7 | #include "md5.h" 8 | #include "lib.h" 9 | #include "license_manager.h" 10 | #include "signature.h" 11 | #include 12 | 13 | using namespace std; 14 | 15 | int main88(int argc, const char * argv[]) { 16 | 17 | return 0; 18 | } -------------------------------------------------------------------------------- /stock_live_trade/GSyncObject.h: -------------------------------------------------------------------------------- 1 | #ifndef GSYNCOBJECT_H_HEADER_INCLUDED_B84DE627 2 | #define GSYNCOBJECT_H_HEADER_INCLUDED_B84DE627 3 | 4 | class SyncObject 5 | { 6 | public: 7 | virtual ~SyncObject() {} 8 | virtual void Lock() = 0; 9 | virtual void Unlock() = 0; 10 | }; 11 | 12 | class NullSyncObject : public SyncObject 13 | { 14 | public: 15 | virtual ~NullSyncObject() {} 16 | virtual void Lock() {} 17 | virtual void Unlock() {} 18 | }; 19 | 20 | #endif -------------------------------------------------------------------------------- /stock_live_trade/alpha_trade_constants.h: -------------------------------------------------------------------------------- 1 | /** 2 | * Autogenerated by Thrift Compiler (0.10.0) 3 | * 4 | * DO NOT EDIT UNLESS YOU ARE SURE THAT YOU KNOW WHAT YOU ARE DOING 5 | * @generated 6 | */ 7 | #ifndef alpha_trade_CONSTANTS_H 8 | #define alpha_trade_CONSTANTS_H 9 | 10 | #include "alpha_trade_types.h" 11 | 12 | 13 | 14 | class alpha_tradeConstants { 15 | public: 16 | alpha_tradeConstants(); 17 | 18 | }; 19 | 20 | extern const alpha_tradeConstants g_alpha_trade_constants; 21 | 22 | 23 | 24 | #endif 25 | -------------------------------------------------------------------------------- /stock_live_trade/alpha_config_constants.h: -------------------------------------------------------------------------------- 1 | /** 2 | * Autogenerated by Thrift Compiler (0.10.0) 3 | * 4 | * DO NOT EDIT UNLESS YOU ARE SURE THAT YOU KNOW WHAT YOU ARE DOING 5 | * @generated 6 | */ 7 | #ifndef alpha_config_CONSTANTS_H 8 | #define alpha_config_CONSTANTS_H 9 | 10 | #include "alpha_config_types.h" 11 | 12 | 13 | 14 | class alpha_configConstants { 15 | public: 16 | alpha_configConstants(); 17 | 18 | }; 19 | 20 | extern const alpha_configConstants g_alpha_config_constants; 21 | 22 | 23 | 24 | #endif 25 | -------------------------------------------------------------------------------- /stock_live_trade/GWin32CriticalSection.cpp: -------------------------------------------------------------------------------- 1 | #include "GWin32CriticalSection.h" 2 | 3 | //#ifdef HAVE_WIN32_CRITICAL_SECTION_API 4 | 5 | Win32CriticalSection::Win32CriticalSection() 6 | { 7 | ::InitializeCriticalSection(&m_CS); 8 | } 9 | 10 | Win32CriticalSection::~Win32CriticalSection() 11 | { 12 | ::DeleteCriticalSection(&m_CS); 13 | } 14 | 15 | void Win32CriticalSection::Lock() 16 | { 17 | ::EnterCriticalSection(&m_CS); 18 | } 19 | 20 | void Win32CriticalSection::Unlock() 21 | { 22 | ::LeaveCriticalSection(&m_CS); 23 | } 24 | 25 | //#endif -------------------------------------------------------------------------------- /stock_live_trade/GWin32CriticalSection.h: -------------------------------------------------------------------------------- 1 | #ifndef GWIN32CRITICALSECTION_H_HEADER_INCLUDED_B84DE627 2 | #define GWIN32CRITICALSECTION_H_HEADER_INCLUDED_B84DE627 3 | 4 | 5 | //#ifdef HAVE_WIN32_CRITICAL_SECTION_API 6 | #include 7 | #include "GSyncObject.h" 8 | 9 | class Win32CriticalSection : public SyncObject 10 | { 11 | public: 12 | Win32CriticalSection(); 13 | virtual ~Win32CriticalSection(); 14 | 15 | virtual void Lock(); 16 | virtual void Unlock(); 17 | 18 | private: 19 | CRITICAL_SECTION m_CS; 20 | }; 21 | 22 | //#endif 23 | 24 | #endif -------------------------------------------------------------------------------- /stock_live_trade/signature.h: -------------------------------------------------------------------------------- 1 | #ifndef __SIGNATURE_LIB_H__ 2 | #define __SIGNATURE_LIB_H__ 3 | 4 | #include 5 | #include 6 | 7 | std::string pkc_sign(const unsigned char* data, int32_t len, const unsigned char* private_key, int32_t key_len); 8 | bool pkc_verify(const unsigned char* data, int32_t len, const unsigned char* signature, int32_t sig_len, const unsigned char* public_key, int32_t key_len); 9 | 10 | //std::string pkc_sign_with_key(const unsigned char* data, int32_t len); 11 | bool pkc_verify_with_key(const unsigned char* data, int32_t len, const unsigned char* signature, int32_t sig_len); 12 | 13 | #endif 14 | -------------------------------------------------------------------------------- /stock_live_trade/GWin32Event.h: -------------------------------------------------------------------------------- 1 | #ifndef GWIN32EVENT_H_HEADER_INCLUDED_B84DE627 2 | #define GWIN32EVENT_H_HEADER_INCLUDED_B84DE627 3 | 4 | #include 5 | 6 | class Win32Event 7 | { 8 | public: 9 | typedef HANDLE EVENT_HANDLE; 10 | 11 | Win32Event() { m_hEvent = ::CreateEvent(0, false, false, 0); } 12 | virtual ~Win32Event() { ::CloseHandle(m_hEvent); } 13 | 14 | EVENT_HANDLE Handle() { return m_hEvent; } 15 | void Signal() { ::SetEvent(m_hEvent); } 16 | void Wait(DWORD dwMilliseconds = INFINITE) { ::WaitForSingleObject(m_hEvent, dwMilliseconds); } 17 | 18 | private: 19 | EVENT_HANDLE m_hEvent; 20 | }; 21 | 22 | #endif -------------------------------------------------------------------------------- /example/example.py: -------------------------------------------------------------------------------- 1 | import alpha_live_trade 2 | import time 3 | 4 | if __name__ == "__main__": 5 | #alpha_live_trade.SetServer("ip or host", 58899) 6 | 7 | ret = alpha_live_trade.StartSDK("") 8 | 9 | ret = alpha_live_trade.LiveTradeLogin("XXXXXXXXXXXX", "XXXXXX", "XXXXXX", "stock_huatai") 10 | 11 | #ret = alpha_live_trade.LiveTradeLogin("XXXXXXXXXXXX", "XXXXXX", "", "stock_guangfa") 12 | 13 | live_trade_id = ret.result 14 | 15 | ret = alpha_live_trade.LiveTradeSell(live_trade_id, '600016', 8.95, 100, 'limit') 16 | 17 | ret = alpha_live_trade.LiveTradeSell(live_trade_id, '002610', 2.65, 100, 'market') 18 | 19 | alpha_live_trade.LiveTradeLogout(live_trade_id) 20 | -------------------------------------------------------------------------------- /stock_live_trade/GBase64.h: -------------------------------------------------------------------------------- 1 | // GBase64.h: interface for the Base64 class. 2 | // 3 | ////////////////////////////////////////////////////////////////////// 4 | 5 | #if !defined(AFX_GBASE64_H__FB063327_573C_4D70_A0B4_ECC067B6F4FD__INCLUDED_) 6 | #define AFX_GBASE64_H__FB063327_573C_4D70_A0B4_ECC067B6F4FD__INCLUDED_ 7 | 8 | #include 9 | 10 | class Base64 11 | { 12 | public: 13 | static std::string Encode(const char* pData, int nLen); 14 | static std::string Encode(const std::string& xstrSrc); 15 | static std::string Decode(const std::string& xstrSrc); 16 | static std::string Decode(const char* cpszSrc); 17 | }; 18 | 19 | 20 | #endif // !defined(AFX_GBASE64_H__FB063327_573C_4D70_A0B4_ECC067B6F4FD__INCLUDED_) 21 | -------------------------------------------------------------------------------- /stock_live_trade/GWin32Thread.h: -------------------------------------------------------------------------------- 1 | #ifndef GWIN32THREAD_H_HEADER_INCLUDED_B84DE627 2 | #define GWIN32THREAD_H_HEADER_INCLUDED_B84DE627 3 | 4 | //#ifdef HAVE_WIN32_THREAD_API 5 | 6 | #include 7 | 8 | class Win32Thread 9 | { 10 | public: 11 | typedef u_long ThreadId; 12 | 13 | Win32Thread(); 14 | virtual ~Win32Thread(); 15 | 16 | void StartThread(); 17 | void StartThread(LPTHREAD_START_ROUTINE lpStartAddress, LPVOID lpParameter); 18 | void Join(); 19 | 20 | static void Sleep(u_int nMillSec) { ::Sleep(nMillSec); } 21 | static ThreadId GetCurrentThreadId() { return ::GetCurrentThreadId(); } 22 | virtual u_int Run() { return 0; } 23 | 24 | private: 25 | static u_long WINAPI ThreadProc(LPVOID lParam); 26 | 27 | HANDLE m_hThread; 28 | ThreadId m_idThread; 29 | }; 30 | 31 | //#endif 32 | 33 | #endif -------------------------------------------------------------------------------- /stock_live_trade/lib.h: -------------------------------------------------------------------------------- 1 | #ifndef __WEITUO_LIB_H__ 2 | #define __WEITUO_LIB_H__ 3 | 4 | #include 5 | #include 6 | 7 | void init_lib(); 8 | int gen_key1(unsigned char *out, unsigned char *in); 9 | void client_en_de_crypt(unsigned char* out_buff, unsigned char* src_buff, int32_t *len_ptr, unsigned char* key1, unsigned char* key2, int en_or_de); 10 | void gen_key2(unsigned char *src, int32_t len, unsigned char *dst, int32_t *len_ptr, unsigned char *src2); 11 | 12 | void server_en_de_crypt(unsigned char* out_buff, unsigned char* src_buff, int32_t *len_ptr, unsigned char* key1, unsigned char* key2, int en_or_de); 13 | 14 | void signature(unsigned char *src, int32_t len, unsigned char *dst, int32_t *len_ptr, unsigned char *src2); 15 | 16 | /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 17 | 18 | #endif 19 | -------------------------------------------------------------------------------- /stock_live_trade/GWin32Thread.cpp: -------------------------------------------------------------------------------- 1 | #include "GWin32Thread.h" 2 | 3 | //#ifdef HAVE_WIN32_THREAD_API 4 | 5 | Win32Thread::Win32Thread() 6 | { 7 | m_hThread = NULL; 8 | m_idThread = 0; 9 | } 10 | 11 | Win32Thread::~Win32Thread() 12 | { 13 | if (m_hThread) 14 | ::CloseHandle(m_hThread); 15 | } 16 | 17 | void Win32Thread::StartThread() 18 | { 19 | m_hThread = ::CreateThread(NULL, 0, ThreadProc, this, 0, &m_idThread); 20 | } 21 | 22 | void Win32Thread::StartThread(LPTHREAD_START_ROUTINE lpStartAddress, LPVOID lpParameter) 23 | { 24 | m_hThread = ::CreateThread(NULL, 0, lpStartAddress, lpParameter, 0, &m_idThread); 25 | } 26 | 27 | void Win32Thread::Join() 28 | { 29 | ::WaitForSingleObject(m_hThread, INFINITE); 30 | } 31 | 32 | u_long WINAPI Win32Thread::ThreadProc( LPVOID lParam ) 33 | { 34 | Win32Thread *this_ptr = (Win32Thread*)lParam; 35 | 36 | return this_ptr->Run(); 37 | } 38 | 39 | //#endif -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # alphaquant 2 | 3 | ====================================================================== 4 | 5 | 说明: 6 | 7 | 本SDK仅供参考学习,方便自动化下单,使用请遵守相关国家法规,不配资,不操纵股价。 8 | 9 | ====================================================================== 10 | 11 | 安装部署: 12 | 13 | 系统需求:Windows或Linux系统或Mac,python2或python3 14 | 15 | 下载后解压缩即可,不需要安装运行券商交易客户端 16 | 17 | Windows命令行运行stock_live_trade.exe 18 | 19 | Linux或Mac系统命令行运行 wine stock_live_trade.exe,Linux和Mac系统需安装wine,可在命令行参数指定监听端口,例: stock_live_trade.exe 0.0.0.0 58888 20 | 21 | 命令行运行pip install thrift 22 | 23 | 将alpha_live_trade.py和alpha_trade目录拷贝到自己源代码目录,import alpha_live_trade即可使用 24 | 25 | 解压目录下的example目录中是例子代码,推荐使用pycharm编辑调试代码 26 | 27 | API文档: http://www.alpha-qt.com/?page_id=175 28 | 29 | QQ交流群:575874566 30 | 31 | stock_live_trade.exe编译说明: 32 | vs2015及以上,需要thrift,libevent,boost,zlib,openssl 33 | 34 | ============================================================================= 35 | 36 | 模拟盘使用说明 37 | 38 | 1 模拟盘不需要开启stock_live_trade.exe进程 39 | 40 | 2 有两种方法可使用python接口 41 | 42 | ● 使用实盘api,调用任何其他API先调用:SetServer("sim.bbbstock.com", 62228) 43 | 44 | ● 下载bbbstock_sim_trade.py在代码中 import bbbstock_sim_trade 45 | 46 | 3 请到http://www.bbbstock.com 注册账号,该合作伙伴目前还在起步阶段,有使用问题或建议请在qq群反馈,我们会转达。 47 | -------------------------------------------------------------------------------- /stock_live_trade/stock_live_trade.sln: -------------------------------------------------------------------------------- 1 | 2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio 15 4 | VisualStudioVersion = 15.0.26430.16 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "stock_live_trade", "stock_live_trade\stock_live_trade.vcxproj", "{4F2C8A1C-D43E-4E6C-B435-F334966F5CC0}" 7 | EndProject 8 | Global 9 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 10 | Debug|x64 = Debug|x64 11 | Debug|x86 = Debug|x86 12 | Release|x64 = Release|x64 13 | Release|x86 = Release|x86 14 | EndGlobalSection 15 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 16 | {4F2C8A1C-D43E-4E6C-B435-F334966F5CC0}.Debug|x64.ActiveCfg = Debug|x64 17 | {4F2C8A1C-D43E-4E6C-B435-F334966F5CC0}.Debug|x64.Build.0 = Debug|x64 18 | {4F2C8A1C-D43E-4E6C-B435-F334966F5CC0}.Debug|x86.ActiveCfg = Debug|Win32 19 | {4F2C8A1C-D43E-4E6C-B435-F334966F5CC0}.Debug|x86.Build.0 = Debug|Win32 20 | {4F2C8A1C-D43E-4E6C-B435-F334966F5CC0}.Release|x64.ActiveCfg = Release|x64 21 | {4F2C8A1C-D43E-4E6C-B435-F334966F5CC0}.Release|x64.Build.0 = Release|x64 22 | {4F2C8A1C-D43E-4E6C-B435-F334966F5CC0}.Release|x86.ActiveCfg = Release|Win32 23 | {4F2C8A1C-D43E-4E6C-B435-F334966F5CC0}.Release|x86.Build.0 = Release|Win32 24 | EndGlobalSection 25 | GlobalSection(SolutionProperties) = preSolution 26 | HideSolutionNode = FALSE 27 | EndGlobalSection 28 | EndGlobal 29 | -------------------------------------------------------------------------------- /stock_live_trade/license_manager.h: -------------------------------------------------------------------------------- 1 | // This autogenerated skeleton file illustrates how to build a server. 2 | // You should copy it to another filename to avoid overwriting it. 3 | 4 | #include 5 | #include 6 | #include 7 | 8 | #include "GWin32CriticalSection.h" 9 | #include "GScopedLock.h" 10 | #include "GWin32Thread.h" 11 | 12 | #include "alpha_config_types.h" 13 | 14 | #include 15 | #include 16 | 17 | class LicenseManager 18 | { 19 | public: 20 | static boost::shared_ptr get_instance(); 21 | 22 | LicenseManager(); 23 | 24 | bool license_ok(int32_t& code, std::string& msg); 25 | 26 | 27 | void set_session_id(const std::string& id); 28 | 29 | std::string get_session_id(); 30 | std::vector> get_broker_server(const std::string &broker); 31 | 32 | void set_account_money(const std::string& account, double money); 33 | 34 | double get_money_left(); 35 | 36 | void start(); 37 | 38 | private: 39 | static u_long WINAPI FetchConfigThreadProc(LPVOID lParam); 40 | 41 | private: 42 | double m_max_allowed_fund; 43 | time_t m_expire_time; 44 | 45 | std::map m_account_money_left_map; 46 | 47 | bool m_started; 48 | std::string m_session_id; 49 | Win32Thread m_thread; 50 | Win32CriticalSection m_critical_sec; 51 | 52 | std::map m_license_data; 53 | std::map> m_broker_server; 54 | }; -------------------------------------------------------------------------------- /stock_live_trade/utility.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | std::string gen_uuid(); 9 | 10 | const std::string currentDateTime(); 11 | 12 | void getWin32MacAddresses(std::vector &vMacAddresses); 13 | 14 | template 15 | void SplitString(const STR &strSrc, OI oi, const STR &strDelimiter) { 16 | int nLastPos = 0; 17 | 18 | while (nLastPos <= strSrc.size()) { 19 | int nPos; 20 | if ((nPos = strSrc.find(strDelimiter, nLastPos)) == STR::npos) {//not found 21 | *oi++ = strSrc.substr(nLastPos, STR::npos); 22 | return; 23 | } 24 | else {//found 25 | *oi++ = strSrc.substr(nLastPos, nPos - nLastPos); 26 | nLastPos = nPos + strDelimiter.size(); 27 | } 28 | } 29 | } 30 | 31 | template 32 | void SplitString(const std::string &strSrc, OI oi) { 33 | std::string white_space_str = " \t\n\r\f"; 34 | 35 | std::string::size_type nLastPos = 0; 36 | 37 | while (nLastPos <= strSrc.size()) { 38 | std::string::size_type nPos; 39 | if ((nLastPos = strSrc.find_first_not_of(white_space_str, nLastPos)) == std::string::npos) 40 | return; 41 | 42 | if ((nPos = strSrc.find_first_of(white_space_str, nLastPos)) == std::string::npos) { 43 | *oi++ = strSrc.substr(nLastPos); 44 | return; 45 | } 46 | else { 47 | *oi++ = strSrc.substr(nLastPos, nPos - nLastPos); 48 | nLastPos = nPos; 49 | } 50 | } 51 | } 52 | 53 | boost::tuple DetectBestServer(const std::vector> &server_vec); -------------------------------------------------------------------------------- /stock_live_trade/broker_adapter.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "GWin32Thread.h" 4 | #include "GWin32Event.h" 5 | #include "GScopedLock.h" 6 | #include "GWin32CriticalSection.h" 7 | #include "stock_broker.h" 8 | #include 9 | #include 10 | 11 | enum command_type 12 | { 13 | START = 0, 14 | BUY = 1, 15 | SELL = 2, 16 | STOP = 3, 17 | CANCEL = 4, 18 | 19 | RESPONSE = 5, 20 | }; 21 | 22 | struct command 23 | { 24 | int cmd; 25 | 26 | std::string sid; 27 | double price; 28 | int quant; 29 | std::string order_type; 30 | std::string order_id; 31 | std::string data; 32 | }; 33 | 34 | //{"sid": stockid, "action" : "open", "direction" : 1, "price" : price, "quant" : quant, "deal_quant" : 0, "deal_price" : 0, "state" : "pending", "desc" : "", "id" : user_order_id} 35 | 36 | 37 | class broker_adapter: public Win32Thread 38 | { 39 | public: 40 | broker_adapter(stock_broker *p); 41 | ~broker_adapter(); 42 | 43 | void start(); 44 | void stop(); 45 | 46 | std::string buy(const std::string& sid, int amount, double price, const std::string& order_type); 47 | 48 | std::string sell(const std::string& sid, int amount, double price, const std::string& order_type); 49 | 50 | void closeOrder(const std::string& order_id); 51 | 52 | std::vector getHoldingStock(); 53 | 54 | order_state getOrderState(const std::string& order_id); 55 | 56 | std::string getAccountState(); 57 | 58 | std::vector getBalance(); 59 | 60 | bool stopped() { return m_started == false; } 61 | 62 | void handlePeriodic(); 63 | private: 64 | void updateAccountState(); 65 | void setState(const std::string& state, const std::string& desc); 66 | virtual u_int Run(); 67 | 68 | int getPendingOrderNum(); 69 | 70 | private: 71 | stock_broker *m_broker_ptr; 72 | Win32CriticalSection m_critical_sec; 73 | 74 | std::vector m_holding_stock; 75 | std::vector m_balance; 76 | std::deque m_command_deq; 77 | 78 | std::string m_state; 79 | std::string m_state_desc; 80 | 81 | std::map m_pending_order_map; 82 | std::map m_completed_order_map; 83 | 84 | Win32Event m_event; 85 | bool m_started; 86 | }; 87 | -------------------------------------------------------------------------------- /stock_live_trade/thread_broker_adapter.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "GWin32Thread.h" 4 | #include "GWin32Event.h" 5 | #include "GScopedLock.h" 6 | #include "GWin32CriticalSection.h" 7 | #include "stock_broker.h" 8 | #include 9 | #include 10 | 11 | enum command_type 12 | { 13 | START = 0, 14 | BUY = 1, 15 | SELL = 2, 16 | STOP = 3, 17 | CANCEL = 4, 18 | 19 | RESPONSE = 5, 20 | }; 21 | 22 | struct command 23 | { 24 | int cmd; 25 | 26 | std::string sid; 27 | double price; 28 | int quant; 29 | std::string order_type; 30 | std::string order_id; 31 | std::string data; 32 | }; 33 | 34 | //{"sid": stockid, "action" : "open", "direction" : 1, "price" : price, "quant" : quant, "deal_quant" : 0, "deal_price" : 0, "state" : "pending", "desc" : "", "id" : user_order_id} 35 | 36 | class thread_broker_adapter 37 | { 38 | public: 39 | thread_broker_adapter(stock_broker *p, bool create_recv_thread); 40 | ~thread_broker_adapter(); 41 | 42 | void start(); 43 | void stop(); 44 | 45 | std::string buy(const std::string& sid, int amount, double price, const std::string& order_type); 46 | 47 | std::string sell(const std::string& sid, int amount, double price, const std::string& order_type); 48 | 49 | void closeOrder(const std::string& order_id); 50 | 51 | std::vector getHoldingStock(); 52 | 53 | order_state getOrderState(const std::string& user_order_id); 54 | 55 | std::vector getAllOrder(); 56 | 57 | std::string getAccountState(); 58 | 59 | std::vector getBalance(); 60 | 61 | bool stopped() { return m_started == false; } 62 | 63 | private: 64 | void updateAccountState(); 65 | void setState(const std::string& state, const std::string& desc); 66 | std::string getState(); 67 | 68 | static u_long WINAPI SendThreadProc(LPVOID lParam); 69 | static u_long WINAPI ReceiveThreadProc(LPVOID lParam); 70 | 71 | private: 72 | Win32Thread m_send_thread; 73 | Win32Thread m_recv_thread; 74 | 75 | stock_broker *m_broker_ptr; 76 | Win32CriticalSection m_critical_sec; 77 | 78 | std::deque m_command_deq; 79 | 80 | std::string m_state; 81 | std::string m_state_desc; 82 | 83 | Win32Event m_event; 84 | bool m_started; 85 | bool m_create_recv_thread; 86 | }; 87 | -------------------------------------------------------------------------------- /wine/install-wine-i686-centos7.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # Download, build and install wine 32-bit on CentOS 7 4 | 5 | # For details of this script, please check 6 | # http://www.systutorials.com/239913/install-32-bit-wine-1-8-centos-7/ 7 | 8 | # Author: Eric Zhiqiang Ma (zma@ericzma.com) 9 | 10 | set -o errexit 11 | 12 | log=`mktemp -t install-wine.XXXXXX.log` 13 | 14 | # Install 15 | 16 | ver=1.8.6 17 | #ver=1.9.24 18 | 19 | echo "Hello there. Start to download, build and install wine $ver 32-bit and 64-bit versions..." | tee $log 20 | echo "Logs are in $log" | tee -a $log 21 | 22 | echo "Uninstall old wine64 if you have installed it. Please select yes..." | tee -a $log 23 | 24 | #yum erase wine wine-* 25 | 26 | echo "Install wine building tools..." | tee -a $log 27 | 28 | yum install samba-winbind-clients -y 2>&1 >>$log 29 | yum groupinstall 'Development Tools' -y 2>&1 >> $log 30 | yum install libjpeg-turbo-devel libtiff-devel freetype-devel -y 2>&1 >> $log 31 | yum install glibc-devel.i686 libgcc.i686 libX11-devel.i686 freetype-devel.i686 gnutls-devel.i686 libxml2-devel.i686 libjpeg-turbo-devel.i686 libpng-devel.i686 libXrender-devel.i686 -y 2>&1 >> $log 32 | 33 | echo "Download and unpack the wine source package..." 2>&1 | tee -a $log 34 | 35 | cd /usr/src 2>&1 >> $log 36 | wget http://dl.winehq.org/wine/source/1.8/wine-${ver}.tar.bz2 -O wine-${ver}.tar.bz2 2>&1 >> $log 37 | tar xjf wine-${ver}.tar.bz2 2>&1 >> $log 38 | 39 | echo "Build wine..." 2>&1 | tee -a $log 40 | cd wine-${ver}/ 2>&1 >> $log 41 | mkdir -p wine32 wine64 2>&1 >> $log 42 | 43 | echo " build wine64..." 2>&1 | tee -a $log 44 | cd wine64 2>&1 >> $log 45 | ../configure --enable-win64 2>&1 >> $log 46 | make -j 4 2>&1 >> $log 47 | 48 | echo " build wine32..." 2>&1 | tee -a $log 49 | cd ../wine32 2>&1 >> $log 50 | ../configure --with-wine64=../wine64 2>&1 >> $log 51 | make -j 4 2>&1 >> $log 52 | 53 | echo "Install wine..." 2>&1 | tee -a $log 54 | echo " install wine32..." 2>&1 | tee -a $log 55 | make install 2>&1 >> $log 56 | 57 | echo " install wine64..." 2>&1 | tee -a $log 58 | cd ../wine64 2>&1 >> $log 59 | make install 2>&1 >> $log 60 | 61 | echo "Congratulation! All are done. Enjoy!" 2>&1 | tee -a $log 62 | rm -f $log 63 | 64 | # # Uninstall 65 | # cd /usr/src/wine-${ver}/wine32 66 | # make uninstall 67 | # cd /usr/src/wine-${ver}/wine64 68 | # make uninstall 69 | 70 | -------------------------------------------------------------------------------- /stock_live_trade/stock_broker.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | 13 | 14 | struct holding_item 15 | { 16 | std::string sid; 17 | std::string sname; 18 | double quant; 19 | double quant_available; 20 | double buy_price; 21 | 22 | }; 23 | 24 | struct internal_order_item 25 | { 26 | std::string order_id; 27 | std::string sid; 28 | double price; 29 | uint32_t quant; 30 | uint32_t deal_quant; 31 | double deal_price; 32 | std::string sname; 33 | std::string operation; 34 | std::string state; 35 | std::string day; 36 | std::string time; 37 | }; 38 | 39 | struct order_state 40 | { 41 | order_state() 42 | { 43 | deal_price = 0.0; 44 | deal_quant = 0; 45 | packet_id = 0; 46 | } 47 | 48 | std::string sid; 49 | std::string operation; 50 | std::string direction; 51 | double price; 52 | double deal_price; 53 | int quant; 54 | int deal_quant; 55 | std::string state; 56 | std::string desc; 57 | std::string user_order_id; 58 | std::string internal_order_id; 59 | std::string time; 60 | 61 | uint32_t packet_id; 62 | }; 63 | 64 | class stock_broker 65 | { 66 | public: 67 | virtual ~stock_broker() {} 68 | 69 | virtual int login() = 0; 70 | virtual void logout() = 0; 71 | 72 | virtual std::string create_order(const std::string& operation, const std::string& direction, const std::string& sid, double price, int amount) = 0; 73 | 74 | virtual void buy(const std::string& sid, double price, int quant, const std::string& price_type, const std::string& user_order_id) = 0; 75 | virtual void sell(const std::string& sid, double price, int quant, const std::string& price_type, const std::string& user_order_id) = 0; 76 | 77 | //order_item get_order_state(const std::string& order_id); 78 | virtual void cancel_order(const std::string& user_order_id) = 0; 79 | 80 | virtual order_state get_order_state(const std::string& user_order_id) = 0; 81 | 82 | virtual std::string get_last_msg() = 0; 83 | virtual std::vector get_all_order() = 0; 84 | 85 | virtual std::vector get_holding_stock() = 0; 86 | virtual std::vector get_money_left() = 0; 87 | 88 | virtual int try_recv_packet(std::string& pkt) = 0; 89 | 90 | virtual int handle_packet(const std::string& pkt) = 0; 91 | 92 | virtual int update_account() = 0; 93 | 94 | virtual std::string get_account() = 0; 95 | }; 96 | 97 | -------------------------------------------------------------------------------- /stock_live_trade/GBase64.cpp: -------------------------------------------------------------------------------- 1 | // GBase64.cpp: implementation of the Base64 class. 2 | // 3 | ////////////////////////////////////////////////////////////////////// 4 | 5 | #include "GBase64.h" 6 | 7 | static const char charPad = '='; 8 | static const std::string charTable = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" 9 | "abcdefghijklmnopqrstuvwxyz" 10 | "0123456789+/"; 11 | 12 | std::string Base64::Encode(const char* pData, int nLen) 13 | { 14 | std::string strSrc(pData, pData+nLen); 15 | 16 | return Encode(strSrc); 17 | } 18 | 19 | std::string Base64::Encode(const std::string& xstrSrc) 20 | { 21 | std::string::size_type i; 22 | char c; 23 | std::string::size_type len = xstrSrc.length(); 24 | std::string strRet; 25 | 26 | for (i = 0; i < len; ++i) { 27 | c = (xstrSrc[i] >> 2) & 0x3f; 28 | strRet.append(1, charTable[c]); 29 | c = (xstrSrc[i] << 4) & 0x3f; 30 | if (++i < len) 31 | c |= (xstrSrc[i] >> 4) & 0x0f; 32 | 33 | strRet.append(1, charTable[c]); 34 | if (i < len) { 35 | c = (xstrSrc[i] << 2) & 0x3f; 36 | if (++i < len) 37 | c |= (xstrSrc[i] >> 6) & 0x03; 38 | 39 | strRet.append(1, charTable[c]); 40 | } 41 | else { 42 | ++i; 43 | strRet.append(1, charPad); 44 | } 45 | 46 | if (i < len) { 47 | c = xstrSrc[i] & 0x3f; 48 | strRet.append(1, charTable[c]); 49 | } 50 | else { 51 | strRet.append(1, charPad); 52 | } 53 | } 54 | 55 | return strRet; 56 | } 57 | 58 | std::string Base64::Decode(const std::string& xstrSrc) 59 | { 60 | std::string::size_type i; 61 | char c; 62 | char c1; 63 | std::string::size_type len = xstrSrc.length(); 64 | std::string strRet; 65 | 66 | for (i = 0; i < len; ++i) { 67 | c = (char) charTable.find(xstrSrc[i]); 68 | ++i; 69 | c1 = (char) charTable.find(xstrSrc[i]); 70 | c = (c << 2) | ((c1 >> 4) & 0x3); 71 | strRet.append(1, c); 72 | if (++i < len) { 73 | c = xstrSrc[i]; 74 | if (charPad == c) 75 | break; 76 | 77 | c = (char) charTable.find(c); 78 | c1 = ((c1 << 4) & 0xf0) | ((c >> 2) & 0xf); 79 | strRet.append(1, c1); 80 | } 81 | 82 | if (++i < len) { 83 | c1 = xstrSrc[i]; 84 | if (charPad == c1) 85 | break; 86 | 87 | c1 = (char) charTable.find(c1); 88 | c = ((c << 6) & 0xc0) | c1; 89 | strRet.append(1, c); 90 | } 91 | } 92 | 93 | return strRet; 94 | } 95 | 96 | std::string Base64::Decode(const char* cpszSrc) 97 | { 98 | return Decode(std::string(cpszSrc)); 99 | } -------------------------------------------------------------------------------- /alpha_trade.thrift: -------------------------------------------------------------------------------- 1 | struct HoldingStock { 2 | 1: string stock_id, 3 | 2: double buy_price, 4 | 3: string long_short, 5 | 4: double quant, 6 | 5: double quant_sellable, 7 | } 8 | 9 | struct AccountBalance { 10 | 1: double total_value, 11 | 2: double money_left, 12 | } 13 | 14 | struct OrderState { 15 | 1: string order_id, 16 | 2: string time, 17 | 3: string sid, 18 | 4: string internal_order_id, 19 | 5: string operation, 20 | 6: string direction, 21 | 7: double price, 22 | 8: double quant, 23 | 9: double deal_quant, 24 | 10: double deal_price, 25 | 11: string state, 26 | 12: string desc, 27 | } 28 | 29 | struct LiveTradeLoginResp { 30 | 1: i32 ret_code, 31 | 2: string result, 32 | } 33 | 34 | struct LiveTradeLogoutResp { 35 | 1: i32 ret_code, 36 | } 37 | 38 | struct GetAccountStateResp { 39 | 1: i32 ret_code, 40 | 2: string state, 41 | 3: string desc, 42 | } 43 | 44 | struct GetAccountInfoResp { 45 | 1: i32 ret_code, 46 | 2: string result, 47 | } 48 | 49 | struct GetAccountBalanceResp { 50 | 1: i32 ret_code, 51 | 2: AccountBalance result, 52 | } 53 | 54 | struct GetHoldingStockResp { 55 | 1: i32 ret_code, 56 | 2: list result, 57 | } 58 | 59 | struct GetAllOrderResp { 60 | 1: i32 ret_code, 61 | 2: list result, 62 | } 63 | 64 | struct GetOrderStateResp { 65 | 1: i32 ret_code, 66 | 2: OrderState result, 67 | } 68 | 69 | struct CloseOrderResp { 70 | 1: i32 ret_code, 71 | } 72 | 73 | struct CancelOrderResp { 74 | 1: i32 ret_code, 75 | } 76 | 77 | struct PlaceOrderResp { 78 | 1: i32 ret_code, 79 | 2: string result, 80 | } 81 | 82 | service AlphaTrade { 83 | 84 | bool Running(), 85 | i32 GetPID(), 86 | 87 | void KeepAlive(1:string sessionID), 88 | 89 | LiveTradeLoginResp LiveTradeLogin(1:string sessionID, 2:string account, 3:string password1, 4:string password2, 5:string brokerStr), 90 | LiveTradeLogoutResp LiveTradeLogout(1:string sessionID, 2:string liveTradeID), 91 | GetAccountStateResp GetAccountState(1:string sessionID, 2:string liveTradeID), 92 | 93 | GetAccountBalanceResp GetAccountBalance(1:string sessionID, 2:string liveTradeID), 94 | 95 | GetHoldingStockResp GetHoldingStock(1:string sessionID, 2:string liveTradeID), 96 | GetAllOrderResp GetAllOrder(1:string sessionID, 2:string liveTradeID), 97 | 98 | GetOrderStateResp GetOrderState(1:string sessionID, 2:string liveTradeID, 3:string orderID), 99 | 100 | GetAccountInfoResp GetAccountInfo(1:string sessionID, 2:string liveTradeID, 3:string key), 101 | 102 | CancelOrderResp CancelOrder(1:string sessionID, 2:string liveTradeID, 3:string orderID), 103 | 104 | CloseOrderResp CloseOrder(1:string sessionID, 2:string liveTradeID, 3:string orderID), 105 | PlaceOrderResp LiveTradeBuyOpen(1:string sessionID, 2:string liveTradeID, 3:string sid, 4:double price, 5:double quant, 6:string orderType), 106 | PlaceOrderResp LiveTradeBuyClose(1:string sessionID, 2:string liveTradeID, 3:string sid, 4:double price, 5:double quant, 6:string orderType, 7:bool closeToday), 107 | PlaceOrderResp LiveTradeSellClose(1:string sessionID, 2:string liveTradeID, 3:string sid, 4:double price, 5:double quant, 6:string orderType, 7:bool closeToday), 108 | PlaceOrderResp LiveTradeSellOpen(1:string sessionID, 2:string liveTradeID, 3:string sid, 4:double price, 5:double quant, 6:string orderType), 109 | 110 | } 111 | -------------------------------------------------------------------------------- /stock_live_trade/md5.h: -------------------------------------------------------------------------------- 1 | /** 2 | * @file md5.h 3 | * @The header file of md5. 4 | * @author Jiewei Wei 5 | * @mail weijieweijerry@163.com 6 | * @github https://github.com/JieweiWei 7 | * @data Oct 19 2014 8 | * 9 | */ 10 | 11 | #ifndef MD5_H 12 | #define MD5_H 13 | 14 | /* Parameters of MD5. */ 15 | #define s11 7 16 | #define s12 12 17 | #define s13 17 18 | #define s14 22 19 | #define s21 5 20 | #define s22 9 21 | #define s23 14 22 | #define s24 20 23 | #define s31 4 24 | #define s32 11 25 | #define s33 16 26 | #define s34 23 27 | #define s41 6 28 | #define s42 10 29 | #define s43 15 30 | #define s44 21 31 | 32 | /** 33 | * @Basic MD5 functions. 34 | * 35 | * @param there bit32. 36 | * 37 | * @return one bit32. 38 | */ 39 | #define F(x, y, z) (((x) & (y)) | ((~x) & (z))) 40 | #define G(x, y, z) (((x) & (z)) | ((y) & (~z))) 41 | #define H(x, y, z) ((x) ^ (y) ^ (z)) 42 | #define I(x, y, z) ((y) ^ ((x) | (~z))) 43 | 44 | /** 45 | * @Rotate Left. 46 | * 47 | * @param {num} the raw number. 48 | * 49 | * @param {n} rotate left n. 50 | * 51 | * @return the number after rotated left. 52 | */ 53 | #define ROTATELEFT(num, n) (((num) << (n)) | ((num) >> (32-(n)))) 54 | 55 | /** 56 | * @Transformations for rounds 1, 2, 3, and 4. 57 | */ 58 | #define FF(a, b, c, d, x, s, ac) { \ 59 | (a) += F ((b), (c), (d)) + (x) + ac; \ 60 | (a) = ROTATELEFT ((a), (s)); \ 61 | (a) += (b); \ 62 | } 63 | #define GG(a, b, c, d, x, s, ac) { \ 64 | (a) += G ((b), (c), (d)) + (x) + ac; \ 65 | (a) = ROTATELEFT ((a), (s)); \ 66 | (a) += (b); \ 67 | } 68 | #define HH(a, b, c, d, x, s, ac) { \ 69 | (a) += H ((b), (c), (d)) + (x) + ac; \ 70 | (a) = ROTATELEFT ((a), (s)); \ 71 | (a) += (b); \ 72 | } 73 | #define II(a, b, c, d, x, s, ac) { \ 74 | (a) += I ((b), (c), (d)) + (x) + ac; \ 75 | (a) = ROTATELEFT ((a), (s)); \ 76 | (a) += (b); \ 77 | } 78 | 79 | #include 80 | #include 81 | 82 | using std::string; 83 | 84 | /* Define of btye.*/ 85 | typedef unsigned char byte; 86 | /* Define of byte. */ 87 | typedef unsigned int bit32; 88 | 89 | class MD5 { 90 | public: 91 | /* Construct a MD5 object with a string. */ 92 | MD5(const string& message); 93 | 94 | /* Generate md5 digest. */ 95 | const byte* getDigest(); 96 | 97 | /* Convert digest to string value */ 98 | string toStr(); 99 | 100 | private: 101 | /* Initialization the md5 object, processing another message block, 102 | * and updating the context.*/ 103 | void init(const byte* input, size_t len); 104 | 105 | /* MD5 basic transformation. Transforms state based on block. */ 106 | void transform(const byte block[64]); 107 | 108 | /* Encodes input (usigned long) into output (byte). */ 109 | void encode(const bit32* input, byte* output, size_t length); 110 | 111 | /* Decodes input (byte) into output (usigned long). */ 112 | void decode(const byte* input, bit32* output, size_t length); 113 | 114 | private: 115 | /* Flag for mark whether calculate finished. */ 116 | bool finished; 117 | 118 | /* state (ABCD). */ 119 | bit32 state[4]; 120 | 121 | /* number of bits, low-order word first. */ 122 | bit32 count[2]; 123 | 124 | /* input buffer. */ 125 | byte buffer[64]; 126 | 127 | /* message digest. */ 128 | byte digest[16]; 129 | 130 | /* padding for calculate. */ 131 | static const byte PADDING[64]; 132 | 133 | /* Hex numbers. */ 134 | static const char HEX_NUMBERS[16]; 135 | }; 136 | 137 | #endif // MD5_H 138 | -------------------------------------------------------------------------------- /stock_live_trade/alpha_config_types.h: -------------------------------------------------------------------------------- 1 | /** 2 | * Autogenerated by Thrift Compiler (0.10.0) 3 | * 4 | * DO NOT EDIT UNLESS YOU ARE SURE THAT YOU KNOW WHAT YOU ARE DOING 5 | * @generated 6 | */ 7 | #ifndef alpha_config_TYPES_H 8 | #define alpha_config_TYPES_H 9 | 10 | #include 11 | 12 | #include 13 | #include 14 | #include 15 | #include 16 | #include 17 | 18 | #include 19 | 20 | 21 | 22 | 23 | class ServerItem; 24 | 25 | class ConfigData; 26 | 27 | typedef struct _ServerItem__isset { 28 | _ServerItem__isset() : host(false), port(false) {} 29 | bool host :1; 30 | bool port :1; 31 | } _ServerItem__isset; 32 | 33 | class ServerItem : public virtual ::apache::thrift::TBase { 34 | public: 35 | 36 | ServerItem(const ServerItem&); 37 | ServerItem& operator=(const ServerItem&); 38 | ServerItem() : host(), port(0) { 39 | } 40 | 41 | virtual ~ServerItem() throw(); 42 | std::string host; 43 | int32_t port; 44 | 45 | _ServerItem__isset __isset; 46 | 47 | void __set_host(const std::string& val); 48 | 49 | void __set_port(const int32_t val); 50 | 51 | bool operator == (const ServerItem & rhs) const 52 | { 53 | if (!(host == rhs.host)) 54 | return false; 55 | if (!(port == rhs.port)) 56 | return false; 57 | return true; 58 | } 59 | bool operator != (const ServerItem &rhs) const { 60 | return !(*this == rhs); 61 | } 62 | 63 | bool operator < (const ServerItem & ) const; 64 | 65 | uint32_t read(::apache::thrift::protocol::TProtocol* iprot); 66 | uint32_t write(::apache::thrift::protocol::TProtocol* oprot) const; 67 | 68 | virtual void printTo(std::ostream& out) const; 69 | }; 70 | 71 | void swap(ServerItem &a, ServerItem &b); 72 | 73 | inline std::ostream& operator<<(std::ostream& out, const ServerItem& obj) 74 | { 75 | obj.printTo(out); 76 | return out; 77 | } 78 | 79 | typedef struct _ConfigData__isset { 80 | _ConfigData__isset() : broker_server(false), license_data(false) {} 81 | bool broker_server :1; 82 | bool license_data :1; 83 | } _ConfigData__isset; 84 | 85 | class ConfigData : public virtual ::apache::thrift::TBase { 86 | public: 87 | 88 | ConfigData(const ConfigData&); 89 | ConfigData& operator=(const ConfigData&); 90 | ConfigData() { 91 | } 92 | 93 | virtual ~ConfigData() throw(); 94 | std::map > broker_server; 95 | std::map license_data; 96 | 97 | _ConfigData__isset __isset; 98 | 99 | void __set_broker_server(const std::map > & val); 100 | 101 | void __set_license_data(const std::map & val); 102 | 103 | bool operator == (const ConfigData & rhs) const 104 | { 105 | if (!(broker_server == rhs.broker_server)) 106 | return false; 107 | if (!(license_data == rhs.license_data)) 108 | return false; 109 | return true; 110 | } 111 | bool operator != (const ConfigData &rhs) const { 112 | return !(*this == rhs); 113 | } 114 | 115 | bool operator < (const ConfigData & ) const; 116 | 117 | uint32_t read(::apache::thrift::protocol::TProtocol* iprot); 118 | uint32_t write(::apache::thrift::protocol::TProtocol* oprot) const; 119 | 120 | virtual void printTo(std::ostream& out) const; 121 | }; 122 | 123 | void swap(ConfigData &a, ConfigData &b); 124 | 125 | inline std::ostream& operator<<(std::ostream& out, const ConfigData& obj) 126 | { 127 | obj.printTo(out); 128 | return out; 129 | } 130 | 131 | 132 | 133 | #endif 134 | -------------------------------------------------------------------------------- /stock_live_trade/utility.cpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "utility.h" 4 | #include "md5.h" 5 | #include "PracticalSocket.h" 6 | 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | #pragma comment(lib, "IPHLPAPI.lib") 16 | 17 | using namespace std; 18 | 19 | std::string gen_uuid() 20 | { 21 | static uint32_t s_count = 0; 22 | 23 | time_t rawtime; 24 | struct tm * timeinfo; 25 | char buffer[80] = { 0 }; 26 | 27 | time(&rawtime); 28 | timeinfo = localtime(&rawtime); 29 | 30 | strftime(buffer, 80, "%Y%m%d%H%M%S", timeinfo); 31 | 32 | ostringstream oss; 33 | oss << std::string(buffer) << ::GetTickCount() << ++s_count << &rawtime; 34 | 35 | MD5 hash(oss.str()); 36 | return hash.toStr(); 37 | } 38 | 39 | const std::string currentDateTime() { 40 | time_t now = time(0); 41 | struct tm tstruct; 42 | char buf[80]; 43 | tstruct = *localtime(&now); 44 | // Visit http://en.cppreference.com/w/cpp/chrono/c/strftime 45 | // for more information about date/time format 46 | strftime(buf, sizeof(buf), "%Y-%m-%d %X", &tstruct); 47 | 48 | return buf; 49 | } 50 | 51 | 52 | void getWin32MacAddresses(std::vector &vMacAddresses) 53 | { 54 | vMacAddresses.clear(); 55 | IP_ADAPTER_INFO AdapterInfo[32]; // Allocate information for up to 32 NICs 56 | DWORD dwBufLen = sizeof(AdapterInfo); // Save memory size of buffer 57 | DWORD dwStatus = GetAdaptersInfo( // Call GetAdapterInfo 58 | AdapterInfo, // [out] buffer to receive data 59 | &dwBufLen); // [in] size of receive data buffer 60 | 61 | //No network card? Other error? 62 | if (dwStatus != ERROR_SUCCESS) 63 | return; 64 | 65 | PIP_ADAPTER_INFO pAdapterInfo = AdapterInfo; 66 | char szBuffer[512] = {0}; 67 | while (pAdapterInfo) 68 | { 69 | if (pAdapterInfo->Type == MIB_IF_TYPE_ETHERNET) 70 | { 71 | sprintf_s(szBuffer, sizeof(szBuffer), "%.2x-%.2x-%.2x-%.2x-%.2x-%.2x" 72 | , pAdapterInfo->Address[0] 73 | , pAdapterInfo->Address[1] 74 | , pAdapterInfo->Address[2] 75 | , pAdapterInfo->Address[3] 76 | , pAdapterInfo->Address[4] 77 | , pAdapterInfo->Address[5] 78 | ); 79 | vMacAddresses.push_back(szBuffer); 80 | } 81 | pAdapterInfo = pAdapterInfo->Next; 82 | 83 | } 84 | 85 | std::sort(vMacAddresses.begin(), vMacAddresses.end()); 86 | } 87 | 88 | boost::tuple DetectBestServer(const std::vector> &server_vec) 89 | { 90 | cout << "detect server" << endl; 91 | 92 | std::string host; 93 | uint16_t port = 0; 94 | 95 | long long min = 100000; 96 | 97 | for (int i = 0; i < server_vec.size(); ++i) { 98 | long long microseconds; 99 | auto start = std::chrono::steady_clock::now(); 100 | try { 101 | /* 102 | apache::thrift::transport::TSocket s(server_vec[i].get<0>(), server_vec[i].get<1>()); 103 | s.setConnTimeout(3000); 104 | s.open(); 105 | */ 106 | 107 | TCPSocket socket(server_vec[i].get<0>(), server_vec[i].get<1>()); 108 | 109 | auto elapsed = std::chrono::steady_clock::now() - start; 110 | microseconds = std::chrono::duration_cast(elapsed).count(); 111 | 112 | } 113 | catch (...) { 114 | microseconds = 10000; 115 | } 116 | 117 | if (microseconds < min || host.size() == 0) { 118 | min = microseconds; 119 | host = server_vec[i].get<0>(); 120 | port = server_vec[i].get<1>(); 121 | } 122 | } 123 | 124 | cout << "detect server end" << endl; 125 | 126 | return boost::make_tuple(host, port); 127 | } -------------------------------------------------------------------------------- /stock_live_trade/hexin_stock_broker.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "PracticalSocket.h" 4 | #include "GWin32CriticalSection.h" 5 | #include "GScopedLock.h" 6 | #include "lib.h" 7 | #include "zlib\zlib.h" 8 | #include "md5.h" 9 | #include "stock_broker.h" 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | #include 17 | #include 18 | #include 19 | #include 20 | 21 | #include 22 | 23 | class hexin_stock_broker : public stock_broker 24 | { 25 | public: 26 | hexin_stock_broker(const std::vector> &server_vec, const std::string& account, const std::string& password1, const std::string& password2, bool wait_response); 27 | virtual ~hexin_stock_broker(); 28 | 29 | virtual int login(); 30 | virtual void logout(); 31 | 32 | virtual std::vector get_holding_stock(); 33 | virtual std::vector get_money_left(); 34 | 35 | virtual std::string create_order(const std::string& operation, const std::string& direction, const std::string& sid, double price, int amount); 36 | 37 | virtual void buy(const std::string& sid, double price, int quant, const std::string& price_type, const std::string& user_order_id); 38 | virtual void sell(const std::string& sid, double price, int quant, const std::string& price_type, const std::string& user_order_id); 39 | 40 | virtual order_state get_order_state(const std::string& user_order_id); 41 | 42 | //order_item get_order_state(const std::string& order_id); 43 | virtual void cancel_order(const std::string& user_order_id); 44 | 45 | virtual std::string get_last_msg() { return m_last_msg; } 46 | virtual std::vector get_all_order(); 47 | 48 | virtual int try_recv_packet(std::string& pkt); 49 | virtual int handle_packet(const std::string& pkt); 50 | 51 | virtual int update_account(); 52 | 53 | virtual std::string get_account(); 54 | private: 55 | int send_login_packet(); 56 | int send_get_gddm_packet(); 57 | 58 | int send_get_account_holding(); 59 | int handle_get_holding_response(const std::string& resp); 60 | 61 | int send_get_account_balance(); 62 | int handle_get_balance_response(const std::string& resp); 63 | 64 | int send_get_account_all_order(); 65 | int handle_all_order_response(const std::string& resp); 66 | 67 | int send_buy(const std::string& sid, double price, int quant, const std::string& price_type); 68 | int handle_buy_response(const std::string& resp); 69 | 70 | int send_sell(const std::string& sid, double price, int quant, const std::string& price_type); 71 | int handle_sell_response(const std::string& resp); 72 | 73 | int send_cancel(const std::string& order_id, const std::string& sid); 74 | int handle_cancel_response(const std::string& resp); 75 | 76 | int handle_deal_notification(const std::string& resp); 77 | 78 | 79 | private: 80 | bool m_wait_response; 81 | 82 | std::string m_server; 83 | int m_port; 84 | 85 | bool m_login; 86 | 87 | std::vector> m_server_vec; 88 | 89 | std::string m_encrypt_key1; 90 | std::string m_encrypt_key2; 91 | 92 | std::string m_decrypt_key1; 93 | std::string m_decrypt_key2; 94 | 95 | uint32_t m_packet_index = 2; 96 | 97 | char m_session_id[4] = { 0 }; 98 | 99 | TCPSocket m_socket; 100 | 101 | std::string m_account; 102 | std::string m_password; 103 | std::string m_comm_password; 104 | std::string m_account_name; 105 | 106 | std::vector m_sh_gddm_list; 107 | std::vector m_sz_gddm_list; 108 | 109 | std::vector m_balance; 110 | std::vector m_holding_stock; 111 | std::vector m_all_internal_order; 112 | 113 | std::string m_last_msg; 114 | 115 | uint8_t m_decrypt_out_buff[0x5a0] = { 0 };//important 116 | 117 | Win32CriticalSection m_critical_sec; 118 | 119 | std::map m_pending_order_map; 120 | std::map m_completed_order_map; 121 | }; 122 | 123 | -------------------------------------------------------------------------------- /stock_live_trade/hexin_stock_broker_guangfa.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "PracticalSocket.h" 4 | #include "lib.h" 5 | #include "zlib\zlib.h" 6 | #include "md5.h" 7 | #include "stock_broker.h" 8 | #include "GWin32CriticalSection.h" 9 | 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | #include 17 | #include 18 | #include 19 | #include 20 | #include 21 | 22 | 23 | class hexin_stock_broker_guangfa : public stock_broker 24 | { 25 | public: 26 | hexin_stock_broker_guangfa(const std::vector> &server_vec, const std::string& account, const std::string& password1, const std::string& password2, bool wait_response = true); 27 | virtual ~hexin_stock_broker_guangfa(); 28 | 29 | virtual int login(); 30 | virtual void logout(); 31 | 32 | virtual std::vector get_holding_stock(); 33 | virtual std::vector get_money_left(); 34 | 35 | virtual std::string create_order(const std::string& operation, const std::string& direction, const std::string& sid, double price, int amount); 36 | 37 | virtual void buy(const std::string& sid, double price, int quant, const std::string& price_type, const std::string& user_order_id); 38 | virtual void sell(const std::string& sid, double price, int quant, const std::string& price_type, const std::string& user_order_id); 39 | 40 | //order_item get_order_state(const std::string& order_id); 41 | virtual void cancel_order(const std::string& user_order_id); 42 | 43 | virtual order_state get_order_state(const std::string& user_order_id); 44 | 45 | virtual std::string get_last_msg() { return m_last_msg; } 46 | virtual std::vector get_all_order(); 47 | 48 | virtual int try_recv_packet(std::string& pkt); 49 | virtual int handle_packet(const std::string& pkt); 50 | 51 | virtual int update_account(); 52 | 53 | virtual std::string get_account(); 54 | 55 | private: 56 | int send_login_packet(); 57 | int send_get_gddm_packet(); 58 | 59 | int send_get_account_holding(); 60 | int handle_get_holding_response(const std::string& resp); 61 | 62 | int send_get_account_balance(); 63 | int handle_get_balance_response(const std::string& resp); 64 | 65 | int send_get_account_all_order(); 66 | int handle_all_order_response(const std::string& resp); 67 | 68 | int send_buy(const std::string& sid, double price, int quant, const std::string& price_type); 69 | int handle_buy_response(const std::string& resp); 70 | 71 | int send_sell(const std::string& sid, double price, int quant, const std::string& price_type); 72 | int handle_sell_response(const std::string& resp); 73 | 74 | int send_cancel(const std::string& order_id, const std::string& sid); 75 | int handle_cancel_response(const std::string& resp); 76 | 77 | private: 78 | bool m_wait_response; 79 | bool m_login; 80 | 81 | bool m_waiting_holding_resp; 82 | bool m_waiting_balance_resp; 83 | bool m_waiting_get_order_resp; 84 | 85 | std::string m_server; 86 | int m_port; 87 | 88 | std::vector> m_server_vec; 89 | 90 | std::string m_encrypt_key1; 91 | std::string m_encrypt_key2; 92 | 93 | std::string m_decrypt_key1; 94 | std::string m_decrypt_key2; 95 | 96 | uint32_t m_packet_index = 2; 97 | 98 | char m_session_id[4] = { 0 }; 99 | 100 | TCPSocket m_socket; 101 | 102 | std::string m_account; 103 | std::string m_password; 104 | std::string m_comm_password; 105 | std::string m_account_name; 106 | std::string m_fund_account; 107 | 108 | std::vector m_sh_gddm_list; 109 | std::vector m_sz_gddm_list; 110 | 111 | std::vector m_balance; 112 | std::vector m_holding_stock; 113 | std::vector m_all_internal_order; 114 | 115 | std::string m_last_msg; 116 | 117 | Win32CriticalSection m_critical_sec; 118 | 119 | std::map m_pending_order_map; 120 | std::map m_completed_order_map; 121 | }; 122 | 123 | -------------------------------------------------------------------------------- /stock_live_trade/hexin_stock_broker_huatai.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "PracticalSocket.h" 4 | #include "GWin32CriticalSection.h" 5 | #include "GScopedLock.h" 6 | #include "lib.h" 7 | #include "zlib\zlib.h" 8 | #include "md5.h" 9 | #include "stock_broker.h" 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | #include 17 | #include 18 | #include 19 | #include 20 | 21 | #include 22 | 23 | class hexin_stock_broker_huatai : public stock_broker 24 | { 25 | public: 26 | hexin_stock_broker_huatai(const std::vector> &server_vec, const std::string& account, const std::string& password1, const std::string& password2, bool wait_response); 27 | virtual ~hexin_stock_broker_huatai(); 28 | 29 | virtual int login(); 30 | virtual void logout(); 31 | 32 | virtual std::vector get_holding_stock(); 33 | virtual std::vector get_money_left(); 34 | 35 | virtual std::string create_order(const std::string& operation, const std::string& direction, const std::string& sid, double price, int amount); 36 | 37 | virtual void buy(const std::string& sid, double price, int quant, const std::string& price_type, const std::string& user_order_id); 38 | virtual void sell(const std::string& sid, double price, int quant, const std::string& price_type, const std::string& user_order_id); 39 | 40 | virtual order_state get_order_state(const std::string& user_order_id); 41 | 42 | //order_item get_order_state(const std::string& order_id); 43 | virtual void cancel_order(const std::string& user_order_id); 44 | 45 | virtual std::string get_last_msg() { return m_last_msg; } 46 | virtual std::vector get_all_order(); 47 | 48 | virtual int try_recv_packet(std::string& pkt); 49 | virtual int handle_packet(const std::string& pkt); 50 | 51 | virtual int update_account(); 52 | 53 | virtual std::string get_account(); 54 | private: 55 | int send_login_packet(); 56 | int send_get_gddm_packet(); 57 | 58 | int send_get_account_holding(); 59 | int handle_get_holding_response(const std::string& resp); 60 | 61 | int send_get_account_balance(); 62 | int handle_get_balance_response(const std::string& resp); 63 | 64 | int send_get_account_all_order(); 65 | int handle_all_order_response(const std::string& resp); 66 | 67 | int send_buy(const std::string& sid, double price, int quant, const std::string& price_type); 68 | int handle_buy_response(const std::string& resp); 69 | 70 | int send_sell(const std::string& sid, double price, int quant, const std::string& price_type); 71 | int handle_sell_response(const std::string& resp); 72 | 73 | int send_cancel(const std::string& order_id, const std::string& sid); 74 | int handle_cancel_response(const std::string& resp); 75 | 76 | int handle_deal_notification(const std::string& resp); 77 | 78 | 79 | private: 80 | bool m_wait_response; 81 | 82 | std::string m_server; 83 | int m_port; 84 | 85 | bool m_login; 86 | 87 | std::vector> m_server_vec; 88 | 89 | std::string m_encrypt_key1; 90 | std::string m_encrypt_key2; 91 | 92 | std::string m_decrypt_key1; 93 | std::string m_decrypt_key2; 94 | 95 | uint32_t m_packet_index = 2; 96 | 97 | char m_session_id[4] = { 0 }; 98 | 99 | TCPSocket m_socket; 100 | 101 | std::string m_account; 102 | std::string m_password; 103 | std::string m_comm_password; 104 | std::string m_account_name; 105 | 106 | std::vector m_sh_gddm_list; 107 | std::vector m_sz_gddm_list; 108 | 109 | std::vector m_balance; 110 | std::vector m_holding_stock; 111 | std::vector m_all_internal_order; 112 | 113 | std::string m_last_msg; 114 | 115 | uint8_t m_decrypt_out_buff[0x5a0] = { 0 };//important 116 | 117 | Win32CriticalSection m_critical_sec; 118 | 119 | std::map m_pending_order_map; 120 | std::map m_completed_order_map; 121 | }; 122 | 123 | -------------------------------------------------------------------------------- /alpha_live_data.py: -------------------------------------------------------------------------------- 1 | import sys 2 | import traceback 3 | if sys.version_info >= (3,0): 4 | import urllib.request 5 | else: 6 | import urllib2 7 | import sys 8 | import threading 9 | import time 10 | 11 | g_lock = threading.Lock() 12 | g_stock_price_data = {} 13 | g_stock_set = set() 14 | 15 | 16 | def _parse_sina_real_time_data_response(resp_str): 17 | ret = {} 18 | for line in resp_str.split("\n"): 19 | try: 20 | start = line.find('="') 21 | if start == -1 : 22 | continue 23 | 24 | sid = line[start-6:start] 25 | 26 | start += 2 27 | 28 | end = line.find('";') 29 | if end == -1: 30 | continue 31 | 32 | content = line[start:end] 33 | 34 | result_list = content.split(',') 35 | #result_list[0] = result_list[0].decode('gbk') 36 | 37 | ret[sid] = result_list 38 | except: 39 | print(sys.exc_info()) 40 | 41 | return ret 42 | 43 | def _get_sina_name(sid): 44 | code = int(sid) 45 | highest_bit = int(code/100000) 46 | prefix = "sz" 47 | if highest_bit == 6: 48 | prefix = "sh" 49 | 50 | code = "%s%06d" % (prefix, code) 51 | 52 | return code 53 | 54 | 55 | def _get_real_time_data(sid_list): 56 | try: 57 | url = "http://hq.sinajs.cn/list=" 58 | for i in range(len(sid_list)): 59 | 60 | sina_name = _get_sina_name(sid_list[i]) 61 | fix = "" 62 | if i != 0: 63 | fix = "," 64 | url = url + fix + sina_name 65 | 66 | if sys.version_info >= (3,0): 67 | resp_str = str(urllib.request.urlopen(url).read().decode("GBK")) 68 | else: 69 | resp_str = urllib2.urlopen(url).read().decode("GBK") 70 | 71 | ret = _parse_sina_real_time_data_response(resp_str) 72 | #print("get ", len(sid_list), " realtime data success") 73 | return ret 74 | except: 75 | print("get realtime data failed ", sys.exc_info(), traceback.format_exc()) 76 | return {} 77 | 78 | 79 | def subscribe_stock(stock_id): 80 | g_lock.acquire() 81 | g_stock_set.add(stock_id) 82 | g_lock.release() 83 | 84 | def unsubscribe_stock(stock_id): 85 | g_lock.acquire() 86 | try: 87 | g_stock_set.remove(stock_id) 88 | except: 89 | pass 90 | g_lock.release() 91 | 92 | 93 | def get_stock_live_data(stock_id): 94 | 95 | ret = None 96 | 97 | subscribe_stock(stock_id) 98 | 99 | g_lock.acquire() 100 | if stock_id in g_stock_price_data: 101 | try: 102 | ret = {} 103 | info = g_stock_price_data[stock_id] 104 | ret['name'] = info[0] 105 | ret['datetime'] = info[30] + " " + info[31] 106 | ret['open'] = float(info[1]) 107 | ret['high'] = float(info[4])#high 108 | ret['low'] = float(info[5])#low 109 | current_price = float(info[3]) 110 | ret['close'] = current_price#close current price 111 | ret['volume'] = int(info[8])#Volume 112 | ret['preclose'] = float(info[2]) 113 | 114 | ret['bid'] = [[float(info[11]), float(info[10])], [float(info[13]), float(info[12])], [float(info[15]), float(info[14])], [float(info[17]), float(info[16])], [float(info[19]), float(info[18])]] 115 | ret['ask'] = [[float(info[21]), float(info[20])], [float(info[23]), float(info[22])], [float(info[25]), float(info[24])], [float(info[27]), float(info[26])], [float(info[29]), float(info[28])]] 116 | 117 | except: 118 | print("except ",sys.exc_info()) 119 | 120 | g_lock.release() 121 | 122 | return ret 123 | 124 | 125 | def _thread_proc(): 126 | while True: 127 | try: 128 | g_lock.acquire() 129 | stockid_list = list(g_stock_set.copy()) 130 | g_lock.release() 131 | 132 | if len(stockid_list) > 0: 133 | ret = _get_real_time_data(stockid_list) 134 | g_lock.acquire() 135 | try: 136 | g_stock_price_data.update(ret) 137 | except: 138 | print('except: ', sys.exc_info()) 139 | g_lock.release() 140 | except: 141 | print('except: ', sys.exc_info()) 142 | 143 | time.sleep(1) 144 | 145 | 146 | ret = threading._start_new_thread(_thread_proc, ()) 147 | 148 | #for test 149 | if __name__ == "__main__": 150 | subscribe_stock('600016') 151 | #subscribe_stock('600036') 152 | 153 | while True: 154 | print(get_stock_live_data('600016')) 155 | time.sleep(1) -------------------------------------------------------------------------------- /stock_live_trade/signature.cpp: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include "md5.h" 11 | #include "GBase64.h" 12 | 13 | using namespace std; 14 | 15 | 16 | #include 17 | 18 | #include 19 | #include 20 | #include 21 | #include 22 | #include 23 | 24 | RSA* createPublicRSA(std::string key) { 25 | RSA *rsa = NULL; 26 | BIO *keybio; 27 | const char* c_string = key.c_str(); 28 | keybio = BIO_new_mem_buf((void*)c_string, -1); 29 | if (keybio == NULL) { 30 | return 0; 31 | } 32 | rsa = PEM_read_bio_RSA_PUBKEY(keybio, &rsa, NULL, NULL); 33 | return rsa; 34 | } 35 | 36 | RSA* createPrivateRSA(std::string key) { 37 | RSA *rsa = NULL; 38 | const char* c_string = key.c_str(); 39 | BIO * keybio = BIO_new_mem_buf((void*)c_string, -1); 40 | if (keybio == NULL) { 41 | return 0; 42 | } 43 | rsa = PEM_read_bio_RSAPrivateKey(keybio, &rsa, NULL, NULL); 44 | return rsa; 45 | } 46 | 47 | bool RSAVerifySignature(RSA* rsa, const unsigned char* MsgHash, size_t MsgHashLen, const char* Msg, size_t MsgLen) { 48 | //*Authentic = false; 49 | EVP_PKEY* pubKey = EVP_PKEY_new(); 50 | EVP_PKEY_assign_RSA(pubKey, rsa); 51 | EVP_MD_CTX* m_RSAVerifyCtx = EVP_MD_CTX_create(); 52 | 53 | bool ret; 54 | 55 | if (EVP_DigestVerifyInit(m_RSAVerifyCtx, NULL, EVP_sha256(), NULL, pubKey) <= 0) { 56 | ret = false; 57 | } 58 | if (EVP_DigestVerifyUpdate(m_RSAVerifyCtx, Msg, MsgLen) <= 0) { 59 | ret = false; 60 | } 61 | int AuthStatus = EVP_DigestVerifyFinal(m_RSAVerifyCtx, MsgHash, MsgHashLen); 62 | if (AuthStatus == 1) { 63 | //*Authentic = true; 64 | 65 | ret = true; 66 | } 67 | else if (AuthStatus == 0) { 68 | //*Authentic = false; 69 | ret = true; 70 | } 71 | else { 72 | //*Authentic = false; 73 | ret = false; 74 | } 75 | 76 | EVP_PKEY_free(pubKey); 77 | EVP_MD_CTX_destroy(m_RSAVerifyCtx); 78 | 79 | return ret; 80 | } 81 | 82 | std::string RSASign(RSA* rsa, 83 | const unsigned char* Msg, 84 | size_t MsgLen 85 | ) 86 | { 87 | size_t MsgLenEnc = 0; 88 | std::string ret; 89 | EVP_MD_CTX* m_RSASignCtx = EVP_MD_CTX_create(); 90 | EVP_PKEY* priKey = EVP_PKEY_new(); 91 | EVP_PKEY_assign_RSA(priKey, rsa); 92 | if (EVP_DigestSignInit(m_RSASignCtx, NULL, EVP_sha256(), NULL, priKey) <= 0) { 93 | EVP_PKEY_free(priKey); 94 | return ret; 95 | } 96 | if (EVP_DigestSignUpdate(m_RSASignCtx, Msg, MsgLen) <= 0) { 97 | EVP_PKEY_free(priKey); 98 | EVP_MD_CTX_destroy(m_RSASignCtx); 99 | return ret; 100 | } 101 | if (EVP_DigestSignFinal(m_RSASignCtx, NULL, &MsgLenEnc) <= 0) { 102 | EVP_PKEY_free(priKey); 103 | EVP_MD_CTX_destroy(m_RSASignCtx); 104 | return ret; 105 | } 106 | 107 | std::auto_ptr ptr(new unsigned char[MsgLenEnc]); 108 | 109 | if (EVP_DigestSignFinal(m_RSASignCtx, ptr.get(), &MsgLenEnc) <= 0) { 110 | EVP_PKEY_free(priKey); 111 | EVP_MD_CTX_destroy(m_RSASignCtx); 112 | return ret; 113 | } 114 | 115 | EVP_MD_CTX_destroy(m_RSASignCtx); 116 | EVP_PKEY_free(priKey); 117 | 118 | return Base64::Encode((const char*)ptr.get(), MsgLenEnc); 119 | } 120 | 121 | std::string PUBLIC_KEY = "-----BEGIN PUBLIC KEY-----\n" 122 | "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCRiwSQLmRckBoBXrxj1+TE5lF1\n" 123 | "AHvyKeDKU6nt6UnFKlxacl6KqZPUTfD9FlV1ddpTOGUZL3z+yq6Z2GOCAPiid9dg\n" 124 | "0VED2bvBxtCMEo/RQJFBUHWP3UaKZyu0btcDAUAGWhJydqMaoHEIRrHJbcN/FACa\n" 125 | "XRu7YKQqbdkEr5ze0QIDAQAB\n" 126 | "-----END PUBLIC KEY-----"; 127 | 128 | 129 | 130 | std::string pkc_sign(const unsigned char* data, int32_t len, const unsigned char* private_key, int32_t key_len) 131 | { 132 | RSA* rsa = createPrivateRSA(std::string((const char*)private_key, key_len)); 133 | 134 | std::string ret = RSASign(rsa, data, len); 135 | 136 | //RSA_free(rsa); 137 | 138 | return ret; 139 | } 140 | 141 | bool pkc_verify(const unsigned char* data, int32_t len, const unsigned char* b64_signature, int32_t b64_sig_len, const unsigned char* public_key, int32_t key_len) 142 | { 143 | RSA* rsa = createPublicRSA(std::string((const char*)public_key, key_len)); 144 | 145 | std::string signature = Base64::Decode(std::string((const char*)b64_signature, b64_sig_len)); 146 | 147 | bool ret = RSAVerifySignature(rsa, (const unsigned char*)signature.c_str(), signature.size(), (const char*)data, len); 148 | 149 | //RSA_free(rsa); 150 | 151 | return ret; 152 | } 153 | 154 | /* 155 | std::string pkc_sign_with_key(const unsigned char* data, int32_t len) 156 | { 157 | return pkc_sign(data, len, (const unsigned char*)PRIVATE_KEY.c_str(), PRIVATE_KEY.size()); 158 | } 159 | */ 160 | 161 | bool pkc_verify_with_key(const unsigned char* data, int32_t len, const unsigned char* signature, int32_t sig_len) 162 | { 163 | return pkc_verify(data, len, signature, sig_len, (const unsigned char*)PUBLIC_KEY.c_str(), PUBLIC_KEY.size()); 164 | } 165 | -------------------------------------------------------------------------------- /bbbstock_sim_trade.py: -------------------------------------------------------------------------------- 1 | import sys 2 | 3 | from thrift.transport import TSocket 4 | from thrift.transport import TTransport 5 | from thrift.protocol import TBinaryProtocol 6 | 7 | from alpha_trade_python import AlphaTrade 8 | 9 | import time 10 | 11 | class Map(dict): 12 | """ 13 | Example: 14 | m = Map({'first_name': 'Eduardo'}, last_name='Pool', age=24, sports=['Soccer']) 15 | """ 16 | def __init__(self, *args, **kwargs): 17 | super(Map, self).__init__(*args, **kwargs) 18 | for arg in args: 19 | if isinstance(arg, dict): 20 | for k, v in arg.items(): 21 | self[k] = v 22 | 23 | if kwargs: 24 | for k, v in kwargs.items(): 25 | self[k] = v 26 | 27 | def __getattr__(self, attr): 28 | return self.get(attr) 29 | 30 | def __setattr__(self, key, value): 31 | self.__setitem__(key, value) 32 | 33 | def __setitem__(self, key, value): 34 | super(Map, self).__setitem__(key, value) 35 | self.__dict__.update({key: value}) 36 | 37 | def __delattr__(self, item): 38 | self.__delitem__(item) 39 | 40 | def __delitem__(self, key): 41 | super(Map, self).__delitem__(key) 42 | del self.__dict__[key] 43 | 44 | 45 | _g_session_str = "" 46 | _g_server = "sim.bbbstock.com" 47 | _g_port = 62228 48 | 49 | ERR_BROKER_NOT_EXIST = 1 50 | ERR_NOT_SUPPORT = 2 51 | ERR_GET_ACCOUNT_INFO_FAILED = 3 52 | ERR_EXCEPTION = 4 53 | ERR_INTERNAL = 5 54 | ERR_NETWORK = 6 55 | ERR_SIGNATURE= 7 56 | ERR_NOT_LOGIN = 10 57 | ERR_ORDER_NOT_EXIST = 11 58 | ERR_EXCEED_MAX_DAY_ORDER_NUM = 12 59 | 60 | def _create_client(): 61 | 62 | transport = TTransport.TFramedTransport(TSocket.TSocket(_g_server, _g_port)) 63 | protocol = TBinaryProtocol.TBinaryProtocol(transport) 64 | client = AlphaTrade.Client(protocol) 65 | 66 | transport.open() 67 | 68 | return client 69 | 70 | 71 | def StartSDK(str2): 72 | global _g_session_str 73 | _g_session_str = str2 74 | 75 | try: 76 | _create_client().KeepAlive(str2) 77 | except: 78 | print("except: ", sys.exc_info()) 79 | 80 | def SetServer(host, port): 81 | pass 82 | 83 | def Running(): 84 | return _create_client().Running() 85 | 86 | def GetPID(): 87 | return _create_client().GetPID() 88 | 89 | 90 | def LiveTradeLogin(account, password1, password2, brokerstr, wait_result = True): 91 | 92 | ret = Map({}) 93 | 94 | try: 95 | login_ret = _create_client().LiveTradeLogin(_g_session_str, account, password1, password2, brokerstr) 96 | liveTradeID = login_ret.result 97 | 98 | while wait_result: 99 | state = GetAccountState(liveTradeID) 100 | if state.state == "idle" or state.state == "logining": 101 | time.sleep(0.05) 102 | else: 103 | break 104 | 105 | return login_ret 106 | except: 107 | print("except: ", sys.exc_info()) 108 | 109 | ret.ret_code = ERR_EXCEPTION 110 | 111 | return ret 112 | 113 | 114 | def LiveTradeLogout(liveTradeID): 115 | 116 | ret = Map({}) 117 | 118 | try: 119 | return _create_client().LiveTradeLogout(_g_session_str, liveTradeID) 120 | except: 121 | print("except: ", sys.exc_info()) 122 | 123 | ret.ret_code = ERR_EXCEPTION 124 | 125 | return ret 126 | 127 | def GetAccountState(liveTradeID): 128 | 129 | ret = Map({}) 130 | 131 | 132 | try: 133 | return _create_client().GetAccountState(_g_session_str, liveTradeID) 134 | except: 135 | print("except: ", sys.exc_info()) 136 | 137 | ret.ret_code = ERR_EXCEPTION 138 | 139 | return ret 140 | 141 | def GetAccountBalance(liveTradeID): 142 | 143 | """ 144 | struct AccountBalance { 145 | 1: double total_value, 146 | 2: double money_left, 147 | } 148 | """ 149 | ret = Map({}) 150 | 151 | 152 | try: 153 | return _create_client().GetAccountBalance(_g_session_str, liveTradeID) 154 | 155 | except: 156 | print("except: ", sys.exc_info()) 157 | 158 | ret.ret_code = ERR_EXCEPTION 159 | 160 | return ret 161 | 162 | def GetHoldingStock(liveTradeID): 163 | 164 | ret = Map({}) 165 | 166 | try: 167 | return _create_client().GetHoldingStock(_g_session_str, liveTradeID) 168 | except: 169 | print("except: ", sys.exc_info()) 170 | 171 | ret.ret_code = ERR_EXCEPTION 172 | 173 | return ret 174 | 175 | def GetOrderState(liveTradeID, orderID): 176 | 177 | ret = Map({}) 178 | 179 | try: 180 | 181 | return _create_client().GetOrderState(_g_session_str, liveTradeID, orderID) 182 | except: 183 | print("except: ", sys.exc_info()) 184 | 185 | ret.ret_code = ERR_EXCEPTION 186 | 187 | return ret 188 | 189 | def GetAllOrder(liveTradeID): 190 | ret = Map({}) 191 | 192 | try: 193 | 194 | return _create_client().GetAllOrder(_g_session_str, liveTradeID) 195 | except: 196 | print("except: ", sys.exc_info()) 197 | 198 | ret.ret_code = ERR_EXCEPTION 199 | 200 | return ret 201 | 202 | def CancelOrder(liveTradeID, orderID): 203 | 204 | ret = Map({}) 205 | 206 | try: 207 | return _create_client().CancelOrder(_g_session_str, liveTradeID, orderID) 208 | except: 209 | print("except: ", sys.exc_info()) 210 | 211 | ret.ret_code = ERR_EXCEPTION 212 | 213 | return ret 214 | 215 | def LiveTradeBuy(liveTradeID, sid, price, quant, orderType): 216 | return LiveTradeBuyOpen(liveTradeID, sid, price, quant, orderType) 217 | 218 | def LiveTradeBuyOpen(liveTradeID, sid, price, quant, orderType): 219 | 220 | ret = Map({}) 221 | 222 | try: 223 | return _create_client().LiveTradeBuyOpen(_g_session_str, liveTradeID, sid, price, quant, orderType) 224 | except: 225 | print("except: ", sys.exc_info()) 226 | 227 | ret.ret_code = ERR_EXCEPTION 228 | 229 | return ret 230 | 231 | def LiveTradeBuyClose(liveTradeID, sid, price, quant, orderType, closeToday=True): 232 | 233 | ret = Map({}) 234 | 235 | try: 236 | return _create_client().LiveTradeBuyClose(_g_session_str, liveTradeID, sid, price, quant, orderType, closeToday) 237 | except: 238 | print("except: ", sys.exc_info()) 239 | 240 | ret.ret_code = ERR_EXCEPTION 241 | 242 | return ret 243 | 244 | def LiveTradeSell(liveTradeID, sid, price, quant, orderType, closeToday=True): 245 | return LiveTradeSellClose(liveTradeID, sid, price, quant, orderType, closeToday) 246 | 247 | def LiveTradeSellClose(liveTradeID, sid, price, quant, orderType, closeToday=True): 248 | 249 | ret = Map({}) 250 | 251 | try: 252 | return _create_client().LiveTradeSellClose(_g_session_str, liveTradeID, sid, price, quant, orderType, closeToday) 253 | except: 254 | print("except: ", sys.exc_info()) 255 | 256 | ret.ret_code = ERR_EXCEPTION 257 | 258 | return ret 259 | 260 | def LiveTradeSellOpen(liveTradeID, sid, price, quant, orderType): 261 | 262 | ret = Map({}) 263 | 264 | try: 265 | return _create_client().LiveTradeSellOpen(_g_session_str, liveTradeID, sid, price, quant, orderType) 266 | except: 267 | print("except: ", sys.exc_info()) 268 | 269 | ret.ret_code = ERR_EXCEPTION 270 | 271 | return ret 272 | -------------------------------------------------------------------------------- /stock_live_trade/license_manager.cpp: -------------------------------------------------------------------------------- 1 | // This autogenerated skeleton file illustrates how to build a server. 2 | // You should copy it to another filename to avoid overwriting it. 3 | 4 | #include 5 | #include 6 | #include 7 | 8 | #include 9 | 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | 16 | #include "AlphaConfig.h" 17 | #include "GWin32CriticalSection.h" 18 | #include "GScopedLock.h" 19 | #include "GWin32Thread.h" 20 | #include "utility.h" 21 | #include "signature.h" 22 | 23 | #include "license_manager.h" 24 | 25 | using namespace ::apache::thrift; 26 | using namespace ::apache::thrift::protocol; 27 | using namespace ::apache::thrift::transport; 28 | using namespace ::apache::thrift::server; 29 | 30 | using boost::shared_ptr; 31 | 32 | boost::shared_ptr g_license_data_ptr(new LicenseManager()); 33 | 34 | boost::shared_ptr LicenseManager::get_instance() 35 | { 36 | return g_license_data_ptr; 37 | } 38 | 39 | LicenseManager::LicenseManager() 40 | { 41 | m_max_allowed_fund = 200000.009; 42 | m_expire_time = time(NULL)+15*24*60*60; 43 | //m_money_left = 0.0; 44 | 45 | m_started = false; 46 | 47 | } 48 | 49 | bool LicenseManager::license_ok(int32_t& code, std::string& msg) 50 | { 51 | 52 | double total_money_left = get_money_left(); 53 | 54 | ScopedLock lck(m_critical_sec); 55 | 56 | if (total_money_left > m_max_allowed_fund) { 57 | code = 9; 58 | return false; 59 | } 60 | 61 | code = 0; 62 | return true; 63 | } 64 | 65 | double LicenseManager::get_money_left() 66 | { 67 | double total_money_left = 0; 68 | 69 | ScopedLock lck(m_critical_sec); 70 | 71 | std::map::iterator iter; 72 | for (iter = m_account_money_left_map.begin(); iter != m_account_money_left_map.end(); ++iter) { 73 | total_money_left += iter->second; 74 | } 75 | 76 | return total_money_left; 77 | } 78 | 79 | void LicenseManager::set_session_id(const std::string& id) 80 | { 81 | ScopedLock lck(m_critical_sec); 82 | 83 | m_session_id = id; 84 | } 85 | 86 | std::string LicenseManager::get_session_id() 87 | { 88 | ScopedLock lck(m_critical_sec); 89 | 90 | return m_session_id; 91 | } 92 | 93 | std::vector> LicenseManager::get_broker_server(const std::string &broker) 94 | { 95 | std::vector> server_vec; 96 | 97 | ScopedLock lck(m_critical_sec); 98 | 99 | std::map>::iterator iter = m_broker_server.find(broker); 100 | if (iter != m_broker_server.end()) { 101 | for (int i = 0; i < iter->second.size(); ++i) { 102 | server_vec.push_back(boost::make_tuple(iter->second[i].host, iter->second[i].port)); 103 | } 104 | } 105 | 106 | return server_vec; 107 | } 108 | 109 | std::string get_str1() 110 | { 111 | std::vector vec_mac; 112 | getWin32MacAddresses(vec_mac); 113 | 114 | std::string str1; 115 | for (int i = 0; i < vec_mac.size(); ++i) { 116 | str1 += vec_mac[i]; 117 | } 118 | 119 | return str1; 120 | } 121 | 122 | u_long WINAPI LicenseManager::FetchConfigThreadProc(LPVOID lParam) 123 | { 124 | std::string last_session_id; 125 | time_t last_get_config = 0; 126 | time_t last_report = time(NULL) - 24*60*60; 127 | 128 | LicenseManager *pthis = (LicenseManager*)lParam; 129 | 130 | do { 131 | bool fetch_data = false; 132 | do { 133 | ScopedLock lck(pthis->m_critical_sec); 134 | 135 | if (last_session_id != pthis->m_session_id) { 136 | last_session_id = pthis->m_session_id; 137 | fetch_data = true; 138 | } 139 | 140 | if (time(NULL) - last_get_config > 10 * 60 * 60) { 141 | fetch_data = true; 142 | } 143 | 144 | } while (false); 145 | 146 | std::string str1 = get_str1(); 147 | 148 | if (fetch_data) { 149 | 150 | try { 151 | boost::shared_ptr socket(new TSocket("config.alpha-qt.com", 61118)); 152 | //boost::shared_ptr socket(new TSocket("192.168.3.2", 61118)); 153 | boost::shared_ptr transport(new TFramedTransport(socket)); 154 | boost::shared_ptr protocol(new TBinaryProtocol(transport)); 155 | AlphaConfigClient client(protocol); 156 | 157 | 158 | transport->open(); 159 | 160 | ConfigData cd; 161 | client.GetConfig(cd, str1, last_session_id); 162 | 163 | std::string raw_data = str1 + last_session_id + cd.license_data["mxf"] + cd.license_data["xpr"]; 164 | std::string signature = cd.license_data["sgn"]; 165 | //verify signature 166 | 167 | //bool signature_ok = true;//TODO 168 | bool signature_ok = pkc_verify_with_key((const unsigned char*)raw_data.c_str(), raw_data.size(), (const unsigned char*)signature.c_str(), signature.size()); 169 | 170 | 171 | ScopedLock lck(pthis->m_critical_sec); 172 | pthis->m_license_data = cd.license_data; 173 | pthis->m_broker_server = cd.broker_server; 174 | 175 | if (signature_ok) { 176 | pthis->m_expire_time = atoi(cd.license_data["xpr"].c_str()); 177 | if (time(NULL) < pthis->m_expire_time) { 178 | pthis->m_max_allowed_fund = atof(cd.license_data["mxf"].c_str()); 179 | } 180 | } 181 | 182 | last_get_config = time(NULL); 183 | 184 | } 185 | catch (...) { 186 | //TODO 187 | } 188 | 189 | } 190 | 191 | double money_left = pthis->get_money_left(); 192 | if (time(NULL) - last_report > 10 * 60 * 60 && money_left > 0) { 193 | last_report = time(NULL); 194 | 195 | try { 196 | boost::shared_ptr socket(new TSocket("config.alpha-qt.com", 61118)); 197 | //boost::shared_ptr socket(new TSocket("192.168.3.2", 61118)); 198 | boost::shared_ptr transport(new TFramedTransport(socket)); 199 | boost::shared_ptr protocol(new TBinaryProtocol(transport)); 200 | AlphaConfigClient client(protocol); 201 | 202 | transport->open(); 203 | 204 | std::map data_map; 205 | 206 | do { 207 | ScopedLock lck(pthis->m_critical_sec); 208 | 209 | std::ostringstream oss_an; 210 | oss_an << pthis->m_account_money_left_map.size(); 211 | std::ostringstream oss_tm; 212 | oss_tm << money_left; 213 | //Please do not remove the following code, we want to know how many users are using our software. 214 | //Dont worry about your privacy. We only know your mac address and fund scale. 215 | data_map["str1"] = str1;//mac address 216 | data_map["str2"] = pthis->m_session_id;// this is the StartSDK param passed in 217 | data_map["an"] = oss_an.str(); 218 | data_map["tm"] = oss_tm.str();//fund scale 219 | } while (0); 220 | 221 | client.Report(data_map); 222 | } 223 | catch (...) { 224 | //TODO 225 | } 226 | } 227 | 228 | Win32Thread::Sleep(3000); 229 | 230 | } while (true); 231 | 232 | return 0; 233 | } 234 | 235 | void LicenseManager::start() 236 | { 237 | if (m_started) 238 | return; 239 | 240 | m_started = true; 241 | 242 | m_thread.StartThread(FetchConfigThreadProc, this); 243 | } 244 | 245 | void LicenseManager::set_account_money(const std::string& account, double money) 246 | { 247 | ScopedLock lck(m_critical_sec); 248 | 249 | m_account_money_left_map[account] = money; 250 | } -------------------------------------------------------------------------------- /alpha_live_trade.py: -------------------------------------------------------------------------------- 1 | import sys 2 | 3 | from thrift.transport import TSocket 4 | from thrift.transport import TTransport 5 | from thrift.protocol import TBinaryProtocol 6 | 7 | from alpha_trade_python import AlphaTrade 8 | 9 | import time 10 | 11 | class Map(dict): 12 | """ 13 | Example: 14 | m = Map({'first_name': 'Eduardo'}, last_name='Pool', age=24, sports=['Soccer']) 15 | """ 16 | def __init__(self, *args, **kwargs): 17 | super(Map, self).__init__(*args, **kwargs) 18 | for arg in args: 19 | if isinstance(arg, dict): 20 | for k, v in arg.items(): 21 | self[k] = v 22 | 23 | if kwargs: 24 | for k, v in kwargs.items(): 25 | self[k] = v 26 | 27 | def __getattr__(self, attr): 28 | return self.get(attr) 29 | 30 | def __setattr__(self, key, value): 31 | self.__setitem__(key, value) 32 | 33 | def __setitem__(self, key, value): 34 | super(Map, self).__setitem__(key, value) 35 | self.__dict__.update({key: value}) 36 | 37 | def __delattr__(self, item): 38 | self.__delitem__(item) 39 | 40 | def __delitem__(self, key): 41 | super(Map, self).__delitem__(key) 42 | del self.__dict__[key] 43 | 44 | 45 | _g_session_str = "" 46 | _g_server = "127.0.0.1" 47 | _g_port = 58899 48 | 49 | ERR_BROKER_NOT_EXIST = 1 50 | ERR_NOT_SUPPORT = 2 51 | ERR_GET_ACCOUNT_INFO_FAILED = 3 52 | ERR_EXCEPTION = 4 53 | ERR_INTERNAL = 5 54 | ERR_NETWORK = 6 55 | ERR_SIGNATURE= 7 56 | ERR_NOT_LOGIN = 10 57 | ERR_ORDER_NOT_EXIST = 11 58 | ERR_EXCEED_MAX_DAY_ORDER_NUM = 12 59 | ERR_LOGIN_FAILED = 13 60 | 61 | def _create_client(): 62 | 63 | transport = TTransport.TFramedTransport(TSocket.TSocket(_g_server, _g_port)) 64 | protocol = TBinaryProtocol.TBinaryProtocol(transport) 65 | client = AlphaTrade.Client(protocol) 66 | 67 | transport.open() 68 | 69 | return client 70 | 71 | 72 | def StartSDK(str2): 73 | global _g_session_str 74 | _g_session_str = str2 75 | 76 | try: 77 | _create_client().KeepAlive(str2) 78 | except: 79 | print("except: ", sys.exc_info()) 80 | 81 | def SetServer(host, port): 82 | global _g_server 83 | global _g_port 84 | 85 | _g_server = host 86 | _g_port = port 87 | 88 | def Running(): 89 | return _create_client().Running() 90 | 91 | def GetPID(): 92 | return _create_client().GetPID() 93 | 94 | 95 | def LiveTradeLogin(account, password1, password2, brokerstr, wait_result = True): 96 | 97 | ret = Map({}) 98 | 99 | try: 100 | login_ret = _create_client().LiveTradeLogin(_g_session_str, account, password1, password2, brokerstr) 101 | liveTradeID = login_ret.result 102 | 103 | while wait_result: 104 | state = GetAccountState(liveTradeID) 105 | if state.state == "idle" or state.state == "logining": 106 | time.sleep(0.05) 107 | else: 108 | break 109 | 110 | return login_ret 111 | except: 112 | print("except: ", sys.exc_info()) 113 | 114 | ret.ret_code = ERR_EXCEPTION 115 | 116 | return ret 117 | 118 | 119 | def LiveTradeLogout(liveTradeID): 120 | 121 | ret = Map({}) 122 | 123 | try: 124 | return _create_client().LiveTradeLogout(_g_session_str, liveTradeID) 125 | except: 126 | print("except: ", sys.exc_info()) 127 | 128 | ret.ret_code = ERR_EXCEPTION 129 | 130 | return ret 131 | 132 | def GetAccountState(liveTradeID): 133 | 134 | ret = Map({}) 135 | 136 | 137 | try: 138 | return _create_client().GetAccountState(_g_session_str, liveTradeID) 139 | except: 140 | print("except: ", sys.exc_info()) 141 | 142 | ret.ret_code = ERR_EXCEPTION 143 | 144 | return ret 145 | 146 | def GetAccountBalance(liveTradeID): 147 | 148 | """ 149 | struct AccountBalance { 150 | 1: double total_value, 151 | 2: double money_left, 152 | } 153 | """ 154 | ret = Map({}) 155 | 156 | 157 | try: 158 | return _create_client().GetAccountBalance(_g_session_str, liveTradeID) 159 | 160 | except: 161 | print("except: ", sys.exc_info()) 162 | 163 | ret.ret_code = ERR_EXCEPTION 164 | 165 | return ret 166 | 167 | def GetAccountInfo(liveTradeID, key): 168 | ret = Map({}) 169 | 170 | try: 171 | return _create_client().GetAccountInfo(_g_session_str, liveTradeID, key) 172 | except: 173 | print("except: ", sys.exc_info()) 174 | 175 | ret.ret_code = ERR_EXCEPTION 176 | 177 | return ret 178 | 179 | def GetHoldingStock(liveTradeID): 180 | 181 | ret = Map({}) 182 | 183 | try: 184 | return _create_client().GetHoldingStock(_g_session_str, liveTradeID) 185 | except: 186 | print("except: ", sys.exc_info()) 187 | 188 | ret.ret_code = ERR_EXCEPTION 189 | 190 | return ret 191 | 192 | def GetOrderState(liveTradeID, orderID): 193 | 194 | ret = Map({}) 195 | 196 | try: 197 | 198 | return _create_client().GetOrderState(_g_session_str, liveTradeID, orderID) 199 | except: 200 | print("except: ", sys.exc_info()) 201 | 202 | ret.ret_code = ERR_EXCEPTION 203 | 204 | return ret 205 | 206 | def GetAllOrder(liveTradeID): 207 | ret = Map({}) 208 | 209 | try: 210 | 211 | return _create_client().GetAllOrder(_g_session_str, liveTradeID) 212 | except: 213 | print("except: ", sys.exc_info()) 214 | 215 | ret.ret_code = ERR_EXCEPTION 216 | 217 | return ret 218 | 219 | def CancelOrder(liveTradeID, orderID): 220 | 221 | ret = Map({}) 222 | 223 | try: 224 | return _create_client().CancelOrder(_g_session_str, liveTradeID, orderID) 225 | except: 226 | print("except: ", sys.exc_info()) 227 | 228 | ret.ret_code = ERR_EXCEPTION 229 | 230 | return ret 231 | 232 | def LiveTradeBuy(liveTradeID, sid, price, quant, orderType): 233 | return LiveTradeBuyOpen(liveTradeID, sid, price, quant, orderType) 234 | 235 | def LiveTradeBuyOpen(liveTradeID, sid, price, quant, orderType): 236 | 237 | ret = Map({}) 238 | 239 | try: 240 | return _create_client().LiveTradeBuyOpen(_g_session_str, liveTradeID, sid, price, quant, orderType) 241 | except: 242 | print("except: ", sys.exc_info()) 243 | 244 | ret.ret_code = ERR_EXCEPTION 245 | 246 | return ret 247 | 248 | def LiveTradeBuyClose(liveTradeID, sid, price, quant, orderType, closeToday=True): 249 | 250 | ret = Map({}) 251 | 252 | try: 253 | return _create_client().LiveTradeBuyClose(_g_session_str, liveTradeID, sid, price, quant, orderType, closeToday) 254 | except: 255 | print("except: ", sys.exc_info()) 256 | 257 | ret.ret_code = ERR_EXCEPTION 258 | 259 | return ret 260 | 261 | def LiveTradeSell(liveTradeID, sid, price, quant, orderType, closeToday=True): 262 | return LiveTradeSellClose(liveTradeID, sid, price, quant, orderType, closeToday) 263 | 264 | def LiveTradeSellClose(liveTradeID, sid, price, quant, orderType, closeToday=True): 265 | 266 | ret = Map({}) 267 | 268 | try: 269 | return _create_client().LiveTradeSellClose(_g_session_str, liveTradeID, sid, price, quant, orderType, closeToday) 270 | except: 271 | print("except: ", sys.exc_info()) 272 | 273 | ret.ret_code = ERR_EXCEPTION 274 | 275 | return ret 276 | 277 | def LiveTradeSellOpen(liveTradeID, sid, price, quant, orderType): 278 | 279 | ret = Map({}) 280 | 281 | try: 282 | return _create_client().LiveTradeSellOpen(_g_session_str, liveTradeID, sid, price, quant, orderType) 283 | except: 284 | print("except: ", sys.exc_info()) 285 | 286 | ret.ret_code = ERR_EXCEPTION 287 | 288 | return ret 289 | -------------------------------------------------------------------------------- /stock_live_trade/thread_broker_adapter.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include "thread_broker_adapter.h" 4 | #include "md5.h" 5 | #include "lib.h" 6 | #include "license_manager.h" 7 | #include 8 | 9 | using namespace std; 10 | 11 | thread_broker_adapter::thread_broker_adapter(stock_broker *p, bool create_recv_thread) 12 | : 13 | m_broker_ptr(p), 14 | m_started(false), 15 | m_create_recv_thread(create_recv_thread) 16 | { 17 | setState("idle", ""); 18 | } 19 | 20 | thread_broker_adapter::~thread_broker_adapter() 21 | { 22 | delete m_broker_ptr; 23 | } 24 | 25 | void thread_broker_adapter::start() 26 | { 27 | if (m_started) { 28 | return; 29 | } 30 | 31 | m_started = true; 32 | command cmd; 33 | cmd.cmd = START; 34 | 35 | ScopedLock lck(m_critical_sec); 36 | m_command_deq.push_back(cmd); 37 | m_event.Signal(); 38 | 39 | m_send_thread.StartThread(SendThreadProc, this); 40 | cout << "send thread " << m_send_thread.GetCurrentThreadId() << " created" << endl; 41 | } 42 | 43 | void thread_broker_adapter::stop() 44 | { 45 | command cmd; 46 | cmd.cmd = STOP; 47 | 48 | ScopedLock lck(m_critical_sec); 49 | m_command_deq.push_back(cmd); 50 | m_event.Signal(); 51 | } 52 | 53 | //{"sid": stockid, "action" : "open", "direction" : 1, "price" : price, "quant" : quant, "deal_quant" : 0, "deal_price" : 0, "state" : "pending", "desc" : "", "id" : user_order_id} 54 | 55 | std::string thread_broker_adapter::buy(const std::string& sid, int amount, double price, const std::string& order_type) 56 | { 57 | if (getState() != "login") { 58 | return ""; 59 | } 60 | 61 | std::string ret = m_broker_ptr->create_order("buy", "open", sid, price, amount); 62 | 63 | command cmd; 64 | cmd.cmd = BUY; 65 | cmd.sid = sid; 66 | cmd.quant = amount; 67 | cmd.price = price; 68 | cmd.order_type = order_type; 69 | cmd.order_id = ret; 70 | 71 | ScopedLock lck(m_critical_sec); 72 | 73 | m_command_deq.push_back(cmd); 74 | m_event.Signal(); 75 | 76 | return ret; 77 | } 78 | 79 | std::string thread_broker_adapter::sell(const std::string& sid, int amount, double price, const std::string& order_type) 80 | { 81 | if (getState() != "login") { 82 | return ""; 83 | } 84 | 85 | std::string ret = m_broker_ptr->create_order("sell", "close", sid, price, amount); 86 | 87 | command cmd; 88 | cmd.cmd = SELL; 89 | cmd.sid = sid; 90 | cmd.quant = amount; 91 | cmd.price = price; 92 | cmd.order_type = order_type; 93 | cmd.order_id = ret; 94 | 95 | ScopedLock lck(m_critical_sec); 96 | 97 | m_command_deq.push_back(cmd); 98 | 99 | m_event.Signal(); 100 | return ret; 101 | } 102 | 103 | void thread_broker_adapter::closeOrder(const std::string& order_id) 104 | { 105 | command cmd; 106 | cmd.cmd = CANCEL; 107 | cmd.order_id = order_id; 108 | 109 | ScopedLock lck(m_critical_sec); 110 | m_command_deq.push_back(cmd); 111 | m_event.Signal(); 112 | } 113 | 114 | std::vector thread_broker_adapter::getHoldingStock() 115 | { 116 | return m_broker_ptr->get_holding_stock(); 117 | } 118 | 119 | order_state thread_broker_adapter::getOrderState(const std::string& user_order_id) 120 | { 121 | return m_broker_ptr->get_order_state(user_order_id); 122 | } 123 | 124 | std::vector thread_broker_adapter::getAllOrder() 125 | { 126 | return m_broker_ptr->get_all_order(); 127 | } 128 | 129 | std::string thread_broker_adapter::getAccountState() 130 | { 131 | ScopedLock lck(m_critical_sec); 132 | 133 | //cout << "current state " << m_state << endl; 134 | 135 | return m_state; 136 | } 137 | 138 | std::vector thread_broker_adapter::getBalance() 139 | { 140 | return m_broker_ptr->get_money_left(); 141 | } 142 | 143 | u_long WINAPI thread_broker_adapter::SendThreadProc(LPVOID lParam) 144 | { 145 | thread_broker_adapter *pThis = (thread_broker_adapter*)lParam; 146 | 147 | DWORD last_update_account = ::GetTickCount(); 148 | try { 149 | while (true) { 150 | 151 | bool command_ok = false; 152 | int command_left = 0; 153 | command cmd; 154 | 155 | pThis->m_critical_sec.Lock(); 156 | 157 | if (pThis->m_command_deq.size() > 0) { 158 | command_ok = true; 159 | cmd = pThis->m_command_deq[0]; 160 | pThis->m_command_deq.pop_front(); 161 | } 162 | 163 | pThis->m_critical_sec.Unlock(); 164 | 165 | if (command_ok) { 166 | if (cmd.cmd == START) { 167 | int ret = pThis->m_broker_ptr->login(); 168 | 169 | if (ret == 0) { 170 | 171 | std::string account = pThis->m_broker_ptr->get_account(); 172 | double money_left = pThis->m_broker_ptr->get_money_left()[1]; 173 | 174 | LicenseManager::get_instance()->set_account_money(account, money_left); 175 | 176 | pThis->setState("login", "login success"); 177 | 178 | if (pThis->m_create_recv_thread) { 179 | pThis->m_recv_thread.StartThread(ReceiveThreadProc, pThis); 180 | cout << "recv thread " << pThis->m_recv_thread.GetCurrentThreadId << " created" << endl; 181 | } 182 | } 183 | else { 184 | pThis->setState("failed", "login failed"); 185 | } 186 | } 187 | else if (cmd.cmd == BUY) { 188 | pThis->m_broker_ptr->buy(cmd.sid, cmd.price, cmd.quant, cmd.order_type, cmd.order_id); 189 | } 190 | else if (cmd.cmd == SELL) { 191 | pThis->m_broker_ptr->sell(cmd.sid, cmd.price, cmd.quant, cmd.order_type, cmd.order_id); 192 | 193 | } 194 | else if (cmd.cmd == RESPONSE) { 195 | int ret = pThis->m_broker_ptr->handle_packet(cmd.data); 196 | } 197 | else if (cmd.cmd == CANCEL) { 198 | std::string oi = cmd.order_id; 199 | pThis->m_broker_ptr->cancel_order(oi); 200 | } 201 | else if (cmd.cmd == STOP) { 202 | 203 | break; 204 | } 205 | } 206 | else { 207 | DWORD current_tickcount = ::GetTickCount(); 208 | if (current_tickcount - last_update_account > 400) { 209 | pThis->updateAccountState(); 210 | last_update_account = current_tickcount; 211 | } 212 | pThis->m_event.Wait(100); 213 | } 214 | } 215 | 216 | } 217 | catch (std::exception& e) { 218 | cout << "send thread except: " << e.what() << endl; 219 | pThis->setState("failed", e.what()); 220 | } 221 | pThis->m_broker_ptr->logout(); 222 | pThis->m_started = false; 223 | 224 | cout <<"send thread "<< Win32Thread::GetCurrentThreadId() << " exited" << endl; 225 | 226 | return 0; 227 | } 228 | 229 | u_long WINAPI thread_broker_adapter::ReceiveThreadProc(LPVOID lParam) 230 | { 231 | thread_broker_adapter *pThis = (thread_broker_adapter*)lParam; 232 | 233 | try { 234 | while (true) { 235 | 236 | command cmd; 237 | cmd.cmd = command_type::RESPONSE; 238 | 239 | int ret = pThis->m_broker_ptr->try_recv_packet(cmd.data); 240 | if (ret > 0) { 241 | ScopedLock lck(pThis->m_critical_sec); 242 | pThis->m_command_deq.push_back(cmd); 243 | pThis->m_event.Signal(); 244 | } 245 | else { 246 | cout << "recv thread recv return " << ret << endl; 247 | break; 248 | } 249 | } 250 | } 251 | catch (std::exception& e) { 252 | pThis->setState("failed", e.what()); 253 | } 254 | 255 | cout <<"recv thread "<< Win32Thread::GetCurrentThreadId() << " exited" << endl; 256 | 257 | return 0; 258 | } 259 | 260 | void thread_broker_adapter::updateAccountState() 261 | { 262 | m_broker_ptr->update_account(); 263 | } 264 | 265 | void thread_broker_adapter::setState(const std::string& state, const std::string& desc) 266 | { 267 | ScopedLock lck(m_critical_sec); 268 | 269 | m_state = state; 270 | m_state_desc = desc; 271 | } 272 | 273 | std::string thread_broker_adapter::getState() 274 | { 275 | ScopedLock lck(m_critical_sec); 276 | 277 | return m_state; 278 | } -------------------------------------------------------------------------------- /stock_live_trade/md5.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * @file md5.cpp 3 | * @The implement of md5. 4 | * @author Jiewei Wei 5 | * @mail weijieweijerry@163.com 6 | * @github https://github.com/JieweiWei 7 | * @data Oct 19 2014 8 | * 9 | */ 10 | 11 | #include "md5.h" 12 | 13 | /* Define the static member of MD5. */ 14 | const byte MD5::PADDING[64] = { 0x80 }; 15 | const char MD5::HEX_NUMBERS[16] = { 16 | '0', '1', '2', '3', 17 | '4', '5', '6', '7', 18 | '8', '9', 'a', 'b', 19 | 'c', 'd', 'e', 'f' 20 | }; 21 | 22 | /** 23 | * @Construct a MD5 object with a string. 24 | * 25 | * @param {message} the message will be transformed. 26 | * 27 | */ 28 | MD5::MD5(const string& message) { 29 | finished = false; 30 | /* Reset number of bits. */ 31 | count[0] = count[1] = 0; 32 | /* Initialization constants. */ 33 | state[0] = 0x67452301; 34 | state[1] = 0xefcdab89; 35 | state[2] = 0x98badcfe; 36 | state[3] = 0x10325476; 37 | 38 | /* Initialization the object according to message. */ 39 | init((const byte*)message.c_str(), message.length()); 40 | } 41 | 42 | /** 43 | * @Generate md5 digest. 44 | * 45 | * @return the message-digest. 46 | * 47 | */ 48 | const byte* MD5::getDigest() { 49 | if (!finished) { 50 | finished = true; 51 | 52 | byte bits[8]; 53 | bit32 oldState[4]; 54 | bit32 oldCount[2]; 55 | bit32 index, padLen; 56 | 57 | /* Save current state and count. */ 58 | memcpy(oldState, state, 16); 59 | memcpy(oldCount, count, 8); 60 | 61 | /* Save number of bits */ 62 | encode(count, bits, 8); 63 | 64 | /* Pad out to 56 mod 64. */ 65 | index = (bit32)((count[0] >> 3) & 0x3f); 66 | padLen = (index < 56) ? (56 - index) : (120 - index); 67 | init(PADDING, padLen); 68 | 69 | /* Append length (before padding) */ 70 | init(bits, 8); 71 | 72 | /* Store state in digest */ 73 | encode(state, digest, 16); 74 | 75 | /* Restore current state and count. */ 76 | memcpy(state, oldState, 16); 77 | memcpy(count, oldCount, 8); 78 | } 79 | return digest; 80 | } 81 | 82 | /** 83 | * @Initialization the md5 object, processing another message block, 84 | * and updating the context. 85 | * 86 | * @param {input} the input message. 87 | * 88 | * @param {len} the number btye of message. 89 | * 90 | */ 91 | void MD5::init(const byte* input, size_t len) { 92 | 93 | bit32 i, index, partLen; 94 | 95 | finished = false; 96 | 97 | /* Compute number of bytes mod 64 */ 98 | index = (bit32)((count[0] >> 3) & 0x3f); 99 | 100 | /* update number of bits */ 101 | if ((count[0] += ((bit32)len << 3)) < ((bit32)len << 3)) { 102 | ++count[1]; 103 | } 104 | count[1] += ((bit32)len >> 29); 105 | 106 | partLen = 64 - index; 107 | 108 | /* transform as many times as possible. */ 109 | if (len >= partLen) { 110 | 111 | memcpy(&buffer[index], input, partLen); 112 | transform(buffer); 113 | 114 | for (i = partLen; i + 63 < len; i += 64) { 115 | transform(&input[i]); 116 | } 117 | index = 0; 118 | 119 | } else { 120 | i = 0; 121 | } 122 | 123 | /* Buffer remaining input */ 124 | memcpy(&buffer[index], &input[i], len - i); 125 | } 126 | 127 | /** 128 | * @MD5 basic transformation. Transforms state based on block. 129 | * 130 | * @param {block} the message block. 131 | */ 132 | void MD5::transform(const byte block[64]) { 133 | 134 | bit32 a = state[0], b = state[1], c = state[2], d = state[3], x[16]; 135 | 136 | decode(block, x, 64); 137 | 138 | /* Round 1 */ 139 | FF (a, b, c, d, x[ 0], s11, 0xd76aa478); 140 | FF (d, a, b, c, x[ 1], s12, 0xe8c7b756); 141 | FF (c, d, a, b, x[ 2], s13, 0x242070db); 142 | FF (b, c, d, a, x[ 3], s14, 0xc1bdceee); 143 | FF (a, b, c, d, x[ 4], s11, 0xf57c0faf); 144 | FF (d, a, b, c, x[ 5], s12, 0x4787c62a); 145 | FF (c, d, a, b, x[ 6], s13, 0xa8304613); 146 | FF (b, c, d, a, x[ 7], s14, 0xfd469501); 147 | FF (a, b, c, d, x[ 8], s11, 0x698098d8); 148 | FF (d, a, b, c, x[ 9], s12, 0x8b44f7af); 149 | FF (c, d, a, b, x[10], s13, 0xffff5bb1); 150 | FF (b, c, d, a, x[11], s14, 0x895cd7be); 151 | FF (a, b, c, d, x[12], s11, 0x6b901122); 152 | FF (d, a, b, c, x[13], s12, 0xfd987193); 153 | FF (c, d, a, b, x[14], s13, 0xa679438e); 154 | FF (b, c, d, a, x[15], s14, 0x49b40821); 155 | 156 | /* Round 2 */ 157 | GG (a, b, c, d, x[ 1], s21, 0xf61e2562); 158 | GG (d, a, b, c, x[ 6], s22, 0xc040b340); 159 | GG (c, d, a, b, x[11], s23, 0x265e5a51); 160 | GG (b, c, d, a, x[ 0], s24, 0xe9b6c7aa); 161 | GG (a, b, c, d, x[ 5], s21, 0xd62f105d); 162 | GG (d, a, b, c, x[10], s22, 0x2441453); 163 | GG (c, d, a, b, x[15], s23, 0xd8a1e681); 164 | GG (b, c, d, a, x[ 4], s24, 0xe7d3fbc8); 165 | GG (a, b, c, d, x[ 9], s21, 0x21e1cde6); 166 | GG (d, a, b, c, x[14], s22, 0xc33707d6); 167 | GG (c, d, a, b, x[ 3], s23, 0xf4d50d87); 168 | GG (b, c, d, a, x[ 8], s24, 0x455a14ed); 169 | GG (a, b, c, d, x[13], s21, 0xa9e3e905); 170 | GG (d, a, b, c, x[ 2], s22, 0xfcefa3f8); 171 | GG (c, d, a, b, x[ 7], s23, 0x676f02d9); 172 | GG (b, c, d, a, x[12], s24, 0x8d2a4c8a); 173 | 174 | /* Round 3 */ 175 | HH (a, b, c, d, x[ 5], s31, 0xfffa3942); 176 | HH (d, a, b, c, x[ 8], s32, 0x8771f681); 177 | HH (c, d, a, b, x[11], s33, 0x6d9d6122); 178 | HH (b, c, d, a, x[14], s34, 0xfde5380c); 179 | HH (a, b, c, d, x[ 1], s31, 0xa4beea44); 180 | HH (d, a, b, c, x[ 4], s32, 0x4bdecfa9); 181 | HH (c, d, a, b, x[ 7], s33, 0xf6bb4b60); 182 | HH (b, c, d, a, x[10], s34, 0xbebfbc70); 183 | HH (a, b, c, d, x[13], s31, 0x289b7ec6); 184 | HH (d, a, b, c, x[ 0], s32, 0xeaa127fa); 185 | HH (c, d, a, b, x[ 3], s33, 0xd4ef3085); 186 | HH (b, c, d, a, x[ 6], s34, 0x4881d05); 187 | HH (a, b, c, d, x[ 9], s31, 0xd9d4d039); 188 | HH (d, a, b, c, x[12], s32, 0xe6db99e5); 189 | HH (c, d, a, b, x[15], s33, 0x1fa27cf8); 190 | HH (b, c, d, a, x[ 2], s34, 0xc4ac5665); 191 | 192 | /* Round 4 */ 193 | II (a, b, c, d, x[ 0], s41, 0xf4292244); 194 | II (d, a, b, c, x[ 7], s42, 0x432aff97); 195 | II (c, d, a, b, x[14], s43, 0xab9423a7); 196 | II (b, c, d, a, x[ 5], s44, 0xfc93a039); 197 | II (a, b, c, d, x[12], s41, 0x655b59c3); 198 | II (d, a, b, c, x[ 3], s42, 0x8f0ccc92); 199 | II (c, d, a, b, x[10], s43, 0xffeff47d); 200 | II (b, c, d, a, x[ 1], s44, 0x85845dd1); 201 | II (a, b, c, d, x[ 8], s41, 0x6fa87e4f); 202 | II (d, a, b, c, x[15], s42, 0xfe2ce6e0); 203 | II (c, d, a, b, x[ 6], s43, 0xa3014314); 204 | II (b, c, d, a, x[13], s44, 0x4e0811a1); 205 | II (a, b, c, d, x[ 4], s41, 0xf7537e82); 206 | II (d, a, b, c, x[11], s42, 0xbd3af235); 207 | II (c, d, a, b, x[ 2], s43, 0x2ad7d2bb); 208 | II (b, c, d, a, x[ 9], s44, 0xeb86d391); 209 | 210 | state[0] += a; 211 | state[1] += b; 212 | state[2] += c; 213 | state[3] += d; 214 | } 215 | 216 | /** 217 | * @Encodes input (unsigned long) into output (byte). 218 | * 219 | * @param {input} usigned long. 220 | * 221 | * @param {output} byte. 222 | * 223 | * @param {length} the length of input. 224 | * 225 | */ 226 | void MD5::encode(const bit32* input, byte* output, size_t length) { 227 | 228 | for (size_t i = 0, j = 0; j < length; ++i, j += 4) { 229 | output[j]= (byte)(input[i] & 0xff); 230 | output[j + 1] = (byte)((input[i] >> 8) & 0xff); 231 | output[j + 2] = (byte)((input[i] >> 16) & 0xff); 232 | output[j + 3] = (byte)((input[i] >> 24) & 0xff); 233 | } 234 | } 235 | 236 | /** 237 | * @Decodes input (byte) into output (usigned long). 238 | * 239 | * @param {input} bytes. 240 | * 241 | * @param {output} unsigned long. 242 | * 243 | * @param {length} the length of input. 244 | * 245 | */ 246 | void MD5::decode(const byte* input, bit32* output, size_t length) { 247 | for (size_t i = 0, j = 0; j < length; ++i, j += 4) { 248 | output[i] = ((bit32)input[j]) | (((bit32)input[j + 1]) << 8) | 249 | (((bit32)input[j + 2]) << 16) | (((bit32)input[j + 3]) << 24); 250 | } 251 | } 252 | 253 | 254 | /** 255 | * @Convert digest to string value. 256 | * 257 | * @return the hex string of digest. 258 | * 259 | */ 260 | string MD5::toStr() { 261 | const byte* digest_ = getDigest(); 262 | string str; 263 | str.reserve(16 << 1); 264 | for (size_t i = 0; i < 16; ++i) { 265 | int t = digest_[i]; 266 | int a = t / 16; 267 | int b = t % 16; 268 | str.append(1, HEX_NUMBERS[a]); 269 | str.append(1, HEX_NUMBERS[b]); 270 | } 271 | return str; 272 | } 273 | -------------------------------------------------------------------------------- /stock_live_trade/alpha_config_types.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Autogenerated by Thrift Compiler (0.10.0) 3 | * 4 | * DO NOT EDIT UNLESS YOU ARE SURE THAT YOU KNOW WHAT YOU ARE DOING 5 | * @generated 6 | */ 7 | #include "alpha_config_types.h" 8 | 9 | #include 10 | #include 11 | 12 | #include 13 | 14 | 15 | 16 | 17 | ServerItem::~ServerItem() throw() { 18 | } 19 | 20 | 21 | void ServerItem::__set_host(const std::string& val) { 22 | this->host = val; 23 | } 24 | 25 | void ServerItem::__set_port(const int32_t val) { 26 | this->port = val; 27 | } 28 | 29 | uint32_t ServerItem::read(::apache::thrift::protocol::TProtocol* iprot) { 30 | 31 | apache::thrift::protocol::TInputRecursionTracker tracker(*iprot); 32 | uint32_t xfer = 0; 33 | std::string fname; 34 | ::apache::thrift::protocol::TType ftype; 35 | int16_t fid; 36 | 37 | xfer += iprot->readStructBegin(fname); 38 | 39 | using ::apache::thrift::protocol::TProtocolException; 40 | 41 | 42 | while (true) 43 | { 44 | xfer += iprot->readFieldBegin(fname, ftype, fid); 45 | if (ftype == ::apache::thrift::protocol::T_STOP) { 46 | break; 47 | } 48 | switch (fid) 49 | { 50 | case 1: 51 | if (ftype == ::apache::thrift::protocol::T_STRING) { 52 | xfer += iprot->readString(this->host); 53 | this->__isset.host = true; 54 | } else { 55 | xfer += iprot->skip(ftype); 56 | } 57 | break; 58 | case 2: 59 | if (ftype == ::apache::thrift::protocol::T_I32) { 60 | xfer += iprot->readI32(this->port); 61 | this->__isset.port = true; 62 | } else { 63 | xfer += iprot->skip(ftype); 64 | } 65 | break; 66 | default: 67 | xfer += iprot->skip(ftype); 68 | break; 69 | } 70 | xfer += iprot->readFieldEnd(); 71 | } 72 | 73 | xfer += iprot->readStructEnd(); 74 | 75 | return xfer; 76 | } 77 | 78 | uint32_t ServerItem::write(::apache::thrift::protocol::TProtocol* oprot) const { 79 | uint32_t xfer = 0; 80 | apache::thrift::protocol::TOutputRecursionTracker tracker(*oprot); 81 | xfer += oprot->writeStructBegin("ServerItem"); 82 | 83 | xfer += oprot->writeFieldBegin("host", ::apache::thrift::protocol::T_STRING, 1); 84 | xfer += oprot->writeString(this->host); 85 | xfer += oprot->writeFieldEnd(); 86 | 87 | xfer += oprot->writeFieldBegin("port", ::apache::thrift::protocol::T_I32, 2); 88 | xfer += oprot->writeI32(this->port); 89 | xfer += oprot->writeFieldEnd(); 90 | 91 | xfer += oprot->writeFieldStop(); 92 | xfer += oprot->writeStructEnd(); 93 | return xfer; 94 | } 95 | 96 | void swap(ServerItem &a, ServerItem &b) { 97 | using ::std::swap; 98 | swap(a.host, b.host); 99 | swap(a.port, b.port); 100 | swap(a.__isset, b.__isset); 101 | } 102 | 103 | ServerItem::ServerItem(const ServerItem& other0) { 104 | host = other0.host; 105 | port = other0.port; 106 | __isset = other0.__isset; 107 | } 108 | ServerItem& ServerItem::operator=(const ServerItem& other1) { 109 | host = other1.host; 110 | port = other1.port; 111 | __isset = other1.__isset; 112 | return *this; 113 | } 114 | void ServerItem::printTo(std::ostream& out) const { 115 | using ::apache::thrift::to_string; 116 | out << "ServerItem("; 117 | out << "host=" << to_string(host); 118 | out << ", " << "port=" << to_string(port); 119 | out << ")"; 120 | } 121 | 122 | 123 | ConfigData::~ConfigData() throw() { 124 | } 125 | 126 | 127 | void ConfigData::__set_broker_server(const std::map > & val) { 128 | this->broker_server = val; 129 | } 130 | 131 | void ConfigData::__set_license_data(const std::map & val) { 132 | this->license_data = val; 133 | } 134 | 135 | uint32_t ConfigData::read(::apache::thrift::protocol::TProtocol* iprot) { 136 | 137 | apache::thrift::protocol::TInputRecursionTracker tracker(*iprot); 138 | uint32_t xfer = 0; 139 | std::string fname; 140 | ::apache::thrift::protocol::TType ftype; 141 | int16_t fid; 142 | 143 | xfer += iprot->readStructBegin(fname); 144 | 145 | using ::apache::thrift::protocol::TProtocolException; 146 | 147 | 148 | while (true) 149 | { 150 | xfer += iprot->readFieldBegin(fname, ftype, fid); 151 | if (ftype == ::apache::thrift::protocol::T_STOP) { 152 | break; 153 | } 154 | switch (fid) 155 | { 156 | case 1: 157 | if (ftype == ::apache::thrift::protocol::T_MAP) { 158 | { 159 | this->broker_server.clear(); 160 | uint32_t _size2; 161 | ::apache::thrift::protocol::TType _ktype3; 162 | ::apache::thrift::protocol::TType _vtype4; 163 | xfer += iprot->readMapBegin(_ktype3, _vtype4, _size2); 164 | uint32_t _i6; 165 | for (_i6 = 0; _i6 < _size2; ++_i6) 166 | { 167 | std::string _key7; 168 | xfer += iprot->readString(_key7); 169 | std::vector & _val8 = this->broker_server[_key7]; 170 | { 171 | _val8.clear(); 172 | uint32_t _size9; 173 | ::apache::thrift::protocol::TType _etype12; 174 | xfer += iprot->readListBegin(_etype12, _size9); 175 | _val8.resize(_size9); 176 | uint32_t _i13; 177 | for (_i13 = 0; _i13 < _size9; ++_i13) 178 | { 179 | xfer += _val8[_i13].read(iprot); 180 | } 181 | xfer += iprot->readListEnd(); 182 | } 183 | } 184 | xfer += iprot->readMapEnd(); 185 | } 186 | this->__isset.broker_server = true; 187 | } else { 188 | xfer += iprot->skip(ftype); 189 | } 190 | break; 191 | case 2: 192 | if (ftype == ::apache::thrift::protocol::T_MAP) { 193 | { 194 | this->license_data.clear(); 195 | uint32_t _size14; 196 | ::apache::thrift::protocol::TType _ktype15; 197 | ::apache::thrift::protocol::TType _vtype16; 198 | xfer += iprot->readMapBegin(_ktype15, _vtype16, _size14); 199 | uint32_t _i18; 200 | for (_i18 = 0; _i18 < _size14; ++_i18) 201 | { 202 | std::string _key19; 203 | xfer += iprot->readString(_key19); 204 | std::string& _val20 = this->license_data[_key19]; 205 | xfer += iprot->readString(_val20); 206 | } 207 | xfer += iprot->readMapEnd(); 208 | } 209 | this->__isset.license_data = true; 210 | } else { 211 | xfer += iprot->skip(ftype); 212 | } 213 | break; 214 | default: 215 | xfer += iprot->skip(ftype); 216 | break; 217 | } 218 | xfer += iprot->readFieldEnd(); 219 | } 220 | 221 | xfer += iprot->readStructEnd(); 222 | 223 | return xfer; 224 | } 225 | 226 | uint32_t ConfigData::write(::apache::thrift::protocol::TProtocol* oprot) const { 227 | uint32_t xfer = 0; 228 | apache::thrift::protocol::TOutputRecursionTracker tracker(*oprot); 229 | xfer += oprot->writeStructBegin("ConfigData"); 230 | 231 | xfer += oprot->writeFieldBegin("broker_server", ::apache::thrift::protocol::T_MAP, 1); 232 | { 233 | xfer += oprot->writeMapBegin(::apache::thrift::protocol::T_STRING, ::apache::thrift::protocol::T_LIST, static_cast(this->broker_server.size())); 234 | std::map > ::const_iterator _iter21; 235 | for (_iter21 = this->broker_server.begin(); _iter21 != this->broker_server.end(); ++_iter21) 236 | { 237 | xfer += oprot->writeString(_iter21->first); 238 | { 239 | xfer += oprot->writeListBegin(::apache::thrift::protocol::T_STRUCT, static_cast(_iter21->second.size())); 240 | std::vector ::const_iterator _iter22; 241 | for (_iter22 = _iter21->second.begin(); _iter22 != _iter21->second.end(); ++_iter22) 242 | { 243 | xfer += (*_iter22).write(oprot); 244 | } 245 | xfer += oprot->writeListEnd(); 246 | } 247 | } 248 | xfer += oprot->writeMapEnd(); 249 | } 250 | xfer += oprot->writeFieldEnd(); 251 | 252 | xfer += oprot->writeFieldBegin("license_data", ::apache::thrift::protocol::T_MAP, 2); 253 | { 254 | xfer += oprot->writeMapBegin(::apache::thrift::protocol::T_STRING, ::apache::thrift::protocol::T_STRING, static_cast(this->license_data.size())); 255 | std::map ::const_iterator _iter23; 256 | for (_iter23 = this->license_data.begin(); _iter23 != this->license_data.end(); ++_iter23) 257 | { 258 | xfer += oprot->writeString(_iter23->first); 259 | xfer += oprot->writeString(_iter23->second); 260 | } 261 | xfer += oprot->writeMapEnd(); 262 | } 263 | xfer += oprot->writeFieldEnd(); 264 | 265 | xfer += oprot->writeFieldStop(); 266 | xfer += oprot->writeStructEnd(); 267 | return xfer; 268 | } 269 | 270 | void swap(ConfigData &a, ConfigData &b) { 271 | using ::std::swap; 272 | swap(a.broker_server, b.broker_server); 273 | swap(a.license_data, b.license_data); 274 | swap(a.__isset, b.__isset); 275 | } 276 | 277 | ConfigData::ConfigData(const ConfigData& other24) { 278 | broker_server = other24.broker_server; 279 | license_data = other24.license_data; 280 | __isset = other24.__isset; 281 | } 282 | ConfigData& ConfigData::operator=(const ConfigData& other25) { 283 | broker_server = other25.broker_server; 284 | license_data = other25.license_data; 285 | __isset = other25.__isset; 286 | return *this; 287 | } 288 | void ConfigData::printTo(std::ostream& out) const { 289 | using ::apache::thrift::to_string; 290 | out << "ConfigData("; 291 | out << "broker_server=" << to_string(broker_server); 292 | out << ", " << "license_data=" << to_string(license_data); 293 | out << ")"; 294 | } 295 | 296 | 297 | -------------------------------------------------------------------------------- /stock_live_trade/PracticalSocket.h: -------------------------------------------------------------------------------- 1 | /* 2 | * C++ sockets on Unix and Windows 3 | * Copyright (C) 2002 4 | * 5 | * This program is free software; you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License as published by 7 | * the Free Software Foundation; either version 2 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * This program is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU General Public License 16 | * along with this program; if not, write to the Free Software 17 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 18 | */ 19 | 20 | #ifndef __PRACTICALSOCKET_INCLUDED__ 21 | #define __PRACTICALSOCKET_INCLUDED__ 22 | 23 | #include // For string 24 | #include // For exception class 25 | 26 | using namespace std; 27 | 28 | /** 29 | * Signals a problem with the execution of a socket call. 30 | */ 31 | class SocketException : public exception { 32 | public: 33 | /** 34 | * Construct a SocketException with a explanatory message. 35 | * @param message explanatory message 36 | * @param incSysMsg true if system message (from strerror(errno)) 37 | * should be postfixed to the user provided message 38 | */ 39 | SocketException(const string &message, bool inclSysMsg = false) throw(); 40 | 41 | /** 42 | * Provided just to guarantee that no exceptions are thrown. 43 | */ 44 | ~SocketException() throw(); 45 | 46 | /** 47 | * Get the exception message 48 | * @return exception message 49 | */ 50 | const char *what() const throw(); 51 | 52 | private: 53 | string userMessage; // Exception message 54 | }; 55 | 56 | /** 57 | * Base class representing basic communication endpoint 58 | */ 59 | class Socket { 60 | public: 61 | /** 62 | * Close and deallocate this socket 63 | */ 64 | ~Socket(); 65 | 66 | void close(); 67 | 68 | /** 69 | * Get the local address 70 | * @return local address of socket 71 | * @exception SocketException thrown if fetch fails 72 | */ 73 | string getLocalAddress() throw(SocketException); 74 | 75 | /** 76 | * Get the local port 77 | * @return local port of socket 78 | * @exception SocketException thrown if fetch fails 79 | */ 80 | unsigned short getLocalPort() throw(SocketException); 81 | 82 | /** 83 | * Set the local port to the specified port and the local address 84 | * to any interface 85 | * @param localPort local port 86 | * @exception SocketException thrown if setting local port fails 87 | */ 88 | void setLocalPort(unsigned short localPort) throw(SocketException); 89 | 90 | /** 91 | * Set the local port to the specified port and the local address 92 | * to the specified address. If you omit the port, a random port 93 | * will be selected. 94 | * @param localAddress local address 95 | * @param localPort local port 96 | * @exception SocketException thrown if setting local port or address fails 97 | */ 98 | void setLocalAddressAndPort(const string &localAddress, 99 | unsigned short localPort = 0) throw(SocketException); 100 | 101 | /** 102 | * If WinSock, unload the WinSock DLLs; otherwise do nothing. We ignore 103 | * this in our sample client code but include it in the library for 104 | * completeness. If you are running on Windows and you are concerned 105 | * about DLL resource consumption, call this after you are done with all 106 | * Socket instances. If you execute this on Windows while some instance of 107 | * Socket exists, you are toast. For portability of client code, this is 108 | * an empty function on non-Windows platforms so you can always include it. 109 | * @param buffer buffer to receive the data 110 | * @param bufferLen maximum number of bytes to read into buffer 111 | * @return number of bytes read, 0 for EOF, and -1 for error 112 | * @exception SocketException thrown WinSock clean up fails 113 | */ 114 | static void cleanUp() throw(SocketException); 115 | 116 | /** 117 | * Resolve the specified service for the specified protocol to the 118 | * corresponding port number in host byte order 119 | * @param service service to resolve (e.g., "http") 120 | * @param protocol protocol of service to resolve. Default is "tcp". 121 | */ 122 | static unsigned short resolveService(const string &service, 123 | const string &protocol = "tcp"); 124 | 125 | bool isReadable(); 126 | private: 127 | // Prevent the user from trying to use value semantics on this object 128 | Socket(const Socket &sock); 129 | void operator=(const Socket &sock); 130 | 131 | protected: 132 | int sockDesc; // Socket descriptor 133 | Socket(int type, int protocol) throw(SocketException); 134 | Socket(int sockDesc); 135 | }; 136 | 137 | /** 138 | * Socket which is able to connect, send, and receive 139 | */ 140 | class CommunicatingSocket : public Socket { 141 | public: 142 | /** 143 | * Establish a socket connection with the given foreign 144 | * address and port 145 | * @param foreignAddress foreign address (IP address or name) 146 | * @param foreignPort foreign port 147 | * @exception SocketException thrown if unable to establish connection 148 | */ 149 | void connect(const string &foreignAddress, unsigned short foreignPort) 150 | throw(SocketException); 151 | 152 | /** 153 | * Write the given buffer to this socket. Call connect() before 154 | * calling send() 155 | * @param buffer buffer to be written 156 | * @param bufferLen number of bytes from buffer to be written 157 | * @exception SocketException thrown if unable to send data 158 | */ 159 | void send(const void *buffer, int bufferLen) throw(SocketException); 160 | 161 | /** 162 | * Read into the given buffer up to bufferLen bytes data from this 163 | * socket. Call connect() before calling recv() 164 | * @param buffer buffer to receive the data 165 | * @param bufferLen maximum number of bytes to read into buffer 166 | * @return number of bytes read, 0 for EOF, and -1 for error 167 | * @exception SocketException thrown if unable to receive data 168 | */ 169 | int recv(void *buffer, int bufferLen) throw(SocketException); 170 | 171 | /** 172 | * Get the foreign address. Call connect() before calling recv() 173 | * @return foreign address 174 | * @exception SocketException thrown if unable to fetch foreign address 175 | */ 176 | string getForeignAddress() throw(SocketException); 177 | 178 | /** 179 | * Get the foreign port. Call connect() before calling recv() 180 | * @return foreign port 181 | * @exception SocketException thrown if unable to fetch foreign port 182 | */ 183 | unsigned short getForeignPort() throw(SocketException); 184 | 185 | protected: 186 | CommunicatingSocket(int type, int protocol) throw(SocketException); 187 | CommunicatingSocket(int newConnSD); 188 | }; 189 | 190 | /** 191 | * TCP socket for communication with other TCP sockets 192 | */ 193 | class TCPSocket : public CommunicatingSocket { 194 | public: 195 | /** 196 | * Construct a TCP socket with no connection 197 | * @exception SocketException thrown if unable to create TCP socket 198 | */ 199 | TCPSocket() throw(SocketException); 200 | 201 | /** 202 | * Construct a TCP socket with a connection to the given foreign address 203 | * and port 204 | * @param foreignAddress foreign address (IP address or name) 205 | * @param foreignPort foreign port 206 | * @exception SocketException thrown if unable to create TCP socket 207 | */ 208 | TCPSocket(const string &foreignAddress, unsigned short foreignPort) 209 | throw(SocketException); 210 | 211 | private: 212 | // Access for TCPServerSocket::accept() connection creation 213 | friend class TCPServerSocket; 214 | TCPSocket(int newConnSD); 215 | }; 216 | 217 | /** 218 | * TCP socket class for servers 219 | */ 220 | class TCPServerSocket : public Socket { 221 | public: 222 | /** 223 | * Construct a TCP socket for use with a server, accepting connections 224 | * on the specified port on any interface 225 | * @param localPort local port of server socket, a value of zero will 226 | * give a system-assigned unused port 227 | * @param queueLen maximum queue length for outstanding 228 | * connection requests (default 5) 229 | * @exception SocketException thrown if unable to create TCP server socket 230 | */ 231 | TCPServerSocket(unsigned short localPort, int queueLen = 5) 232 | throw(SocketException); 233 | 234 | /** 235 | * Construct a TCP socket for use with a server, accepting connections 236 | * on the specified port on the interface specified by the given address 237 | * @param localAddress local interface (address) of server socket 238 | * @param localPort local port of server socket 239 | * @param queueLen maximum queue length for outstanding 240 | * connection requests (default 5) 241 | * @exception SocketException thrown if unable to create TCP server socket 242 | */ 243 | TCPServerSocket(const string &localAddress, unsigned short localPort, 244 | int queueLen = 5) throw(SocketException); 245 | 246 | /** 247 | * Blocks until a new connection is established on this socket or error 248 | * @return new connection socket 249 | * @exception SocketException thrown if attempt to accept a new connection fails 250 | */ 251 | TCPSocket *accept() throw(SocketException); 252 | 253 | private: 254 | void setListen(int queueLen) throw(SocketException); 255 | }; 256 | 257 | 258 | #endif 259 | -------------------------------------------------------------------------------- /stock_live_trade/PracticalSocket.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * C++ sockets on Unix and Windows 3 | * Copyright (C) 2002 4 | * 5 | * This program is free software; you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License as published by 7 | * the Free Software Foundation; either version 2 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * This program is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU General Public License 16 | * along with this program; if not, write to the Free Software 17 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 18 | */ 19 | 20 | #include "PracticalSocket.h" 21 | 22 | #ifdef _WIN32 23 | #define WIN32 24 | #endif // _WIN32 25 | 26 | 27 | #ifdef WIN32 28 | #pragma comment(lib, "Ws2_32.lib") 29 | #include // For socket(), connect(), send(), and recv() 30 | typedef int socklen_t; 31 | typedef char raw_type; // Type used for raw data on this platform 32 | #else 33 | #include // For data types 34 | #include // For socket(), connect(), send(), and recv() 35 | #include // For gethostbyname() 36 | #include // For inet_addr() 37 | #include // For close() 38 | #include // For sockaddr_in 39 | typedef void raw_type; // Type used for raw data on this platform 40 | #endif 41 | 42 | #include // For errno 43 | 44 | using namespace std; 45 | 46 | #ifdef WIN32 47 | static bool initialized = false; 48 | #endif 49 | 50 | // SocketException Code 51 | 52 | SocketException::SocketException(const string &message, bool inclSysMsg) 53 | throw() : userMessage(message) { 54 | if (inclSysMsg) { 55 | userMessage.append(": "); 56 | userMessage.append(strerror(errno)); 57 | } 58 | } 59 | 60 | SocketException::~SocketException() throw() { 61 | } 62 | 63 | const char *SocketException::what() const throw() { 64 | return userMessage.c_str(); 65 | } 66 | 67 | // Function to fill in address structure given an address and port 68 | static void fillAddr(const string &address, unsigned short port, 69 | sockaddr_in &addr) { 70 | memset(&addr, 0, sizeof(addr)); // Zero out address structure 71 | addr.sin_family = AF_INET; // Internet address 72 | 73 | hostent *host; // Resolve name 74 | if ((host = gethostbyname(address.c_str())) == NULL) { 75 | // strerror() will not work for gethostbyname() and hstrerror() 76 | // is supposedly obsolete 77 | throw SocketException("Failed to resolve name (gethostbyname())"); 78 | } 79 | addr.sin_addr.s_addr = *((unsigned long *) host->h_addr_list[0]); 80 | 81 | addr.sin_port = htons(port); // Assign port in network byte order 82 | } 83 | 84 | // Socket Code 85 | 86 | Socket::Socket(int type, int protocol) throw(SocketException) { 87 | #ifdef WIN32 88 | if (!initialized) { 89 | WORD wVersionRequested; 90 | WSADATA wsaData; 91 | 92 | wVersionRequested = MAKEWORD(2, 0); // Request WinSock v2.0 93 | if (WSAStartup(wVersionRequested, &wsaData) != 0) { // Load WinSock DLL 94 | throw SocketException("Unable to load WinSock DLL"); 95 | } 96 | initialized = true; 97 | } 98 | #endif 99 | 100 | // Make a new socket 101 | if ((sockDesc = socket(PF_INET, type, protocol)) < 0) { 102 | throw SocketException("Socket creation failed (socket())", true); 103 | } 104 | } 105 | 106 | Socket::Socket(int sockDesc) { 107 | this->sockDesc = sockDesc; 108 | } 109 | 110 | Socket::~Socket() { 111 | close(); 112 | } 113 | 114 | void Socket::close() 115 | { 116 | #ifdef WIN32 117 | ::closesocket(sockDesc); 118 | #else 119 | ::close(sockDesc); 120 | #endif 121 | sockDesc = -1; 122 | } 123 | 124 | string Socket::getLocalAddress() throw(SocketException) { 125 | sockaddr_in addr; 126 | unsigned int addr_len = sizeof(addr); 127 | 128 | if (getsockname(sockDesc, (sockaddr *) &addr, (socklen_t *) &addr_len) < 0) { 129 | throw SocketException("Fetch of local address failed (getsockname())", true); 130 | } 131 | return inet_ntoa(addr.sin_addr); 132 | } 133 | 134 | unsigned short Socket::getLocalPort() throw(SocketException) { 135 | sockaddr_in addr; 136 | unsigned int addr_len = sizeof(addr); 137 | 138 | if (getsockname(sockDesc, (sockaddr *) &addr, (socklen_t *) &addr_len) < 0) { 139 | throw SocketException("Fetch of local port failed (getsockname())", true); 140 | } 141 | return ntohs(addr.sin_port); 142 | } 143 | 144 | void Socket::setLocalPort(unsigned short localPort) throw(SocketException) { 145 | // Bind the socket to its port 146 | sockaddr_in localAddr; 147 | memset(&localAddr, 0, sizeof(localAddr)); 148 | localAddr.sin_family = AF_INET; 149 | localAddr.sin_addr.s_addr = htonl(INADDR_ANY); 150 | localAddr.sin_port = htons(localPort); 151 | 152 | if (bind(sockDesc, (sockaddr *) &localAddr, sizeof(sockaddr_in)) < 0) { 153 | throw SocketException("Set of local port failed (bind())", true); 154 | } 155 | } 156 | 157 | void Socket::setLocalAddressAndPort(const string &localAddress, 158 | unsigned short localPort) throw(SocketException) { 159 | // Get the address of the requested host 160 | sockaddr_in localAddr; 161 | fillAddr(localAddress, localPort, localAddr); 162 | 163 | if (bind(sockDesc, (sockaddr *) &localAddr, sizeof(sockaddr_in)) < 0) { 164 | throw SocketException("Set of local address and port failed (bind())", true); 165 | } 166 | } 167 | 168 | bool Socket::isReadable() 169 | { 170 | 171 | #ifdef WIN32 172 | u_long bytes_available = 0; 173 | ioctlsocket(sockDesc, FIONREAD, &bytes_available); 174 | 175 | return bytes_available > 0; 176 | #else 177 | u_long bytes_available = 0; 178 | ioctl(sockDesc, FIONREAD, &bytes_available); 179 | 180 | return bytes_available > 0; 181 | #endif 182 | } 183 | 184 | 185 | void Socket::cleanUp() throw(SocketException) { 186 | #ifdef WIN32 187 | if (WSACleanup() != 0) { 188 | throw SocketException("WSACleanup() failed"); 189 | } 190 | #endif 191 | } 192 | 193 | unsigned short Socket::resolveService(const string &service, 194 | const string &protocol) { 195 | struct servent *serv; /* Structure containing service information */ 196 | 197 | if ((serv = getservbyname(service.c_str(), protocol.c_str())) == NULL) 198 | return atoi(service.c_str()); /* Service is port number */ 199 | else 200 | return ntohs(serv->s_port); /* Found port (network byte order) by name */ 201 | } 202 | 203 | // CommunicatingSocket Code 204 | 205 | CommunicatingSocket::CommunicatingSocket(int type, int protocol) 206 | throw(SocketException) : Socket(type, protocol) { 207 | } 208 | 209 | CommunicatingSocket::CommunicatingSocket(int newConnSD) : Socket(newConnSD) { 210 | } 211 | 212 | void CommunicatingSocket::connect(const string &foreignAddress, 213 | unsigned short foreignPort) throw(SocketException) { 214 | // Get the address of the requested host 215 | sockaddr_in destAddr; 216 | fillAddr(foreignAddress, foreignPort, destAddr); 217 | 218 | // Try to connect to the given port 219 | if (::connect(sockDesc, (sockaddr *) &destAddr, sizeof(destAddr)) < 0) { 220 | throw SocketException("Connect failed (connect())", true); 221 | } 222 | } 223 | 224 | void CommunicatingSocket::send(const void *buffer, int bufferLen) 225 | throw(SocketException) { 226 | if (::send(sockDesc, (raw_type *) buffer, bufferLen, 0) < 0) { 227 | throw SocketException("Send failed (send())", true); 228 | } 229 | } 230 | 231 | int CommunicatingSocket::recv(void *buffer, int bufferLen) 232 | throw(SocketException) { 233 | int rtn; 234 | if ((rtn = ::recv(sockDesc, (raw_type *) buffer, bufferLen, 0)) < 0) { 235 | throw SocketException("Received failed (recv())", true); 236 | } 237 | 238 | return rtn; 239 | } 240 | 241 | string CommunicatingSocket::getForeignAddress() 242 | throw(SocketException) { 243 | sockaddr_in addr; 244 | unsigned int addr_len = sizeof(addr); 245 | 246 | if (getpeername(sockDesc, (sockaddr *) &addr,(socklen_t *) &addr_len) < 0) { 247 | throw SocketException("Fetch of foreign address failed (getpeername())", true); 248 | } 249 | return inet_ntoa(addr.sin_addr); 250 | } 251 | 252 | unsigned short CommunicatingSocket::getForeignPort() throw(SocketException) { 253 | sockaddr_in addr; 254 | unsigned int addr_len = sizeof(addr); 255 | 256 | if (getpeername(sockDesc, (sockaddr *) &addr, (socklen_t *) &addr_len) < 0) { 257 | throw SocketException("Fetch of foreign port failed (getpeername())", true); 258 | } 259 | return ntohs(addr.sin_port); 260 | } 261 | 262 | // TCPSocket Code 263 | 264 | TCPSocket::TCPSocket() 265 | throw(SocketException) : CommunicatingSocket(SOCK_STREAM, 266 | IPPROTO_TCP) { 267 | } 268 | 269 | TCPSocket::TCPSocket(const string &foreignAddress, unsigned short foreignPort) 270 | throw(SocketException) : CommunicatingSocket(SOCK_STREAM, IPPROTO_TCP) { 271 | connect(foreignAddress, foreignPort); 272 | } 273 | 274 | TCPSocket::TCPSocket(int newConnSD) : CommunicatingSocket(newConnSD) { 275 | } 276 | 277 | // TCPServerSocket Code 278 | 279 | TCPServerSocket::TCPServerSocket(unsigned short localPort, int queueLen) 280 | throw(SocketException) : Socket(SOCK_STREAM, IPPROTO_TCP) { 281 | setLocalPort(localPort); 282 | setListen(queueLen); 283 | } 284 | 285 | TCPServerSocket::TCPServerSocket(const string &localAddress, 286 | unsigned short localPort, int queueLen) 287 | throw(SocketException) : Socket(SOCK_STREAM, IPPROTO_TCP) { 288 | setLocalAddressAndPort(localAddress, localPort); 289 | setListen(queueLen); 290 | } 291 | 292 | TCPSocket *TCPServerSocket::accept() throw(SocketException) { 293 | int newConnSD; 294 | if ((newConnSD = ::accept(sockDesc, NULL, 0)) < 0) { 295 | throw SocketException("Accept failed (accept())", true); 296 | } 297 | 298 | return new TCPSocket(newConnSD); 299 | } 300 | 301 | void TCPServerSocket::setListen(int queueLen) throw(SocketException) { 302 | if (listen(sockDesc, queueLen) < 0) { 303 | throw SocketException("Set listening socket failed (listen())", true); 304 | } 305 | } 306 | 307 | -------------------------------------------------------------------------------- /stock_live_trade/stock_live_trade/stock_live_trade.vcxproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Debug 6 | Win32 7 | 8 | 9 | Release 10 | Win32 11 | 12 | 13 | Debug 14 | x64 15 | 16 | 17 | Release 18 | x64 19 | 20 | 21 | 22 | {4F2C8A1C-D43E-4E6C-B435-F334966F5CC0} 23 | Win32Proj 24 | stock_live_trade 25 | 7.0 26 | 27 | 28 | 29 | Application 30 | true 31 | LLVM-vs2014_xp 32 | MultiByte 33 | 34 | 35 | Application 36 | false 37 | v140_xp 38 | true 39 | Unicode 40 | 41 | 42 | Application 43 | true 44 | v141 45 | Unicode 46 | 47 | 48 | Application 49 | false 50 | v141 51 | true 52 | Unicode 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | true 74 | C:\dev\thrift-1.0.0\lib\cpp\src;C:\dev\boost_1_62_0;C:\dev\libevent-2.0.22-stable;C:\dev\libevent-2.0.22-stable\include;C:\dev;$(IncludePath) 75 | 76 | 77 | true 78 | 79 | 80 | false 81 | C:\dev\boost_1_62_0;C:\dev\libevent-2.0.22-stable;C:\dev\libevent-2.0.22-stable\include;C:\dev\thrift-1.0.0\lib\cpp\src;C:\dev\;C:\dev\openssl-1.1.0b-vs2015\include;$(IncludePath) 82 | C:\dev\lib;C:\dev\boost_1_62_0\stage\lib;C:\dev\openssl-1.1.0b-vs2015\lib;$(LibraryPath) 83 | 84 | 85 | false 86 | 87 | 88 | 89 | 90 | 91 | Level3 92 | Disabled 93 | _CRT_SECURE_NO_DEPRECATE;_SCL_SECURE_NO_DEPRECATE;WIN32;_DEBUG;_WINDOWS;%(PreprocessorDefinitions) 94 | true 95 | 96 | 97 | Windows 98 | true 99 | 100 | 101 | 102 | 103 | 104 | 105 | Level3 106 | Disabled 107 | _DEBUG;_WINDOWS;%(PreprocessorDefinitions) 108 | true 109 | 110 | 111 | Windows 112 | true 113 | 114 | 115 | 116 | 117 | Level3 118 | 119 | 120 | Disabled 121 | true 122 | false 123 | WIN32;_CRT_SECURE_NO_DEPRECATE;_SCL_SECURE_NO_DEPRECATE;NDEBUG;_WINDOWS;%(PreprocessorDefinitions) 124 | true 125 | MultiThreaded 126 | Default 127 | false 128 | 129 | 130 | Console 131 | true 132 | true 133 | true 134 | libthriftnbMT.lib;zlibstatic.lib;libthriftMT.lib;libcryptoMT.lib;libsslMT.lib;libevent.lib;crypt32.lib;%(AdditionalDependencies) 135 | 136 | 137 | 138 | 139 | Level3 140 | 141 | 142 | MaxSpeed 143 | true 144 | true 145 | NDEBUG;_WINDOWS;%(PreprocessorDefinitions) 146 | true 147 | 148 | 149 | Windows 150 | true 151 | true 152 | true 153 | 154 | 155 | 156 | 157 | 158 | 159 | 160 | 161 | 162 | 163 | 164 | 165 | 166 | 167 | 168 | 169 | 170 | 171 | 172 | 173 | 174 | 175 | 176 | 177 | 178 | 179 | 180 | 181 | 182 | 183 | 184 | 185 | 186 | 187 | 188 | 189 | 190 | 191 | 192 | 193 | 194 | 195 | 196 | 197 | 198 | 199 | 200 | 201 | 202 | 203 | 204 | 205 | 206 | -------------------------------------------------------------------------------- /stock_live_trade/AlphaTrade_server.cpp: -------------------------------------------------------------------------------- 1 | // This autogenerated skeleton file illustrates how to build a server. 2 | // You should copy it to another filename to avoid overwriting it. 3 | 4 | #include 5 | 6 | #include "AlphaTrade.h" 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | 13 | #include "thread_broker_adapter.h" 14 | #include "GBase64.h" 15 | #include "utility.h" 16 | #include "hexin_stock_broker_huatai.h" 17 | #include "hexin_stock_broker_guangfa.h" 18 | #include "GWin32CriticalSection.h" 19 | #include "GScopedLock.h" 20 | #include "license_manager.h" 21 | 22 | #include 23 | 24 | #ifdef _WINDOWS 25 | #define NULL_DEVICE "NUL:" 26 | #else 27 | #define NULL_DEVICE "/dev/null" 28 | #endif 29 | 30 | using namespace ::apache::thrift; 31 | using namespace ::apache::thrift::protocol; 32 | using namespace ::apache::thrift::transport; 33 | using namespace ::apache::thrift::server; 34 | 35 | using boost::shared_ptr; 36 | 37 | std::map> g_map_tradeid_broker; 38 | std::deque> g_deq_wait_4_delete; 39 | 40 | 41 | class AlphaTradeHandler : virtual public AlphaTradeIf { 42 | public: 43 | AlphaTradeHandler() { 44 | // Your initialization goes here 45 | } 46 | 47 | bool Running() { 48 | // Your implementation goes here 49 | return true; 50 | } 51 | 52 | int32_t GetPID() { 53 | #ifdef _WINDOWS 54 | return GetCurrentProcessId(); 55 | #else 56 | return ::getpid(); 57 | #endif 58 | } 59 | 60 | 61 | void KeepAlive(const std::string& sessionID) 62 | { 63 | LicenseManager::get_instance()->set_session_id(sessionID); 64 | } 65 | 66 | void LiveTradeLogin(LiveTradeLoginResp& _return, const std::string& sessionID, const std::string& account, const std::string& password1, const std::string& password2, const std::string& brokerStr) { 67 | 68 | std::string strUUID = gen_uuid(); 69 | thread_broker_adapter *broker_ptr = NULL; 70 | 71 | if (brokerStr == "stock_huatai") { 72 | 73 | broker_ptr = new thread_broker_adapter(new hexin_stock_broker_huatai(LicenseManager::get_instance()->get_broker_server("stock_huatai"), account, password1, password2, true), false); 74 | } 75 | else if (brokerStr == "stock_guangfa") { 76 | broker_ptr = new thread_broker_adapter(new hexin_stock_broker_guangfa(LicenseManager::get_instance()->get_broker_server("stock_guangfa"), account, password1, password2, true), false); 77 | } 78 | else { 79 | _return.ret_code = 1; 80 | return; 81 | } 82 | 83 | broker_ptr->start(); 84 | g_map_tradeid_broker[strUUID] = boost::shared_ptr(broker_ptr); 85 | 86 | _return.result = strUUID; 87 | _return.ret_code = 0; 88 | 89 | while (g_deq_wait_4_delete.size() > 0 && g_deq_wait_4_delete[0]->stopped()) { 90 | g_deq_wait_4_delete.pop_front(); 91 | } 92 | } 93 | 94 | void LiveTradeLogout(LiveTradeLogoutResp& _return, const std::string& sessionID, const std::string& liveTradeID) { 95 | 96 | std::map>::iterator iter = g_map_tradeid_broker.find(liveTradeID); 97 | if (iter != g_map_tradeid_broker.end()) { 98 | iter->second->stop(); 99 | g_deq_wait_4_delete.push_back(iter->second); 100 | g_map_tradeid_broker.erase(iter); 101 | 102 | //TODO 103 | } 104 | else { 105 | 106 | } 107 | 108 | _return.ret_code = 0; 109 | } 110 | 111 | void GetAccountState(GetAccountStateResp& _return, const std::string& sessionID, const std::string& liveTradeID) { 112 | 113 | std::map>::iterator iter = g_map_tradeid_broker.find(liveTradeID); 114 | if (iter != g_map_tradeid_broker.end()) { 115 | _return.ret_code = 0; 116 | _return.state = iter->second->getAccountState(); 117 | } 118 | else { 119 | 120 | } 121 | } 122 | 123 | void GetAccountBalance(GetAccountBalanceResp& _return, const std::string& sessionID, const std::string& liveTradeID) { 124 | 125 | std::map>::iterator iter = g_map_tradeid_broker.find(liveTradeID); 126 | if (iter != g_map_tradeid_broker.end()) { 127 | 128 | std::vector balance = iter->second->getBalance(); 129 | if (balance.size() > 0) { 130 | _return.ret_code = 0; 131 | _return.result.total_value = balance[0]; 132 | _return.result.money_left = balance[1]; 133 | } 134 | else { 135 | _return.ret_code = 3; 136 | } 137 | } 138 | else { 139 | 140 | } 141 | 142 | } 143 | 144 | void GetHoldingStock(GetHoldingStockResp& _return, const std::string& sessionID, const std::string& liveTradeID) { 145 | 146 | std::map>::iterator iter = g_map_tradeid_broker.find(liveTradeID); 147 | if (iter != g_map_tradeid_broker.end()) { 148 | _return.ret_code = 0; 149 | std::vector holding = iter->second->getHoldingStock(); 150 | 151 | for (int i = 0; i < holding.size(); ++i) { 152 | HoldingStock item; 153 | item.stock_id = holding[i].sid; 154 | item.long_short = "long"; 155 | item.buy_price = holding[i].buy_price; 156 | item.quant = holding[i].quant; 157 | item.quant_sellable = holding[i].quant_available; 158 | 159 | _return.result.push_back(item); 160 | } 161 | } 162 | 163 | } 164 | 165 | void GetAllOrder(GetAllOrderResp& _return, const std::string& sessionID, const std::string& liveTradeID) { 166 | std::map>::iterator iter = g_map_tradeid_broker.find(liveTradeID); 167 | if (iter != g_map_tradeid_broker.end()) { 168 | _return.ret_code = 0; 169 | std::vector order_vec = iter->second->getAllOrder(); 170 | 171 | for (int i = 0; i < order_vec.size(); ++i) { 172 | OrderState os; 173 | os.operation = order_vec[i].operation; 174 | os.direction = order_vec[i].direction; 175 | os.deal_price = order_vec[i].deal_price; 176 | os.deal_quant = order_vec[i].deal_quant; 177 | os.desc = Base64::Encode(order_vec[i].desc); 178 | os.direction = order_vec[i].direction; 179 | os.price = order_vec[i].price; 180 | os.quant = order_vec[i].quant; 181 | os.sid = order_vec[i].sid; 182 | os.state = order_vec[i].state; 183 | os.time = order_vec[i].time; 184 | os.internal_order_id = order_vec[i].internal_order_id; 185 | os.order_id = order_vec[i].user_order_id; 186 | _return.result.push_back(os); 187 | } 188 | } 189 | } 190 | 191 | void GetOrderState(GetOrderStateResp& _return, const std::string& sessionID, const std::string& liveTradeID, const std::string& orderID) { 192 | 193 | std::map>::iterator iter = g_map_tradeid_broker.find(liveTradeID); 194 | if (iter != g_map_tradeid_broker.end()) { 195 | _return.ret_code = 0; 196 | order_state ost = iter->second->getOrderState(orderID); 197 | _return.result.operation = ost.operation; 198 | _return.result.deal_price = ost.deal_price; 199 | _return.result.deal_quant = ost.deal_quant; 200 | _return.result.desc = Base64::Encode(ost.desc); 201 | _return.result.direction = ost.direction; 202 | _return.result.price = ost.price; 203 | _return.result.quant = ost.quant; 204 | _return.result.sid = ost.sid; 205 | _return.result.state = ost.state; 206 | _return.result.time = ost.time; 207 | _return.result.order_id = ost.user_order_id; 208 | _return.result.internal_order_id = ost.internal_order_id; 209 | } 210 | } 211 | 212 | void CloseOrder(CloseOrderResp& _return, const std::string& sessionID, const std::string& liveTradeID, const std::string& orderID) { 213 | 214 | std::map>::iterator iter = g_map_tradeid_broker.find(liveTradeID); 215 | if (iter != g_map_tradeid_broker.end()) { 216 | _return.ret_code = 0; 217 | iter->second->closeOrder(orderID); 218 | } 219 | 220 | } 221 | 222 | void CancelOrder(CancelOrderResp& _return, const std::string& sessionID, const std::string& liveTradeID, const std::string& orderID) { 223 | std::map>::iterator iter = g_map_tradeid_broker.find(liveTradeID); 224 | if (iter != g_map_tradeid_broker.end()) { 225 | _return.ret_code = 0; 226 | iter->second->closeOrder(orderID); 227 | } 228 | } 229 | 230 | void LiveTradeBuyOpen(PlaceOrderResp& _return, const std::string& sessionID, const std::string& liveTradeID, const std::string& sid, const double price, const double quant, const std::string& orderType) 231 | { 232 | 233 | LicenseManager::get_instance()->set_session_id(sessionID); 234 | 235 | std::string msg; 236 | if (!LicenseManager::get_instance()->license_ok(_return.ret_code, msg)) { 237 | return; 238 | } 239 | 240 | std::map>::iterator iter = g_map_tradeid_broker.find(liveTradeID); 241 | if (iter != g_map_tradeid_broker.end()) { 242 | _return.ret_code = 0; 243 | std::string orderID = iter->second->buy(sid, quant, price, orderType); 244 | _return.result = orderID; 245 | 246 | if (orderID == "") { 247 | _return.ret_code = 10; 248 | } 249 | } 250 | 251 | } 252 | 253 | void LiveTradeSellClose(PlaceOrderResp& _return, const std::string& sessionID, const std::string& liveTradeID, const std::string& sid, const double price, const double quant, const std::string& orderType, const bool closeToday) 254 | { 255 | 256 | LicenseManager::get_instance()->set_session_id(sessionID); 257 | 258 | std::string msg; 259 | if (!LicenseManager::get_instance()->license_ok(_return.ret_code, msg)) { 260 | return; 261 | } 262 | 263 | std::map>::iterator iter = g_map_tradeid_broker.find(liveTradeID); 264 | if (iter != g_map_tradeid_broker.end()) { 265 | _return.ret_code = 0; 266 | std::string orderID = iter->second->sell(sid, quant, price, orderType); 267 | 268 | _return.result = orderID; 269 | if (orderID == "") { 270 | _return.ret_code = 10; 271 | } 272 | } 273 | } 274 | 275 | void LiveTradeSellOpen(PlaceOrderResp& _return, const std::string& sessionID, const std::string& liveTradeID, const std::string& sid, const double price, const double quant, const std::string& orderType) 276 | { 277 | 278 | } 279 | 280 | void LiveTradeBuyClose(PlaceOrderResp& _return, const std::string& sessionID, const std::string& liveTradeID, const std::string& sid, const double price, const double quant, const std::string& orderType, const bool closeToday) { 281 | 282 | } 283 | }; 284 | 285 | int main(int argc, char **argv) { 286 | 287 | //freopen(NULL_DEVICE, "w", stderr); 288 | 289 | int port = 58899; 290 | 291 | init_lib(); 292 | 293 | try { 294 | if (argc >= 3) { 295 | port = atoi(argv[2]); 296 | } 297 | boost::shared_ptr handler(new AlphaTradeHandler()); 298 | boost::shared_ptr processor(new AlphaTradeProcessor(handler)); 299 | boost::shared_ptr serverTransport(new TServerSocket(port)); 300 | boost::shared_ptr transportFactory(new TBufferedTransportFactory()); 301 | boost::shared_ptr protocolFactory(new TBinaryProtocolFactory()); 302 | 303 | TNonblockingServer server(processor, protocolFactory, port); 304 | 305 | LicenseManager::get_instance()->start(); 306 | 307 | std::cout << "AlphaQuant 1.1.8.3346 listen on port " << port << std::endl; 308 | 309 | //TSimpleServer server(processor, serverTransport, transportFactory, protocolFactory); 310 | server.serve(); 311 | } 312 | catch (std::exception& e) { 313 | std::cout << "except: " << e.what() << endl; 314 | } 315 | return 0; 316 | } 317 | 318 | -------------------------------------------------------------------------------- /stock_live_trade/defs.h: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | This file contains definitions used by the Hex-Rays decompiler output. 4 | It has type definitions and convenience macros to make the 5 | output more readable. 6 | 7 | Copyright (c) 2007-2015 Hex-Rays 8 | 9 | */ 10 | 11 | #ifndef HEXRAYS_DEFS_H 12 | #define HEXRAYS_DEFS_H 13 | 14 | #if defined(__GNUC__) 15 | typedef long long ll; 16 | typedef unsigned long long ull; 17 | #define __int64 long long 18 | #define __int32 int 19 | #define __int16 short 20 | #define __int8 char 21 | #define MAKELL(num) num ## LL 22 | #define FMT_64 "ll" 23 | #elif defined(_MSC_VER) 24 | typedef __int64 ll; 25 | typedef unsigned __int64 ull; 26 | #define MAKELL(num) num ## i64 27 | #define FMT_64 "I64" 28 | #elif defined (__BORLANDC__) 29 | typedef __int64 ll; 30 | typedef unsigned __int64 ull; 31 | #define MAKELL(num) num ## i64 32 | #define FMT_64 "L" 33 | #else 34 | #error "unknown compiler" 35 | #endif 36 | typedef unsigned int uint; 37 | typedef unsigned char uchar; 38 | typedef unsigned short ushort; 39 | typedef unsigned long ulong; 40 | 41 | typedef char int8; 42 | typedef signed char sint8; 43 | typedef unsigned char uint8; 44 | typedef short int16; 45 | typedef signed short sint16; 46 | typedef unsigned short uint16; 47 | typedef int int32; 48 | typedef signed int sint32; 49 | typedef unsigned int uint32; 50 | typedef ll int64; 51 | typedef ll sint64; 52 | typedef ull uint64; 53 | 54 | // Partially defined types. They are used when the decompiler does not know 55 | // anything about the type except its size. 56 | #define _BYTE uint8 57 | #define _WORD uint16 58 | #define _DWORD uint32 59 | #define _QWORD uint64 60 | #if !defined(_MSC_VER) 61 | #define _LONGLONG __int128 62 | #endif 63 | 64 | // Non-standard boolean types. They are used when the decompiler can not use 65 | // the standard "bool" type because of the size mistmatch but the possible 66 | // values are only 0 and 1. See also 'BOOL' type below. 67 | typedef int8 _BOOL1; 68 | typedef int16 _BOOL2; 69 | typedef int32 _BOOL4; 70 | 71 | #ifndef _WINDOWS_ 72 | typedef int8 BYTE; 73 | typedef int16 WORD; 74 | typedef int32 DWORD; 75 | typedef int32 LONG; 76 | typedef int BOOL; // uppercase BOOL is usually 4 bytes 77 | #endif 78 | typedef int64 QWORD; 79 | #ifndef __cplusplus 80 | typedef int bool; // we want to use bool in our C programs 81 | #endif 82 | 83 | #define __pure // pure function: always returns the same value, has no 84 | // side effects 85 | 86 | // Non-returning function 87 | #if defined(__GNUC__) 88 | #define __noreturn __attribute__((noreturn)) 89 | #else 90 | #define __noreturn __declspec(noreturn) 91 | #endif 92 | 93 | 94 | #ifndef NULL 95 | #define NULL 0 96 | #endif 97 | 98 | // Some convenience macros to make partial accesses nicer 99 | // first unsigned macros: 100 | #define LOBYTE(x) (*((_BYTE*)&(x))) // low byte 101 | #define LOWORD(x) (*((_WORD*)&(x))) // low word 102 | #define LODWORD(x) (*((_DWORD*)&(x))) // low dword 103 | #define HIBYTE(x) (*((_BYTE*)&(x)+1)) 104 | #define HIWORD(x) (*((_WORD*)&(x)+1)) 105 | #define HIDWORD(x) (*((_DWORD*)&(x)+1)) 106 | #define BYTEn(x, n) (*((_BYTE*)&(x)+n)) 107 | #define WORDn(x, n) (*((_WORD*)&(x)+n)) 108 | #define BYTE1(x) BYTEn(x, 1) // byte 1 (counting from 0) 109 | #define BYTE2(x) BYTEn(x, 2) 110 | #define BYTE3(x) BYTEn(x, 3) 111 | #define BYTE4(x) BYTEn(x, 4) 112 | #define BYTE5(x) BYTEn(x, 5) 113 | #define BYTE6(x) BYTEn(x, 6) 114 | #define BYTE7(x) BYTEn(x, 7) 115 | #define BYTE8(x) BYTEn(x, 8) 116 | #define BYTE9(x) BYTEn(x, 9) 117 | #define BYTE10(x) BYTEn(x, 10) 118 | #define BYTE11(x) BYTEn(x, 11) 119 | #define BYTE12(x) BYTEn(x, 12) 120 | #define BYTE13(x) BYTEn(x, 13) 121 | #define BYTE14(x) BYTEn(x, 14) 122 | #define BYTE15(x) BYTEn(x, 15) 123 | #define WORD1(x) WORDn(x, 1) 124 | #define WORD2(x) WORDn(x, 2) // third word of the object, unsigned 125 | #define WORD3(x) WORDn(x, 3) 126 | #define WORD4(x) WORDn(x, 4) 127 | #define WORD5(x) WORDn(x, 5) 128 | #define WORD6(x) WORDn(x, 6) 129 | #define WORD7(x) WORDn(x, 7) 130 | 131 | // now signed macros (the same but with sign extension) 132 | #define SLOBYTE(x) (*((int8*)&(x))) 133 | #define SLOWORD(x) (*((int16*)&(x))) 134 | #define SLODWORD(x) (*((int32*)&(x))) 135 | #define SHIBYTE(x) (*((int8*)&(x)+1)) 136 | #define SHIWORD(x) (*((int16*)&(x)+1)) 137 | #define SHIDWORD(x) (*((int32*)&(x)+1)) 138 | #define SBYTEn(x, n) (*((int8*)&(x)+n)) 139 | #define SWORDn(x, n) (*((int16*)&(x)+n)) 140 | #define SBYTE1(x) SBYTEn(x, 1) 141 | #define SBYTE2(x) SBYTEn(x, 2) 142 | #define SBYTE3(x) SBYTEn(x, 3) 143 | #define SBYTE4(x) SBYTEn(x, 4) 144 | #define SBYTE5(x) SBYTEn(x, 5) 145 | #define SBYTE6(x) SBYTEn(x, 6) 146 | #define SBYTE7(x) SBYTEn(x, 7) 147 | #define SBYTE8(x) SBYTEn(x, 8) 148 | #define SBYTE9(x) SBYTEn(x, 9) 149 | #define SBYTE10(x) SBYTEn(x, 10) 150 | #define SBYTE11(x) SBYTEn(x, 11) 151 | #define SBYTE12(x) SBYTEn(x, 12) 152 | #define SBYTE13(x) SBYTEn(x, 13) 153 | #define SBYTE14(x) SBYTEn(x, 14) 154 | #define SBYTE15(x) SBYTEn(x, 15) 155 | #define SWORD1(x) SWORDn(x, 1) 156 | #define SWORD2(x) SWORDn(x, 2) 157 | #define SWORD3(x) SWORDn(x, 3) 158 | #define SWORD4(x) SWORDn(x, 4) 159 | #define SWORD5(x) SWORDn(x, 5) 160 | #define SWORD6(x) SWORDn(x, 6) 161 | #define SWORD7(x) SWORDn(x, 7) 162 | 163 | 164 | // Helper functions to represent some assembly instructions. 165 | 166 | #ifdef __cplusplus 167 | 168 | // compile time assertion 169 | #define __CASSERT_N0__(l) COMPILE_TIME_ASSERT_ ## l 170 | #define __CASSERT_N1__(l) __CASSERT_N0__(l) 171 | #define CASSERT(cnd) typedef char __CASSERT_N1__(__LINE__) [(cnd) ? 1 : -1] 172 | 173 | // check that unsigned multiplication does not overflow 174 | template bool is_mul_ok(T count, T elsize) 175 | { 176 | CASSERT((T)(-1) > 0); // make sure T is unsigned 177 | if ( elsize == 0 || count == 0 ) 178 | return true; 179 | return count <= ((T)(-1)) / elsize; 180 | } 181 | 182 | // multiplication that saturates (yields the biggest value) instead of overflowing 183 | // such a construct is useful in "operator new[]" 184 | template bool saturated_mul(T count, T elsize) 185 | { 186 | return is_mul_ok(count, elsize) ? count * elsize : T(-1); 187 | } 188 | 189 | #include // for size_t 190 | 191 | // memcpy() with determined behavoir: it always copies 192 | // from the start to the end of the buffer 193 | // note: it copies byte by byte, so it is not equivalent to, for example, rep movsd 194 | inline void *qmemcpy(void *dst, const void *src, size_t cnt) 195 | { 196 | char *out = (char *)dst; 197 | const char *in = (const char *)src; 198 | while ( cnt > 0 ) 199 | { 200 | *out++ = *in++; 201 | --cnt; 202 | } 203 | return dst; 204 | } 205 | 206 | // Generate a reference to pair of operands 207 | template int16 __PAIR__( int8 high, T low) { return ((( int16)high) << sizeof(high)*8) | uint8(low); } 208 | template int32 __PAIR__( int16 high, T low) { return ((( int32)high) << sizeof(high)*8) | uint16(low); } 209 | template int64 __PAIR__( int32 high, T low) { return ((( int64)high) << sizeof(high)*8) | uint32(low); } 210 | template uint16 __PAIR__(uint8 high, T low) { return (((uint16)high) << sizeof(high)*8) | uint8(low); } 211 | template uint32 __PAIR__(uint16 high, T low) { return (((uint32)high) << sizeof(high)*8) | uint16(low); } 212 | template uint64 __PAIR__(uint32 high, T low) { return (((uint64)high) << sizeof(high)*8) | uint32(low); } 213 | 214 | // rotate left 215 | template T __ROL__(T value, int count) 216 | { 217 | const uint nbits = sizeof(T) * 8; 218 | 219 | if ( count > 0 ) 220 | { 221 | count %= nbits; 222 | T high = value >> (nbits - count); 223 | if ( T(-1) < 0 ) // signed value 224 | high &= ~((T(-1) << count)); 225 | value <<= count; 226 | value |= high; 227 | } 228 | else 229 | { 230 | count = -count % nbits; 231 | T low = value << (nbits - count); 232 | value >>= count; 233 | value |= low; 234 | } 235 | return value; 236 | } 237 | 238 | inline uint8 __ROL1__(uint8 value, int count) { return __ROL__((uint8)value, count); } 239 | inline uint16 __ROL2__(uint16 value, int count) { return __ROL__((uint16)value, count); } 240 | inline uint32 __ROL4__(uint32 value, int count) { return __ROL__((uint32)value, count); } 241 | inline uint64 __ROL8__(uint64 value, int count) { return __ROL__((uint64)value, count); } 242 | inline uint8 __ROR1__(uint8 value, int count) { return __ROL__((uint8)value, -count); } 243 | inline uint16 __ROR2__(uint16 value, int count) { return __ROL__((uint16)value, -count); } 244 | inline uint32 __ROR4__(uint32 value, int count) { return __ROL__((uint32)value, -count); } 245 | inline uint64 __ROR8__(uint64 value, int count) { return __ROL__((uint64)value, -count); } 246 | 247 | // carry flag of left shift 248 | template int8 __MKCSHL__(T value, uint count) 249 | { 250 | const uint nbits = sizeof(T) * 8; 251 | count %= nbits; 252 | 253 | return (value >> (nbits-count)) & 1; 254 | } 255 | 256 | // carry flag of right shift 257 | template int8 __MKCSHR__(T value, uint count) 258 | { 259 | return (value >> (count-1)) & 1; 260 | } 261 | 262 | // sign flag 263 | template int8 __SETS__(T x) 264 | { 265 | if ( sizeof(T) == 1 ) 266 | return int8(x) < 0; 267 | if ( sizeof(T) == 2 ) 268 | return int16(x) < 0; 269 | if ( sizeof(T) == 4 ) 270 | return int32(x) < 0; 271 | return int64(x) < 0; 272 | } 273 | 274 | // overflow flag of subtraction (x-y) 275 | template int8 __OFSUB__(T x, U y) 276 | { 277 | if ( sizeof(T) < sizeof(U) ) 278 | { 279 | U x2 = x; 280 | int8 sx = __SETS__(x2); 281 | return (sx ^ __SETS__(y)) & (sx ^ __SETS__(x2-y)); 282 | } 283 | else 284 | { 285 | T y2 = y; 286 | int8 sx = __SETS__(x); 287 | return (sx ^ __SETS__(y2)) & (sx ^ __SETS__(x-y2)); 288 | } 289 | } 290 | 291 | // overflow flag of addition (x+y) 292 | template int8 __OFADD__(T x, U y) 293 | { 294 | if ( sizeof(T) < sizeof(U) ) 295 | { 296 | U x2 = x; 297 | int8 sx = __SETS__(x2); 298 | return ((1 ^ sx) ^ __SETS__(y)) & (sx ^ __SETS__(x2+y)); 299 | } 300 | else 301 | { 302 | T y2 = y; 303 | int8 sx = __SETS__(x); 304 | return ((1 ^ sx) ^ __SETS__(y2)) & (sx ^ __SETS__(x+y2)); 305 | } 306 | } 307 | 308 | // carry flag of subtraction (x-y) 309 | template int8 __CFSUB__(T x, U y) 310 | { 311 | int size = sizeof(T) > sizeof(U) ? sizeof(T) : sizeof(U); 312 | if ( size == 1 ) 313 | return uint8(x) < uint8(y); 314 | if ( size == 2 ) 315 | return uint16(x) < uint16(y); 316 | if ( size == 4 ) 317 | return uint32(x) < uint32(y); 318 | return uint64(x) < uint64(y); 319 | } 320 | 321 | // carry flag of addition (x+y) 322 | template int8 __CFADD__(T x, U y) 323 | { 324 | int size = sizeof(T) > sizeof(U) ? sizeof(T) : sizeof(U); 325 | if ( size == 1 ) 326 | return uint8(x) > uint8(x+y); 327 | if ( size == 2 ) 328 | return uint16(x) > uint16(x+y); 329 | if ( size == 4 ) 330 | return uint32(x) > uint32(x+y); 331 | return uint64(x) > uint64(x+y); 332 | } 333 | 334 | #else 335 | // The following definition is not quite correct because it always returns 336 | // uint64. The above C++ functions are good, though. 337 | #define __PAIR__(high, low) (((uint64)(high)<>y) 343 | #define __CFADD__(x, y) invalid_operation // Generate carry flag for (x+y) 344 | #define __CFSUB__(x, y) invalid_operation // Generate carry flag for (x-y) 345 | #define __OFADD__(x, y) invalid_operation // Generate overflow flag for (x+y) 346 | #define __OFSUB__(x, y) invalid_operation // Generate overflow flag for (x-y) 347 | #endif 348 | 349 | // No definition for rcl/rcr because the carry flag is unknown 350 | #define __RCL__(x, y) invalid_operation // Rotate left thru carry 351 | #define __RCR__(x, y) invalid_operation // Rotate right thru carry 352 | #define __MKCRCL__(x, y) invalid_operation // Generate carry flag for a RCL 353 | #define __MKCRCR__(x, y) invalid_operation // Generate carry flag for a RCR 354 | #define __SETP__(x, y) invalid_operation // Generate parity flag for (x-y) 355 | 356 | // In the decompilation listing there are some objects declarared as _UNKNOWN 357 | // because we could not determine their types. Since the C compiler does not 358 | // accept void item declarations, we replace them by anything of our choice, 359 | // for example a char: 360 | 361 | #define _UNKNOWN char 362 | 363 | #ifdef _MSC_VER 364 | #define snprintf _snprintf 365 | #define vsnprintf _vsnprintf 366 | #endif 367 | 368 | #endif // HEXRAYS_DEFS_H 369 | -------------------------------------------------------------------------------- /stock_live_trade/AlphaConfig.h: -------------------------------------------------------------------------------- 1 | /** 2 | * Autogenerated by Thrift Compiler (0.10.0) 3 | * 4 | * DO NOT EDIT UNLESS YOU ARE SURE THAT YOU KNOW WHAT YOU ARE DOING 5 | * @generated 6 | */ 7 | #ifndef AlphaConfig_H 8 | #define AlphaConfig_H 9 | 10 | #include 11 | #include 12 | #include "alpha_config_types.h" 13 | 14 | 15 | 16 | #ifdef _WIN32 17 | #pragma warning( push ) 18 | #pragma warning (disable : 4250 ) //inheriting methods via dominance 19 | #endif 20 | 21 | class AlphaConfigIf { 22 | public: 23 | virtual ~AlphaConfigIf() {} 24 | virtual void GetConfig(ConfigData& _return, const std::string& str1, const std::string& str2) = 0; 25 | virtual void Report(const std::map & data) = 0; 26 | }; 27 | 28 | class AlphaConfigIfFactory { 29 | public: 30 | typedef AlphaConfigIf Handler; 31 | 32 | virtual ~AlphaConfigIfFactory() {} 33 | 34 | virtual AlphaConfigIf* getHandler(const ::apache::thrift::TConnectionInfo& connInfo) = 0; 35 | virtual void releaseHandler(AlphaConfigIf* /* handler */) = 0; 36 | }; 37 | 38 | class AlphaConfigIfSingletonFactory : virtual public AlphaConfigIfFactory { 39 | public: 40 | AlphaConfigIfSingletonFactory(const boost::shared_ptr& iface) : iface_(iface) {} 41 | virtual ~AlphaConfigIfSingletonFactory() {} 42 | 43 | virtual AlphaConfigIf* getHandler(const ::apache::thrift::TConnectionInfo&) { 44 | return iface_.get(); 45 | } 46 | virtual void releaseHandler(AlphaConfigIf* /* handler */) {} 47 | 48 | protected: 49 | boost::shared_ptr iface_; 50 | }; 51 | 52 | class AlphaConfigNull : virtual public AlphaConfigIf { 53 | public: 54 | virtual ~AlphaConfigNull() {} 55 | void GetConfig(ConfigData& /* _return */, const std::string& /* str1 */, const std::string& /* str2 */) { 56 | return; 57 | } 58 | void Report(const std::map & /* data */) { 59 | return; 60 | } 61 | }; 62 | 63 | typedef struct _AlphaConfig_GetConfig_args__isset { 64 | _AlphaConfig_GetConfig_args__isset() : str1(false), str2(false) {} 65 | bool str1 :1; 66 | bool str2 :1; 67 | } _AlphaConfig_GetConfig_args__isset; 68 | 69 | class AlphaConfig_GetConfig_args { 70 | public: 71 | 72 | AlphaConfig_GetConfig_args(const AlphaConfig_GetConfig_args&); 73 | AlphaConfig_GetConfig_args& operator=(const AlphaConfig_GetConfig_args&); 74 | AlphaConfig_GetConfig_args() : str1(), str2() { 75 | } 76 | 77 | virtual ~AlphaConfig_GetConfig_args() throw(); 78 | std::string str1; 79 | std::string str2; 80 | 81 | _AlphaConfig_GetConfig_args__isset __isset; 82 | 83 | void __set_str1(const std::string& val); 84 | 85 | void __set_str2(const std::string& val); 86 | 87 | bool operator == (const AlphaConfig_GetConfig_args & rhs) const 88 | { 89 | if (!(str1 == rhs.str1)) 90 | return false; 91 | if (!(str2 == rhs.str2)) 92 | return false; 93 | return true; 94 | } 95 | bool operator != (const AlphaConfig_GetConfig_args &rhs) const { 96 | return !(*this == rhs); 97 | } 98 | 99 | bool operator < (const AlphaConfig_GetConfig_args & ) const; 100 | 101 | uint32_t read(::apache::thrift::protocol::TProtocol* iprot); 102 | uint32_t write(::apache::thrift::protocol::TProtocol* oprot) const; 103 | 104 | }; 105 | 106 | 107 | class AlphaConfig_GetConfig_pargs { 108 | public: 109 | 110 | 111 | virtual ~AlphaConfig_GetConfig_pargs() throw(); 112 | const std::string* str1; 113 | const std::string* str2; 114 | 115 | uint32_t write(::apache::thrift::protocol::TProtocol* oprot) const; 116 | 117 | }; 118 | 119 | typedef struct _AlphaConfig_GetConfig_result__isset { 120 | _AlphaConfig_GetConfig_result__isset() : success(false) {} 121 | bool success :1; 122 | } _AlphaConfig_GetConfig_result__isset; 123 | 124 | class AlphaConfig_GetConfig_result { 125 | public: 126 | 127 | AlphaConfig_GetConfig_result(const AlphaConfig_GetConfig_result&); 128 | AlphaConfig_GetConfig_result& operator=(const AlphaConfig_GetConfig_result&); 129 | AlphaConfig_GetConfig_result() { 130 | } 131 | 132 | virtual ~AlphaConfig_GetConfig_result() throw(); 133 | ConfigData success; 134 | 135 | _AlphaConfig_GetConfig_result__isset __isset; 136 | 137 | void __set_success(const ConfigData& val); 138 | 139 | bool operator == (const AlphaConfig_GetConfig_result & rhs) const 140 | { 141 | if (!(success == rhs.success)) 142 | return false; 143 | return true; 144 | } 145 | bool operator != (const AlphaConfig_GetConfig_result &rhs) const { 146 | return !(*this == rhs); 147 | } 148 | 149 | bool operator < (const AlphaConfig_GetConfig_result & ) const; 150 | 151 | uint32_t read(::apache::thrift::protocol::TProtocol* iprot); 152 | uint32_t write(::apache::thrift::protocol::TProtocol* oprot) const; 153 | 154 | }; 155 | 156 | typedef struct _AlphaConfig_GetConfig_presult__isset { 157 | _AlphaConfig_GetConfig_presult__isset() : success(false) {} 158 | bool success :1; 159 | } _AlphaConfig_GetConfig_presult__isset; 160 | 161 | class AlphaConfig_GetConfig_presult { 162 | public: 163 | 164 | 165 | virtual ~AlphaConfig_GetConfig_presult() throw(); 166 | ConfigData* success; 167 | 168 | _AlphaConfig_GetConfig_presult__isset __isset; 169 | 170 | uint32_t read(::apache::thrift::protocol::TProtocol* iprot); 171 | 172 | }; 173 | 174 | typedef struct _AlphaConfig_Report_args__isset { 175 | _AlphaConfig_Report_args__isset() : data(false) {} 176 | bool data :1; 177 | } _AlphaConfig_Report_args__isset; 178 | 179 | class AlphaConfig_Report_args { 180 | public: 181 | 182 | AlphaConfig_Report_args(const AlphaConfig_Report_args&); 183 | AlphaConfig_Report_args& operator=(const AlphaConfig_Report_args&); 184 | AlphaConfig_Report_args() { 185 | } 186 | 187 | virtual ~AlphaConfig_Report_args() throw(); 188 | std::map data; 189 | 190 | _AlphaConfig_Report_args__isset __isset; 191 | 192 | void __set_data(const std::map & val); 193 | 194 | bool operator == (const AlphaConfig_Report_args & rhs) const 195 | { 196 | if (!(data == rhs.data)) 197 | return false; 198 | return true; 199 | } 200 | bool operator != (const AlphaConfig_Report_args &rhs) const { 201 | return !(*this == rhs); 202 | } 203 | 204 | bool operator < (const AlphaConfig_Report_args & ) const; 205 | 206 | uint32_t read(::apache::thrift::protocol::TProtocol* iprot); 207 | uint32_t write(::apache::thrift::protocol::TProtocol* oprot) const; 208 | 209 | }; 210 | 211 | 212 | class AlphaConfig_Report_pargs { 213 | public: 214 | 215 | 216 | virtual ~AlphaConfig_Report_pargs() throw(); 217 | const std::map * data; 218 | 219 | uint32_t write(::apache::thrift::protocol::TProtocol* oprot) const; 220 | 221 | }; 222 | 223 | 224 | class AlphaConfig_Report_result { 225 | public: 226 | 227 | AlphaConfig_Report_result(const AlphaConfig_Report_result&); 228 | AlphaConfig_Report_result& operator=(const AlphaConfig_Report_result&); 229 | AlphaConfig_Report_result() { 230 | } 231 | 232 | virtual ~AlphaConfig_Report_result() throw(); 233 | 234 | bool operator == (const AlphaConfig_Report_result & /* rhs */) const 235 | { 236 | return true; 237 | } 238 | bool operator != (const AlphaConfig_Report_result &rhs) const { 239 | return !(*this == rhs); 240 | } 241 | 242 | bool operator < (const AlphaConfig_Report_result & ) const; 243 | 244 | uint32_t read(::apache::thrift::protocol::TProtocol* iprot); 245 | uint32_t write(::apache::thrift::protocol::TProtocol* oprot) const; 246 | 247 | }; 248 | 249 | 250 | class AlphaConfig_Report_presult { 251 | public: 252 | 253 | 254 | virtual ~AlphaConfig_Report_presult() throw(); 255 | 256 | uint32_t read(::apache::thrift::protocol::TProtocol* iprot); 257 | 258 | }; 259 | 260 | class AlphaConfigClient : virtual public AlphaConfigIf { 261 | public: 262 | AlphaConfigClient(boost::shared_ptr< ::apache::thrift::protocol::TProtocol> prot) { 263 | setProtocol(prot); 264 | } 265 | AlphaConfigClient(boost::shared_ptr< ::apache::thrift::protocol::TProtocol> iprot, boost::shared_ptr< ::apache::thrift::protocol::TProtocol> oprot) { 266 | setProtocol(iprot,oprot); 267 | } 268 | private: 269 | void setProtocol(boost::shared_ptr< ::apache::thrift::protocol::TProtocol> prot) { 270 | setProtocol(prot,prot); 271 | } 272 | void setProtocol(boost::shared_ptr< ::apache::thrift::protocol::TProtocol> iprot, boost::shared_ptr< ::apache::thrift::protocol::TProtocol> oprot) { 273 | piprot_=iprot; 274 | poprot_=oprot; 275 | iprot_ = iprot.get(); 276 | oprot_ = oprot.get(); 277 | } 278 | public: 279 | boost::shared_ptr< ::apache::thrift::protocol::TProtocol> getInputProtocol() { 280 | return piprot_; 281 | } 282 | boost::shared_ptr< ::apache::thrift::protocol::TProtocol> getOutputProtocol() { 283 | return poprot_; 284 | } 285 | void GetConfig(ConfigData& _return, const std::string& str1, const std::string& str2); 286 | void send_GetConfig(const std::string& str1, const std::string& str2); 287 | void recv_GetConfig(ConfigData& _return); 288 | void Report(const std::map & data); 289 | void send_Report(const std::map & data); 290 | void recv_Report(); 291 | protected: 292 | boost::shared_ptr< ::apache::thrift::protocol::TProtocol> piprot_; 293 | boost::shared_ptr< ::apache::thrift::protocol::TProtocol> poprot_; 294 | ::apache::thrift::protocol::TProtocol* iprot_; 295 | ::apache::thrift::protocol::TProtocol* oprot_; 296 | }; 297 | 298 | class AlphaConfigProcessor : public ::apache::thrift::TDispatchProcessor { 299 | protected: 300 | boost::shared_ptr iface_; 301 | virtual bool dispatchCall(::apache::thrift::protocol::TProtocol* iprot, ::apache::thrift::protocol::TProtocol* oprot, const std::string& fname, int32_t seqid, void* callContext); 302 | private: 303 | typedef void (AlphaConfigProcessor::*ProcessFunction)(int32_t, ::apache::thrift::protocol::TProtocol*, ::apache::thrift::protocol::TProtocol*, void*); 304 | typedef std::map ProcessMap; 305 | ProcessMap processMap_; 306 | void process_GetConfig(int32_t seqid, ::apache::thrift::protocol::TProtocol* iprot, ::apache::thrift::protocol::TProtocol* oprot, void* callContext); 307 | void process_Report(int32_t seqid, ::apache::thrift::protocol::TProtocol* iprot, ::apache::thrift::protocol::TProtocol* oprot, void* callContext); 308 | public: 309 | AlphaConfigProcessor(boost::shared_ptr iface) : 310 | iface_(iface) { 311 | processMap_["GetConfig"] = &AlphaConfigProcessor::process_GetConfig; 312 | processMap_["Report"] = &AlphaConfigProcessor::process_Report; 313 | } 314 | 315 | virtual ~AlphaConfigProcessor() {} 316 | }; 317 | 318 | class AlphaConfigProcessorFactory : public ::apache::thrift::TProcessorFactory { 319 | public: 320 | AlphaConfigProcessorFactory(const ::boost::shared_ptr< AlphaConfigIfFactory >& handlerFactory) : 321 | handlerFactory_(handlerFactory) {} 322 | 323 | ::boost::shared_ptr< ::apache::thrift::TProcessor > getProcessor(const ::apache::thrift::TConnectionInfo& connInfo); 324 | 325 | protected: 326 | ::boost::shared_ptr< AlphaConfigIfFactory > handlerFactory_; 327 | }; 328 | 329 | class AlphaConfigMultiface : virtual public AlphaConfigIf { 330 | public: 331 | AlphaConfigMultiface(std::vector >& ifaces) : ifaces_(ifaces) { 332 | } 333 | virtual ~AlphaConfigMultiface() {} 334 | protected: 335 | std::vector > ifaces_; 336 | AlphaConfigMultiface() {} 337 | void add(boost::shared_ptr iface) { 338 | ifaces_.push_back(iface); 339 | } 340 | public: 341 | void GetConfig(ConfigData& _return, const std::string& str1, const std::string& str2) { 342 | size_t sz = ifaces_.size(); 343 | size_t i = 0; 344 | for (; i < (sz - 1); ++i) { 345 | ifaces_[i]->GetConfig(_return, str1, str2); 346 | } 347 | ifaces_[i]->GetConfig(_return, str1, str2); 348 | return; 349 | } 350 | 351 | void Report(const std::map & data) { 352 | size_t sz = ifaces_.size(); 353 | size_t i = 0; 354 | for (; i < (sz - 1); ++i) { 355 | ifaces_[i]->Report(data); 356 | } 357 | ifaces_[i]->Report(data); 358 | } 359 | 360 | }; 361 | 362 | // The 'concurrent' client is a thread safe client that correctly handles 363 | // out of order responses. It is slower than the regular client, so should 364 | // only be used when you need to share a connection among multiple threads 365 | class AlphaConfigConcurrentClient : virtual public AlphaConfigIf { 366 | public: 367 | AlphaConfigConcurrentClient(boost::shared_ptr< ::apache::thrift::protocol::TProtocol> prot) { 368 | setProtocol(prot); 369 | } 370 | AlphaConfigConcurrentClient(boost::shared_ptr< ::apache::thrift::protocol::TProtocol> iprot, boost::shared_ptr< ::apache::thrift::protocol::TProtocol> oprot) { 371 | setProtocol(iprot,oprot); 372 | } 373 | private: 374 | void setProtocol(boost::shared_ptr< ::apache::thrift::protocol::TProtocol> prot) { 375 | setProtocol(prot,prot); 376 | } 377 | void setProtocol(boost::shared_ptr< ::apache::thrift::protocol::TProtocol> iprot, boost::shared_ptr< ::apache::thrift::protocol::TProtocol> oprot) { 378 | piprot_=iprot; 379 | poprot_=oprot; 380 | iprot_ = iprot.get(); 381 | oprot_ = oprot.get(); 382 | } 383 | public: 384 | boost::shared_ptr< ::apache::thrift::protocol::TProtocol> getInputProtocol() { 385 | return piprot_; 386 | } 387 | boost::shared_ptr< ::apache::thrift::protocol::TProtocol> getOutputProtocol() { 388 | return poprot_; 389 | } 390 | void GetConfig(ConfigData& _return, const std::string& str1, const std::string& str2); 391 | int32_t send_GetConfig(const std::string& str1, const std::string& str2); 392 | void recv_GetConfig(ConfigData& _return, const int32_t seqid); 393 | void Report(const std::map & data); 394 | int32_t send_Report(const std::map & data); 395 | void recv_Report(const int32_t seqid); 396 | protected: 397 | boost::shared_ptr< ::apache::thrift::protocol::TProtocol> piprot_; 398 | boost::shared_ptr< ::apache::thrift::protocol::TProtocol> poprot_; 399 | ::apache::thrift::protocol::TProtocol* iprot_; 400 | ::apache::thrift::protocol::TProtocol* oprot_; 401 | ::apache::thrift::async::TConcurrentClientSyncInfo sync_; 402 | }; 403 | 404 | #ifdef _WIN32 405 | #pragma warning( pop ) 406 | #endif 407 | 408 | 409 | 410 | #endif 411 | -------------------------------------------------------------------------------- /stock_live_trade/alpha_trade_types.h: -------------------------------------------------------------------------------- 1 | /** 2 | * Autogenerated by Thrift Compiler (0.10.0) 3 | * 4 | * DO NOT EDIT UNLESS YOU ARE SURE THAT YOU KNOW WHAT YOU ARE DOING 5 | * @generated 6 | */ 7 | #ifndef alpha_trade_TYPES_H 8 | #define alpha_trade_TYPES_H 9 | 10 | #include 11 | 12 | #include 13 | #include 14 | #include 15 | #include 16 | #include 17 | 18 | #include 19 | 20 | 21 | 22 | 23 | class HoldingStock; 24 | 25 | class AccountBalance; 26 | 27 | class OrderState; 28 | 29 | class LiveTradeLoginResp; 30 | 31 | class LiveTradeLogoutResp; 32 | 33 | class GetAccountStateResp; 34 | 35 | class GetAccountBalanceResp; 36 | 37 | class GetHoldingStockResp; 38 | 39 | class GetAllOrderResp; 40 | 41 | class GetOrderStateResp; 42 | 43 | class CloseOrderResp; 44 | 45 | class CancelOrderResp; 46 | 47 | class PlaceOrderResp; 48 | 49 | typedef struct _HoldingStock__isset { 50 | _HoldingStock__isset() : stock_id(false), buy_price(false), long_short(false), quant(false), quant_sellable(false) {} 51 | bool stock_id :1; 52 | bool buy_price :1; 53 | bool long_short :1; 54 | bool quant :1; 55 | bool quant_sellable :1; 56 | } _HoldingStock__isset; 57 | 58 | class HoldingStock : public virtual ::apache::thrift::TBase { 59 | public: 60 | 61 | HoldingStock(const HoldingStock&); 62 | HoldingStock& operator=(const HoldingStock&); 63 | HoldingStock() : stock_id(), buy_price(0), long_short(), quant(0), quant_sellable(0) { 64 | } 65 | 66 | virtual ~HoldingStock() throw(); 67 | std::string stock_id; 68 | double buy_price; 69 | std::string long_short; 70 | double quant; 71 | double quant_sellable; 72 | 73 | _HoldingStock__isset __isset; 74 | 75 | void __set_stock_id(const std::string& val); 76 | 77 | void __set_buy_price(const double val); 78 | 79 | void __set_long_short(const std::string& val); 80 | 81 | void __set_quant(const double val); 82 | 83 | void __set_quant_sellable(const double val); 84 | 85 | bool operator == (const HoldingStock & rhs) const 86 | { 87 | if (!(stock_id == rhs.stock_id)) 88 | return false; 89 | if (!(buy_price == rhs.buy_price)) 90 | return false; 91 | if (!(long_short == rhs.long_short)) 92 | return false; 93 | if (!(quant == rhs.quant)) 94 | return false; 95 | if (!(quant_sellable == rhs.quant_sellable)) 96 | return false; 97 | return true; 98 | } 99 | bool operator != (const HoldingStock &rhs) const { 100 | return !(*this == rhs); 101 | } 102 | 103 | bool operator < (const HoldingStock & ) const; 104 | 105 | uint32_t read(::apache::thrift::protocol::TProtocol* iprot); 106 | uint32_t write(::apache::thrift::protocol::TProtocol* oprot) const; 107 | 108 | virtual void printTo(std::ostream& out) const; 109 | }; 110 | 111 | void swap(HoldingStock &a, HoldingStock &b); 112 | 113 | inline std::ostream& operator<<(std::ostream& out, const HoldingStock& obj) 114 | { 115 | obj.printTo(out); 116 | return out; 117 | } 118 | 119 | typedef struct _AccountBalance__isset { 120 | _AccountBalance__isset() : total_value(false), money_left(false) {} 121 | bool total_value :1; 122 | bool money_left :1; 123 | } _AccountBalance__isset; 124 | 125 | class AccountBalance : public virtual ::apache::thrift::TBase { 126 | public: 127 | 128 | AccountBalance(const AccountBalance&); 129 | AccountBalance& operator=(const AccountBalance&); 130 | AccountBalance() : total_value(0), money_left(0) { 131 | } 132 | 133 | virtual ~AccountBalance() throw(); 134 | double total_value; 135 | double money_left; 136 | 137 | _AccountBalance__isset __isset; 138 | 139 | void __set_total_value(const double val); 140 | 141 | void __set_money_left(const double val); 142 | 143 | bool operator == (const AccountBalance & rhs) const 144 | { 145 | if (!(total_value == rhs.total_value)) 146 | return false; 147 | if (!(money_left == rhs.money_left)) 148 | return false; 149 | return true; 150 | } 151 | bool operator != (const AccountBalance &rhs) const { 152 | return !(*this == rhs); 153 | } 154 | 155 | bool operator < (const AccountBalance & ) const; 156 | 157 | uint32_t read(::apache::thrift::protocol::TProtocol* iprot); 158 | uint32_t write(::apache::thrift::protocol::TProtocol* oprot) const; 159 | 160 | virtual void printTo(std::ostream& out) const; 161 | }; 162 | 163 | void swap(AccountBalance &a, AccountBalance &b); 164 | 165 | inline std::ostream& operator<<(std::ostream& out, const AccountBalance& obj) 166 | { 167 | obj.printTo(out); 168 | return out; 169 | } 170 | 171 | typedef struct _OrderState__isset { 172 | _OrderState__isset() : order_id(false), time(false), sid(false), internal_order_id(false), operation(false), direction(false), price(false), quant(false), deal_quant(false), deal_price(false), state(false), desc(false) {} 173 | bool order_id :1; 174 | bool time :1; 175 | bool sid :1; 176 | bool internal_order_id :1; 177 | bool operation :1; 178 | bool direction :1; 179 | bool price :1; 180 | bool quant :1; 181 | bool deal_quant :1; 182 | bool deal_price :1; 183 | bool state :1; 184 | bool desc :1; 185 | } _OrderState__isset; 186 | 187 | class OrderState : public virtual ::apache::thrift::TBase { 188 | public: 189 | 190 | OrderState(const OrderState&); 191 | OrderState& operator=(const OrderState&); 192 | OrderState() : order_id(), time(), sid(), internal_order_id(), operation(), direction(), price(0), quant(0), deal_quant(0), deal_price(0), state(), desc() { 193 | } 194 | 195 | virtual ~OrderState() throw(); 196 | std::string order_id; 197 | std::string time; 198 | std::string sid; 199 | std::string internal_order_id; 200 | std::string operation; 201 | std::string direction; 202 | double price; 203 | double quant; 204 | double deal_quant; 205 | double deal_price; 206 | std::string state; 207 | std::string desc; 208 | 209 | _OrderState__isset __isset; 210 | 211 | void __set_order_id(const std::string& val); 212 | 213 | void __set_time(const std::string& val); 214 | 215 | void __set_sid(const std::string& val); 216 | 217 | void __set_internal_order_id(const std::string& val); 218 | 219 | void __set_operation(const std::string& val); 220 | 221 | void __set_direction(const std::string& val); 222 | 223 | void __set_price(const double val); 224 | 225 | void __set_quant(const double val); 226 | 227 | void __set_deal_quant(const double val); 228 | 229 | void __set_deal_price(const double val); 230 | 231 | void __set_state(const std::string& val); 232 | 233 | void __set_desc(const std::string& val); 234 | 235 | bool operator == (const OrderState & rhs) const 236 | { 237 | if (!(order_id == rhs.order_id)) 238 | return false; 239 | if (!(time == rhs.time)) 240 | return false; 241 | if (!(sid == rhs.sid)) 242 | return false; 243 | if (!(internal_order_id == rhs.internal_order_id)) 244 | return false; 245 | if (!(operation == rhs.operation)) 246 | return false; 247 | if (!(direction == rhs.direction)) 248 | return false; 249 | if (!(price == rhs.price)) 250 | return false; 251 | if (!(quant == rhs.quant)) 252 | return false; 253 | if (!(deal_quant == rhs.deal_quant)) 254 | return false; 255 | if (!(deal_price == rhs.deal_price)) 256 | return false; 257 | if (!(state == rhs.state)) 258 | return false; 259 | if (!(desc == rhs.desc)) 260 | return false; 261 | return true; 262 | } 263 | bool operator != (const OrderState &rhs) const { 264 | return !(*this == rhs); 265 | } 266 | 267 | bool operator < (const OrderState & ) const; 268 | 269 | uint32_t read(::apache::thrift::protocol::TProtocol* iprot); 270 | uint32_t write(::apache::thrift::protocol::TProtocol* oprot) const; 271 | 272 | virtual void printTo(std::ostream& out) const; 273 | }; 274 | 275 | void swap(OrderState &a, OrderState &b); 276 | 277 | inline std::ostream& operator<<(std::ostream& out, const OrderState& obj) 278 | { 279 | obj.printTo(out); 280 | return out; 281 | } 282 | 283 | typedef struct _LiveTradeLoginResp__isset { 284 | _LiveTradeLoginResp__isset() : ret_code(false), result(false) {} 285 | bool ret_code :1; 286 | bool result :1; 287 | } _LiveTradeLoginResp__isset; 288 | 289 | class LiveTradeLoginResp : public virtual ::apache::thrift::TBase { 290 | public: 291 | 292 | LiveTradeLoginResp(const LiveTradeLoginResp&); 293 | LiveTradeLoginResp& operator=(const LiveTradeLoginResp&); 294 | LiveTradeLoginResp() : ret_code(0), result() { 295 | } 296 | 297 | virtual ~LiveTradeLoginResp() throw(); 298 | int32_t ret_code; 299 | std::string result; 300 | 301 | _LiveTradeLoginResp__isset __isset; 302 | 303 | void __set_ret_code(const int32_t val); 304 | 305 | void __set_result(const std::string& val); 306 | 307 | bool operator == (const LiveTradeLoginResp & rhs) const 308 | { 309 | if (!(ret_code == rhs.ret_code)) 310 | return false; 311 | if (!(result == rhs.result)) 312 | return false; 313 | return true; 314 | } 315 | bool operator != (const LiveTradeLoginResp &rhs) const { 316 | return !(*this == rhs); 317 | } 318 | 319 | bool operator < (const LiveTradeLoginResp & ) const; 320 | 321 | uint32_t read(::apache::thrift::protocol::TProtocol* iprot); 322 | uint32_t write(::apache::thrift::protocol::TProtocol* oprot) const; 323 | 324 | virtual void printTo(std::ostream& out) const; 325 | }; 326 | 327 | void swap(LiveTradeLoginResp &a, LiveTradeLoginResp &b); 328 | 329 | inline std::ostream& operator<<(std::ostream& out, const LiveTradeLoginResp& obj) 330 | { 331 | obj.printTo(out); 332 | return out; 333 | } 334 | 335 | typedef struct _LiveTradeLogoutResp__isset { 336 | _LiveTradeLogoutResp__isset() : ret_code(false) {} 337 | bool ret_code :1; 338 | } _LiveTradeLogoutResp__isset; 339 | 340 | class LiveTradeLogoutResp : public virtual ::apache::thrift::TBase { 341 | public: 342 | 343 | LiveTradeLogoutResp(const LiveTradeLogoutResp&); 344 | LiveTradeLogoutResp& operator=(const LiveTradeLogoutResp&); 345 | LiveTradeLogoutResp() : ret_code(0) { 346 | } 347 | 348 | virtual ~LiveTradeLogoutResp() throw(); 349 | int32_t ret_code; 350 | 351 | _LiveTradeLogoutResp__isset __isset; 352 | 353 | void __set_ret_code(const int32_t val); 354 | 355 | bool operator == (const LiveTradeLogoutResp & rhs) const 356 | { 357 | if (!(ret_code == rhs.ret_code)) 358 | return false; 359 | return true; 360 | } 361 | bool operator != (const LiveTradeLogoutResp &rhs) const { 362 | return !(*this == rhs); 363 | } 364 | 365 | bool operator < (const LiveTradeLogoutResp & ) const; 366 | 367 | uint32_t read(::apache::thrift::protocol::TProtocol* iprot); 368 | uint32_t write(::apache::thrift::protocol::TProtocol* oprot) const; 369 | 370 | virtual void printTo(std::ostream& out) const; 371 | }; 372 | 373 | void swap(LiveTradeLogoutResp &a, LiveTradeLogoutResp &b); 374 | 375 | inline std::ostream& operator<<(std::ostream& out, const LiveTradeLogoutResp& obj) 376 | { 377 | obj.printTo(out); 378 | return out; 379 | } 380 | 381 | typedef struct _GetAccountStateResp__isset { 382 | _GetAccountStateResp__isset() : ret_code(false), state(false), desc(false) {} 383 | bool ret_code :1; 384 | bool state :1; 385 | bool desc :1; 386 | } _GetAccountStateResp__isset; 387 | 388 | class GetAccountStateResp : public virtual ::apache::thrift::TBase { 389 | public: 390 | 391 | GetAccountStateResp(const GetAccountStateResp&); 392 | GetAccountStateResp& operator=(const GetAccountStateResp&); 393 | GetAccountStateResp() : ret_code(0), state(), desc() { 394 | } 395 | 396 | virtual ~GetAccountStateResp() throw(); 397 | int32_t ret_code; 398 | std::string state; 399 | std::string desc; 400 | 401 | _GetAccountStateResp__isset __isset; 402 | 403 | void __set_ret_code(const int32_t val); 404 | 405 | void __set_state(const std::string& val); 406 | 407 | void __set_desc(const std::string& val); 408 | 409 | bool operator == (const GetAccountStateResp & rhs) const 410 | { 411 | if (!(ret_code == rhs.ret_code)) 412 | return false; 413 | if (!(state == rhs.state)) 414 | return false; 415 | if (!(desc == rhs.desc)) 416 | return false; 417 | return true; 418 | } 419 | bool operator != (const GetAccountStateResp &rhs) const { 420 | return !(*this == rhs); 421 | } 422 | 423 | bool operator < (const GetAccountStateResp & ) const; 424 | 425 | uint32_t read(::apache::thrift::protocol::TProtocol* iprot); 426 | uint32_t write(::apache::thrift::protocol::TProtocol* oprot) const; 427 | 428 | virtual void printTo(std::ostream& out) const; 429 | }; 430 | 431 | void swap(GetAccountStateResp &a, GetAccountStateResp &b); 432 | 433 | inline std::ostream& operator<<(std::ostream& out, const GetAccountStateResp& obj) 434 | { 435 | obj.printTo(out); 436 | return out; 437 | } 438 | 439 | typedef struct _GetAccountBalanceResp__isset { 440 | _GetAccountBalanceResp__isset() : ret_code(false), result(false) {} 441 | bool ret_code :1; 442 | bool result :1; 443 | } _GetAccountBalanceResp__isset; 444 | 445 | class GetAccountBalanceResp : public virtual ::apache::thrift::TBase { 446 | public: 447 | 448 | GetAccountBalanceResp(const GetAccountBalanceResp&); 449 | GetAccountBalanceResp& operator=(const GetAccountBalanceResp&); 450 | GetAccountBalanceResp() : ret_code(0) { 451 | } 452 | 453 | virtual ~GetAccountBalanceResp() throw(); 454 | int32_t ret_code; 455 | AccountBalance result; 456 | 457 | _GetAccountBalanceResp__isset __isset; 458 | 459 | void __set_ret_code(const int32_t val); 460 | 461 | void __set_result(const AccountBalance& val); 462 | 463 | bool operator == (const GetAccountBalanceResp & rhs) const 464 | { 465 | if (!(ret_code == rhs.ret_code)) 466 | return false; 467 | if (!(result == rhs.result)) 468 | return false; 469 | return true; 470 | } 471 | bool operator != (const GetAccountBalanceResp &rhs) const { 472 | return !(*this == rhs); 473 | } 474 | 475 | bool operator < (const GetAccountBalanceResp & ) const; 476 | 477 | uint32_t read(::apache::thrift::protocol::TProtocol* iprot); 478 | uint32_t write(::apache::thrift::protocol::TProtocol* oprot) const; 479 | 480 | virtual void printTo(std::ostream& out) const; 481 | }; 482 | 483 | void swap(GetAccountBalanceResp &a, GetAccountBalanceResp &b); 484 | 485 | inline std::ostream& operator<<(std::ostream& out, const GetAccountBalanceResp& obj) 486 | { 487 | obj.printTo(out); 488 | return out; 489 | } 490 | 491 | typedef struct _GetHoldingStockResp__isset { 492 | _GetHoldingStockResp__isset() : ret_code(false), result(false) {} 493 | bool ret_code :1; 494 | bool result :1; 495 | } _GetHoldingStockResp__isset; 496 | 497 | class GetHoldingStockResp : public virtual ::apache::thrift::TBase { 498 | public: 499 | 500 | GetHoldingStockResp(const GetHoldingStockResp&); 501 | GetHoldingStockResp& operator=(const GetHoldingStockResp&); 502 | GetHoldingStockResp() : ret_code(0) { 503 | } 504 | 505 | virtual ~GetHoldingStockResp() throw(); 506 | int32_t ret_code; 507 | std::vector result; 508 | 509 | _GetHoldingStockResp__isset __isset; 510 | 511 | void __set_ret_code(const int32_t val); 512 | 513 | void __set_result(const std::vector & val); 514 | 515 | bool operator == (const GetHoldingStockResp & rhs) const 516 | { 517 | if (!(ret_code == rhs.ret_code)) 518 | return false; 519 | if (!(result == rhs.result)) 520 | return false; 521 | return true; 522 | } 523 | bool operator != (const GetHoldingStockResp &rhs) const { 524 | return !(*this == rhs); 525 | } 526 | 527 | bool operator < (const GetHoldingStockResp & ) const; 528 | 529 | uint32_t read(::apache::thrift::protocol::TProtocol* iprot); 530 | uint32_t write(::apache::thrift::protocol::TProtocol* oprot) const; 531 | 532 | virtual void printTo(std::ostream& out) const; 533 | }; 534 | 535 | void swap(GetHoldingStockResp &a, GetHoldingStockResp &b); 536 | 537 | inline std::ostream& operator<<(std::ostream& out, const GetHoldingStockResp& obj) 538 | { 539 | obj.printTo(out); 540 | return out; 541 | } 542 | 543 | typedef struct _GetAllOrderResp__isset { 544 | _GetAllOrderResp__isset() : ret_code(false), result(false) {} 545 | bool ret_code :1; 546 | bool result :1; 547 | } _GetAllOrderResp__isset; 548 | 549 | class GetAllOrderResp : public virtual ::apache::thrift::TBase { 550 | public: 551 | 552 | GetAllOrderResp(const GetAllOrderResp&); 553 | GetAllOrderResp& operator=(const GetAllOrderResp&); 554 | GetAllOrderResp() : ret_code(0) { 555 | } 556 | 557 | virtual ~GetAllOrderResp() throw(); 558 | int32_t ret_code; 559 | std::vector result; 560 | 561 | _GetAllOrderResp__isset __isset; 562 | 563 | void __set_ret_code(const int32_t val); 564 | 565 | void __set_result(const std::vector & val); 566 | 567 | bool operator == (const GetAllOrderResp & rhs) const 568 | { 569 | if (!(ret_code == rhs.ret_code)) 570 | return false; 571 | if (!(result == rhs.result)) 572 | return false; 573 | return true; 574 | } 575 | bool operator != (const GetAllOrderResp &rhs) const { 576 | return !(*this == rhs); 577 | } 578 | 579 | bool operator < (const GetAllOrderResp & ) const; 580 | 581 | uint32_t read(::apache::thrift::protocol::TProtocol* iprot); 582 | uint32_t write(::apache::thrift::protocol::TProtocol* oprot) const; 583 | 584 | virtual void printTo(std::ostream& out) const; 585 | }; 586 | 587 | void swap(GetAllOrderResp &a, GetAllOrderResp &b); 588 | 589 | inline std::ostream& operator<<(std::ostream& out, const GetAllOrderResp& obj) 590 | { 591 | obj.printTo(out); 592 | return out; 593 | } 594 | 595 | typedef struct _GetOrderStateResp__isset { 596 | _GetOrderStateResp__isset() : ret_code(false), result(false) {} 597 | bool ret_code :1; 598 | bool result :1; 599 | } _GetOrderStateResp__isset; 600 | 601 | class GetOrderStateResp : public virtual ::apache::thrift::TBase { 602 | public: 603 | 604 | GetOrderStateResp(const GetOrderStateResp&); 605 | GetOrderStateResp& operator=(const GetOrderStateResp&); 606 | GetOrderStateResp() : ret_code(0) { 607 | } 608 | 609 | virtual ~GetOrderStateResp() throw(); 610 | int32_t ret_code; 611 | OrderState result; 612 | 613 | _GetOrderStateResp__isset __isset; 614 | 615 | void __set_ret_code(const int32_t val); 616 | 617 | void __set_result(const OrderState& val); 618 | 619 | bool operator == (const GetOrderStateResp & rhs) const 620 | { 621 | if (!(ret_code == rhs.ret_code)) 622 | return false; 623 | if (!(result == rhs.result)) 624 | return false; 625 | return true; 626 | } 627 | bool operator != (const GetOrderStateResp &rhs) const { 628 | return !(*this == rhs); 629 | } 630 | 631 | bool operator < (const GetOrderStateResp & ) const; 632 | 633 | uint32_t read(::apache::thrift::protocol::TProtocol* iprot); 634 | uint32_t write(::apache::thrift::protocol::TProtocol* oprot) const; 635 | 636 | virtual void printTo(std::ostream& out) const; 637 | }; 638 | 639 | void swap(GetOrderStateResp &a, GetOrderStateResp &b); 640 | 641 | inline std::ostream& operator<<(std::ostream& out, const GetOrderStateResp& obj) 642 | { 643 | obj.printTo(out); 644 | return out; 645 | } 646 | 647 | typedef struct _CloseOrderResp__isset { 648 | _CloseOrderResp__isset() : ret_code(false) {} 649 | bool ret_code :1; 650 | } _CloseOrderResp__isset; 651 | 652 | class CloseOrderResp : public virtual ::apache::thrift::TBase { 653 | public: 654 | 655 | CloseOrderResp(const CloseOrderResp&); 656 | CloseOrderResp& operator=(const CloseOrderResp&); 657 | CloseOrderResp() : ret_code(0) { 658 | } 659 | 660 | virtual ~CloseOrderResp() throw(); 661 | int32_t ret_code; 662 | 663 | _CloseOrderResp__isset __isset; 664 | 665 | void __set_ret_code(const int32_t val); 666 | 667 | bool operator == (const CloseOrderResp & rhs) const 668 | { 669 | if (!(ret_code == rhs.ret_code)) 670 | return false; 671 | return true; 672 | } 673 | bool operator != (const CloseOrderResp &rhs) const { 674 | return !(*this == rhs); 675 | } 676 | 677 | bool operator < (const CloseOrderResp & ) const; 678 | 679 | uint32_t read(::apache::thrift::protocol::TProtocol* iprot); 680 | uint32_t write(::apache::thrift::protocol::TProtocol* oprot) const; 681 | 682 | virtual void printTo(std::ostream& out) const; 683 | }; 684 | 685 | void swap(CloseOrderResp &a, CloseOrderResp &b); 686 | 687 | inline std::ostream& operator<<(std::ostream& out, const CloseOrderResp& obj) 688 | { 689 | obj.printTo(out); 690 | return out; 691 | } 692 | 693 | typedef struct _CancelOrderResp__isset { 694 | _CancelOrderResp__isset() : ret_code(false) {} 695 | bool ret_code :1; 696 | } _CancelOrderResp__isset; 697 | 698 | class CancelOrderResp : public virtual ::apache::thrift::TBase { 699 | public: 700 | 701 | CancelOrderResp(const CancelOrderResp&); 702 | CancelOrderResp& operator=(const CancelOrderResp&); 703 | CancelOrderResp() : ret_code(0) { 704 | } 705 | 706 | virtual ~CancelOrderResp() throw(); 707 | int32_t ret_code; 708 | 709 | _CancelOrderResp__isset __isset; 710 | 711 | void __set_ret_code(const int32_t val); 712 | 713 | bool operator == (const CancelOrderResp & rhs) const 714 | { 715 | if (!(ret_code == rhs.ret_code)) 716 | return false; 717 | return true; 718 | } 719 | bool operator != (const CancelOrderResp &rhs) const { 720 | return !(*this == rhs); 721 | } 722 | 723 | bool operator < (const CancelOrderResp & ) const; 724 | 725 | uint32_t read(::apache::thrift::protocol::TProtocol* iprot); 726 | uint32_t write(::apache::thrift::protocol::TProtocol* oprot) const; 727 | 728 | virtual void printTo(std::ostream& out) const; 729 | }; 730 | 731 | void swap(CancelOrderResp &a, CancelOrderResp &b); 732 | 733 | inline std::ostream& operator<<(std::ostream& out, const CancelOrderResp& obj) 734 | { 735 | obj.printTo(out); 736 | return out; 737 | } 738 | 739 | typedef struct _PlaceOrderResp__isset { 740 | _PlaceOrderResp__isset() : ret_code(false), result(false) {} 741 | bool ret_code :1; 742 | bool result :1; 743 | } _PlaceOrderResp__isset; 744 | 745 | class PlaceOrderResp : public virtual ::apache::thrift::TBase { 746 | public: 747 | 748 | PlaceOrderResp(const PlaceOrderResp&); 749 | PlaceOrderResp& operator=(const PlaceOrderResp&); 750 | PlaceOrderResp() : ret_code(0), result() { 751 | } 752 | 753 | virtual ~PlaceOrderResp() throw(); 754 | int32_t ret_code; 755 | std::string result; 756 | 757 | _PlaceOrderResp__isset __isset; 758 | 759 | void __set_ret_code(const int32_t val); 760 | 761 | void __set_result(const std::string& val); 762 | 763 | bool operator == (const PlaceOrderResp & rhs) const 764 | { 765 | if (!(ret_code == rhs.ret_code)) 766 | return false; 767 | if (!(result == rhs.result)) 768 | return false; 769 | return true; 770 | } 771 | bool operator != (const PlaceOrderResp &rhs) const { 772 | return !(*this == rhs); 773 | } 774 | 775 | bool operator < (const PlaceOrderResp & ) const; 776 | 777 | uint32_t read(::apache::thrift::protocol::TProtocol* iprot); 778 | uint32_t write(::apache::thrift::protocol::TProtocol* oprot) const; 779 | 780 | virtual void printTo(std::ostream& out) const; 781 | }; 782 | 783 | void swap(PlaceOrderResp &a, PlaceOrderResp &b); 784 | 785 | inline std::ostream& operator<<(std::ostream& out, const PlaceOrderResp& obj) 786 | { 787 | obj.printTo(out); 788 | return out; 789 | } 790 | 791 | 792 | 793 | #endif 794 | --------------------------------------------------------------------------------