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

金融证券系统

开发平台:

Visual C++

  1. /*
  2. Cross Platform Core Code.
  3. Copyright(R) 2001-2002 Balang Software.
  4. All rights reserved.
  5. Using:
  6. Packet convertor functions;
  7. */
  8. #include "StdAfx.h"
  9. #include "../Include/DayTrader.h"
  10. #ifdef _DEBUG
  11. #undef THIS_FILE
  12. static char THIS_FILE[] = __FILE__;
  13. #endif
  14. #ifdef _DEBUG
  15. #define new DEBUG_NEW
  16. #endif
  17. //////////////////////////////////////////////////////////////////////
  18. // CDayTrader
  19. CDayTrader::CDayTrader()
  20. {
  21. }
  22. CDayTrader::~CDayTrader()
  23. {
  24. }
  25. BOOL CDayTrader::SetMaxStocks( int nMaxStocks )
  26. {
  27. if( nMaxStocks > 0 )
  28. {
  29. m_opparam.m_nStoreDiv = nMaxStocks;
  30. return TRUE;
  31. }
  32. return FALSE;
  33. }
  34. BOOL CDayTrader::BuyIt( CSPTime tmCur, CTechStock & techstock )
  35. {
  36. OPRECORD record;
  37. memset( &record, 0, sizeof(record) );
  38. record.lOpType = STRATEGY_OPTYPE_BUY;
  39. record.bViewed = TRUE;
  40. record.time = tmCur.GetTime();
  41. record.dwMarket = techstock.m_info.GetMarket();
  42. strncpy( record.szCode, techstock.m_info.GetStockCode(),
  43.  min(sizeof(record.szCode)-1,strlen(techstock.m_info.GetStockCode())) );
  44. if( m_opparam.m_nStoreDiv-m_SimuStockOwn.GetSize() <= 0 ) // 股票已经够多了,不能再买入新的了
  45. return FALSE;
  46. double dUseCash = m_SimuCurrentCash / (m_opparam.m_nStoreDiv-m_SimuStockOwn.GetSize()); // 需使用资金
  47. if( m_SimuCurrentCash < dUseCash )
  48. dUseCash = m_SimuCurrentCash; // 资金不够,则有多少用多少
  49. double dSharePrice = 0;
  50. if( !techstock.GetClosePrice( tmCur, &dSharePrice ) )
  51. return FALSE;
  52. double dTemp = dSharePrice * ( 1 + m_rate.GetRate( techstock.m_info ) );
  53. if( fabs(dTemp) < 1e-4 || dUseCash < 1e-4 )
  54. return FALSE;
  55. DWORD dwShare = (DWORD)( dUseCash / dTemp ); // 买入股数
  56. dwShare = ( dwShare / 100 ) * 100; // 取整
  57. if( 0 == dwShare )
  58. return FALSE;
  59. record.dwShare = dwShare;
  60. record.dSharePrice = dSharePrice;
  61. record.dRateCost = record.dwShare * record.dSharePrice * m_rate.GetRate(techstock.m_info);
  62. return SimuOperate( record );
  63. }
  64. BOOL CDayTrader::SellIt( CSPTime tmCur, CTechStock & techstock )
  65. {
  66. OPRECORD record;
  67. memset( &record, 0, sizeof(record) );
  68. record.lOpType = STRATEGY_OPTYPE_SELL;
  69. record.bViewed = TRUE;
  70. record.time = tmCur.GetTime();
  71. record.dwMarket = techstock.m_info.GetMarket();
  72. strncpy( record.szCode, techstock.m_info.GetStockCode(),
  73.  min(sizeof(record.szCode)-1,strlen(techstock.m_info.GetStockCode())) );
  74. double dSharePrice = 0;
  75. if( !techstock.GetClosePrice( tmCur, &dSharePrice ) )
  76. return FALSE;
  77. STOCKOWN own;
  78. memset( &own, 0, sizeof(own) );
  79. if( !m_SimuStockOwn.HasThisStock( techstock.m_info, &own ) )
  80. return FALSE;
  81. DWORD dwShare = own.dwShare; // 卖出股数
  82. record.dwShare = dwShare;
  83. record.dSharePrice = dSharePrice;
  84. record.dRateCost = record.dwShare * record.dSharePrice * m_rate.GetRate(techstock.m_info);
  85. return SimuOperate( record );
  86. }
  87. BOOL CDayTrader::SimuRun( SIMULATION_CALLBACK fnCallback, void * cookie )
  88. {
  89. // 准备数据
  90. if( !PrepareData( fnCallback, cookie ) )
  91. return FALSE;
  92. if( m_techstocks.GetSize() <= m_opparam.m_nStoreDiv )
  93. {
  94. SP_ASSERT( FALSE );
  95. }
  96. ClearLastIntensity( );
  97. int nCount = 0;
  98. int nSeperate = ::GetPrivateProfileInt( "DayTrader", "Seperate", 5, AfxGetProfile().GetWorkDirectory()+"DayTrader.ini" );
  99. char szGap[128];
  100. memset( szGap, 0, sizeof(szGap) );
  101. ::GetPrivateProfileString( "DayTrader", "Gap", "5.0", szGap, sizeof(szGap)-2, AfxGetProfile().GetWorkDirectory()+"DayTrader.ini" );
  102. double dGap = atof(szGap);
  103. do {
  104. CSPTime tmCur = SimuGetCurrentTime(); // 模拟当前时间
  105. int nMaxDiffPercent = -1;
  106. double dMaxDiffPercent = -100; // 最大涨幅%
  107. int nMinDiffPercent = -1;
  108. double dMinDiffPercent = 100; // 最大跌幅%
  109. // Find
  110. for( int i=0; i<m_techstocks.GetSize(); i++ ) // 每只股票依次判断
  111. {
  112. CTechStock & techstock = m_techstocks.ElementAt(i);
  113. if( techstock.IsStopTrading(tmCur) ) // 停牌吗
  114. continue;
  115. // Judge Whether to operate, if yes, save to nextop
  116. STOCKOWN own;
  117. memset( &own, 0, sizeof(own) );
  118. if( m_SimuStockOwn.HasThisStock( techstock.m_info, &own ) ) // 如果已经有这支股票,判断是否卖出
  119. {
  120. if( m_SimuStockOwn.GetSize() >= m_opparam.m_nStoreDiv )
  121. {
  122. double dDiffPercent;
  123. if( techstock.GetCloseDiffPercent( tmCur, &dDiffPercent )
  124. && (dDiffPercent > dMaxDiffPercent || -1 == nMaxDiffPercent) )
  125. {
  126. dMaxDiffPercent = dDiffPercent;
  127. nMaxDiffPercent = i;
  128. }
  129. }
  130. }
  131. else if( m_SimuStockOwn.GetSize() <= m_opparam.m_nStoreDiv )
  132. {
  133. double dDiffPercent;
  134. if( techstock.GetCloseDiffPercent( tmCur, &dDiffPercent )
  135. && (dDiffPercent < dMinDiffPercent || -1 == nMinDiffPercent) )
  136. {
  137. dMinDiffPercent = dDiffPercent;
  138. nMinDiffPercent = i;
  139. }
  140. }
  141. }
  142. // operate
  143. if( -1 != nMaxDiffPercent && -1 != nMinDiffPercent )
  144. {
  145. double dRate = m_rate.GetRate( m_techstocks.ElementAt(nMaxDiffPercent).m_info )
  146. + m_rate.GetRate( m_techstocks.ElementAt(nMinDiffPercent).m_info );
  147. if( dMaxDiffPercent - dMinDiffPercent <= dGap + dRate * 100 )
  148. {
  149. nMaxDiffPercent = nMinDiffPercent = -1;
  150. }
  151. }
  152. nCount ++;
  153. if( -1 != nMaxDiffPercent && -1 != nMinDiffPercent && nCount >= nSeperate )
  154. {
  155. nCount = 0;
  156. SellIt( tmCur, m_techstocks.ElementAt(nMaxDiffPercent) );
  157. }
  158. if( -1 != nMinDiffPercent )
  159. BuyIt( tmCur, m_techstocks.ElementAt(nMinDiffPercent) );
  160. // 进度显示
  161. DWORD dwProgress = SimuGetCurrentProgress( STRATEGY_MAX_PROGRESS );
  162. double dYield = SimuGetCurrentYield( );
  163. if( fnCallback && !fnCallback( SIMULATION_PROGRESS, dwProgress, NULL, cookie ) )
  164. return FALSE;
  165. if( fnCallback && !fnCallback( SIMULATION_YIELD, (DWORD)dYield, NULL, cookie ) )
  166. return FALSE;
  167. } while( SimuGotoNextTime() ); // 模拟的下一个交易日
  168. if( fnCallback )
  169. fnCallback( SIMULATION_PROGRESS, STRATEGY_MAX_PROGRESS, NULL, cookie );
  170. return TRUE;
  171. }