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

金融证券系统

开发平台:

Visual C++

  1. /*
  2. Cross Platform Core Code.
  3. Copyright(R) 2001-2002 Balang Software.
  4. All rights reserved.
  5. */
  6. #include "StdAfx.h"
  7. #include "../Include/Stock.h"
  8. #include "../Include/Technique.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. // CTechUserDrv
  18. class CTechUserDrv
  19. {
  20. public:
  21. CTechUserDrv();
  22. ~CTechUserDrv();
  23. BOOL LoadDriver( );
  24. UINT GetTechUserCount( );
  25. BOOL GetTechUserInfo( UINT nID, PTECHUSER_INFO pInfo );
  26. BOOL Calculate( UINT nID, PCALCULATE_INFO pInfo );
  27. int GetSignal( UINT nID, PCALCULATE_INFO pInfo );
  28. protected:
  29. UINT (WINAPI * m_pfnGetTechUserCount)( );
  30. BOOL (WINAPI * m_pfnGetTechUserInfo)( UINT nID, PTECHUSER_INFO pInfo );
  31. BOOL (WINAPI * m_pfnCalculate)( UINT nID, PCALCULATE_INFO pInfo );
  32. int (WINAPI * m_pfnGetSignal)( UINT nID, PCALCULATE_INFO pInfo );
  33. HINSTANCE m_hDrv;
  34. };
  35. CTechUserDrv & GetTechUserDrv( )
  36. {
  37. static CTechUserDrv g_techuserdrv;
  38. g_techuserdrv.LoadDriver( );
  39. return g_techuserdrv;
  40. }
  41. CTechUserDrv::CTechUserDrv()
  42. {
  43. m_pfnGetTechUserCount = NULL;
  44. m_pfnGetTechUserInfo = NULL;
  45. m_pfnCalculate = NULL;
  46. m_pfnGetSignal = NULL;
  47. m_hDrv = NULL;
  48. }
  49. CTechUserDrv::~CTechUserDrv()
  50. {
  51. #ifdef SP_WINDOWS
  52. if( m_hDrv )
  53. FreeLibrary( m_hDrv );
  54. #endif
  55. m_pfnGetTechUserCount = NULL;
  56. m_pfnGetTechUserInfo = NULL;
  57. m_pfnCalculate = NULL;
  58. m_pfnGetSignal = NULL;
  59. m_hDrv = NULL;
  60. }
  61. BOOL CTechUserDrv::LoadDriver( )
  62. {
  63. #ifdef SP_WINDOWS
  64. if( !m_hDrv )
  65. {
  66. m_hDrv = ::LoadLibrary( "StkTech.dll" );
  67. if( m_hDrv )
  68. {
  69. m_pfnGetTechUserCount = (UINT (WINAPI *)())GetProcAddress(m_hDrv,"_GetTechUserCount@0");
  70. m_pfnGetTechUserInfo = (BOOL (WINAPI *)(UINT,PTECHUSER_INFO))GetProcAddress(m_hDrv,"_GetTechUserInfo@8");
  71. m_pfnCalculate = (BOOL (WINAPI *)(UINT,PCALCULATE_INFO))GetProcAddress(m_hDrv,"_Calculate@8");
  72. m_pfnGetSignal = (int (WINAPI *)(UINT,PCALCULATE_INFO))GetProcAddress(m_hDrv,"_GetSignal@8");
  73. if( !m_pfnGetTechUserCount )
  74. m_pfnGetTechUserCount = (UINT (WINAPI *)())GetProcAddress(m_hDrv,"GetTechUserCount");
  75. if( !m_pfnGetTechUserInfo )
  76. m_pfnGetTechUserInfo = (BOOL (WINAPI *)(UINT,PTECHUSER_INFO))GetProcAddress(m_hDrv,"GetTechUserInfo");
  77. if( !m_pfnCalculate )
  78. m_pfnCalculate = (BOOL (WINAPI *)(UINT,PCALCULATE_INFO))GetProcAddress(m_hDrv,"Calculate");
  79. if( !m_pfnGetSignal )
  80. m_pfnGetSignal = (int (WINAPI *)(UINT,PCALCULATE_INFO))GetProcAddress(m_hDrv,"GetSignal");
  81. }
  82. }
  83. #endif
  84. return NULL != m_hDrv;
  85. }
  86. UINT CTechUserDrv::GetTechUserCount( )
  87. {
  88. if( !m_pfnGetTechUserCount )
  89. return 0;
  90. return( (*m_pfnGetTechUserCount)());
  91. }
  92. BOOL CTechUserDrv::GetTechUserInfo( UINT nID, PTECHUSER_INFO pInfo )
  93. {
  94. if( !m_pfnGetTechUserInfo )
  95. return FALSE;
  96. return( (*m_pfnGetTechUserInfo)(nID-STT_USER_MIN,pInfo));
  97. }
  98. BOOL CTechUserDrv::Calculate( UINT nID, PCALCULATE_INFO pInfo )
  99. {
  100. if( !m_pfnCalculate )
  101. return FALSE;
  102. return( (*m_pfnCalculate)(nID-STT_USER_MIN,pInfo));
  103. }
  104. int CTechUserDrv::GetSignal( UINT nID, PCALCULATE_INFO pInfo )
  105. {
  106. if( !m_pfnCalculate )
  107. return ITS_NOTHING;
  108. return( (*m_pfnGetSignal)(nID-STT_USER_MIN,pInfo));
  109. }
  110. //////////////////////////////////////////////////////////////////////
  111. // CTechUser
  112. //  static functions
  113. UINT CTechUser::GetTechUserCount()
  114. {
  115. return GetTechUserDrv().GetTechUserCount();
  116. }
  117. CSPString CTechUser::GetTechUserName( int nTech )
  118. {
  119. TECHUSER_INFO info;
  120. memset( &info, 0, sizeof(info) );
  121. info.m_nStructSize = sizeof(info);
  122. if( GetTechUserDrv().GetTechUserInfo( nTech, &info ) )
  123. return info.m_szName;
  124. return "";
  125. }
  126. CSPString CTechUser::GetTechUserShortName( int nTech )
  127. {
  128. TECHUSER_INFO info;
  129. memset( &info, 0, sizeof(info) );
  130. info.m_nStructSize = sizeof(info);
  131. if( GetTechUserDrv().GetTechUserInfo( nTech, &info ) )
  132. return info.m_szShortName;
  133. return "";
  134. }
  135. CTechUser::CTechUser( )
  136. {
  137. SetDefaultParameters( );
  138. }
  139. CTechUser::CTechUser( CKData * pKData )
  140. : CTechnique( pKData )
  141. {
  142. SetDefaultParameters( );
  143. }
  144. CTechUser::~CTechUser()
  145. {
  146. Clear( );
  147. }
  148. BOOL CTechUser::SetTechUserID( int nTechUserID )
  149. {
  150. m_nTechUserID = nTechUserID;
  151. if( m_nTechUserID >= STT_USER_MIN
  152. && m_nTechUserID-STT_USER_MIN<CTechUser::GetTechUserCount() )
  153. return GetTechUserDrv().GetTechUserInfo( m_nTechUserID, &m_techuserinfo );
  154. return FALSE;
  155. }
  156. UINT CTechUser::GetTechUserID( )
  157. {
  158. return m_nTechUserID;
  159. }
  160. void CTechUser::SetStock( CStock * pStock )
  161. {
  162. m_pStock = pStock;
  163. }
  164. UINT CTechUser::GetLineCount( )
  165. {
  166. if( 1 == m_techuserinfo.m_nLineCount && m_techuserinfo.m_bAutoMA )
  167. return 2;
  168. return m_techuserinfo.m_nLineCount;
  169. }
  170. CSPString CTechUser::GetLineName( int nLine )
  171. {
  172. switch( nLine )
  173. {
  174. case 1: return m_techuserinfo.m_aszLineName[0];
  175. case 2:
  176. if( 1 == m_techuserinfo.m_nLineCount && m_techuserinfo.m_bAutoMA )
  177. return "MA";
  178. return m_techuserinfo.m_aszLineName[1];
  179. case 3: return m_techuserinfo.m_aszLineName[2];
  180. case 4: return m_techuserinfo.m_aszLineName[3];
  181. case 5: return m_techuserinfo.m_aszLineName[4];
  182. default:;
  183. }
  184. return "";
  185. }
  186. void CTechUser::SetDefaultParameters( )
  187. {
  188. m_nTechUserID = -1;
  189. memset( &m_techuserinfo, 0, sizeof(m_techuserinfo) );
  190. }
  191. void CTechUser::AttachParameters( CTechUser & src )
  192. {
  193. m_nTechUserID = src.m_nTechUserID;
  194. memcpy( &m_techuserinfo, &(src.m_techuserinfo), sizeof(m_techuserinfo) );
  195. }
  196. BOOL CTechUser::IsValidParameters( )
  197. {
  198. return ( m_nTechUserID >= STT_USER_MIN
  199. && m_nTechUserID-STT_USER_MIN<CTechUser::GetTechUserCount() );
  200. }
  201. void CTechUser::Clear( )
  202. {
  203. CTechnique::Clear( );
  204. }
  205. int CTechUser::GetSignal( int nIndex, UINT * pnCode )
  206. {
  207. if( pnCode ) *pnCode = ITSC_NOTHING;
  208. if( ( m_techuserinfo.m_nLineCount == 2 || (m_techuserinfo.m_nLineCount == 1 && m_techuserinfo.m_bAutoMA ) )
  209. && ( ITS_NOTHING != m_techuserinfo.m_itsGoldenFork || ITS_NOTHING != m_techuserinfo.m_itsDeadFork ) )
  210. {
  211. int nMaxDays = 0;
  212. if( m_techuserinfo.m_nLineCount == 1 && m_techuserinfo.m_bAutoMA )
  213. nMaxDays = m_techuserinfo.m_nMADays;
  214. double dLiminalLow = 0, dLiminalHigh = 0;
  215. if( !IntensityPrepare( nIndex, pnCode, nMaxDays, ITS_GETMINMAXDAYRANGE, &dLiminalLow, &dLiminalHigh, 0.3, 0.7 ) )
  216. return ITS_NOTHING;
  217. if( nIndex <= 1 )
  218. return ITS_NOTHING;
  219. double dValue1, dValue2;
  220. if( !Calculate( &dValue1, &dValue2, nIndex, FALSE ) )
  221. return ITS_NOTHING;
  222. int nSignal = GetForkSignal( nIndex, m_techuserinfo.m_itsGoldenFork, m_techuserinfo.m_itsDeadFork, pnCode );
  223. if( dValue1 < dLiminalLow && nSignal == ITS_BUY )
  224. { // 低位金叉
  225. if( pnCode ) *pnCode = ITSC_GOLDENFORK;
  226. return ITS_BUY;
  227. }
  228. if( dValue1 > dLiminalHigh && nSignal == ITS_SELL )
  229. { // 高位死叉
  230. if( pnCode ) *pnCode = ITSC_DEADFORK;
  231. return ITS_SELL;
  232. }
  233. }
  234. CALCULATE_INFO info;
  235. memset( &info, 0, sizeof(info) );
  236. info.m_nStructSize = sizeof(info);
  237. info.m_pStock = m_pStock;
  238. info.m_pStockInfo = &(m_pStock->GetStockInfo());
  239. info.m_pKData = m_pKData;
  240. info.m_nIndex = nIndex;
  241. int nSignal = ITS_NOTHING;
  242. if( GetTechUserDrv().GetSignal( m_nTechUserID, &info ) )
  243. {
  244. nSignal = info.m_nSignal;
  245. if( nSignal > ITS_NOTHING && pnCode )
  246. *pnCode = ITSC_LONG;
  247. if( nSignal < ITS_NOTHING && pnCode )
  248. *pnCode = ITSC_SHORT;
  249. }
  250. return nSignal;
  251. }
  252. // 得到趋势信号
  253. int CTechUser::GetIntensity( int nIndex, UINT * pnCode, BOOL bTraceBack )
  254. {
  255. return CTechnique::GetIntensity( nIndex, pnCode, bTraceBack );
  256. }
  257. int CTechUser::GetIntensityTraceback( int nIndex, UINT * pnCode )
  258. {
  259. return CTechnique::GetIntensityTraceback( nIndex, pnCode );
  260. }
  261. BOOL CTechUser::GetMinMaxInfo(int nStart, int nEnd,
  262.    double *pdMin, double *pdMax )
  263. {
  264. switch( m_techuserinfo.m_nLineCount )
  265. {
  266. case 1:
  267. return AfxGetMinMaxInfo1( nStart, nEnd, pdMin, pdMax, this );
  268. case 2:
  269. return AfxGetMinMaxInfo2( nStart, nEnd, pdMin, pdMax, this );
  270. case 3:
  271. return AfxGetMinMaxInfo3( nStart, nEnd, pdMin, pdMax, this );
  272. case 4:
  273. return AfxGetMinMaxInfo4( nStart, nEnd, pdMin, pdMax, this );
  274. case 5:
  275. return AfxGetMinMaxInfo5( nStart, nEnd, pdMin, pdMax, this );
  276. default:;
  277. }
  278. return FALSE;
  279. }
  280. BOOL CTechUser::Calculate( double * pValue, int nIndex, BOOL bUseLast )
  281. {
  282. STT_ASSERT_CALCULATE1( m_pKData, nIndex );
  283. SP_ASSERT( 1 == m_techuserinfo.m_nLineCount );
  284. if( 1 != m_techuserinfo.m_nLineCount )
  285. return FALSE;
  286. if( LoadFromCache( nIndex, pValue ) )
  287. return TRUE;
  288. CALCULATE_INFO info;
  289. memset( &info, 0, sizeof(info) );
  290. info.m_nStructSize = sizeof(info);
  291. info.m_pStock = m_pStock;
  292. info.m_pStockInfo = &(m_pStock->GetStockInfo());
  293. info.m_pKData = m_pKData;
  294. info.m_nIndex = nIndex;
  295. info.m_bUseLast = bUseLast;
  296. if( bUseLast )
  297. info.m_dValue1 = *pValue;
  298. if( !GetTechUserDrv().Calculate( m_nTechUserID, &info ) )
  299. return FALSE;
  300. if( pValue )
  301. *pValue = info.m_dValue1;
  302. StoreToCache( nIndex, pValue );
  303. return TRUE;
  304. }
  305. BOOL CTechUser::Calculate( double * pValue1, double * pValue2, int nIndex, BOOL bUseLast )
  306. {
  307. if( 1 == m_techuserinfo.m_nLineCount && m_techuserinfo.m_bAutoMA )
  308. return CalculateMA( pValue1, pValue2, nIndex, bUseLast, m_techuserinfo.m_nMADays );
  309. STT_ASSERT_CALCULATE1( m_pKData, nIndex );
  310. SP_ASSERT( 2 == m_techuserinfo.m_nLineCount );
  311. if( 2 != m_techuserinfo.m_nLineCount )
  312. return FALSE;
  313. if( LoadFromCache( nIndex, pValue1, pValue2 ) )
  314. return TRUE;
  315. CALCULATE_INFO info;
  316. memset( &info, 0, sizeof(info) );
  317. info.m_nStructSize = sizeof(info);
  318. info.m_pStock = m_pStock;
  319. info.m_pStockInfo = &(m_pStock->GetStockInfo());
  320. info.m_pKData = m_pKData;
  321. info.m_nIndex = nIndex;
  322. info.m_bUseLast = bUseLast;
  323. if( bUseLast )
  324. {
  325. info.m_dValue1 = *pValue1;
  326. info.m_dValue2 = *pValue2;
  327. }
  328. if( !GetTechUserDrv().Calculate( m_nTechUserID, &info ) )
  329. return FALSE;
  330. if( pValue1 ) *pValue1 = info.m_dValue1;
  331. if( pValue2 ) *pValue2 = info.m_dValue2;
  332. StoreToCache( nIndex, pValue1, pValue2 );
  333. return TRUE;
  334. }
  335. BOOL CTechUser::Calculate( double * pValue1, double * pValue2, double * pValue3, int nIndex, BOOL bUseLast )
  336. {
  337. STT_ASSERT_CALCULATE1( m_pKData, nIndex );
  338. SP_ASSERT( 3 == m_techuserinfo.m_nLineCount );
  339. if( 3 != m_techuserinfo.m_nLineCount )
  340. return FALSE;
  341. if( LoadFromCache( nIndex, pValue1, pValue2, pValue3 ) )
  342. return TRUE;
  343. CALCULATE_INFO info;
  344. memset( &info, 0, sizeof(info) );
  345. info.m_nStructSize = sizeof(info);
  346. info.m_pStock = m_pStock;
  347. info.m_pStockInfo = &(m_pStock->GetStockInfo());
  348. info.m_pKData = m_pKData;
  349. info.m_nIndex = nIndex;
  350. info.m_bUseLast = bUseLast;
  351. if( bUseLast )
  352. {
  353. info.m_dValue1 = *pValue1;
  354. info.m_dValue2 = *pValue2;
  355. info.m_dValue3 = *pValue3;
  356. }
  357. if( !GetTechUserDrv().Calculate( m_nTechUserID, &info ) )
  358. return FALSE;
  359. if( pValue1 ) *pValue1 = info.m_dValue1;
  360. if( pValue2 ) *pValue2 = info.m_dValue2;
  361. if( pValue3 ) *pValue3 = info.m_dValue3;
  362. StoreToCache( nIndex, pValue1, pValue2, pValue3 );
  363. return TRUE;
  364. }
  365. BOOL CTechUser::Calculate( double * pValue1, double * pValue2, double * pValue3, double *pValue4, int nIndex, BOOL bUseLast )
  366. {
  367. STT_ASSERT_CALCULATE1( m_pKData, nIndex );
  368. SP_ASSERT( 4 == m_techuserinfo.m_nLineCount );
  369. if( 4 == m_techuserinfo.m_nLineCount )
  370. return FALSE;
  371. if( LoadFromCache( nIndex, pValue1, pValue2, pValue3, pValue4 ) )
  372. return TRUE;
  373. CALCULATE_INFO info;
  374. memset( &info, 0, sizeof(info) );
  375. info.m_nStructSize = sizeof(info);
  376. info.m_pStock = m_pStock;
  377. info.m_pStockInfo = &(m_pStock->GetStockInfo());
  378. info.m_pKData = m_pKData;
  379. info.m_nIndex = nIndex;
  380. info.m_bUseLast = bUseLast;
  381. if( bUseLast )
  382. {
  383. info.m_dValue1 = *pValue1;
  384. info.m_dValue2 = *pValue2;
  385. info.m_dValue3 = *pValue3;
  386. info.m_dValue4 = *pValue4;
  387. }
  388. if( !GetTechUserDrv().Calculate( m_nTechUserID, &info ) )
  389. return FALSE;
  390. if( pValue1 ) *pValue1 = info.m_dValue1;
  391. if( pValue2 ) *pValue2 = info.m_dValue2;
  392. if( pValue3 ) *pValue3 = info.m_dValue3;
  393. if( pValue4 ) *pValue4 = info.m_dValue4;
  394. StoreToCache( nIndex, pValue1, pValue2, pValue3, pValue4 );
  395. return TRUE;
  396. }
  397. BOOL CTechUser::Calculate( double * pValue1, double * pValue2, double * pValue3, double *pValue4, double *pValue5, int nIndex, BOOL bUseLast )
  398. {
  399. STT_ASSERT_CALCULATE1( m_pKData, nIndex );
  400. SP_ASSERT( 5 == m_techuserinfo.m_nLineCount );
  401. if( 5 == m_techuserinfo.m_nLineCount )
  402. return FALSE;
  403. if( LoadFromCache( nIndex, pValue1, pValue2, pValue3, pValue4, pValue5 ) )
  404. return TRUE;
  405. CALCULATE_INFO info;
  406. memset( &info, 0, sizeof(info) );
  407. info.m_nStructSize = sizeof(info);
  408. info.m_pStock = m_pStock;
  409. info.m_pStockInfo = &(m_pStock->GetStockInfo());
  410. info.m_pKData = m_pKData;
  411. info.m_nIndex = nIndex;
  412. info.m_bUseLast = bUseLast;
  413. if( bUseLast )
  414. {
  415. info.m_dValue1 = *pValue1;
  416. info.m_dValue2 = *pValue2;
  417. info.m_dValue3 = *pValue3;
  418. info.m_dValue4 = *pValue4;
  419. info.m_dValue5 = *pValue5;
  420. }
  421. if( !GetTechUserDrv().Calculate( m_nTechUserID, &info ) )
  422. return FALSE;
  423. if( pValue1 ) *pValue1 = info.m_dValue1;
  424. if( pValue2 ) *pValue2 = info.m_dValue2;
  425. if( pValue3 ) *pValue3 = info.m_dValue3;
  426. if( pValue4 ) *pValue4 = info.m_dValue4;
  427. if( pValue5 ) *pValue5 = info.m_dValue5;
  428. StoreToCache( nIndex, pValue1, pValue2, pValue3, pValue4, pValue5 );
  429. return TRUE;
  430. }