Strategy.h
上传用户:zhanglf88
上传日期:2013-11-19
资源大小:6036k
文件大小:15k
源码类别:

金融证券系统

开发平台:

Visual C++

  1. /*
  2. Cross Platform Core Code.
  3. Copyright(R) 2001-2002 Balang Software.
  4. All rights reserved.
  5. Using:
  6. class CRateParam;
  7. class CSPTimeContainer;
  8. class COpParam;
  9. class CStockOwnContainer;
  10. class COpRecordContainer;
  11. class CAssetSerialContainer;
  12. class CTechStock;
  13. class CTechStockContainer;
  14. class CStrategy;
  15. */
  16. #if !defined( __STKLIB_STRATEGY_H__ )
  17. #define __STKLIB_STRATEGY_H__
  18. /////////////////////////////////////////////////////////////////////////////
  19. // CRateParam
  20. /***
  21. 交易税费设置类
  22. */
  23. class STKLIB_API CRateParam
  24. {
  25. public:
  26. CRateParam( );
  27. BOOL IsValid( );
  28. void SetDefault( );
  29. void Serialize( CSPArchive &ar );
  30. double GetRate( CStockInfo & info );
  31. double m_dShaa;
  32. double m_dShab;
  33. double m_dShafund;
  34. double m_dSzna;
  35. double m_dSznb;
  36. double m_dSznfund;
  37. };
  38. /////////////////////////////////////////////////////////////////////////////
  39. // CSPTimeContainer
  40. //template class __declspec(dllexport) CSPArray<CSPTime, CSPTime &>;
  41. typedef CSPArray<CSPTime, CSPTime &> CSPTimeArray;
  42. class STKLIB_API CSPTimeContainer : public CSPTimeArray
  43. {
  44. public:
  45. CSPTimeContainer() {}
  46. virtual ~CSPTimeContainer() { RemoveAll(); }
  47. // for access CSPTimeArray
  48. virtual int Add( CSPTime &tm ) { return CSPTimeArray::Add( tm ); }
  49. virtual void RemoveAll( ) { CSPTimeArray::RemoveAll(); }
  50. };
  51. /////////////////////////////////////////////////////////////////////////////
  52. // COpParam
  53. STKLIB_API CSPString AfxGetLogicString( int nLogic );
  54. STKLIB_API CSPString AfxGetStoreDivString( int nStoreDiv );
  55. /***
  56. 策略模拟操作条件类
  57. */
  58. class STKLIB_API COpParam
  59. {
  60. public:
  61. COpParam( );
  62. // Attributes
  63. BOOL IsValid( );
  64. void SetDefault( );
  65. void SetDefaultOfInvalidMember( );
  66. void Serialize( CSPArchive &ar );
  67. // Operation
  68. BOOL IsInTimeZones( CSPTime tm );
  69. CSPTime GetBeginTime( );
  70. CSPTime GetEndTime( );
  71. BOOL GetNextTradeTime( CSPTime tmNow, CSPTime &tmNext );
  72. DWORD GetProgress( CSPTime tmNow, DWORD dwProgressMax );
  73. enum storeDivs {
  74. storedivMin = 0x01,
  75. storedivOnce = 0x01,
  76. storedivTwice = 0x02,
  77. storedivThird = 0x03,
  78. storedivForth = 0x04,
  79. storedivFifth = 0x05,
  80. storedivSixth = 0x06,
  81. storedivSeventh = 0x07,
  82. storedivEighth = 0x08,
  83. storedivNinth = 0x09,
  84. storedivTenth = 0x0A,
  85. storedivMax = 0x0A,
  86. };
  87. enum Logical {
  88. logicMin = 0x01,
  89. logicAnd = 0x01,
  90. logicOr = 0x02,
  91. logicMax = 0x02,
  92. };
  93. int m_nKType;
  94. int m_nMaindataType;
  95. int m_nBuyLogic;
  96. int m_nBuyLimit;
  97. double m_dBuyMulti;
  98. int m_nSellLogic;
  99. int m_nSellLimit;
  100. double m_dSellMulti;
  101. CSPTimeContainer m_atmBegin;
  102. CSPTimeContainer m_atmEnd;
  103. long m_nStartAmount; // 元
  104. int m_nStoreDiv;
  105. BOOL m_bStopLosing;
  106. BOOL m_bStopProfit;
  107. double m_dStopLosing;
  108. double m_dStopProfit;
  109. BOOL m_bLongTrend;
  110. BOOL m_bIndexTrend;
  111. int m_nKTypeLong;
  112. int m_nKTypeIndex;
  113. };
  114. /////////////////////////////////////////////////////////////////////////////
  115. // stockown_t
  116. #pragma pack(1)
  117. typedef struct stockown_t {
  118. char szCode[STKLIB_MAX_CODE];
  119. DWORD dwShare;
  120. double dBuyPrice;
  121. DWORD dwMarket;
  122. char szReserved[20];
  123. } STOCKOWN, * LPSTOCKOWN;
  124. //template class __declspec(dllexport) CSPArray< STOCKOWN, STOCKOWN & >;
  125. typedef CSPArray< STOCKOWN, STOCKOWN & > CStockOwnArray;
  126. /***
  127. 拥有股票数组
  128. */
  129. class STKLIB_API CStockOwnContainer : public CStockOwnArray
  130. {
  131. public:
  132. virtual ~CStockOwnContainer() { RemoveAll(); }
  133. // for access CStockOwnArray
  134. virtual void RemoveAll( ) { CStockOwnArray::RemoveAll(); }
  135. BOOL AddStock( CStockInfo & info, DWORD dwShare, double dBuyPrice );
  136. BOOL RemoveStock( CStockInfo & info, DWORD dwShare );
  137. BOOL HasThisStock( CStockInfo & info, LPSTOCKOWN lpOwn );
  138. };
  139. /////////////////////////////////////////////////////////////////////////////
  140. // oprecord_t
  141. // 操作类型
  142. #define STRATEGY_OPTYPE_BUY 0x01 // 买入
  143. #define STRATEGY_OPTYPE_SELL 0x02 // 卖出
  144. #define STRATEGY_OPTYPE_ADDSTOCK 0x03 // 添加股票
  145. #define STRATEGY_OPTYPE_REMOVESTOCK 0x04 // 移除股票
  146. #define STRATEGY_OPTYPE_ADDCASH 0x05 // 添加资金
  147. #define STRATEGY_OPTYPE_REMOVECASH 0x06 // 取走资金
  148. STKLIB_API CSPString AfxGetStrategyOpTypeString( long lOpType );
  149. typedef struct oprecord_t {
  150. long lOpType;
  151. time_t time;
  152. char szCode[STKLIB_MAX_CODE];
  153. DWORD dwShare;
  154. double dSharePrice;
  155. double dRateCost;
  156. long bViewed; // for next op use in realop. 
  157. DWORD dwMarket;
  158. char szReserved[24];
  159. } OPRECORD, * LPOPRECORD;
  160. //template class __declspec(dllexport) CSPArray< OPRECORD, OPRECORD & >;
  161. typedef CSPArray< OPRECORD, OPRECORD & > COpRecordArray;
  162. /***
  163. 操作记录数组
  164. */
  165. class STKLIB_API COpRecordContainer : public COpRecordArray
  166. {
  167. public:
  168. virtual ~COpRecordContainer() { RemoveAll(); }
  169. // for access COpRecordArray
  170. virtual int Add( OPRECORD &newElement){ return COpRecordArray::Add(newElement); }
  171. virtual void RemoveAt(int nIndex, int nCount = 1){ COpRecordArray::RemoveAt(nIndex,nCount); }
  172. virtual void RemoveAll( ) { COpRecordArray::RemoveAll(); }
  173. virtual void Copy(const COpRecordArray& src) { COpRecordArray::Copy(src); }
  174. // operation
  175. int AddRecord( long lOpType, CSPTime tm, const char *szCode,
  176. DWORD dwShare, double dSharePrice, double dRateCost );
  177. int AddRecordUniqueStock(long lOpType, CSPTime tm, const char *szCode,
  178.    DWORD dwShare, double dSharePrice, double dRateCost );
  179. int GetBuyRecordCount();
  180. BOOL HasBuyStock( const char * szCode );
  181. BOOL StatResults( int *pnTimes, int *pnVictoryTimes, double *pdYieldMax, double *pdYieldMin );
  182. };
  183. typedef struct assetserial_t {
  184. time_t time;
  185. double dAsset;
  186. double dCash;
  187. char szReserved[32];
  188. } ASSETSERIAL, * LPASSETSERIAL;
  189. //template class __declspec(dllexport) CSPArray< ASSETSERIAL, ASSETSERIAL & >;
  190. typedef CSPArray< ASSETSERIAL, ASSETSERIAL & > CAssetSerialArray;
  191. /***
  192. 资产数组
  193. */
  194. class STKLIB_API CAssetSerialContainer : public CAssetSerialArray
  195. {
  196. public:
  197. virtual ~CAssetSerialContainer() { RemoveAll(); }
  198. // for access CAssetSerialArray
  199. virtual void RemoveAt(int nIndex, int nCount = 1){ CAssetSerialArray::RemoveAt(nIndex,nCount); }
  200. virtual void RemoveAll( ) { CAssetSerialArray::RemoveAll(); }
  201. virtual void Copy(const CAssetSerialArray& src) { CAssetSerialArray::Copy(src); }
  202. // operation
  203. BOOL SortInsert( ASSETSERIAL serial );
  204. BOOL GetStdDev( double *pdStdDev, double * pdXiapu );
  205. };
  206. #pragma pack()
  207. //////////////////////////////////////////////////////////////////////
  208. // CTechStock
  209. /***
  210. 一只待操作的股票,包含股票的信息,K线数据,长周期K线数据,所需技术指标,所需长周期技术指标
  211. */
  212. class STKLIB_API CTechStock
  213. {
  214. public:
  215. CTechStock( );
  216. CTechStock( const CTechStock & src );
  217. virtual ~CTechStock( );
  218. CTechStock & operator = ( const CTechStock &src );
  219. void SetAutoDelete( BOOL bAutoDelete = TRUE );
  220. void Clear( );
  221. BOOL GetPriceOK( long lOpType, CSPTime tmCur, DWORD dwShare, double dSharePrice, double *pdPriceOK );
  222. BOOL IsStopTrading( CSPTime tmCur );
  223. BOOL GetClosePrice( CSPTime tmCur, double *pdPrice );
  224. BOOL GetCloseDiffPercent( CSPTime tmCur, double *pdDiffPercent );
  225. CStockInfo m_info; // 股票信息
  226. CKData m_kdata; // 所需K线数据
  227. CKData m_kdataLong; // 长周期K线数据
  228. CSPPtrArray m_techs; // 所需技术指标
  229. CSPPtrArray m_techsLong; // 所需长周期技术指标
  230. protected:
  231. BOOL m_bAutoDelete;
  232. };
  233. //template class __declspec(dllexport) CSPArray< CTechStock, CTechStock & >;
  234. typedef CSPArray< CTechStock, CTechStock & > CTechStockArray;
  235. /***
  236. CTechStock数组
  237. */
  238. class STKLIB_API CTechStockContainer : public CTechStockArray
  239. {
  240. public:
  241. virtual ~CTechStockContainer( );
  242. // for access CTechStockArray
  243. virtual void SetSize(int nNewSize, int nGrowBy = -1) { CTechStockArray::SetSize(nNewSize,nGrowBy); }
  244. // operation
  245. void Clear( );
  246. // Operation
  247. BOOL GetShouldBuyShare( CTechStock & techstock, CSPTime tmCur, double dCash, CRateParam &rate, COpParam &opparam, int nOwnedStockCount,
  248. CSPTime *ptmOp, DWORD *pdwShare, double *pdSharePrice );
  249. BOOL GetShouldSellShare( CTechStock & techstock, CSPTime tmCur, STOCKOWN &own, COpParam &opparam,
  250. CSPTime *ptmOp, DWORD *pdwShare, double *pdSharePrice );
  251. BOOL GetClosePrice( const char * szCode, CSPTime tmCur, double * pdPrice );
  252. BOOL GetSumAsset( CSPTime tmCur, CStockOwnContainer &container, double * pdAsset );
  253. BOOL GetNextExistTradeTime( CSPTime tmCur, CSPTime & tmNext );
  254. CStockInfo m_infoIndex;
  255. CKData m_kdataIndex;
  256. CSPPtrArray m_techsIndex;
  257. };
  258. /////////////////////////////////////////////////////////////////////////////
  259. // CStrategy
  260. typedef BOOL (CALLBACK* SIMULATION_CALLBACK)(DWORD dwType, DWORD dwValue, LPCTSTR lpszMsg, VOID * cookie);
  261. #define STRATEGY_MAX_TITLE 256
  262. #define SIMULATION_PROGRESS 1
  263. #define SIMULATION_YIELD 2
  264. #define STRATEGY_MAX_PROGRESS 10000
  265. #define STRATEGY_MAXF_PROGRESS 10000.
  266. #define STRATEGY_BASE_YIELD 10000
  267. #define STRATEGY_BASEF_YIELD 10000.
  268. /***
  269. 策略
  270. */
  271. class STKLIB_API CStrategy
  272. {
  273. public:
  274. CStrategy();
  275. virtual ~CStrategy();
  276. static CSPString GetName( LPCTSTR lpszPath );
  277. // Overrides
  278. public:
  279. virtual BOOL OpenStrategyFile( LPCTSTR lpszPathName, LPSTR lpErr, UINT nMaxSize );
  280. virtual BOOL SaveStrategyFile( LPCTSTR lpszPathName );
  281. virtual BOOL Serialize(CSPArchive& ar, LPSTR lpErr, UINT nMaxSize);
  282. virtual BOOL DoFileSave( );
  283. virtual BOOL OnClose( );
  284. virtual void OnRealOpViewed( );
  285. protected:
  286. // 存盘文件路径和名称,不写到策略文件中
  287. CSPString m_strPathName;
  288. // Attributes
  289. public:
  290. void SetPathName( LPCTSTR lpszPathName );
  291. CSPString GetPathName( );
  292. void SetName( LPCTSTR lpszName );
  293. CSPString GetName( );
  294. void SetDescript( LPCTSTR lpszDescript );
  295. CSPString GetDescript( );
  296. CSPStringArray & GetStocks( );
  297. void SetStocks( CSPStringArray & astr );
  298. void AddStock( LPCTSTR lpszCode );
  299. void RemoveStock( LPCTSTR lpszCode );
  300. CSPString GetStockTechString( );
  301. CRateParam & GetRateParam( );
  302. COpParam & GetOpParam( );
  303. CTechParameters & GetTechParametersBuy( );
  304. CTechParameters & GetTechParametersSell( );
  305. CTechParameters & GetTechParametersLongTrend( );
  306. CTechParameters & GetTechParametersIndexTrend( );
  307. CSPDWordArray & GetSelectedTechsBuy( );
  308. CSPDWordArray & GetSelectedTechsSell( );
  309. CSPDWordArray & GetSelectedTechsLongTrend( );
  310. CSPDWordArray & GetSelectedTechsIndexTrend( );
  311. // Operations
  312. public:
  313. // Simulation
  314. enum SimuStatus {
  315. statusInit = 0x01,
  316. statusRunning = 0x02,
  317. statusPaused = 0x03,
  318. statusFinished = 0x04,
  319. };
  320. // Data Cache
  321. BOOL PrepareData( SIMULATION_CALLBACK fnCallback, void * cookie );
  322. void ClearLastIntensity( );
  323. void ClearCache( );
  324. CTechStockContainer & GetTechStockContainer( );
  325. BOOL CanModifiedNow() { return ( SimuIsStatusInit() || SimuIsStatusFinished() ); }
  326. // Simulation Status
  327. int SimuGetCurStatus( ) { return m_SimuCurrentStatus; }
  328. BOOL SimuIsStatusInit( ) { return statusInit == m_SimuCurrentStatus; }
  329. BOOL SimuIsStatusRunning( ) { return statusRunning == m_SimuCurrentStatus; }
  330. BOOL SimuIsStatusPaused( ) { return statusPaused == m_SimuCurrentStatus; }
  331. BOOL SimuIsStatusFinished( ) { return statusFinished == m_SimuCurrentStatus; }
  332. void SimuSetStatusInit( ) { m_SimuCurrentStatus = statusInit; }
  333. void SimuSetStatusRunning( ) { m_SimuCurrentStatus = statusRunning; }
  334. void SimuSetStatusPaused( ) { m_SimuCurrentStatus = statusPaused; }
  335. void SimuSetStatusFinished( ){ m_SimuCurrentStatus = statusFinished; }
  336. // Simulation
  337. void SimuReset( );
  338. BOOL SimuGotoNextTime( );
  339. CSPTime SimuGetCurrentTime( );
  340. double SimuGetCurrentCash( );
  341. BOOL SimuOperate( OPRECORD record, BOOL bTimeStrict = TRUE );
  342. COpRecordContainer & SimuGetOpRecord( );
  343. COpRecordContainer & SimuGetNextOp( );
  344. CStockOwnContainer & SimuGetStockOwn( );
  345. CAssetSerialContainer & SimuGetAssetSerial( );
  346. DWORD SimuGetCurrentProgress( DWORD dwProgressMax = STRATEGY_MAX_PROGRESS ); // Not Same as Real
  347. double SimuGetAsset( CSPTime tmCur );
  348. double SimuGetCurrentYield( );
  349. double SimuGetCurrentYieldIndexPercent( );
  350. double SimuGetCurrentYieldPercent( );
  351. BOOL SimuOperateNextop( CSPTime tmCur, COpRecordContainer & nextop, CTechStock & techstock );
  352. virtual BOOL SimuRun( SIMULATION_CALLBACK fnCallback, void * cookie );
  353. // Real
  354. void RealReset( );
  355. BOOL RealGotoNextTime( );
  356. CSPTime RealGetBeginTime( ); // Not Same as Simu
  357. CSPTime RealGetCurrentTime( );
  358. double RealGetCurrentCash( );
  359. BOOL RealOperate( OPRECORD record, BOOL bTimeStrict = TRUE );
  360. COpRecordContainer & RealGetOpRecord( );
  361. COpRecordContainer & RealGetNextOp( );
  362. CStockOwnContainer & RealGetStockOwn( );
  363. CAssetSerialContainer & RealGetAssetSerial( );
  364. double RealGetAsset( CSPTime tmCur );
  365. double RealGetCurrentYield( );
  366. double RealGetCurrentYieldIndexPercent( );
  367. double RealGetCurrentYieldPercent( );
  368. BOOL RealOperateNextop( CSPTime tmCur, COpRecordContainer & nextop, CTechStock & techstock );
  369. virtual BOOL RealRun( SIMULATION_CALLBACK fnCallback, void * cookie );
  370. BOOL RealAddOpRecordStrict( OPRECORD record ); // Not Same as Simu
  371. BOOL RealDeleteOpRecordStrict( int nRecord ); // Not Same as Simu
  372. protected:
  373. // Properties
  374. DWORD m_dwFileVersion;
  375. CSPString m_strName; // 策略名称
  376. CSPString m_strDescript; // 策略说明
  377. // Stocks
  378. CDomain m_stocks; // 策略备选股票
  379. // Rate
  380. CRateParam m_rate; // 交易费用
  381. // Tech parameters
  382. CTechParameters m_paramBuy; // 买入指标规则
  383. CTechParameters m_paramSell; // 卖出指标规则
  384. CTechParameters m_paramLongTrend; // 长期指标规则
  385. CTechParameters m_paramIndexTrend; // 指数指标规则
  386. CSPDWordArray m_anTechsBuy; // 买入依据的指标
  387. CSPDWordArray m_anTechsSell; // 卖出依据的指标
  388. CSPDWordArray m_anTechsLongTrend; // 长周期趋势依据的指标
  389. CSPDWordArray m_anTechsIndexTrend;// 指数趋势依据的指标
  390. // Operate Parameters
  391. COpParam m_opparam; // 操作规则
  392. // Simulation
  393. int m_SimuCurrentStatus; // Not Same as Real, 起始,正在进行,暂停,完成
  394. CSPTime m_SimuCurrentTime; // 模拟当前时间
  395. double m_SimuCurrentCash; // 模拟当前资金
  396. CStockOwnContainer m_SimuStockOwn; // 模拟当前拥有股票
  397. COpRecordContainer m_SimuOpRecord; // 模拟记录
  398. COpRecordContainer m_SimuNextOp; // 模拟下一步操作
  399. CAssetSerialContainer m_SimuAssetSerial; // 模拟资产值序列
  400. // Real Operate
  401. CSPTime m_RealBeginTime; // Not Same as Simu,实战起始时间
  402. CSPTime m_RealCurrentTime; // 实战当前时间
  403. double m_RealCurrentCash; // 实战当前资金
  404. CStockOwnContainer m_RealStockOwn; // 实战当前拥有股票
  405. COpRecordContainer m_RealOpRecord; // 实战操作记录
  406. COpRecordContainer m_RealNextOp; // 实战下一步操作
  407. CAssetSerialContainer m_RealAssetSerial; // 实战资产值序列
  408. // Cache Data, Not Saved
  409. CTechStockContainer m_techstocks; // 备选股票,包含信息、数据、技术指标等
  410. };
  411. #endif // __STKLIB_STRATEGY_H__