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

金融证券系统

开发平台:

Visual C++

  1. // Simulation.cpp: implementation of the CSimulation class.
  2. //
  3. //////////////////////////////////////////////////////////////////////
  4. #include "stdafx.h"
  5. #include "Simulation.h"
  6. #include "../Dialog/DownloadDlg.h"
  7. #ifdef _DEBUG
  8. #undef THIS_FILE
  9. static char THIS_FILE[]=__FILE__;
  10. #define new DEBUG_NEW
  11. #endif
  12. //////////////////////////////////////////////////////////////////////
  13. // CSimulation
  14. HANDLE CSimulation::m_hEventKillSimulationThread = NULL;
  15. HANDLE CSimulation::m_hEventSimulationThreadKilled = NULL;
  16. /***
  17. 策略模拟进度回调函数
  18. */
  19. BOOL CALLBACK SimulationCallback(DWORD dwType, DWORD dwValue,
  20. LPCTSTR lpszMsg, void *cookie)
  21. {
  22. LPSIMULATION_INFO pInfo=(LPSIMULATION_INFO)cookie;
  23. if( SIMULATION_PROGRESS == dwType )
  24. {
  25. ::SendMessage( pInfo->hMainWnd, WM_USER_SIMULATION_PROGRESS, dwValue, (LPARAM)lpszMsg );
  26. }
  27. else if( SIMULATION_YIELD == dwType )
  28. {
  29. ::SendMessage( pInfo->hMainWnd, WM_USER_SIMULATION_YIELD, 0, dwValue );
  30. }
  31. // User wants to quit program
  32. if(WAIT_OBJECT_0 == WaitForSingleObject(CSimulation::m_hEventKillSimulationThread,0) )
  33. {
  34. return FALSE;
  35. }
  36. return TRUE;
  37. }
  38. /***
  39. 策略模拟线程主函数
  40. */
  41. UINT SimulationMain(LPVOID pParam)
  42. {
  43. LPSIMULATION_INFO pInfo=(LPSIMULATION_INFO)pParam;
  44. ::PostMessage( pInfo->hMainWnd, WM_USER_SIMULATION_PROGRESS, 0, 0 );
  45. ASSERT( pInfo->pStrategy );
  46. // Run
  47. pInfo->pStrategy->ClearCache( );
  48. BOOL bFinished = pInfo->pStrategy->SimuRun( SimulationCallback, pInfo );
  49. // End OK
  50. ::PostMessage( pInfo->hMainWnd, WM_USER_SIMULATION_END, bFinished, (LPARAM)(pInfo->pStrategy) );
  51. SetEvent(CSimulation::m_hEventSimulationThreadKilled);
  52. AfxEndThread( 0, TRUE );
  53. return 0;
  54. }
  55. CSimulation::CSimulation()
  56. {
  57. m_pSimulationInfo = NULL;
  58. m_pStrategy = NULL;
  59. m_hMainWnd = NULL;
  60. m_bStopAndReset = FALSE;
  61. }
  62. CSimulation::~CSimulation()
  63. {
  64. if( m_pSimulationInfo )
  65. {
  66. delete m_pSimulationInfo;
  67. m_pSimulationInfo = NULL;
  68. }
  69. }
  70. /***
  71. 设定模拟策略
  72. */
  73. void CSimulation::SetStrategy( CStrategy * pStrategy, HWND hMainWnd)
  74. {
  75. m_pStrategy = pStrategy;
  76. m_hMainWnd = hMainWnd;
  77. if( m_pStrategy )
  78. m_pStrategy->ClearCache();
  79. }
  80. /***
  81. 如果模拟备选股票的数据不够用,下载之
  82. */
  83. BOOL CSimulation::DownloadDataIfNeed( )
  84. {
  85. if( NULL == m_pStrategy )
  86. return FALSE;
  87. CSPTime tmInitial, tmPioneer, tmLatest;
  88. CSPTime tmBegin = m_pStrategy->GetOpParam().GetBeginTime();
  89. CSPTime tmEnd = m_pStrategy->GetOpParam().GetEndTime();
  90. CSPTime tmDLBegin = tmBegin, tmDLEnd = tmEnd;
  91. BOOL bNeedDownload = FALSE;
  92. for( int i=0; i<m_pStrategy->GetStocks().GetSize(); i++ )
  93. {
  94. CSPTime tmDLBeginLocal, tmDLEndLocal;
  95. CStockInfo info;
  96. if( AfxGetStockContainer().GetStockInfo( m_pStrategy->GetStocks().ElementAt(i), &info )
  97. && AfxGetDB().GetNeedDownloadRange( info, tmBegin, tmEnd, tmDLBeginLocal, tmDLEndLocal ) )
  98. {
  99. if( !bNeedDownload )
  100. {
  101. tmDLBegin = tmDLBeginLocal;
  102. tmDLEnd = tmDLEndLocal;
  103. }
  104. bNeedDownload = TRUE;
  105. if( tmDLBeginLocal < tmDLBegin )
  106. tmDLBegin = tmDLBeginLocal;
  107. if( tmDLEndLocal > tmDLEnd )
  108. tmDLEnd = tmDLEndLocal;
  109. }
  110. }
  111. if( bNeedDownload )
  112. {
  113. if( IDOK != AfxMessageBox( IDS_SIMULATION_DOWNLOADNOW, MB_YESNO ) )
  114. return TRUE;
  115. CDownloadDlg dlg;
  116. dlg.SetInitDownloadTime( tmDLBegin.GetTime(), tmDLEnd.GetTime(), TRUE );
  117. dlg.DoModal();
  118. }
  119. return TRUE;
  120. }
  121. /***
  122. 重新开始模拟
  123. */
  124. void CSimulation::Restart( )
  125. {
  126. ASSERT( m_pStrategy && m_hMainWnd );
  127. m_bStopAndReset = FALSE;
  128. CSimulation::m_hEventKillSimulationThread = CreateEvent(NULL,FALSE,FALSE,NULL);
  129. CSimulation::m_hEventSimulationThreadKilled = CreateEvent(NULL,FALSE,FALSE,NULL);
  130. if( NULL == m_pSimulationInfo )
  131. m_pSimulationInfo = new SIMULATION_INFO;
  132. memset( m_pSimulationInfo, 0, sizeof(SIMULATION_INFO) );
  133. m_pSimulationInfo->hMainWnd = m_hMainWnd;
  134. m_pSimulationInfo->pStrategy = m_pStrategy;
  135. if( NULL == m_pStrategy )
  136. return;
  137. if( !DownloadDataIfNeed( ) )
  138. return;
  139. m_pStrategy->ClearCache();
  140. m_pStrategy->SimuReset( );
  141. m_pStrategy->SimuSetStatusRunning();
  142. AfxBeginThread( SimulationMain, (LPVOID)m_pSimulationInfo,THREAD_PRIORITY_NORMAL);
  143. }
  144. /***
  145. 暂停模拟
  146. */
  147. void CSimulation::Pause()
  148. {
  149. ASSERT( m_pStrategy && m_hMainWnd );
  150. m_bStopAndReset = FALSE;
  151. // TODO: Add your control notification handler code here
  152. if( CSimulation::m_hEventKillSimulationThread )
  153. SetEvent( CSimulation::m_hEventKillSimulationThread );
  154. // if( CSimulation::m_hEventSimulationThreadKilled )
  155. // WaitForSingleObject( CSimulation::m_hEventSimulationThreadKilled, INFINITE );
  156. if( m_pStrategy )
  157. m_pStrategy->SimuSetStatusPaused();
  158. }
  159. /***
  160. 继续模拟
  161. */
  162. void CSimulation::Continue( )
  163. {
  164. ASSERT( m_pStrategy && m_hMainWnd );
  165. m_bStopAndReset = FALSE;
  166. CSimulation::m_hEventKillSimulationThread = CreateEvent(NULL,FALSE,FALSE,NULL);
  167. CSimulation::m_hEventSimulationThreadKilled = CreateEvent(NULL,FALSE,FALSE,NULL);
  168. if( NULL == m_pSimulationInfo )
  169. m_pSimulationInfo = new SIMULATION_INFO;
  170. memset( m_pSimulationInfo, 0, sizeof(SIMULATION_INFO) );
  171. m_pSimulationInfo->hMainWnd = m_hMainWnd;
  172. m_pSimulationInfo->pStrategy = m_pStrategy;
  173. if( m_pStrategy )
  174. {
  175. m_pStrategy->SimuSetStatusRunning();
  176. AfxBeginThread( SimulationMain, (LPVOID)m_pSimulationInfo,THREAD_PRIORITY_NORMAL);
  177. }
  178. }
  179. /***
  180. 停止模拟并清除结果
  181. */
  182. void CSimulation::Stop()
  183. {
  184. ASSERT( m_pStrategy && m_hMainWnd );
  185. m_bStopAndReset = TRUE;
  186. // TODO: Add your control notification handler code here
  187. if( CSimulation::m_hEventKillSimulationThread )
  188. SetEvent( CSimulation::m_hEventKillSimulationThread );
  189. // if( CSimulation::m_hEventSimulationThreadKilled )
  190. // WaitForSingleObject( CSimulation::m_hEventSimulationThreadKilled, INFINITE );
  191. if( m_pStrategy && m_pStrategy->SimuIsStatusPaused() )
  192. OnEnd( FALSE );
  193. }
  194. /***
  195. 模拟暂停或者停止或者完成消息响应
  196. */
  197. void CSimulation::OnEnd( BOOL bFinished )
  198. {
  199. ASSERT( m_pStrategy && m_hMainWnd );
  200. if( CSimulation::m_hEventKillSimulationThread )
  201. CloseHandle(CSimulation::m_hEventKillSimulationThread);
  202. if( CSimulation::m_hEventSimulationThreadKilled )
  203. CloseHandle(CSimulation::m_hEventSimulationThreadKilled);
  204. CSimulation::m_hEventKillSimulationThread = NULL;
  205. CSimulation::m_hEventSimulationThreadKilled = NULL;
  206. if( bFinished && m_pStrategy )
  207. {
  208. m_pStrategy->SimuSetStatusFinished();
  209. }
  210. if( m_bStopAndReset && m_pStrategy )
  211. {
  212. m_pStrategy->SimuReset( );
  213. m_pStrategy->SimuSetStatusInit();
  214. }
  215. if( m_pStrategy )
  216. {
  217. m_pStrategy->DoFileSave( );
  218. m_pStrategy->ClearCache();
  219. }
  220. }