1. 下载C++ TA-Lib 源码并编译静态库: https://ta-lib.org/hdr_dw.html
  2. 编码
#include <vector>
#include "ta_libc.h"

std::vector<double> lastPriceVec;
double sma5[11];
double sma10[11];

void onTick(double lastPrice)
{
    lastPriceVec.push_back(lastPrice);
    if(lastPriceVec.size() < 10){
        // There must be at least ten items,
        // otherwise, it would raise an error when we calculate SMA10.
        return;
    }

    int outBegIdx = 0;
    int outNbElement = 0;
    int startIdx = lastPriceVec.size()-10;
    int endIdx = lastPriceVec.size()-1;

    TA_RetCode ret = TA_SMA(endIdx, endIdx, lastPriceVec.data(), 5, &outBegIdx,
        &outNbElement, sma5+startIdx);
    if(ret != TA_SUCCESS){
        printf("TA_SMA5 ERROR: %d\n", ret);
    } else {
        printf("TA_SMA5 outBegIdx:%d, outNbElement:%d, %f\n", outBegIdx, outNbElement, sma5[startIdx]);
    }

    ret = TA_SMA(endIdx, endIdx, lastPriceVec.data(), 10, &outBegIdx,
        &outNbElement, sma10+startIdx);
    if(ret != TA_SUCCESS){
        printf("TA_SMA10 ERROR: %d\n", ret);
    } else {
        printf("TA_SMA10 outBegIdx:%d, outNbElement:%d, %f\n", outBegIdx, outNbElement, sma10[startIdx]);
    }

    printf("sma5: %f, sma10: %f\n"
           "-----------------------------------------\n",
           sma5[startIdx], sma10[startIdx]);
}

int main()
{
    // i=1,2,3,...20
    for(int i = 1; i < 21; i++)
    {
        onTick(i);
    }
    return 0;
}
  1. 编译运行:
g++ -g -o ./bin/smatest ./src/smatest.cpp -I lib/ta-lib-0.4.0/include/ lib/ta-lib-0.4.0/lib/libta_lib.a
TA_SMA5 outBegIdx:9, outNbElement:1, 8.000000
TA_SMA10 outBegIdx:9, outNbElement:1, 5.500000
sma5: 8.000000, sma10: 5.500000
-----------------------------------------
TA_SMA5 outBegIdx:10, outNbElement:1, 9.000000
TA_SMA10 outBegIdx:10, outNbElement:1, 6.500000
sma5: 9.000000, sma10: 6.500000
-----------------------------------------
TA_SMA5 outBegIdx:11, outNbElement:1, 10.000000
TA_SMA10 outBegIdx:11, outNbElement:1, 7.500000
sma5: 10.000000, sma10: 7.500000
-----------------------------------------
TA_SMA5 outBegIdx:12, outNbElement:1, 11.000000
TA_SMA10 outBegIdx:12, outNbElement:1, 8.500000
sma5: 11.000000, sma10: 8.500000
-----------------------------------------
TA_SMA5 outBegIdx:13, outNbElement:1, 12.000000
TA_SMA10 outBegIdx:13, outNbElement:1, 9.500000
sma5: 12.000000, sma10: 9.500000
-----------------------------------------
TA_SMA5 outBegIdx:14, outNbElement:1, 13.000000
TA_SMA10 outBegIdx:14, outNbElement:1, 10.500000
sma5: 13.000000, sma10: 10.500000
-----------------------------------------
TA_SMA5 outBegIdx:15, outNbElement:1, 14.000000
TA_SMA10 outBegIdx:15, outNbElement:1, 11.500000
sma5: 14.000000, sma10: 11.500000
-----------------------------------------
TA_SMA5 outBegIdx:16, outNbElement:1, 15.000000
TA_SMA10 outBegIdx:16, outNbElement:1, 12.500000
sma5: 15.000000, sma10: 12.500000
-----------------------------------------
TA_SMA5 outBegIdx:17, outNbElement:1, 16.000000
TA_SMA10 outBegIdx:17, outNbElement:1, 13.500000
sma5: 16.000000, sma10: 13.500000
-----------------------------------------
TA_SMA5 outBegIdx:18, outNbElement:1, 17.000000
TA_SMA10 outBegIdx:18, outNbElement:1, 14.500000
sma5: 17.000000, sma10: 14.500000
-----------------------------------------
TA_SMA5 outBegIdx:19, outNbElement:1, 18.000000
TA_SMA10 outBegIdx:19, outNbElement:1, 15.500000
sma5: 18.000000, sma10: 15.500000
-----------------------------------------

Logo

DAMO开发者矩阵,由阿里巴巴达摩院和中国互联网协会联合发起,致力于探讨最前沿的技术趋势与应用成果,搭建高质量的交流与分享平台,推动技术创新与产业应用链接,围绕“人工智能与新型计算”构建开放共享的开发者生态。

更多推荐