Skip to content

Commit

Permalink
update Simulator
Browse files Browse the repository at this point in the history
  • Loading branch information
pegasusTrader committed May 6, 2020
1 parent b0e3dbd commit 479afb8
Show file tree
Hide file tree
Showing 19 changed files with 164 additions and 57 deletions.
5 changes: 5 additions & 0 deletions Interface/include/cwBasicCout.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,17 @@ class cwBasicCout
//static void AddLog(bool bNoPara, const char * pData);
static void AddLog(const char * lpParam, ...);

static void SetNoWorkRequired(bool NoWork = false);
private:
static std::thread m_CoutWorkingThread;
static volatile bool m_bCoutWorkingThreadRun;
static void CoutWorkingThread();

static int m_iInitialCount;

static cwMUTEX m_DequeMutex;

//²»ÐèÒª¹¤×÷
static volatile bool m_bNoWorkRequired;
};

13 changes: 9 additions & 4 deletions Interface/include/cwBasicMdSpi.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,18 +75,25 @@ class cwBasicMdSpi
}

//User Setting Method
inline void RegisterTradeSPI(cwBasicTradeSpi * pTradeSpi)
inline void RegisterTradeSPI(cwBasicTradeSpi * pTradeSpi)
{
m_pTradeSpi = pTradeSpi;
}

virtual void RegisterStrategy(cwBasicStrategy * pBasicStrategy) = 0;
virtual void RegisterStrategy(cwBasicStrategy * pBasicStrategy) = 0;

void SetMdInfo(const char * pszInfo);


const cwMDAPIType m_cwMdAPIType;
char m_szMdInfo[128];
std::deque <cwMarketDataPtr> m_DepthMarketDataDeque;
size_t m_iDequeSize;
volatile bool m_MdDequeDone;
bool m_bNoUseBasicMdUpdate;

cwMUTEX m_MarketDataUpdateMutex;
cwBasicStrategy* m_pBasicStrategy;
protected:
PriceServerStatus m_CurrentStatus;

Expand Down Expand Up @@ -156,15 +163,13 @@ ORIGIN->MEMBER = 0;\
#endif // CV_NOTIFY


cwMUTEX m_MarketDataUpdateMutex;
std::condition_variable m_MDUpdateMutexCv;

std::thread m_MarketDataUpdateThread;
volatile bool m_bMarketDataUpdateThreadRun;
void MarketDataUpdate();

cwBasicTradeSpi* m_pTradeSpi;
cwBasicStrategy* m_pBasicStrategy;

std::map<std::string, cwMarketDataPtr> m_LastestMarketDataMap;

Expand Down
2 changes: 1 addition & 1 deletion Interface/include/cwBasicSimulator.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class cwBasicSimulator
cwBasicSimulator();
~cwBasicSimulator();

virtual void InitialStrategy(const char * pConfigFilePath);
virtual void InitialSimulator(const char * pConfigFilePath);
std::string m_strConfigFileFullPath;


Expand Down
41 changes: 30 additions & 11 deletions Interface/include/cwBasicStrategy.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include <vector>
#include <set>
#include <map>
#include <thread>
#include "cwProductTradeTime.h"
#include "cwTradeCommonDefine.h"
#include "cwStrategyLog.h"
Expand Down Expand Up @@ -62,6 +63,9 @@ class cwBasicStrategy
//报单操作请求响应
virtual void OnRspOrderCancel(cwOrderPtr pOrder, cwFtdcRspInfoField * pRspInfo) {};

///Strategy Timer
virtual void OnStrategyTimer(int iTimerId) {};

///Special For Simulation
///These functions will NOT be called in normal mode
//回测部分结束(夜盘结束和日盘结束将被调用)
Expand All @@ -80,15 +84,15 @@ class cwBasicStrategy
cwMarketDataPtr GetLastestMarketData(std::string InstrumentID);
//获取账户信息
cwAccountPtr GetAccount();
//获取挂单列表,传入map用于返回信息,本地报单编号为Key
//获取挂单列表,传入map用于返回信息,本地报单编号(OrderRef)为Key
bool GetActiveOrders(std::map<std::string, cwOrderPtr>& ActiveOrders); ///key OrderRef
//获取挂单列表,传入合约,map用于返回信息,本地报单编号为Key
//获取挂单列表,传入合约,map用于返回信息,本地报单编号(OrderRef)为Key
bool GetActiveOrders(std::string InstrumentID, std::map<std::string, cwOrderPtr>& ActiveOrders); ///key OrderRef
//获取多头挂单数量,传入合约
int GetActiveOrdersLong(std::string InstrumentID); ///key OrderRef
//获取空头挂单数量,传入合约
int GetActiveOrdersShort(std::string InstrumentID); ///key OrderRef
//获取所有报单列表,传入map用于返回信息,交易所报单编号为Key
//获取所有报单列表,传入map用于返回信息,交易所报单编号(sysOrderID)为Key
bool GetAllOrders(std::map<std::string, cwOrderPtr>& Orders); ///Key OrderSysID
//获取持仓列表,传入map用于返回信息,合约ID为Key
bool GetPositions(std::map<std::string, cwPositionPtr>& PositionMap); ///Key InstrumentID
Expand Down Expand Up @@ -136,28 +140,43 @@ class cwBasicStrategy

//获取合约当前撤单次数
int GetInstrumentCancelCount(std::string InstrumentID);
//获取合约是否是订阅
bool IsThisStrategySubScribed(std::string InstrumentID);
//获取当前状态是否为回测模拟情况
inline bool GetIsSimulation() { return m_bIsSimulation; }

//设置定时器 iTimerId定时器id,在OnTimer回调依据此id判定是哪个定时器触发, iElapse 触发间隔(毫秒)
//目前仅支持10个定时器,定时器内回调函数请勿处理复杂逻辑,所有定时器回调共用一个线程。
//同个id下,触发间隔将会被覆盖
bool SetTimer(int iTimerId, int iElapse);
void RemoveTimer(int iTimerId);

///如果重载该函数,请确保最后基类的函数能够被调用到!
virtual void SetStrategyReady();
///系统自用接口信息,勿动
void SetMdSpi(cwMDAPIType apiType, void * pSpi);
void SetTradeSpi(cwTradeAPIType apiType, void *pSpi);
void SetIsSimulation(bool IsSimulation = false) { m_bIsSimulation = IsSimulation; };
void SetMdSpi(cwMDAPIType apiType, void * pSpi);
void SetTradeSpi(cwTradeAPIType apiType, void *pSpi);
void SetIsSimulation(bool IsSimulation = false) { m_bIsSimulation = IsSimulation; };
private:
///系统自用接口信息,勿动
bool m_bIsSimulation;
bool m_bIsSimulation;

void * m_pTradeSpi;
cwTradeAPIType m_TradeApiType;
void * m_pTradeSpi;
cwTradeAPIType m_TradeApiType;

void * m_pMdSpi;
cwMDAPIType m_MdApiType;
void * m_pMdSpi;
cwMDAPIType m_MdApiType;


std::set<std::string> m_SubscribeInstrumentSet;
cwProductTradeTime m_ProductTradeTime;

cwStrategyLog m_BasicStrategyLog;

//Timer key:TimerID, value:Elapse in ms
std::map<int, int> m_cwTimerElapseMap;

std::thread m_StrategyTimerWorkingThread;
volatile bool m_bStrategyTimerWorkingThreadRun;
void StrategyTimerWorkingThread();
};
3 changes: 3 additions & 0 deletions Interface/include/cwBasicTradeSpi.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ class cwBasicTradeSpi

virtual void RegisterBasicStrategy(cwBasicStrategy * pBasicStrategy, void * pSpi = NULL) = 0;

void SetTradeInfo(const char * pszInfo);

inline cwAccountPtr GetAccount()
{
return m_pAccount;
Expand Down Expand Up @@ -164,6 +166,7 @@ class cwBasicTradeSpi
bool GenerateInstrumentDataToFile();

const cwTradeAPIType m_cwTradeAPIType;
char m_szTradeInfo[128];

bool m_bHasPositionChanged;
bool m_bHasOrdersChanged;
Expand Down
3 changes: 3 additions & 0 deletions Interface/include/cwFtdTradeSpi.h
Original file line number Diff line number Diff line change
Expand Up @@ -485,6 +485,7 @@ class cwFtdTradeSpi
cwFtdcTradeSourceType GetCtp2CwTradeSourceType(TThostFtdcTradeSourceType tradesourcetype);
cwFtdcInstrumentStatusType GetCtp2CwInstrumentStatusType(TThostFtdcInstrumentStatusType intrumentstatustype);

THOST_TE_RESUME_TYPE Getcw2CtpResumeType(CW_TE_RESUME_TYPE type);
TThostFtdcDirectionType GetCw2CtpDirectionType(cwFtdcDirectionType direction);

cwOrderPtr GetcwOrderPtr(CThostFtdcOrderField * pOrder);
Expand Down Expand Up @@ -538,5 +539,7 @@ class cwFtdTradeSpi
#ifdef CWCOUTINFO
cwBasicCout m_cwShow;
#endif

THOST_TE_RESUME_TYPE m_iResumeType;
};

37 changes: 25 additions & 12 deletions Interface/include/cwPegasusSimulator.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class cwPegasusSimulator :
cwPegasusSimulator();
~cwPegasusSimulator();

virtual void InitialStrategy(const char * pConfigFilePath);
virtual void InitialSimulator(const char * pConfigFilePath);

virtual int ReqUserMdLogin();
virtual int ReqQryInstrument();
Expand All @@ -36,22 +36,23 @@ class cwPegasusSimulator :
cwFtdcTimeType m_CurrentSimulationTime;

volatile bool m_bSimulationFinished;

cwSettlement m_cwSettlement;
private:
enum SIMTYPE:int
{
type_CSV_file = 0,
type_BIN_file,
type_CSV_List_file,
type_BIN_List_file,
type_DB
type_DB,
type_REAL_Time_Quote
};

SIMTYPE m_SimType;
int m_iInterval;
SIMTYPE m_SimType;
int m_iInterval;

std::string m_strFrontAddr;
std::string m_strInstrumentFile;
std::string m_strFrontAddr;
std::string m_strInstrumentFile;

std::thread m_SimulatorProcessorThread;
volatile bool m_bMarketDataUpdateThreadRun;
Expand All @@ -60,6 +61,7 @@ class cwPegasusSimulator :
std::thread m_MarketDataUpdateThread;
void CsvMarketDataUpdate();
void BinMarketDataUpdate();
void RealTimeMarketDataUpdate();

std::map<std::string, std::string> m_MarketDataFileMap;

Expand All @@ -74,11 +76,11 @@ class cwPegasusSimulator :
std::map<std::string, cwMarketDataPtr> m_LastestMarketDataMap;

std::map<std::string, cwOrderPtr> m_TotalOrderMap;
std::map<std::string, std::map<uint32_t, std::deque<cwOrderPtr>>> m_TotalLongOrderMap;
std::map<std::string, std::map<uint32_t, std::deque<cwOrderPtr>>> m_TotalShortOrderMap;
std::map<std::string, std::map<int64_t, std::deque<cwOrderPtr>>> m_TotalLongOrderMap;
std::map<std::string, std::map<int64_t, std::deque<cwOrderPtr>>> m_TotalShortOrderMap;

std::map<std::string, std::map <uint32_t, std::deque<cwOrderPtr>>> m_LongWaitOrderListMap;
std::map<std::string, std::map <uint32_t, std::deque<cwOrderPtr>>> m_ShortWaitOrderListMap;
std::map<std::string, std::map<int64_t, std::deque<cwOrderPtr>>> m_LongWaitOrderListMap;
std::map<std::string, std::map<int64_t, std::deque<cwOrderPtr>>> m_ShortWaitOrderListMap;

enum UserActionType :int
{
Expand Down Expand Up @@ -106,6 +108,17 @@ class cwPegasusSimulator :
volatile bool m_bMDCasheMutexReady;
volatile bool m_bSimulationPartEnd;

cwSettlement m_cwSettlement;
cwAccountPtr m_pAccount;

double m_dDeposit;

//Result
bool m_bSaveAccountResult;
int m_iAccountResultInterval;
std::map<std::string, double> m_dTimeBalanceMap;

std::map<std::string, bool> m_bSaveInsResultMap;
std::map<std::string, int> m_iInsResultInterval;
std::map<std::string, std::map<std::string, double>> m_dInsTimeBalanceMap;
};

2 changes: 1 addition & 1 deletion Interface/include/cwProductTradeTime.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ class cwProductTradeTime
enum cwTradeTimeSpace
{
NoTrading = 0
, NightPartOne
, AMPartOne
, AMPartTwo
, PMPartOne
, NightPartOne
, TradeTimeSpaceCnt
};

Expand Down
45 changes: 37 additions & 8 deletions Interface/include/cwSettlement.h
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
#pragma once

#include "cwTradeCommonDefine.h"
#include "cwMutex.h"

#include <map>
#include <string>

#include <deque>

class cwSettlement
{
Expand All @@ -24,25 +25,53 @@ class cwSettlement

void UpdatePrice(cwMarketDataPtr pMdData);

void SettlementPrice(std::string instrumentid, double price);

void UpdateOrder(cwOrderPtr pOrder);
void UpdateTrade(cwTradePtr pTrade);

//入金
void Deposit(double ddeposit)
{
m_dPreBalance += ddeposit;
m_dBalance += ddeposit;
}

//update Instrument Data
void SetInstrumentData(std::map<std::string, cwInstrumentDataPtr>& InstrumentMap);

std::map<std::string, cwInstrumentDataPtr> m_InstrumentMap;
cwMUTEX m_ProcessMutex;

std::map<std::string, cwInstrumentDataPtr> m_InstrumentMap; //kye Isntrumentid, value: InstrumentData

std::map<std::string, cwMarketDataPtr> m_LastPriceMap; //Key Insrumentid, value: MarketData

std::map<std::string, int> m_Position; //key Instrumentid
std::map<std::string, int> m_Position; //key Instrumentid value: NetPosition

std::map<std::string, cwOrderPtr> m_OrderList; //key OrderRef
std::map<std::string, cwTradePtr> m_TradeList; //key TradeId
std::map<std::string, cwOrderPtr> m_OrderList; //key OrderRef value: orders
std::map<std::string, cwTradePtr> m_TradeList; //key TradeId value: trades

std::map<std::string, int> m_CancelCount; //key Instrumentid value: Cancel Count

//Profit and loss Statistics
std::map<std::string, double> m_ProfitAndLossMap;
std::map<std::string, std::deque<double>> m_UnClosePositionCost;
std::map<std::string, int> m_TradeCountMap;
std::map<std::string, double> m_CloseProfitMap; //key:InstrumentID, value: This Instrument ClosePrifit
std::map<std::string, double> m_PositionProfitMap; //key:InstrumentID, value: This Instrument PositionProfit
std::map<std::string, double> m_FeeMap; //key:InstrumentID, value: This Instrument Fee

std::map<std::string, std::deque<double>> m_UnClosePositionCost; //key:InstrumentID, value: Position Cost deque
std::map<std::string, int> m_TradeCountMap; //key:InstrumentID, value: TradeCount

std::map<std::string, double> m_dInsMaxBalanceMap; //key:InstrumentID, value: Instrument Max Balance For Max Drop Down
std::map<std::string, double> m_dInsMaxDropDownMap; //Key:InstrumentID, value: Max Drop Down

double m_dPreBalance; //初始权益
double m_dBalance; //当前权益

double m_dPositionProfit; //持仓盈亏
double m_dCloseProfit; //平仓盈亏
double m_dFee; //手续费

double m_dCurMaxBalance; //截止目前最高权益,计算最大回撤用
double m_dMaxDropDown; //最大回撤
};

3 changes: 3 additions & 0 deletions Interface/include/cwSimTradeSpi.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ class cwSimTradeSpi
///成交通知
virtual void OnRtnTrade(cwTradePtr pTrade);

///账户通知
virtual void OnRtnAccount(cwAccountPtr pAccount);

///报单录入错误回报
virtual void OnErrRtnOrderInsert(cwOrderPtr pInputOrder, cwRspInfoPtr pRspInfo);

Expand Down
Loading

0 comments on commit 479afb8

Please sign in to comment.