cinar/indicator

GitHub: cinar/indicator

一个零依赖的 Go 语言技术分析库,提供海量金融指标、可配置交易策略及回测框架,解决量化交易中策略开发与验证的需求。

Stars: 1218 | Forks: 198

[![GoDoc](https://godoc.org/github.com/cinar/indicator/v2?status.svg)](https://godoc.org/github.com/cinar/indicator/v2) [![License](https://img.shields.io/badge/License-AGPLv3-blue.svg)](https://opensource.org/licenses/AGPLv3) [![Go Report Card](https://goreportcard.com/badge/github.com/cinar/indicator/v2)](https://goreportcard.com/report/github.com/cinar/indicator/v2) ![Go CI](https://static.pigsec.cn/wp-content/uploads/repos/cas/3b/3b39c55110f11a97d10cda63d1c1193c1b78ee3e26d559adaea16b191fc65e8a.svg) [![codecov](https://codecov.io/gh/cinar/indicator/graph/badge.svg?token=MB7L69UAWM)](https://codecov.io/gh/cinar/indicator) [![GHCR Version](https://img.shields.io/github/v/tag/cinar/indicator?label=ghcr&sort=semver&logo=github)](https://github.com/cinar/indicator/pkgs/container/indicator) [![GitHub Stars](https://img.shields.io/github/stars/cinar/indicator?style=social)](https://github.com/cinar/indicator/stargazers)

# Indicator Go Indicator 是一个 Golang 模块,提供了丰富的技术分析指标、策略,以及一个支持实时策略执行的回测框架。 ### v2 版本的主要改进: - **提升代码质量:** 进行了完全重写,以实现并保持至少 90% 的代码覆盖率。 - **增强可测试性:** 每个指标和策略都有专用的 CSV 格式测试数据,便于验证。 - **简化数据处理:** 库已重写为通过数据流(Go channels)处理输入和输出。如果您倾向于使用切片,可以使用 [helper.SliceToChan](helper/README.md#SliceToChan) 和 [helper.ChanToSlice](helper/README.md#ChanToSlice) 等辅助函数。或者,您仍然可以使用 [v1 版本](https://github.com/cinar/indicator/tree/v1)。 - **可配置的指标和策略:** 所有指标和策略都设计为完全可配置的,没有预设值。 - **泛型支持:** 该库利用 Golang 泛型来支持各种数字数据格式。 - **MCP 支持:** 库中集成了 MCP(Multi-Client Protocol Server)支持,方便与各种 AI 工具配合使用。 我现在还在 [Indicator TS](https://github.com/cinar/indicatorts) 提供了该模块的 TypeScript 版本。 ## 👆 提供的指标 本包目前支持以下指标列表: ### 📈 趋势指标 - [绝对价格震荡指标 (APO)](trend/README.md#Apo) - [Aroon 指标](trend/README.md#Aroon) - [力量平衡 (BoP)](trend/README.md#Bop) - [钱德预测震荡指标 (CFO)](trend/README.md#Cfo) - [商品通道指标 (CCI)](trend/README.md#Cci) - [包络线](trend/README.md#Envelope) - [赫尔移动平均线 (HMA)](trend/README.md#Hma) - [去趋势价格震荡指标 (DPO)](trend/README.md#Dpo) - [双重指数移动平均线 (DEMA)](trend/README.md#Dema) - [指数移动平均线 (EMA)](trend/README.md#Ema) - [考夫曼自适应移动平均线 (KAMA)](trend/README.md#Kama) - [确知事物指标 (KST)](trend/README.md#Kst) - [质量指数 (MI)](trend/README.md#MassIndex) - [McGinley 动态指标](trend/README.md#McGinleyDynamic) - [平滑异同移动平均线 (MACD)](trend/README.md#Macd) - [移动最小二乘法 (MLS)](trend/README.md#Mls) - [移动线性回归 (MLR)](trend/README.md#Mlr) - [移动最大值](trend/README.md#MovingMax) - [移动最小值](trend/README.md#MovingMin) - [移动总和](trend/README.md#MovingSum) - [枢轴点](trend/README.md#PivotPoint) - [随机指标 (KDJ)](trend/README.md#Kdj) - [变化率 (ROC)](trend/README.md#Roc) - [随机指标](trend/README.md#Stochastic) - [慢速随机指标](trend/README.md#SlowStochastic) - [谢夫趋势周期 (STC)](trend/README.md#Stc) - [滚动移动平均线 (RMA)](trend/README.md#Rma) - [简单移动平均线 (SMA)](trend/README.md#Sma) - [自变化以来](helper/README.md#Since) - [斜率](trend/README.md#Slope) - [平滑移动平均线 (SMMA)](trend/README.md#Smma) - [三重指数移动平均线 (TEMA)](trend/README.md#Tema) - [三角移动平均线 (TRIMA)](trend/README.md#Trima) - [三重指数平均线 (TRIX)](trend/README.md#Trix) - [真实强度指数 (TSI)](trend/README.md#Tsi) - [Tillson T3](trend/README.md#T3) - [典型价格](trend/README.md#TypicalPrice) - [成交量加权移动平均线 (VWMA)](trend/README.md#Vwma) - [加权收盘价](trend/README.md#WeightedClose) - [加权移动平均线 (WMA)](trend/README.md#Wma) ### 🚀 动量指标 - [震荡加速度指标](momentum/README.md#AwesomeOscillator) - [蔡金震荡指标](momentum/README.md#ChaikinOscillator) - [康纳斯 RSI](momentum/README.md#ConnorsRsi) - [估波曲线](momentum/README.md#CoppockCurve) - [长老射线指数](momentum/README.md#ElderRay) - [费雪转换指标](momentum/README.md#Fisher) - [一目均衡表 云图](momentum/README.md#IchimokuCloud) - [内部柱强度 (IBS)](momentum/README.md#InternalBarStrength) - [马丁·普林的特别 K 指标](momentum/README.md#PringsSpecialK) - [价格震荡百分比 (PPO)](momentum/README.md#Ppo) - [成交量震荡百分比 (PVO)](momentum/README.md#Pvo) - [Qstick 指标](momentum/README.md#Qstick) - [相对强弱指数 (RSI)](momentum/README.md#Rsi) - [相对活力指数 (RVI)](momentum/README.md#Rvi) - [随机震荡指标](momentum/README.md#StochasticOscillator) - [随机 RSI](momentum/README.md#StochasticRsi) - [TD 序列指标](momentum/README.md#TdSequential) - [终极震荡指标](momentum/README.md#UltimateOscillator) - [威廉指标 (Williams R)](momentum/README.md#WilliamsR) ### 🎢 波动率指标 - [百分比 B](volatility/README.md#PercentB) - [加速带](volatility/README.md#AccelerationBands) - [年化历史波动率 (AHV)](volatility/README.md#AnnualizedHistoricalVolatility) - [真实波动幅度 (ATR)](volatility/README.md#Atr) - [布林带宽度](volatility/README.md#BollingerBandWidth) - [布林带](volatility/README.md#BollingerBands) - [吊灯止损法](volatility/README.md#ChandelierExit) - [震荡指数 (CHOP)](volatility/README.md#Chop) - [唐奇安通道 (DC)](volatility/README.md#DonchianChannel) - [历史波动率 (HV)](volatility/README.md#HistoricalVolatility) - [肯特纳通道 (KC)](volatility/README.md#KeltnerChannel) - [移动标准差](volatility/README.md#MovingStd) - [投影震荡指标 (PO)](volatility/README.md#Po) - [超级趋势线](volatility/README.md#SuperTrend) - [真实波幅 (TR)](volatility/README.md#TrueRange) - [溃疡指数 (UI)](volatility/README.md#UlcerIndex) - [Z-Score 指标](volatility/README.md#ZScore) ### 📢 成交量指标 - [累积/派发指标 (A/D)](volume/README.md#Ad) - [蔡金资金流量 (CMF)](volume/README.md#Cmf) - [简易波动指标 (EMV)](volume/README.md#Emv) - [力度指数 (FI)](volume/README.md#Fi) - [克林格成交量震荡指标 (KVO)](volume/README.md#Kvo) - [资金流量指数 (MFI)](volume/README.md#Mfi) - [资金流量乘数 (MFM)](volume/README.md#Mfm) - [资金流量体积 (MFV)](volume/README.md#Mfv) - [负成交量指数 (NVI)](volume/README.md#Nvi) - [能量潮指标 (OBV)](volume/README.md#Obv) - [成交量价格趋势 (VPT)](volume/README.md#Vpt) - [成交量加权平均价格 (VWAP)](volume/README.md#Vwap) ### 💰 资产估值 - [未来价值 (FV)](valuation/README.md#Fv) - [净现值 (NPV)](valuation/README.md#Npv) - [现值 (PV)](valuation/README.md#Pv) ## 🧠 提供的策略 本包目前支持以下策略列表: ### ⚖ 基础策略 - [买入并持有策略](strategy/README.md#type-buyandholdstrategy) ### 📈 趋势策略 - [鳄鱼指标策略](strategy/trend/README.md#AlligatorStrategy) - [绝对价格震荡指标 (APO) 策略](strategy/trend/README.md#ApoStrategy) - [Aroon 策略](strategy/trend/README.md#AroonStrategy) - [力量平衡 (BoP) 策略](strategy/trend/README.md#BopStrategy) - [钱德预测震荡指标策略](strategy/trend/README.md#CfoStrategy) - [商品通道指标 (CCI) 策略](strategy/trend/README.md#CciStrategy) - [双重指数移动平均线 (DEMA) 策略](strategy/trend/README.md#DemaStrategy) - [包络线策略](strategy/trend/README.md#EnvelopeStrategy) - [金叉策略](strategy/trend/README.md#GoldenCrossStrategy) - [赫尔移动平均线 (HMA) 策略](strategy/trend/README.md#HmaStrategy) - [考夫曼自适应移动平均线 (KAMA) 策略](strategy/trend/README.md#KamaStrategy) - [平滑异同移动平均线 (MACD) 策略](strategy/trend/README.md#MacdStrategy) - [Qstick 策略](strategy/trend/README.md#QstickStrategy) - [随机指标 (KDJ) 策略](strategy/trend/README.md#KdjStrategy) - [平滑移动平均线 (SMMA) 策略](strategy/trend/README.md#SmmaStrategy) - [三角移动平均线 (TRIMA) 策略](strategy/trend/README.md#TrimaStrategy) - [三重指数平均线 (TRIX) 策略](strategy/trend/README.md#TrixStrategy) - [三重移动平均线交叉策略](strategy/trend/README.md#TripleMovingAverageCrossoverStrategy) - [真实强度指数 (TSI) 策略](strategy/trend/README.md#TsiStrategy) - [成交量加权移动平均线 (VWMA) 策略](strategy/trend/README.md#VwmaStrategy) - [加权收盘价策略](strategy/trend/README.md#WeightedCloseStrategy) ### 🚀 动量策略 - [震荡加速度指标策略](strategy/momentum/README.md#AwesomeOscillatorStrategy) - [估波曲线策略](strategy/momentum/README.md#CoppockCurveStrategy) - [长老射线策略](strategy/momentum/README.md#ElderRayStrategy) - [一目均衡表 云图策略](strategy/momentum/README.md#IchimokuCloudStrategy) - [RSI 策略](strategy/momentum/README.md#RsiStrategy) - [随机震荡指标策略](strategy/momentum/README.md#StochasticOscillatorStrategy) - [随机 RSI 策略](strategy/momentum/README.md#StochasticRsiStrategy) - [三重 RSI 策略](strategy/momentum/README.md#TripleRsiStrategy) - [威廉指标 (Williams R) 策略](strategy/momentum/README.md#WilliamsRStrategy) ### 🎢 波动率策略 - [布林带策略](strategy/volatility/README.md#BollingerBandsStrategy) - [唐奇安通道突破策略](strategy/volatility/README.md#DonchianChannelBreakoutStrategy) - [超级趋势线策略](strategy/volatility/README.md#SuperTrendStrategy) ### 📢 成交量策略 - [蔡金资金流量策略](strategy/volume/README.md#ChaikinMoneyFlowStrategy) - [简易波动指标策略](strategy/volume/README.md#EaseOfMovementStrategy) - [力度指数策略](strategy/volume/README.md#ForceIndexStrategy) - [资金流量指数策略](strategy/volume/README.md#MoneyFlowIndexStrategy) - [负成交量指数策略](strategy/volume/README.md#NegativeVolumeIndexStrategy) - [能量潮指标 (OBV) 策略](strategy/volume/README.md#ObvStrategy) - [百分比带与 MFI 策略](strategy/volume/README.md#PercentBandMFIStrategy) - [加权平均价格策略](strategy/volume/README.md#WeightedAveragePriceStrategy) ### 🧪 复合策略 复合策略将多个策略合并,以产生综合建议。它们使用各种决策逻辑结合各个策略的建议。 - [And 策略](strategy/README.md#AndStrategy) - [多数策略](strategy/README.md#MajorityStrategy) - [MACD-RSI 策略](strategy/compound/README.md#MacdRsiStrategy) - [Or 策略](strategy/README.md#OrStrategy) - [拆分策略](strategy/README.md#SplitStrategy) ### 🎁 装饰器策略 装饰器策略提供了一种改变其他策略建议的方法。 - [反向策略](strategy/decorator/README.md#InverseStrategy) - [无损策略](strategy/decorator/README.md#NoLossStrategy) - [止损策略](strategy/decorator/README.md#StopLossStrategy) ## 🗃 仓库 仓库作为[资产快照](asset/README.md#Snapshot)的集中存储和检索位置。 提供了以下[仓库实现](asset/README.md#Repository)。 - [文件系统仓库](asset/README.md#FileSystemRepository) - [内存仓库](asset/README.md#InMemoryRepository) - [Tiingo 仓库](asset/README.md#TiingoRepository) - [Alpaca Markets 仓库](https://github.com/cinar/indicatoralpaca) [Sync 函数](asset/README.md#Sync)采用多 worker 并发机制,促进了指定源仓库和目标仓库之间资产的同步,从而提高了效率。此函数的目的是从远程仓库获取最新的快照,并无缝地将它们传输到本地仓库(例如文件系统仓库)。 `indicator` 命令行工具还提供了在 Tiingo 仓库和文件系统仓库之间同步数据的功能。为了说明其用法,请参考以下示例命令: ``` $ indicator-sync \ -source-name tiingo \ -source-config $TIINGO_KEY \ -target-name filesystem \ -target-config /home/user/assets \ -days 30 ``` 此命令有效地从 Tiingo 仓库检索位于 `/home/user/assets` 目录中资产的最新快照。如果本地资产文件没有内容,它会自动扩大范围,同步 30 天的快照,确保仓库内容的全面和最新。 ## ⏳ 回测 [回测功能](backtest/README.md#Backtest)使用 [Outcome](strategy/README.md#Outcome),严格评估应用于一组特定资产的指定策略的潜在表现。它为每个策略-资产配对生成全面的可视化表示。 ``` report := backtest.NewHTMLReport(outputDir) bt := backtest.NewBacktest(repository, report) bt.Names = append(bt.Names, "brk-b") bt.Strategies = append(bt.Strategies, trend.NewApoStrategy()) err = bt.Run() if err != nil { t.Fatal(err) } ``` `indicator-backtest` 命令行工具使用户能够对驻留在指定仓库中的资产进行全面的回测。此功能包含应用所有当前已识别的策略,最终在指定的输出目录中生成详细的报告。 ``` $ indicator-backtest \ -source-name filesystem \ -source-config /home/user/assets \ -output /home/user/reports \ -workers 1 ``` ## ☁️ MCP Server [MCP Server](mcp/README.md)(Multi-Client Protocol Server)为向多个客户端提供交易策略提供了一个强大且可扩展的解决方案。它支持实时策略执行和数据处理,非常适合需要高吞吐量和低延迟交互交易算法的应用程序。 ## 🐳 Docker 最简单的入门方法是使用我们的 Docker 镜像。它能在一条命令中处理所有事情——从 Tiingo 同步市场数据到生成回测报告。 ### 快速开始 ``` # 在以下网址获取免费的 Tiingo API key:https://www.tiingo.com/ # 对特定资产运行 backtest docker run -it --rm \ -v $(pwd)/output:/app/output \ ghcr.io/cinar/indicator:latest \ --api-key YOUR_TIINGO_API_KEY \ --days 365 \ --assets aapl msft googl # 查看结果 (macOS) open output/index.html # 查看结果 (Linux) xdg-open output/index.html ``` ### 选项 | 标志 | 描述 | 默认值 | |------|-------------|---------| | `--api-key` | Tiingo API 密钥(必填) | - | | `--days` | 要获取的历史数据天数 | 365 | | `--last` | 要回测的天数 | 365 | | `--assets` | 以空格分隔的股票代码(默认:全部) | all | | `--output` | 报告的输出目录 | /app/output | ### 示例 ``` # 对所有可用资产进行 1 年的 backtest docker run -it --rm \ -v $(pwd)/reports:/app/output \ ghcr.io/cinar/indicator:latest \ --api-key YOUR_TIINGO_API_KEY # 对特定股票进行过去 6 个月的 backtest,测试过去 30 天 docker run -it --rm \ -v $(pwd)/reports:/app/output \ ghcr.io/cinar/indicator:latest \ --api-key YOUR_TIINGO_API_KEY \ --days 180 \ --last 30 \ --assets aapl msft googl amzn # 自定义输出目录 docker run -it --rm \ -v /path/to/my/reports:/app/output \ ghcr.io/cinar/indicator:latest \ --api-key YOUR_TIINGO_API_KEY \ --output /app/output ``` ### 本地构建 ``` docker build -t indicator . docker run -it --rm -v $(pwd)/output:/app/output indicator --api-key YOUR_KEY ``` ## 用法 安装包。 ``` go get github.com/cinar/indicator/v2 ``` 导入 indicator。 ``` import ( "github.com/cinar/indicator/v2" ) ``` ## 🌐 生态系统 Indicator Go 是更广泛的技术分析工具生态系统的一部分: - [Indicator TS](https://github.com/cinar/indicatorts) - 相同指标和策略的 TypeScript/JavaScript 实现 - [Indicator Alpaca](https://github.com/cinar/indicatoralpaca) - 用于实盘交易的 Alpaca Markets 集成 - [MCP Server](mcp/README.md) - 用于 AI 集成的模型上下文协议服务器 ## ⚠️ 风险披露与免责声明 **财务风险:** 交易股票、期货和加密货币涉及巨大的损失风险,并不适合每位投资者。使用杠杆工具可能会导致超过初始投资的损失。 **非财务建议:** 本项目中提供的信息和工具仅供教育和信息参考,不应被解释为购买或出售任何证券的建议或招揽。它们不构成财务、投资或交易建议。Onur Cinar 不是财务顾问。 **无担保:** 本软件按“原样”提供,不提供任何形式的担保。技术分析指标基于历史数据,不保证未来结果。尽管我们努力实现 90% 以上的测试覆盖率,但软件仍可能包含 bug。 **责任限制:** 在任何情况下,对于因使用本库而产生的任何财务损失、数据丢失或任何其他损害,作者或贡献者均不承担责任。使用风险自负。 ## 许可证 `v2.x.x` 及以上版本采用 GNU AGPLv3 许可证和商业许可证双重许可。对于代码的免费使用和修改,您可以使用 AGPLv3 许可证。如果您需要具有不同条款的商业许可,请联系我。 ``` Copyright (c) 2021-2026 Onur Cinar. The source code is provided under GNU AGPLv3 License. This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. You should have received a copy of the GNU Affero General Public License along with this program. If not, see . ``` `v1.x.x` 版本在 MIT 许可证下提供。 ``` Copyright (c) 2021-2026 Onur Cinar. The source code is provided under MIT License. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ```
标签:EVTX分析, Golang, 交易策略, 回测框架, 安全编程, 技术分析, 日志审计, 请求拦截, 量化金融, 金融数据流