使用C++ TA-Lib 计算移动平均线

  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
-----------------------------------------

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值