
#pragma once

#include "CCTCTradeAPIEx.h"
#include "CICTCTradeAPIExEvents.h"
#include "..\TCORE\include\TCTradeAPIDefinitions.h"

// TradeAPI command target

//帐号资料
struct AccountData
{
	CString strAcctType;	//帐号类型
	CString strAcctName;	//帐号名称
	CString strBroker;		//分公司码
	CString strAccount;		//帐号
};

//下单结构
struct ORDER_INFO
{
	CString strAccount;	//帐号
	CString strBrokerID;//分公司码
	CString strSymbol;		//商品代码
	CString strOrderType;	// 1:市价  2:限价
	CString strBuySell;	 // 1:买 2:卖
	CString strTimeInForce;	//1:rod 2:ioc 3:fok
	CString strPositionEffect; // 0:开仓 1:平仓 4:自动
	CString strPrice;	//价格
	CString strQty;		//口数

};

class CTradeEventReceiver
{
public:
	CTradeEventReceiver(void){};
	virtual ~CTradeEventReceiver(void){};
	virtual void OnTradeCommandMsg(long lMsgType, long lMsgCode, LPCTSTR strMsg){};
	virtual void OnTradeAccountUpdate(long Type, LPCTSTR AcctMask, long Count){};
	virtual void OnTradeReportReset(){};
	virtual void OnTradeExecutionReport(LPCTSTR ReportID){};
};

class TradeAPI : public CCmdTarget
{
	DECLARE_DYNAMIC(TradeAPI)

public:
	TradeAPI();
	virtual ~TradeAPI();
public:
	CCTCTradeAPIEx* m_pInterface;
	DWORD m_dwCookie;

	BOOL Init(CTradeEventReceiver* pEventReceiver); //初始化
	void UnAdvise();
	long Connect(LPCTSTR HostAddress, LPCTSTR SystemName, LPCTSTR AppID, long ConnectType);	//跟Tcore连线
	void Disconnect();	//断线
	long GetAccountData(long Type, long Index, LPCTSTR AcctMask, LPDISPATCH AccountDataItem);	//取帐号资料
	long DoPlaceOrder(ORDER_INFO pOrderInfo);	//下单
	CString GetInstrumentInfo(LPCTSTR bstrType, LPCTSTR bstrSymbolID);	//取icm设定
	long GetReportData(long ReportType, LPCTSTR ReportID, LPDISPATCH ReportItems);	//取回报资料
	void DoQueryAccountData(long lType, LPCTSTR BrokerID, LPCTSTR Account);	//帐务资料查询
protected:
	afx_msg void OnCommandMsg(long MsgType, long MsgCode, LPCTSTR MsgString);	//连线状态
	afx_msg void OnAccountUpdate(long Type, LPCTSTR AcctMask, long Count);		//帐号与帐务更新
	afx_msg void OnExecutionReport(LPCTSTR ReportID);	//即时回报
	afx_msg void OnReportReset();						//回报清空回补

	DECLARE_MESSAGE_MAP()
	DECLARE_DISPATCH_MAP()
	DECLARE_INTERFACE_MAP()
protected:
	CTradeEventReceiver* m_pEventReceiver;
	bool bConnect;
};


