资源加载中... loading...

TA指标库

优宽量化的TA指标库优化了常用指标算法,支持JavaScriptPythonC++语言的策略调用。 开源TA库代码优宽量化交易平台API手册

function main(){
    // 鉴于测试代码,不使用商品期货策略一般架构,这里仅仅判断exchange.IO("status")函数,判断连接期货公司前置机成功后立即执行测试代码。股票证券无需使用exchange.IO("status")判断连接状态
    while(true) {
        if (exchange.IO("status")) {
            exchange.SetContractType("rb888")
            var records = exchange.GetRecords()
            // K线需要有足够长度才可以计算指标数据
            if (records && records.length > 100) {
                break
            }
        }
        Sleep(1000)
    }

    var r = exchange.GetRecords()
    var macd = TA.MACD(r)
    var atr = TA.ATR(r, 14)

    // 打印出最后一组指标值
    Log(macd[0][r.length-1], macd[1][r.length-1], macd[2][r.length-1])
    Log(atr[atr.length-1])
}        
# Python需要单独安装talib库
import talib
def main():
    while True:
        if exchange.IO("status"):
            exchange.SetContractType("rb888")
            records = exchange.GetRecords()
            # records 为 None、空数组[] 判断均为假
            if records and len(records) > 100:
                break
        Sleep(1000)

    r = exchange.GetRecords()
    macd = TA.MACD(r)
    atr = TA.ATR(r, 14)
    Log(macd[0][-1], macd[1][-1], macd[2][-1])
    Log(atr[-1])
void main() {
    while (true) {
        if (exchange.IO("status") != 0) {
            exchange.SetContractType("rb888");
            auto records = exchange.GetRecords();
            if (records.Valid && records.size() > 100) {
                break;
            }
        }
        Sleep(1000);
    }

    auto r = exchange.GetRecords();
    auto macd = TA.MACD(r);
    auto atr = TA.ATR(r, 14);
    Log(macd[0][macd[0].size() - 1], macd[1][macd[1].size() - 1], macd[2][macd[2].size() - 1]);
    Log(atr[atr.size() - 1]);
}
JavaScript策略编写说明 talib指标库