用VC开发股票分析软件(一)

 开发思路
 最大的难点:公式算法,图形显示.
   A.算法:凡是涉及公式的MACD,CCI,RSI等的算法,如何解决
   
   
  1. TA_RetCode TA_MACD( int    startIdx,
  2.                     int    endIdx,
  3.                     const double inReal[],
  4.                     int           optInFastPeriod, /* From 2 to 100000 */
  5.                     int           optInSlowPeriod, /* From 2 to 100000 */
  6.                     int           optInSignalPeriod, /* From 1 to 100000 */
  7.                     int          *outBegIdx,
  8.                     int          *outNBElement,
  9.                     double        outMACD[],
  10.                     double        outMACDSignal[],
  11.                     double        outMACDHist[] );
  12. TA_RetCode TA_S_MACD( int    startIdx,
  13.                       int    endIdx,
  14.                       const float  inReal[],
  15.                       int           optInFastPeriod, /* From 2 to 100000 */
  16.                       int           optInSlowPeriod, /* From 2 to 100000 */
  17.                       int           optInSignalPeriod, /* From 1 to 100000 */
  18.                       int          *outBegIdx,
  19.                       int          *outNBElement,
  20.                       double        outMACD[],
  21.                       double        outMACDSignal[],
  22.                       double        outMACDHist[] );
  23. int TA_MACD_Lookback( int           optInFastPeriod, /* From 2 to 100000 */
  24.                     int           optInSlowPeriod, /* From 2 to 100000 */
  25.                     int           optInSignalPeriod );  /* From 1 to 100000 */
 测试MACD
 
  1. // TestMACD.cpp : Defines the entry point for the console application.
  2. //
  3. #include "stdafx.h"
  4. #include "ta_libc.h"
  5. #include "iostream.h"
  6. TA_Real    closePrice[400];
  7. TA_Real    out[400];
  8. TA_Integer outBeg;
  9. TA_Integer outNbElement;
  10. /* ... initialize your closing price here... */
  11. //TA_RetCode retCode = TA_MA(0, 399,&closePrice[0],30,TA_MAType_SMA,&outBeg,&outNbElement,&out[0]);
  12. int main(int argc, char* argv[])
  13. {
  14.     /* The output is displayed here */
  15.    for(int j=0;j<401;)
  16.    {
  17.        closePrice[j++]=1;
  18.       
  19.    }    
  20.    TA_RetCode retCode = TA_MA(0, 399,&closePrice[0],30,TA_MAType_SMA,&outBeg,&outNbElement,&out[0]);
  21.     
  22.    for(int i=0; i < outNbElement; i++ )
  23.    printf( "Day %d = %f/n", outBeg+i, out[i] );
  24.     printf("Hello World!/n");
  25.     return 0;
  26. }
  这些分析函数都是公开源码的。国外有一家学院的研究机构发布了所有证券分析软件公式算法函数。
  如果要用的话,要遵守GPL,采用的TA-LIB的名字,要署名。
  个人非常羡慕这种研究文化....
 
  1. /* List of contributors:
  2.  *
  3.  *  Initial  Name/description
  4.  *  -------------------------------------------------------------------
  5.  *  MF       Mario Fortier
  6.  *  JPP      JP Pienaar (j.pienaar@mci.co.za)
  7.  *
  8.  * Change history:
  9.  *
  10.  *  MMDDYY BY   Description
  11.  *  -------------------------------------------------------------------
  12.  *  112400 MF   Template creation.
  13.  *  052603 MF   Adapt code to compile with .NET Managed C++
  14.  *  080403 JPP  Fix #767653 for logic when swapping periods.
  15.  *
  16.  */
  17. /**** START GENCODE SECTION 1 - DO NOT DELETE THIS LINE ****/
  18. /* All code within this section is automatically
  19.  * generated by gen_code. Any modification will be lost
  20.  * next time gen_code is run.
  21.  */
  22. /* Generated */ 
  23. /* Generated */ #if defined( _MANAGED )
  24. /* Generated */    #include "TA-Lib-Core.h"
  25. /* Generated */    #define TA_INTERNAL_ERROR(Id) (RetCode::InternalError)
  26. /* Generated */    namespace TicTacTec { namespace TA { namespace Library {
  27. /* Generated */ #elif defined( _JAVA )
  28. /* Generated */    #include "ta_defs.h"
  29. /* Generated */    #include "ta_java_defs.h"
  30. /* Generated */    #define TA_INTERNAL_ERROR(Id) (RetCode.InternalError)
  31. /* Generated */ #else
  32. /* Generated */    #include <string.h>
  33. /* Generated */    #include <math.h>
  34. /* Generated */    #include "ta_func.h"
  35. /* Generated */ #endif
  36. /* Generated */ 
  37. /* Generated */ #ifndef TA_UTILITY_H
  38. /* Generated */    #include "ta_utility.h"
  39. /* Generated */ #endif
  40. /* Generated */ 
  41. /* Generated */ #ifndef TA_MEMORY_H
  42. /* Generated */    #include "ta_memory.h"
  43. /* Generated */ #endif
  44. /* Generated */ 
  45. /* Generated */ #define TA_PREFIX(x) TA_##x
  46. /* Generated */ #define INPUT_TYPE   double
  47. /* Generated */ 
  48. /* Generated */ #if defined( _MANAGED )
  49. /* Generated */ int Core::MacdLookback( int           optInFastPeriod, /* From 2 to 100000 */
  50. /* Generated */                       int           optInSlowPeriod, /* From 2 to 100000 */
  51. /* Generated */                       int           optInSignalPeriod )  /* From 1 to 100000 */
  52. /* Generated */ 
  53. /* Generated */ #elif defined( _JAVA )
  54. /* Generated */ public int macdLookback( int           optInFastPeriod, /* From 2 to 100000 */
  55. /* Generated */                        int           optInSlowPeriod, /* From 2 to 100000 */
  56. /* Generated */                        int           optInSignalPeriod )  /* From 1 to 100000 */
  57. /* Generated */ 
  58. /* Generated */ #else
  59. /* Generated */ int TA_MACD_Lookback( int           optInFastPeriod, /* From 2 to 100000 */
  60. /* Generated */                     int           optInSlowPeriod, /* From 2 to 100000 */
  61. /* Generated */                     int           optInSignalPeriod )  /* From 1 to 100000 */
  62. /* Generated */ 
  63. /* Generated */ #endif
  64. /**** END GENCODE SECTION 1 - DO NOT DELETE THIS LINE ****/
  65. {
  66.    /* insert local variable here */
  67.    int tempInteger;
  68. /**** START GENCODE SECTION 2 - DO NOT DELETE THIS LINE ****/
  69. /* Generated */ #ifndef TA_FUNC_NO_RANGE_CHECK
  70. /* Generated */    /* min/max are checked for optInFastPeriod. */
  71. /* Generated */    if( (int)optInFastPeriod == TA_INTEGER_DEFAULT )
  72. /* Generated */       optInFastPeriod = 12;
  73. /* Generated */    else if( ((int)optInFastPeriod < 2) || ((int)optInFastPeriod > 100000) )
  74. /* Generated */       return -1;
  75. /* Generated */ 
  76. /* Generated */    /* min/max are checked for optInSlowPeriod. */
  77. /* Generated */    if( (int)optInSlowPeriod == TA_INTEGER_DEFAULT )
  78. /* Generated */       optInSlowPeriod = 26;
  79. /* Generated */    else if( ((int)optInSlowPeriod < 2) || ((int)optInSlowPeriod > 100000) )
  80. /* Generated */       return -1;
  81. /* Generated */ 
  82. /* Generated */    /* min/max are checked for optInSignalPeriod. */
  83. /* Generated */    if( (int)optInSignalPeriod == TA_INTEGER_DEFAULT )
  84. /* Generated */       optInSignalPeriod = 9;
  85. /* Generated */    else if( ((int)optInSignalPeriod < 1) || ((int)optInSignalPeriod > 100000) )
  86. /* Generated */       return -1;
  87. /* Generated */ 
  88. /* Generated */ #endif /* TA_FUNC_NO_RANGE_CHECK */
  89. /**** END GENCODE SECTION 2 - DO NOT DELETE THIS LINE ****/
  90.    /* insert lookback code here. */
  91.    /* The lookback is driven by the signal line output.
  92.     *
  93.     * (must also account for the initial data consume 
  94.     *  by the slow period).
  95.     */
  96.    /* Make sure slow is really slower than
  97.     * the fast period! if not, swap...
  98.     */
  99.    if( optInSlowPeriod < optInFastPeriod )
  100.    {
  101.        /* swap */
  102.        tempInteger       = optInSlowPeriod;
  103.        optInSlowPeriod = optInFastPeriod;
  104.        optInFastPeriod = tempInteger;
  105.    }
  106.    return   LOOKBACK_CALL(EMA)( optInSlowPeriod   )
  107.           + LOOKBACK_CALL(EMA)( optInSignalPeriod );
  108. }
  109. /**** START GENCODE SECTION 3 - DO NOT DELETE THIS LINE ****/
  110. /*
  111.  * TA_MACD - Moving Average Convergence/Divergence
  112.  * 
  113.  * Input  = double
  114.  * Output = double, double, double
  115.  * 
  116.  * Optional Parameters
  117.  * -------------------
  118.  * optInFastPeriod:(From 2 to 100000)
  119.  *    Number of period for the fast MA
  120.  * 
  121.  * optInSlowPeriod:(From 2 to 100000)
  122.  *    Number of period for the slow MA
  123.  * 
  124.  * optInSignalPeriod:(From 1 to 100000)
  125.  *    Smoothing for the signal line (nb of period)
  126.  * 
  127.  * 
  128.  */
  129. /* Generated */ 
  130. /* Generated */ #if defined( _MANAGED ) && defined( USE_SUBARRAY )
  131. /* Generated */ enum class Core::RetCode Core::Macd( int    startIdx,
  132. /* Generated */                                      int    endIdx,
  133. /* Generated */                                      SubArray^    inReal,
  134. /* Generated */                                      int           optInFastPeriod, /* From 2 to 100000 */
  135. /* Generated */                                      int           optInSlowPeriod, /* From 2 to 100000 */
  136. /* Generated */                                      int           optInSignalPeriod, /* From 1 to 100000 */
  137. /* Generated */                                      [Out]int%    outBegIdx,
  138. /* Generated */                                      [Out]int%    outNBElement,
  139. /* Generated */                                      cli::array<double>^  outMACD,
  140. /* Generated */                                      cli::array<double>^  outMACDSignal,
  141. /* Generated */                                      cli::array<double>^  outMACDHist )
  142. /* Generated */ #elif defined( _MANAGED )
  143. /* Generated */ enum class Core::RetCode Core::Macd( int    startIdx,
  144. /* Generated */                                      int    endIdx,
  145. /* Generated */                                      cli::array<double>^ inReal,
  146. /* Generated */                                      int           optInFastPeriod, /* From 2 to 100000 */
  147. /* Generated */                                      int           optInSlowPeriod, /* From 2 to 100000 */
  148. /* Generated */                                      int           optInSignalPeriod, /* From 1 to 100000 */
  149. /* Generated */                                      [Out]int%    outBegIdx,
  150. /* Generated */                                      [Out]int%    outNBElement,
  151. /* Generated */                                      cli::array<double>^  outMACD,
  152. /* Generated */                                      cli::array<double>^  outMACDSignal,
  153. /* Generated */                                      cli::array<double>^  outMACDHist )
  154. /* Generated */ #elif defined( _JAVA )
  155. /* Generated */ public RetCode macd( int    startIdx,
  156. /* Generated */                      int    endIdx,
  157. /* Generated */                      double       inReal[],
  158. /* Generated */                      int           optInFastPeriod, /* From 2 to 100000 */
  159. /* Generated */                      int           optInSlowPeriod, /* From 2 to 100000 */
  160. /* Generated */                      int           optInSignalPeriod, /* From 1 to 100000 */
  161. /* Generated */                      MInteger     outBegIdx,
  162. /* Generated */                      MInteger     outNBElement,
  163. /* Generated */                      double        outMACD[],
  164. /* Generated */                      double        outMACDSignal[],
  165. /* Generated */                      double        outMACDHist[] )
  166. /* Generated */ #else
  167. /* Generated */ TA_RetCode TA_MACD( int    startIdx,
  168. /* Generated */                     int    endIdx,
  169. /* Generated */                     const double inReal[],
  170. /* Generated */                     int           optInFastPeriod, /* From 2 to 100000 */
  171. /* Generated */                     int           optInSlowPeriod, /* From 2 to 100000 */
  172. /* Generated */                     int           optInSignalPeriod, /* From 1 to 100000 */
  173. /* Generated */                     int          *outBegIdx,
  174. /* Generated */                     int          *outNBElement,
  175. /* Generated */                     double        outMACD[],
  176. /* Generated */                     double        outMACDSignal[],
  177. /* Generated */                     double        outMACDHist[] )
  178. /* Generated */ #endif
  179. /**** END GENCODE SECTION 3 - DO NOT DELETE THIS LINE ****/
  180. {
  181.    /* Insert local variables here. */
  182. /**** START GENCODE SECTION 4 - DO NOT DELETE THIS LINE ****/
  183. /* Generated */ 
  184. /* Generated */ #ifndef TA_FUNC_NO_RANGE_CHECK
  185. /* Generated */ 
  186. /* Generated */    /* Validate the requested output range. */
  187. /* Generated */    if( startIdx < 0 )
  188. /* Generated */       return ENUM_VALUE(RetCode,TA_OUT_OF_RANGE_START_INDEX,OutOfRangeStartIndex);
  189. /* Generated */    if( (endIdx < 0) || (endIdx < startIdx))
  190. /* Generated */       return ENUM_VALUE(RetCode,TA_OUT_OF_RANGE_END_INDEX,OutOfRangeEndIndex);
  191. /* Generated */ 
  192. /* Generated */    #if !defined(_JAVA)
  193. /* Generated */    if( !inReal ) return ENUM_VALUE(RetCode,TA_BAD_PARAM,BadParam);
  194. /* Generated */    #endif /* !defined(_JAVA)*/
  195. /* Generated */    /* min/max are checked for optInFastPeriod. */
  196. /* Generated */    if( (int)optInFastPeriod == TA_INTEGER_DEFAULT )
  197. /* Generated */       optInFastPeriod = 12;
  198. /* Generated */    else if( ((int)optInFastPeriod < 2) || ((int)optInFastPeriod > 100000) )
  199. /* Generated */       return ENUM_VALUE(RetCode,TA_BAD_PARAM,BadParam);
  200. /* Generated */ 
  201. /* Generated */    /* min/max are checked for optInSlowPeriod. */
  202. /* Generated */    if( (int)optInSlowPeriod == TA_INTEGER_DEFAULT )
  203. /* Generated */       optInSlowPeriod = 26;
  204. /* Generated */    else if( ((int)optInSlowPeriod < 2) || ((int)optInSlowPeriod > 100000) )
  205. /* Generated */       return ENUM_VALUE(RetCode,TA_BAD_PARAM,BadParam);
  206. /* Generated */ 
  207. /* Generated */    /* min/max are checked for optInSignalPeriod. */
  208. /* Generated */    if( (int)optInSignalPeriod == TA_INTEGER_DEFAULT )
  209. /* Generated */       optInSignalPeriod = 9;
  210. /* Generated */    else if( ((int)optInSignalPeriod < 1) || ((int)optInSignalPeriod > 100000) )
  211. /* Generated */       return ENUM_VALUE(RetCode,TA_BAD_PARAM,BadParam);
  212. /* Generated */ 
  213. /* Generated */    #if !defined(_JAVA)
  214. /* Generated */    if( !outMACD )
  215. /* Generated */       return ENUM_VALUE(RetCode,TA_BAD_PARAM,BadParam);
  216. /* Generated */ 
  217. /* Generated */    if( !outMACDSignal )
  218. /* Generated */       return ENUM_VALUE(RetCode,TA_BAD_PARAM,BadParam);
  219. /* Generated */ 
  220. /* Generated */    if( !outMACDHist )
  221. /* Generated */       return ENUM_VALUE(RetCode,TA_BAD_PARAM,BadParam);
  222. /* Generated */ 
  223. /* Generated */    #endif /* !defined(_JAVA) */
  224. /* Generated */ #endif /* TA_FUNC_NO_RANGE_CHECK */
  225. /* Generated */ 
  226. /**** END GENCODE SECTION 4 - DO NOT DELETE THIS LINE ****/
  227.    /* Insert TA function code here. */  
  228.    return FUNCTION_CALL(INT_MACD)( startIdx, endIdx, inReal,
  229.                                    optInFastPeriod,
  230.                                    optInSlowPeriod,
  231.                                    optInSignalPeriod,
  232.                                    outBegIdx,
  233.                                    outNBElement,
  234.                                    outMACD,
  235.                                    outMACDSignal,
  236.                                    outMACDHist );
  237. }
  238. #if defined( _MANAGED ) && defined( USE_SUBARRAY ) && !defined( USE_SINGLE_PRECISION_INPUT )
  239.  enum class Core::RetCode Core::TA_INT_MACD( int    startIdx,
  240.                                              int    endIdx,
  241.                                              SubArray^ inReal,
  242.                                              int    optInFastPeriod, /* 0 is fix 12 */
  243.                                              int    optInSlowPeriod, /* 0 is fix 26 */
  244.                                              int    optInSignalPeriod_2,
  245.                                              [Out]int% outBegIdx,
  246.                                              [Out]int% outNBElement,
  247.                                              cli::array<double>^ outMACD,
  248.                                              cli::array<double>^ outMACDSignal,
  249.                                              cli::array<double>^ outMACDHist )
  250. #elif defined( _MANAGED )
  251.  enum class Core::RetCode Core::TA_INT_MACD( int    startIdx,
  252.                                              int    endIdx,
  253.                                              cli::array<INPUT_TYPE>^ inReal,
  254.                                              int    optInFastPeriod, /* 0 is fix 12 */
  255.                                              int    optInSlowPeriod, /* 0 is fix 26 */
  256.                                              int    optInSignalPeriod_2,
  257.                                              [Out]int% outBegIdx,
  258.                                              [Out]int% outNBElement,
  259.                                              cli::array<double>^ outMACD,
  260.                                              cli::array<double>^ outMACDSignal,
  261.                                              cli::array<double>^ outMACDHist )
  262. #elif defined( _JAVA )
  263. RetCode TA_INT_MACD( int        startIdx,
  264.                      int        endIdx,
  265.                      INPUT_TYPE inReal[],
  266.                      int        optInFastPeriod, /* 0 is fix 12 */
  267.                      int        optInSlowPeriod, /* 0 is fix 26 */
  268.                      int        optInSignalPeriod_2,
  269.                      MInteger   outBegIdx,
  270.                      MInteger   outNBElement,
  271.                      double     outMACD[],
  272.                      double     outMACDSignal[],
  273.                      double     outMACDHist[] )
  274. #else
  275. TA_RetCode TA_PREFIX(INT_MACD)( int    startIdx,
  276.                                 int    endIdx,
  277.                                 const INPUT_TYPE inReal[],
  278.                                 int    optInFastPeriod, /* 0 is fix 12 */
  279.                                 int    optInSlowPeriod, /* 0 is fix 26 */
  280.                                 int    optInSignalPeriod_2,
  281.                                 int   *outBegIdx,
  282.                                 int   *outNBElement,
  283.                                 double       outMACD[],
  284.                                 double       outMACDSignal[],
  285.                                 double       outMACDHist[] )
  286. #endif
  287. {
  288.    ARRAY_REF(slowEMABuffer);
  289.    ARRAY_REF(fastEMABuffer);
  290.    double k1, k2;
  291.    ENUM_DECLARATION(RetCode) retCode;
  292.    int tempInteger;
  293.    VALUE_HANDLE_INT(outBegIdx1);
  294.    VALUE_HANDLE_INT(outNbElement1);
  295.    VALUE_HANDLE_INT(outBegIdx2);
  296.    VALUE_HANDLE_INT(outNbElement2);
  297.    int lookbackTotal, lookbackSignal;
  298.    int i;
  299.    /* !!! A lot of speed optimization could be done
  300.     * !!! with this function. 
  301.     * !!!
  302.     * !!! A better approach would be to use TA_INT_EMA
  303.     * !!! just to get the seeding values for the
  304.     * !!! fast and slow EMA. Then process the difference
  305.     * !!! in an allocated buffer until enough data is
  306.     * !!! available for the first signal value.
  307.     * !!! From that point all the processing can
  308.     * !!! be done in a tight loop.
  309.     * !!!
  310.     * !!! That approach will have the following
  311.     * !!! advantage:
  312.     * !!!   1) One mem allocation needed instead of two.
  313.     * !!!   2) The mem allocation size will be only the
  314.     * !!!      signal lookback period instead of the 
  315.     * !!!      whole range of data.
  316.     * !!!   3) Processing will be done in a tight loop.
  317.     * !!!      allowing to avoid a lot of memory store-load
  318.     * !!!      operation.
  319.     * !!!   4) The memcpy at the end will be eliminated!
  320.     * !!!
  321.     * !!! If only I had time....
  322.     */
  323.    /* Make sure slow is really slower than
  324.     * the fast period! if not, swap...
  325.     */
  326.    if( optInSlowPeriod < optInFastPeriod )
  327.    {
  328.        /* swap */
  329.        tempInteger       = optInSlowPeriod;
  330.        optInSlowPeriod = optInFastPeriod;
  331.        optInFastPeriod = tempInteger;
  332.    }
  333.    /* Catch special case for fix 26/12 MACD. */
  334.    if( optInSlowPeriod != 0 )
  335.       k1 = PER_TO_K(optInSlowPeriod);
  336.    else
  337.    {
  338.       optInSlowPeriod = 26;
  339.       k1 = (double)0.075; /* Fix 26 */
  340.    }
  341.    if( optInFastPeriod != 0 )
  342.       k2 = PER_TO_K(optInFastPeriod);
  343.    else
  344.    {
  345.       optInFastPeriod = 12;
  346.       k2 = (double)0.15; /* Fix 12 */
  347.    }
  348.    lookbackSignal = LOOKBACK_CALL(EMA)( optInSignalPeriod_2 ); 
  349.    /* Move up the start index if there is not
  350.     * enough initial data.
  351.     */
  352.    lookbackTotal =  lookbackSignal;
  353.    lookbackTotal += LOOKBACK_CALL(EMA)( optInSlowPeriod );
  354.    if( startIdx < lookbackTotal )
  355.       startIdx = lookbackTotal;
  356.    /* Make sure there is still something to evaluate. */
  357.    if( startIdx > endIdx )
  358.    {
  359.       VALUE_HANDLE_DEREF_TO_ZERO(outBegIdx);
  360.       VALUE_HANDLE_DEREF_TO_ZERO(outNBElement);
  361.       return ENUM_VALUE(RetCode,TA_SUCCESS,Success);
  362.    }
  363.    /* Allocate intermediate buffer for fast/slow EMA. */
  364.    tempInteger = (endIdx-startIdx)+1+lookbackSignal;
  365.    ARRAY_ALLOC( fastEMABuffer, tempInteger );
  366.    #if !defined( _JAVA )
  367.       if( !fastEMABuffer )
  368.       {
  369.          VALUE_HANDLE_DEREF_TO_ZERO(outBegIdx);
  370.          VALUE_HANDLE_DEREF_TO_ZERO(outNBElement);
  371.          return ENUM_VALUE(RetCode,TA_ALLOC_ERR,AllocErr);
  372.       }
  373.    #endif
  374.    ARRAY_ALLOC( slowEMABuffer, tempInteger );
  375.    #if !defined( _JAVA )
  376.       if( !slowEMABuffer )
  377.       {
  378.          VALUE_HANDLE_DEREF_TO_ZERO(outBegIdx);
  379.          VALUE_HANDLE_DEREF_TO_ZERO(outNBElement);
  380.          ARRAY_FREE( fastEMABuffer );
  381.          return ENUM_VALUE(RetCode,TA_ALLOC_ERR,AllocErr);
  382.       }
  383.    #endif
  384.    /* Calculate the slow EMA. 
  385.     *
  386.     * Move back the startIdx to get enough data
  387.     * for the signal period. That way, once the
  388.     * signal calculation is done, all the output
  389.     * will start at the requested 'startIdx'.
  390.     */
  391.    tempInteger = startIdx-lookbackSignal;
  392.    retCode = FUNCTION_CALL(INT_EMA)( tempInteger, endIdx,
  393.                                      inReal, optInSlowPeriod, k1,
  394.                                      VALUE_HANDLE_OUT(outBegIdx1), VALUE_HANDLE_OUT(outNbElement1), slowEMABuffer );
  395.    if( retCode != ENUM_VALUE(RetCode,TA_SUCCESS,Success) )
  396.    {
  397.       VALUE_HANDLE_DEREF_TO_ZERO(outBegIdx);
  398.       VALUE_HANDLE_DEREF_TO_ZERO(outNBElement);
  399.       ARRAY_FREE( fastEMABuffer );
  400.       ARRAY_FREE( slowEMABuffer );
  401.       return retCode;
  402.    }
  403.    /* Calculate the fast EMA. */
  404.    retCode = FUNCTION_CALL(INT_EMA)( tempInteger, endIdx,
  405.                                      inReal, optInFastPeriod, k2,
  406.                                      VALUE_HANDLE_OUT(outBegIdx2), VALUE_HANDLE_OUT(outNbElement2), fastEMABuffer );
  407.    if( retCode != ENUM_VALUE(RetCode,TA_SUCCESS,Success) )
  408.    {
  409.       VALUE_HANDLE_DEREF_TO_ZERO(outBegIdx);
  410.       VALUE_HANDLE_DEREF_TO_ZERO(outNBElement);
  411.       ARRAY_FREE( fastEMABuffer );
  412.       ARRAY_FREE( slowEMABuffer );
  413.       return retCode;
  414.    }
  415.    /* Parano tests. Will be removed eventually. */
  416.    if( (VALUE_HANDLE_GET(outBegIdx1) != tempInteger) || 
  417.        (VALUE_HANDLE_GET(outBegIdx2) != tempInteger) || 
  418.        (VALUE_HANDLE_GET(outNbElement1) != VALUE_HANDLE_GET(outNbElement2)) ||
  419.        (VALUE_HANDLE_GET(outNbElement1) != (endIdx-startIdx)+1+lookbackSignal) )
  420.    {
  421.       VALUE_HANDLE_DEREF_TO_ZERO(outBegIdx);
  422.       VALUE_HANDLE_DEREF_TO_ZERO(outNBElement);
  423.       ARRAY_FREE( fastEMABuffer );
  424.       ARRAY_FREE( slowEMABuffer );
  425.       return TA_INTERNAL_ERROR(119);
  426.    }
  427.    /* Calculate (fast EMA) - (slow EMA). */
  428.    for( i=0; i < VALUE_HANDLE_GET(outNbElement1); i++ )
  429.       fastEMABuffer[i] = fastEMABuffer[i] - slowEMABuffer[i];
  430.    /* Copy the result into the output for the caller. */
  431.    ARRAY_MEMMOVE( outMACD, 0, fastEMABuffer, lookbackSignal, (endIdx-startIdx)+1 );
  432.    /* Calculate the signal/trigger line. */
  433.    retCode = FUNCTION_CALL_DOUBLE(INT_EMA)( 0, VALUE_HANDLE_GET(outNbElement1)-1,
  434.                                             fastEMABuffer, optInSignalPeriod_2, PER_TO_K(optInSignalPeriod_2), 
  435.                                             VALUE_HANDLE_OUT(outBegIdx2), VALUE_HANDLE_OUT(outNbElement2), outMACDSignal );
  436.    ARRAY_FREE( fastEMABuffer );
  437.    ARRAY_FREE( slowEMABuffer );
  438.    if( retCode != ENUM_VALUE(RetCode,TA_SUCCESS,Success) )
  439.    {
  440.       VALUE_HANDLE_DEREF_TO_ZERO(outBegIdx);
  441.       VALUE_HANDLE_DEREF_TO_ZERO(outNBElement);
  442.       return retCode;
  443.    }
  444.    /* Calculate the histogram. */
  445.    for( i=0; i < VALUE_HANDLE_GET(outNbElement2); i++ )
  446.       outMACDHist[i] = outMACD[i]-outMACDSignal[i];
  447.    /* All done! Indicate the output limits and return success. */
  448.    VALUE_HANDLE_DEREF(outBegIdx)     = startIdx;
  449.    VALUE_HANDLE_DEREF(outNBElement)  = VALUE_HANDLE_GET(outNbElement2);
  450.    return ENUM_VALUE(RetCode,TA_SUCCESS,Success);
  451. }
  452. /**** START GENCODE SECTION 5 - DO NOT DELETE THIS LINE ****/
  453. /* Generated */ 
  454. /* Generated */ #define  USE_SINGLE_PRECISION_INPUT
  455. /* Generated */ #if !defined( _MANAGED ) && !defined( _JAVA )
  456. /* Generated */    #undef   TA_PREFIX
  457. /* Generated */    #define  TA_PREFIX(x) TA_S_##x
  458. /* Generated */ #endif
  459. /* Generated */ #undef   INPUT_TYPE
  460. /* Generated */ #define  INPUT_TYPE float
  461. /* Generated */ #if defined( _MANAGED )
  462. /* Generated */ enum class Core::RetCode Core::Macd( int    startIdx,
  463. /* Generated */                                      int    endIdx,
  464. /* Generated */                                      cli::array<float>^ inReal,
  465. /* Generated */                                      int           optInFastPeriod, /* From 2 to 100000 */
  466. /* Generated */                                      int           optInSlowPeriod, /* From 2 to 100000 */
  467. /* Generated */                                      int           optInSignalPeriod, /* From 1 to 100000 */
  468. /* Generated */                                      [Out]int%    outBegIdx,
  469. /* Generated */                                      [Out]int%    outNBElement,
  470. /* Generated */                                      cli::array<double>^  outMACD,
  471. /* Generated */                                      cli::array<double>^  outMACDSignal,
  472. /* Generated */                                      cli::array<double>^  outMACDHist )
  473. /* Generated */ #elif defined( _JAVA )
  474. /* Generated */ public RetCode macd( int    startIdx,
  475. /* Generated */                      int    endIdx,
  476. /* Generated */                      float        inReal[],
  477. /* Generated */                      int           optInFastPeriod, /* From 2 to 100000 */
  478. /* Generated */                      int           optInSlowPeriod, /* From 2 to 100000 */
  479. /* Generated */                      int           optInSignalPeriod, /* From 1 to 100000 */
  480. /* Generated */                      MInteger     outBegIdx,
  481. /* Generated */                      MInteger     outNBElement,
  482. /* Generated */                      double        outMACD[],
  483. /* Generated */                      double        outMACDSignal[],
  484. /* Generated */                      double        outMACDHist[] )
  485. /* Generated */ #else
  486. /* Generated */ TA_RetCode TA_S_MACD( int    startIdx,
  487. /* Generated */                       int    endIdx,
  488. /* Generated */                       const float  inReal[],
  489. /* Generated */                       int           optInFastPeriod, /* From 2 to 100000 */
  490. /* Generated */                       int           optInSlowPeriod, /* From 2 to 100000 */
  491. /* Generated */                       int           optInSignalPeriod, /* From 1 to 100000 */
  492. /* Generated */                       int          *outBegIdx,
  493. /* Generated */                       int          *outNBElement,
  494. /* Generated */                       double        outMACD[],
  495. /* Generated */                       double        outMACDSignal[],
  496. /* Generated */                       double        outMACDHist[] )
  497. /* Generated */ #endif
  498. /* Generated */ {
  499. /* Generated */  #ifndef TA_FUNC_NO_RANGE_CHECK
  500. /* Generated */     if( startIdx < 0 )
  501. /* Generated */        return ENUM_VALUE(RetCode,TA_OUT_OF_RANGE_START_INDEX,OutOfRangeStartIndex);
  502. /* Generated */     if( (endIdx < 0) || (endIdx < startIdx))
  503. /* Generated */        return ENUM_VALUE(RetCode,TA_OUT_OF_RANGE_END_INDEX,OutOfRangeEndIndex);
  504. /* Generated */     #if !defined(_JAVA)
  505. /* Generated */     if( !inReal ) return ENUM_VALUE(RetCode,TA_BAD_PARAM,BadParam);
  506. /* Generated */     #endif 
  507. /* Generated */     if( (int)optInFastPeriod == TA_INTEGER_DEFAULT )
  508. /* Generated */        optInFastPeriod = 12;
  509. /* Generated */     else if( ((int)optInFastPeriod < 2) || ((int)optInFastPeriod > 100000) )
  510. /* Generated */        return ENUM_VALUE(RetCode,TA_BAD_PARAM,BadParam);
  511. /* Generated */     if( (int)optInSlowPeriod == TA_INTEGER_DEFAULT )
  512. /* Generated */        optInSlowPeriod = 26;
  513. /* Generated */     else if( ((int)optInSlowPeriod < 2) || ((int)optInSlowPeriod > 100000) )
  514. /* Generated */        return ENUM_VALUE(RetCode,TA_BAD_PARAM,BadParam);
  515. /* Generated */     if( (int)optInSignalPeriod == TA_INTEGER_DEFAULT )
  516. /* Generated */        optInSignalPeriod = 9;
  517. /* Generated */     else if( ((int)optInSignalPeriod < 1) || ((int)optInSignalPeriod > 100000) )
  518. /* Generated */        return ENUM_VALUE(RetCode,TA_BAD_PARAM,BadParam);
  519. /* Generated */     #if !defined(_JAVA)
  520. /* Generated */     if( !outMACD )
  521. /* Generated */        return ENUM_VALUE(RetCode,TA_BAD_PARAM,BadParam);
  522. /* Generated */     if( !outMACDSignal )
  523. /* Generated */        return ENUM_VALUE(RetCode,TA_BAD_PARAM,BadParam);
  524. /* Generated */     if( !outMACDHist )
  525. /* Generated */        return ENUM_VALUE(RetCode,TA_BAD_PARAM,BadParam);
  526. /* Generated */     #endif 
  527. /* Generated */  #endif 
  528. /* Generated */    return FUNCTION_CALL(INT_MACD)( startIdx, endIdx, inReal,
  529. /* Generated */                                    optInFastPeriod,
  530. /* Generated */                                    optInSlowPeriod,
  531. /* Generated */                                    optInSignalPeriod,
  532. /* Generated */                                    outBegIdx,
  533. /* Generated */                                    outNBElement,
  534. /* Generated */                                    outMACD,
  535. /* Generated */                                    outMACDSignal,
  536. /* Generated */                                    outMACDHist );
  537. /* Generated */ }
  538. /* Generated */ #if defined( _MANAGED ) && defined( USE_SUBARRAY ) && !defined( USE_SINGLE_PRECISION_INPUT )
  539. /* Generated */  enum class Core::RetCode Core::TA_INT_MACD( int    startIdx,
  540. /* Generated */                                              int    endIdx,
  541. /* Generated */                                              SubArray^ inReal,
  542. /* Generated */                                              int    optInFastPeriod, 
  543. /* Generated */                                              int    optInSlowPeriod, 
  544. /* Generated */                                              int    optInSignalPeriod_2,
  545. /* Generated */                                              [Out]int% outBegIdx,
  546. /* Generated */                                              [Out]int% outNBElement,
  547. /* Generated */                                              cli::array<double>^ outMACD,
  548. /* Generated */                                              cli::array<double>^ outMACDSignal,
  549. /* Generated */                                              cli::array<double>^ outMACDHist )
  550. /* Generated */ #elif defined( _MANAGED )
  551. /* Generated */  enum class Core::RetCode Core::TA_INT_MACD( int    startIdx,
  552. /* Generated */                                              int    endIdx,
  553. /* Generated */                                              cli::array<INPUT_TYPE>^ inReal,
  554. /* Generated */                                              int    optInFastPeriod, 
  555. /* Generated */                                              int    optInSlowPeriod, 
  556. /* Generated */                                              int    optInSignalPeriod_2,
  557. /* Generated */                                              [Out]int% outBegIdx,
  558. /* Generated */                                              [Out]int% outNBElement,
  559. /* Generated */                                              cli::array<double>^ outMACD,
  560. /* Generated */                                              cli::array<double>^ outMACDSignal,
  561. /* Generated */                                              cli::array<double>^ outMACDHist )
  562. /* Generated */ #elif defined( _JAVA )
  563. /* Generated */ RetCode TA_INT_MACD( int        startIdx,
  564. /* Generated */                      int        endIdx,
  565. /* Generated */                      INPUT_TYPE inReal[],
  566. /* Generated */                      int        optInFastPeriod, 
  567. /* Generated */                      int        optInSlowPeriod, 
  568. /* Generated */                      int        optInSignalPeriod_2,
  569. /* Generated */                      MInteger   outBegIdx,
  570. /* Generated */                      MInteger   outNBElement,
  571. /* Generated */                      double     outMACD[],
  572. /* Generated */                      double     outMACDSignal[],
  573. /* Generated */                      double     outMACDHist[] )
  574. /* Generated */ #else
  575. /* Generated */ TA_RetCode TA_PREFIX(INT_MACD)( int    startIdx,
  576. /* Generated */                                 int    endIdx,
  577. /* Generated */                                 const INPUT_TYPE inReal[],
  578. /* Generated */                                 int    optInFastPeriod, 
  579. /* Generated */                                 int    optInSlowPeriod, 
  580. /* Generated */                                 int    optInSignalPeriod_2,
  581. /* Generated */                                 int   *outBegIdx,
  582. /* Generated */                                 int   *outNBElement,
  583. /* Generated */                                 double       outMACD[],
  584. /* Generated */                                 double       outMACDSignal[],
  585. /* Generated */                                 double       outMACDHist[] )
  586. /* Generated */ #endif
  587. /* Generated */ {
  588. /* Generated */    ARRAY_REF(slowEMABuffer);
  589. /* Generated */    ARRAY_REF(fastEMABuffer);
  590. /* Generated */    double k1, k2;
  591. /* Generated */    ENUM_DECLARATION(RetCode) retCode;
  592. /* Generated */    int tempInteger;
  593. /* Generated */    VALUE_HANDLE_INT(outBegIdx1);
  594. /* Generated */    VALUE_HANDLE_INT(outNbElement1);
  595. /* Generated */    VALUE_HANDLE_INT(outBegIdx2);
  596. /* Generated */    VALUE_HANDLE_INT(outNbElement2);
  597. /* Generated */    int lookbackTotal, lookbackSignal;
  598. /* Generated */    int i;
  599. /* Generated */    if( optInSlowPeriod < optInFastPeriod )
  600. /* Generated */    {
  601. /* Generated */        tempInteger       = optInSlowPeriod;
  602. /* Generated */        optInSlowPeriod = optInFastPeriod;
  603. /* Generated */        optInFastPeriod = tempInteger;
  604. /* Generated */    }
  605. /* Generated */    if( optInSlowPeriod != 0 )
  606. /* Generated */       k1 = PER_TO_K(optInSlowPeriod);
  607. /* Generated */    else
  608. /* Generated */    {
  609. /* Generated */       optInSlowPeriod = 26;
  610. /* Generated */       k1 = (double)0.075; 
  611. /* Generated */    }
  612. /* Generated */    if( optInFastPeriod != 0 )
  613. /* Generated */       k2 = PER_TO_K(optInFastPeriod);
  614. /* Generated */    else
  615. /* Generated */    {
  616. /* Generated */       optInFastPeriod = 12;
  617. /* Generated */       k2 = (double)0.15; 
  618. /* Generated */    }
  619. /* Generated */    lookbackSignal = LOOKBACK_CALL(EMA)( optInSignalPeriod_2 ); 
  620. /* Generated */    lookbackTotal =  lookbackSignal;
  621. /* Generated */    lookbackTotal += LOOKBACK_CALL(EMA)( optInSlowPeriod );
  622. /* Generated */    if( startIdx < lookbackTotal )
  623. /* Generated */       startIdx = lookbackTotal;
  624. /* Generated */    if( startIdx > endIdx )
  625. /* Generated */    {
  626. /* Generated */       VALUE_HANDLE_DEREF_TO_ZERO(outBegIdx);
  627. /* Generated */       VALUE_HANDLE_DEREF_TO_ZERO(outNBElement);
  628. /* Generated */       return ENUM_VALUE(RetCode,TA_SUCCESS,Success);
  629. /* Generated */    }
  630. /* Generated */    tempInteger = (endIdx-startIdx)+1+lookbackSignal;
  631. /* Generated */    ARRAY_ALLOC( fastEMABuffer, tempInteger );
  632. /* Generated */    #if !defined( _JAVA )
  633. /* Generated */       if( !fastEMABuffer )
  634. /* Generated */       {
  635. /* Generated */          VALUE_HANDLE_DEREF_TO_ZERO(outBegIdx);
  636. /* Generated */          VALUE_HANDLE_DEREF_TO_ZERO(outNBElement);
  637. /* Generated */          return ENUM_VALUE(RetCode,TA_ALLOC_ERR,AllocErr);
  638. /* Generated */       }
  639. /* Generated */    #endif
  640. /* Generated */    ARRAY_ALLOC( slowEMABuffer, tempInteger );
  641. /* Generated */    #if !defined( _JAVA )
  642. /* Generated */       if( !slowEMABuffer )
  643. /* Generated */       {
  644. /* Generated */          VALUE_HANDLE_DEREF_TO_ZERO(outBegIdx);
  645. /* Generated */          VALUE_HANDLE_DEREF_TO_ZERO(outNBElement);
  646. /* Generated */          ARRAY_FREE( fastEMABuffer );
  647. /* Generated */          return ENUM_VALUE(RetCode,TA_ALLOC_ERR,AllocErr);
  648. /* Generated */       }
  649. /* Generated */    #endif
  650. /* Generated */    tempInteger = startIdx-lookbackSignal;
  651. /* Generated */    retCode = FUNCTION_CALL(INT_EMA)( tempInteger, endIdx,
  652. /* Generated */                                      inReal, optInSlowPeriod, k1,
  653. /* Generated */                                      VALUE_HANDLE_OUT(outBegIdx1), VALUE_HANDLE_OUT(outNbElement1), slowEMABuffer );
  654. /* Generated */    if( retCode != ENUM_VALUE(RetCode,TA_SUCCESS,Success) )
  655. /* Generated */    {
  656. /* Generated */       VALUE_HANDLE_DEREF_TO_ZERO(outBegIdx);
  657. /* Generated */       VALUE_HANDLE_DEREF_TO_ZERO(outNBElement);
  658. /* Generated */       ARRAY_FREE( fastEMABuffer );
  659. /* Generated */       ARRAY_FREE( slowEMABuffer );
  660. /* Generated */       return retCode;
  661. /* Generated */    }
  662. /* Generated */    retCode = FUNCTION_CALL(INT_EMA)( tempInteger, endIdx,
  663. /* Generated */                                      inReal, optInFastPeriod, k2,
  664. /* Generated */                                      VALUE_HANDLE_OUT(outBegIdx2), VALUE_HANDLE_OUT(outNbElement2), fastEMABuffer );
  665. /* Generated */    if( retCode != ENUM_VALUE(RetCode,TA_SUCCESS,Success) )
  666. /* Generated */    {
  667. /* Generated */       VALUE_HANDLE_DEREF_TO_ZERO(outBegIdx);
  668. /* Generated */       VALUE_HANDLE_DEREF_TO_ZERO(outNBElement);
  669. /* Generated */       ARRAY_FREE( fastEMABuffer );
  670. /* Generated */       ARRAY_FREE( slowEMABuffer );
  671. /* Generated */       return retCode;
  672. /* Generated */    }
  673. /* Generated */    if( (VALUE_HANDLE_GET(outBegIdx1) != tempInteger) || 
  674. /* Generated */        (VALUE_HANDLE_GET(outBegIdx2) != tempInteger) || 
  675. /* Generated */        (VALUE_HANDLE_GET(outNbElement1) != VALUE_HANDLE_GET(outNbElement2)) ||
  676. /* Generated */        (VALUE_HANDLE_GET(outNbElement1) != (endIdx-startIdx)+1+lookbackSignal) )
  677. /* Generated */    {
  678. /* Generated */       VALUE_HANDLE_DEREF_TO_ZERO(outBegIdx);
  679. /* Generated */       VALUE_HANDLE_DEREF_TO_ZERO(outNBElement);
  680. /* Generated */       ARRAY_FREE( fastEMABuffer );
  681. /* Generated */       ARRAY_FREE( slowEMABuffer );
  682. /* Generated */       return TA_INTERNAL_ERROR(119);
  683. /* Generated */    }
  684. /* Generated */    for( i=0; i < VALUE_HANDLE_GET(outNbElement1); i++ )
  685. /* Generated */       fastEMABuffer[i] = fastEMABuffer[i] - slowEMABuffer[i];
  686. /* Generated */    ARRAY_MEMMOVE( outMACD, 0, fastEMABuffer, lookbackSignal, (endIdx-startIdx)+1 );
  687. /* Generated */    retCode = FUNCTION_CALL_DOUBLE(INT_EMA)( 0, VALUE_HANDLE_GET(outNbElement1)-1,
  688. /* Generated */                                             fastEMABuffer, optInSignalPeriod_2, PER_TO_K(optInSignalPeriod_2), 
  689. /* Generated */                                             VALUE_HANDLE_OUT(outBegIdx2), VALUE_HANDLE_OUT(outNbElement2), outMACDSignal );
  690. /* Generated */    ARRAY_FREE( fastEMABuffer );
  691. /* Generated */    ARRAY_FREE( slowEMABuffer );
  692. /* Generated */    if( retCode != ENUM_VALUE(RetCode,TA_SUCCESS,Success) )
  693. /* Generated */    {
  694. /* Generated */       VALUE_HANDLE_DEREF_TO_ZERO(outBegIdx);
  695. /* Generated */       VALUE_HANDLE_DEREF_TO_ZERO(outNBElement);
  696. /* Generated */       return retCode;
  697. /* Generated */    }
  698. /* Generated */    for( i=0; i < VALUE_HANDLE_GET(outNbElement2); i++ )
  699. /* Generated */       outMACDHist[i] = outMACD[i]-outMACDSignal[i];
  700. /* Generated */    VALUE_HANDLE_DEREF(outBegIdx)     = startIdx;
  701. /* Generated */    VALUE_HANDLE_DEREF(outNBElement)  = VALUE_HANDLE_GET(outNbElement2);
  702. /* Generated */    return ENUM_VALUE(RetCode,TA_SUCCESS,Success);
  703. /* Generated */ }
  704. /* Generated */ 
  705. /* Generated */ #if defined( _MANAGED )
  706. /* Generated */ }}} // Close namespace TicTacTec.TA.Lib
  707. /* Generated */ #endif
  下一部分,分析一下TA-LIB包

  
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值