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

金融证券系统

开发平台:

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. #ifdef _DEBUG
  10. #undef THIS_FILE
  11. static char THIS_FILE[] = __FILE__;
  12. #endif
  13. #ifdef _DEBUG
  14. #define new DEBUG_NEW
  15. #endif
  16. //////////////////////////////////////////////////////////////////////
  17. // allocate and free memory for COMMPACKET
  18. PCOMMPACKET AllocCommPacket( DWORD dwDataType, DWORD dwCount )
  19. {
  20. DWORD dwUnitSize = 0;
  21. switch( dwDataType )
  22. {
  23. case CStock::dataDR:
  24. dwUnitSize = sizeof(DRDATA);
  25. break;
  26. case CStock::dataK:
  27. dwUnitSize = sizeof(KDATA);
  28. break;
  29. case CStock::dataReport:
  30. dwUnitSize = sizeof(REPORT);
  31. break;
  32. case CStock::dataMinute:
  33. dwUnitSize = sizeof(MINUTE);
  34. break;
  35. case CStock::dataMultisort:
  36. dwUnitSize = sizeof(MULTISORT);
  37. break;
  38. case CStock::dataOutline:
  39. dwUnitSize = sizeof(OUTLINE);
  40. break;
  41. case CStock::dataCode:
  42. dwUnitSize = sizeof(STOCKCODE);
  43. break;
  44. default:
  45. SP_ASSERT(FALSE);
  46. }
  47. DWORD nSize = sizeof(COMMPACKET)+dwCount*dwUnitSize;
  48. COMMPACKET * pCommPacket = (COMMPACKET *)new BYTE[nSize];
  49. if( NULL == pCommPacket )
  50. return NULL;
  51. memset( pCommPacket, 0, nSize );
  52. pCommPacket->m_dwTag = STKLIB_COMMPACKET_TAG;
  53. pCommPacket->m_dwDataType = dwDataType;
  54. pCommPacket->m_dwCount = dwCount;
  55. pCommPacket->m_pData = (void *)( pCommPacket + 1 );
  56. if( 0 == dwCount )
  57. pCommPacket->m_pData = NULL;
  58. return pCommPacket;
  59. }
  60. void FreeCommPacket( PCOMMPACKET pCommPacket )
  61. {
  62. SP_ASSERT( pCommPacket );
  63. if( pCommPacket )
  64. delete [] (BYTE *)pCommPacket;
  65. }
  66. BOOL convert_REPORT_to_MINUTE( REPORT * pReport, MINUTE * pMinute )
  67. {
  68. SP_ASSERT( pReport && pMinute );
  69. if( NULL == pReport || NULL == pMinute )
  70. return FALSE;
  71. memset( pMinute, 0, sizeof(MINUTE) );
  72. pMinute->m_dwType = 1; // 1 min
  73. pMinute->m_dwMarket = pReport->m_dwMarket;
  74. strncpy( pMinute->m_szCode, pReport->m_szCode, min(sizeof(pMinute->m_szCode)-1,sizeof(pReport->m_szCode)) );
  75. time_t temp = 60 * (pReport->m_time/60);
  76. if( temp < pReport->m_time )
  77. temp += 60;
  78. pMinute->m_time = temp;
  79. pMinute->m_fNew = pReport->m_fNew;
  80. pMinute->m_fHigh = pReport->m_fHigh;
  81. pMinute->m_fLow = pReport->m_fLow;
  82. pMinute->m_fVolume = pReport->m_fVolume;
  83. pMinute->m_fAmount = pReport->m_fAmount;
  84. return TRUE;
  85. }
  86. /////////////////////////////////////////////////////////////////////////////
  87. // Updator
  88. // update KDATA by Report data
  89. BOOL UpdateKDATAByREPORT( KDATA &kd, REPORT * pReport )
  90. {
  91. SP_ASSERT( pReport );
  92. if( NULL == pReport )
  93. return FALSE;
  94. memset( &kd, 0, sizeof(kd) );
  95. kd.m_dwMarket = pReport->m_dwMarket;
  96. strncpy( kd.m_szCode, pReport->m_szCode, min(sizeof(kd.m_szCode)-1,sizeof(pReport->m_szCode)) );
  97. kd.m_time = pReport->m_time;
  98. if( 0 == pReport->m_time || -1 == pReport->m_time )
  99. kd.m_date = CSPTime::GetCurrentTime().ToStockTimeDay();
  100. else
  101. kd.m_date = CSPTime(pReport->m_time).ToStockTimeDay();
  102. kd.m_fOpen = pReport->m_fOpen;
  103. kd.m_fHigh = pReport->m_fHigh;
  104. kd.m_fLow = pReport->m_fLow;
  105. kd.m_fClose = pReport->m_fNew;
  106. kd.m_fAmount = pReport->m_fAmount;
  107. kd.m_fVolume = pReport->m_fVolume;
  108. return TRUE;
  109. }
  110. // update CStockInfo by Report data
  111. BOOL UpdateStockInfoByREPORT( CStockInfo & info, REPORT * pReport )
  112. {
  113. SP_ASSERT( pReport );
  114. if( !pReport )
  115. return FALSE;
  116. // 股票市场
  117. if( strlen(pReport->m_szCode) > 0 )
  118. info.SetStockCode( pReport->m_dwMarket, pReport->m_szCode );
  119. if( strlen(pReport->m_szName) > 0  )
  120. info.SetStockName( pReport->m_szName );
  121. if( info.GetType() == 0 )
  122. {
  123. if( CStock::marketSHSE == pReport->m_dwMarket )
  124. info.SetType( CStock::typeshA );
  125. else if( CStock::marketSZSE == pReport->m_dwMarket )
  126. info.SetType( CStock::typeszA );
  127. else
  128. info.SetType( CStock::typeshA );
  129. }
  130. // 成交买卖价量信息
  131. if( pReport->m_fLast > 1e-4 ) info.m_fLast = pReport->m_fLast;
  132. info.m_fOpen = pReport->m_fOpen;
  133. info.m_fHigh = pReport->m_fHigh;
  134. info.m_fLow = pReport->m_fLow;
  135. info.m_fClose = pReport->m_fNew;
  136. info.m_fVolume = pReport->m_fVolume;
  137. info.m_fAmount = pReport->m_fAmount;
  138. info.m_fBuyPrice[0] = pReport->m_fBuyPrice[0];
  139. info.m_fBuyPrice[1] = pReport->m_fBuyPrice[1];
  140. info.m_fBuyPrice[2] = pReport->m_fBuyPrice[2];
  141. info.m_fBuyPrice[3] = pReport->m_fBuyPrice[3];
  142. info.m_fBuyPrice[4] = pReport->m_fBuyPrice[4];
  143. info.m_fBuyVolume[0] = pReport->m_fBuyVolume[0];
  144. info.m_fBuyVolume[1] = pReport->m_fBuyVolume[1];
  145. info.m_fBuyVolume[2] = pReport->m_fBuyVolume[2];
  146. info.m_fBuyVolume[3] = pReport->m_fBuyVolume[3];
  147. info.m_fBuyVolume[4] = pReport->m_fBuyVolume[4];
  148. info.m_fSellPrice[0] = pReport->m_fSellPrice[0];
  149. info.m_fSellPrice[1] = pReport->m_fSellPrice[1];
  150. info.m_fSellPrice[2] = pReport->m_fSellPrice[2];
  151. info.m_fSellPrice[3] = pReport->m_fSellPrice[3];
  152. info.m_fSellPrice[4] = pReport->m_fSellPrice[4];
  153. info.m_fSellVolume[0] = pReport->m_fSellVolume[0];
  154. info.m_fSellVolume[1] = pReport->m_fSellVolume[1];
  155. info.m_fSellVolume[2] = pReport->m_fSellVolume[2];
  156. info.m_fSellVolume[3] = pReport->m_fSellVolume[3];
  157. info.m_fSellVolume[4] = pReport->m_fSellVolume[4];
  158. // K线数据、日期
  159. KDATA kd;
  160. UpdateKDATAByREPORT( kd, pReport );
  161. int nLen = info.m_kdata.GetSize();
  162. if( nLen > 0 && info.m_kdata.ElementAt(nLen-1).m_date == kd.m_date )
  163. info.m_kdata.SetAt( nLen-1, kd );
  164. else
  165. info.m_kdata.Add( kd );
  166. info.m_datetech = kd.m_date;
  167. // 保存
  168. memcpy( &(info.m_reportLatest), pReport, sizeof(info.m_reportLatest) );
  169. return TRUE;
  170. }
  171. // update CStockContainer by Report data
  172. BOOL UpdateStockContainerByREPORT( CStockContainer &container, REPORT * pReport, BOOL bAddIfNotExist, REPORT * pReportLast )
  173. {
  174. SP_ASSERT( pReport );
  175. if( NULL == pReport )
  176. return FALSE;
  177. REPORT reportLast;
  178. memset( &reportLast, 0, sizeof(reportLast) );
  179. int id = 0;
  180. if( container.GetStockInfo( pReport->m_szCode, NULL, &id ) )
  181. {
  182. container.Lock();
  183. CStockInfo & info = container.ElementAt(id);
  184. reportLast = info.m_reportLatest;
  185. UpdateStockInfoByREPORT( info, pReport );
  186. container.UnLock();
  187. }
  188. else if( bAddIfNotExist && strlen(pReport->m_szCode)>0 )
  189. {
  190. CStockInfo info;
  191. info.SetStockCode( pReport->m_dwMarket, pReport->m_szCode );
  192. if( UpdateStockInfoByREPORT( info, pReport ) )
  193. {
  194. container.Add( info );
  195. }
  196. }
  197. if( pReportLast )
  198. memcpy( pReportLast, &reportLast, sizeof(reportLast) );
  199. return TRUE;
  200. }
  201. BOOL UpdateStockContainerByKData( CStockContainer &container, LPCTSTR lpszCode, CKData & kdata )
  202. {
  203. container.Lock();
  204. int id = 0;
  205. if( CKData::ktypeDay == kdata.GetKType()
  206. && container.GetStockInfo( lpszCode, NULL, &id ) )
  207. {
  208. CStockInfo & info = container.ElementAt(id);
  209. if( kdata.GetSize() > (int)AfxGetProfile().GetCacheDays() )
  210. {
  211. // 只使用AfxGetProfile().GetCacheDays()天的数据
  212. CKData temp( kdata.GetKType() );
  213. for( int i=kdata.GetSize()-AfxGetProfile().GetCacheDays(); i<kdata.GetSize(); i++ )
  214. temp.Add( kdata.ElementAt(i) );
  215. info.m_kdata.MergeKData( &temp );
  216. }
  217. else
  218. info.m_kdata.MergeKData( &kdata );
  219. // 指数涨跌家数
  220. int nSize = info.m_kdata.GetSize();
  221. if( nSize > 0 )
  222. {
  223. info.m_dwAdvance = info.m_kdata.ElementAt(nSize-1).m_dwAdvance;
  224. info.m_dwDecline = info.m_kdata.ElementAt(nSize-1).m_dwDecline;
  225. }
  226. }
  227. container.UnLock();
  228. return TRUE;
  229. }