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

金融证券系统

开发平台:

Visual C++

  1. // strings.cpp : implementation file
  2. //
  3. // This is a part of the Microsoft Foundation Classes C++ library.
  4. // Copyright (C) 1992-1997 Microsoft Corporation
  5. // All rights reserved.
  6. //
  7. // This source code is only intended as a supplement to the
  8. // Microsoft Foundation Classes Reference and related
  9. // electronic documentation provided with the library.
  10. // See these sources for detailed information regarding the
  11. // Microsoft Foundation Classes product.
  12. #include "StdAfx.h"
  13. #include "../Include/CommFuns.h"
  14. #include "math.h"
  15. BOOL CalculateYieldYear( CKData & kday, double * paverage, double * pstddev, long ldays, int nDayCurrent )
  16. {
  17. if( -1 != nDayCurrent && nDayCurrent >= kday.GetSize() )
  18. return FALSE;
  19. int nDayCount = 0;
  20. double yieldSum = 0., yieldSquareSum = 0.;
  21. if( -1 == nDayCurrent )
  22. nDayCurrent = kday.GetSize()-1;
  23. int nDayEnd = ( -1 == ldays ? 0 : nDayCurrent-ldays );
  24. if( nDayEnd - STKLIB_DAYS_INONEYEAR < 0 )
  25. return FALSE;
  26. for( int nDay = nDayCurrent; nDay >= nDayEnd; nDay -- )
  27. {
  28. int nDayLast = nDay - STKLIB_DAYS_INONEYEAR;
  29. if( nDayLast < 0 )
  30. break;
  31. if( kday.MaindataAt(nDayLast) > 1e-4 )
  32. {
  33. nDayCount ++;
  34. double yieldNow = (double( kday.MaindataAt(nDay) - kday.MaindataAt(nDayLast) )) / kday.MaindataAt(nDayLast);
  35. yieldSum += yieldNow;
  36. yieldSquareSum += yieldNow*yieldNow;
  37. }
  38. }
  39. if( nDayCount > 0 &&
  40. nDayCount >= (-1 == ldays ? STKLIB_DAYS_INONEYEAR : ldays-STKLIB_DAYS_INONEYEAR) )
  41. {
  42. double average = yieldSum / nDayCount;
  43. if( kday.MaindataAt(nDayEnd) > 1e-4 ) // HERE!!!, reset average directly!!!
  44. {
  45. average = (double)kday.MaindataAt(nDayCurrent) / kday.MaindataAt(nDayEnd);
  46. double dYearCount = ((double)nDayCount) / STKLIB_DAYS_INONEYEAR;
  47. average = pow( (double)10, (double)log10(fabs(average))/dYearCount ) - 1;
  48. }
  49. if( paverage )
  50. *paverage = average;
  51. if( pstddev )
  52. {
  53. *pstddev = yieldSquareSum / nDayCount - ((yieldSum / nDayCount)*(yieldSum / nDayCount));
  54. if( *pstddev > 0 )
  55. *pstddev = sqrt( *pstddev );
  56. }
  57. return TRUE;
  58. }
  59. return FALSE;
  60. }
  61. BOOL CalculateYieldDay( CKData & kday, double * paverage, double * pstddev, long ldays, int nDayCurrent )
  62. {
  63. if( -1 != nDayCurrent && nDayCurrent >= kday.GetSize() )
  64. return FALSE;
  65. int nDayCount = 0;
  66. double yieldSum = 0., yieldSquareSum = 0.;
  67. if( -1 == nDayCurrent )
  68. nDayCurrent = kday.GetSize()-1;
  69. int nDayEnd = ( -1 == ldays ? 0 : nDayCurrent-ldays );
  70. if( nDayEnd - 1 < 0 )
  71. return FALSE;
  72. for( int nDay = nDayCurrent; nDay >= nDayEnd; nDay -- )
  73. {
  74. int nDayLast = nDay - 1;
  75. if( nDayLast < 0 )
  76. break;
  77. if( kday.MaindataAt(nDayLast) > 1e-4 )
  78. {
  79. nDayCount ++;
  80. double yieldNow = (double( kday.MaindataAt(nDay) - kday.MaindataAt(nDayLast) )) / kday.MaindataAt(nDayLast);
  81. yieldSum += yieldNow;
  82. yieldSquareSum += yieldNow*yieldNow;
  83. }
  84. }
  85. if( nDayCount > 0 &&
  86. nDayCount >= (-1 == ldays ? STKLIB_DAYS_INONEYEAR : ldays) )
  87. {
  88. double average = yieldSum / nDayCount;
  89. if( kday.MaindataAt(nDayEnd) > 1e-4 ) // HERE!!!, reset average directly!!!
  90. {
  91. average = (double)kday.MaindataAt(nDayCurrent) / kday.MaindataAt(nDayEnd);
  92. average = pow( (double)10, (double)log10(fabs(average))/nDayCount ) - 1;
  93. }
  94. if( paverage )
  95. *paverage = average;
  96. if( pstddev )
  97. {
  98. *pstddev = yieldSquareSum / nDayCount - ((yieldSum / nDayCount)*(yieldSum / nDayCount));
  99. if( *pstddev > 0 )
  100. *pstddev = sqrt( *pstddev );
  101. }
  102. return TRUE;
  103. }
  104. return FALSE;
  105. }
  106. BOOL CalculateBeiteYear( CKData & kday, CKData &kdayMain,
  107. double market_yield_average, double market_yield_d,
  108. double * pbeite, long ldays, int nDayCurrent )
  109. {
  110. if( -1 != nDayCurrent && nDayCurrent >= kday.GetSize() )
  111. return FALSE;
  112. if( kday.GetSize() == 0 || kdayMain.GetSize() == 0 )
  113. return FALSE;
  114. double yield_average = 0., yield_d = 0.;
  115. if( !CalculateYieldYear( kday, &yield_average, &yield_d, ldays, nDayCurrent ) )
  116. return FALSE;
  117. int nDayCount = 0;
  118. double sum = 0.;
  119. // kday Index
  120. if( -1 == nDayCurrent )
  121. nDayCurrent = kday.GetSize()-1;
  122. int nDayEnd = ( -1 == ldays ? 0 : nDayCurrent-ldays );
  123. if( nDayEnd - STKLIB_DAYS_INONEYEAR < 0 )
  124. return FALSE;
  125. // kdayMain Index
  126. int nDayCurrentMain = kdayMain.GetSize()-(kday.GetSize()-nDayCurrent);
  127. if( nDayCurrentMain < 0 || nDayCurrentMain >= kdayMain.GetSize() )
  128. return FALSE;
  129. if( kday.ElementAt(nDayCurrent).m_date != kdayMain.ElementAt(nDayCurrentMain).m_date )
  130. {
  131. for( int k=kdayMain.GetSize()-1; k>=0 ; k-- )
  132. {
  133. if( kday.ElementAt(nDayCurrent).m_date == kdayMain.ElementAt(k).m_date )
  134. {
  135. nDayCurrentMain = k;
  136. break;
  137. }
  138. }
  139. if( k < 0 )
  140. return FALSE;
  141. }
  142. int nDayEndMain = ( -1 == ldays ? 0 : nDayCurrentMain-ldays );
  143. if( nDayEndMain < 0 )
  144. return FALSE;
  145. // Begin Calculate
  146. int nCountAdjust = 0;
  147. for( int nDay = nDayCurrent; nDay >= nDayEnd ; nDay -- )
  148. {
  149. int nDayLast = nDay - STKLIB_DAYS_INONEYEAR;
  150. int nDayMain = nDayCurrentMain-(nDayCurrent-nDay);
  151. int nDayMainLast = nDayMain - STKLIB_DAYS_INONEYEAR;
  152. if( nDayMainLast < 0 || nDayMain >= kdayMain.GetSize() || nDayLast < 0 || nDay >= kday.GetSize() )
  153. break;
  154. // SP_ASSERT( kday.ElementAt(nDay).m_date == kdayMain.ElementAt(nDayMain).m_date );
  155. if( kday.ElementAt(nDay).m_date < kdayMain.ElementAt(nDayMain).m_date )
  156. {
  157. nCountAdjust ++;
  158. if( nCountAdjust >= 5 )
  159. break;
  160. nDayCurrentMain --;
  161. continue;
  162. }
  163. if( kday.ElementAt(nDay).m_date > kdayMain.ElementAt(nDayMain).m_date )
  164. {
  165. nCountAdjust ++;
  166. if( nCountAdjust >= 5 )
  167. break;
  168. nDayCurrentMain ++;
  169. continue;
  170. }
  171. if( kday.MaindataAt(nDayLast) > 1e-4 && kdayMain.MaindataAt(nDayMainLast) > 1e-4 )
  172. {
  173. nDayCount ++;
  174. double yieldNow = (double( kday.MaindataAt(nDay) - kday.MaindataAt(nDayLast) )) / kday.MaindataAt(nDayLast);
  175. double yieldNowMain= (double( kdayMain.MaindataAt(nDayMain) - kdayMain.MaindataAt(nDayMainLast) )) / kdayMain.MaindataAt(nDayMainLast);
  176. sum += (yieldNow-yield_average) * (yieldNowMain-market_yield_average);
  177. }
  178. }
  179. if( nDayCount > 0 &&
  180. nDayCount+nCountAdjust >= (-1 == ldays ? STKLIB_DAYS_INONEYEAR : ldays-STKLIB_DAYS_INONEYEAR) )
  181. {
  182. if( pbeite )
  183. *pbeite = ( sum / nDayCount) / (market_yield_d*market_yield_d);
  184. return TRUE;
  185. }
  186. return FALSE;
  187. }
  188. BOOL CalculateBeiteDay( CKData & kday, CKData &kdayMain,
  189. double market_yield_average, double market_yield_d,
  190. double * pbeite, long ldays, int nDayCurrent )
  191. {
  192. if( -1 != nDayCurrent && nDayCurrent >= kday.GetSize() )
  193. return FALSE;
  194. if( kday.GetSize() == 0 || kdayMain.GetSize() == 0 )
  195. return FALSE;
  196. double yield_average = 0., yield_d = 0.;
  197. if( !CalculateYieldDay( kday, &yield_average, &yield_d, ldays, nDayCurrent ) )
  198. return FALSE;
  199. int nDayCount = 0;
  200. double sum = 0.;
  201. // kday Index
  202. if( -1 == nDayCurrent )
  203. nDayCurrent = kday.GetSize()-1;
  204. int nDayEnd = ( -1 == ldays ? 0 : nDayCurrent-ldays );
  205. if( nDayEnd -1 < 0 )
  206. return FALSE;
  207. // kdayMain Index
  208. int nDayCurrentMain = kdayMain.GetSize()-(kday.GetSize()-nDayCurrent);
  209. if( nDayCurrentMain < 0 || nDayCurrentMain >= kdayMain.GetSize() )
  210. return FALSE;
  211. if( kday.ElementAt(nDayCurrent).m_date != kdayMain.ElementAt(nDayCurrentMain).m_date )
  212. {
  213. for( int k=kdayMain.GetSize()-1; k>=0 ; k-- )
  214. {
  215. if( kday.ElementAt(nDayCurrent).m_date == kdayMain.ElementAt(k).m_date )
  216. {
  217. nDayCurrentMain = k;
  218. break;
  219. }
  220. }
  221. if( k < 0 )
  222. return FALSE;
  223. }
  224. int nDayEndMain = ( -1 == ldays ? 0 : nDayCurrentMain-ldays );
  225. if( nDayEndMain < 0 )
  226. return FALSE;
  227. // Begin Calculate
  228. int nCountAdjust = 0;
  229. for( int nDay = nDayCurrent; nDay >= nDayEnd; nDay -- )
  230. {
  231. int nDayLast = nDay - 1;
  232. int nDayMain = nDayCurrentMain-(nDayCurrent-nDay);
  233. int nDayMainLast = nDayMain-1;
  234. if( nDayMainLast < 0 || nDayMain >= kdayMain.GetSize() || nDayLast < 0 || nDay >= kday.GetSize() )
  235. break;
  236. // SP_ASSERT( kday.ElementAt(nDay).m_date == kdayMain.ElementAt(nDayMain).m_date );
  237. if( kday.ElementAt(nDay).m_date < kdayMain.ElementAt(nDayMain).m_date )
  238. {
  239. nCountAdjust ++;
  240. if( nCountAdjust >= 5 )
  241. break;
  242. nDayCurrentMain --;
  243. continue;
  244. }
  245. if( kday.ElementAt(nDay).m_date > kdayMain.ElementAt(nDayMain).m_date )
  246. {
  247. nCountAdjust ++;
  248. if( nCountAdjust >= 5 )
  249. break;
  250. nDayCurrentMain ++;
  251. continue;
  252. }
  253. if( kday.MaindataAt(nDayLast) > 1e-4 && kdayMain.MaindataAt(nDayMainLast) > 1e-4 )
  254. {
  255. nDayCount ++;
  256. double yieldNow = (double( kday.MaindataAt(nDay) - kday.MaindataAt(nDayLast) )) / kday.MaindataAt(nDayLast);
  257. double yieldNowMain= (double( kdayMain.MaindataAt(nDayMain) - kdayMain.MaindataAt(nDayMainLast) )) / kdayMain.MaindataAt(nDayMainLast);
  258. sum += (yieldNow-yield_average) * (yieldNowMain-market_yield_average);
  259. }
  260. }
  261. if( nDayCount > 0 &&
  262. nDayCount+nCountAdjust >= (-1 == ldays ? STKLIB_DAYS_INONEYEAR : ldays) )
  263. {
  264. if( pbeite )
  265. *pbeite = ( sum / nDayCount) / (market_yield_d*market_yield_d);
  266. return TRUE;
  267. }
  268. return FALSE;
  269. }