{"id":40574663,"url":"https://github.com/banbox/banta","last_synced_at":"2026-01-21T02:02:41.859Z","repository":{"id":211351346,"uuid":"728611884","full_name":"banbox/banta","owner":"banbox","description":"Event driven Technical Analysis library","archived":false,"fork":false,"pushed_at":"2025-11-09T12:06:39.000Z","size":209,"stargazers_count":13,"open_issues_count":6,"forks_count":9,"subscribers_count":1,"default_branch":"main","last_synced_at":"2026-01-14T13:55:42.370Z","etag":null,"topics":["financial","fundamental-analysis","indicator","library","momentum","oscillator","quantitative-finance","strategy","ta-lib","technical","technical-analysis","tradingview","trend"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/banbox.png","metadata":{"files":{"readme":"readme.cn.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null}},"created_at":"2023-12-07T10:09:47.000Z","updated_at":"2025-12-12T03:57:21.000Z","dependencies_parsed_at":null,"dependency_job_id":"ddf4b730-b987-4572-9e8a-02c92647b258","html_url":"https://github.com/banbox/banta","commit_stats":null,"previous_names":["anyongjin/banta","banbox/banta"],"tags_count":22,"template":false,"template_full_name":null,"purl":"pkg:github/banbox/banta","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/banbox%2Fbanta","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/banbox%2Fbanta/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/banbox%2Fbanta/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/banbox%2Fbanta/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/banbox","download_url":"https://codeload.github.com/banbox/banta/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/banbox%2Fbanta/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28622472,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-01-20T23:49:58.628Z","status":"online","status_checked_at":"2026-01-21T02:00:08.227Z","response_time":86,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["financial","fundamental-analysis","indicator","library","momentum","oscillator","quantitative-finance","strategy","ta-lib","technical","technical-analysis","tradingview","trend"],"created_at":"2026-01-21T02:02:39.673Z","updated_at":"2026-01-21T02:02:41.837Z","avatar_url":"https://github.com/banbox.png","language":"Go","readme":"# BanTA 技术分析库\n**banta**是一个高性能的技术分析指标库，支持状态缓存/并行计算两种模式。旨在提供**高自由度、高性能、简洁易用**的指标框架。\n\n[![DeepWiki问答](https://deepwiki.com/badge.svg)](https://deepwiki.com/banbox/banta)\n\n* **状态缓存模式：** 在每个K线上更新并缓存，无需重新计算历史数据，指标计算结果全局重用；类似TradingView\n* **并行计算模式：** 一次性计算所有K线，无缓存，收到新K线需再次重新计算；类似ta-lib\n* **nan值兼容：** 智能忽略输入数据中间的nan值，后续计算继续使用之前的状态\n* **严格测试：** 每个指标都使用各种条件单元测试验证，并和常见指标库结果对比\n* **轻量无依赖：** 仅使用golang，无任何依赖包\n* **支持python：** 已通过gopy打包为bbta包，可直接从python中导入使用\n\n## 支持的指标\n#### 和常见指标平台结果一致性对比\n| banta       | MyTT | TA-lib Class | TA-lib Metastock | Pandas-TA | TradingView |\n|-------------|:----:|:------------:|:----------------:|:---------:|:-----------:| \n| AvgPrice    |  ✔   |      ✔       |        ✔         |     ✔     |      ✔      |\n| Sum         |  ✔   |      ✔       |        ✔         |     ✔     |      ✔      |\n| SMA         |  T1  |      ✔       |        ✔         |     ✔     |      ✔      |\n| EMA         |  T1  |      T1      |        ✔         |     ✔     |     T2      |\n| EMABy1      |  ✔   |      T1      |        T2        |    T2     |     T3      |\n| RMA         |  --  |      --      |        --        |    T1     |     --      |\n| VWMA        |  --  |      --      |        --        |     ✔     |      ✔      |\n| WMA         |  ✔   |      ✔       |        ✔         |     ✔     |      ✔      |\n| HMA         |  --  |      --      |        --        |     ✔     |      ✔      |\n| TR          |  --  |      ✔       |        ✔         |     ✔     |     --      |\n| ATR         |  T1  |      ✔       |        ✔         |    T2     |     T3      |\n| MACD        |  T1  |      T2      |        T1        |     ✔     |     T3      |\n| RSI         |  T1  |      ✔       |        ✔         |    T2     |     T3      |\n| KDJ         |  T1  |      T2      |        T1        |    T3     |      ✔      |\n| Stoch       |  --  |      ✔       |        ✔         |    --     |      ✔      |\n| BBANDS      |  ✔   |      ✔       |        ✔         |     ✔     |      ✔      |\n| Aroon       |  --  |      ✔       |        ✔         |     ✔     |     T1      |\n| ADX         |  --  |      ✔       |        ✔         |    T1     |     T2      |\n| ADXBy1      |  --  |      T1      |        T1        |    T2     |      ✔      |\n| PluMinDI    |  --  |      ✔       |        ✔         |    --     |     --      |\n| PluMinDM    |  --  |      ✔       |        ✔         |    --     |     --      |\n| ROC         |  ✔   |      ✔       |        ✔         |    --     |      ✔      |\n| TNR/ER      |  --  |      --      |        --        |    --     |     --      |\n| CCI         |  ✔   |      ✔       |        ✔         |     ✔     |      ✔      |\n| CMF         |  --  |      --      |        --        |     ✔     |      ✔      |\n| KAMA        |  --  |      ✔       |        ✔         |    T1     |     ✔~      |\n| WillR       |  --  |      ✔       |        ✔         |     ✔     |      ✔      |\n| StochRSI    |  --  |      ✔       |        ✔         |     ✔     |     ✔~      |\n| MFI         |  ✔   |      ✔       |        ✔         |     ✔     |      ✔      |\n| RMI         |  --  |      --      |        --        |    --     |     ✔~      |\n| CTI         |  --  |      --      |        --        |     ✔     |     T1      |\n| LinReg      |  --  |      --      |        --        |     ✔     |      ?      |\n| CMO         |  --  |      ✔       |        ✔         |     ✔     |     T1      |\n| CMOBy1      |  --  |      T1      |        T1        |    T1     |      ✔      |\n| CHOP        |  --  |      --      |        --        |     ✔     |     T1      |\n| ALMA        |  --  |      --      |        --        |     ✔     |     T1      |\n| Stiffness   |  --  |      --      |        --        |    --     |      ✔      |\n| PercentRank |  --  |      --      |        --        |    --     |     ✔~      |\n| CRSI        |  --  |      --      |        --        |    --     |     ✔~      |\n| CRSIBy1     |  --  |  community   |        --        |    --     |     --      |\n| DV          |  --  |      --      |        --        |    --     |     --      |\n| UTBot       |  --  |      --      |        --        |    --     |      ✔      |\n| STC         |  --  |      --      |        --        |    --     |      ✔      |\n```text\n-- 此平台没有该指标\n✔  和此平台计算结果一致\n✔~ 和此平台计算结果基本一致(有一定偏差)\nTi  和此平台计算结果不一致 \n```\n\n## 如何使用(带状态缓存)\n```go\nimport (\n\t\"fmt\"\n\tta \"github.com/banbox/banta\"\n)\n\nvar envMap = make(map[string]*ta.BarEnv)\n\nfunc OnBar(symbol string, timeframe string, bar *ta.Kline) {\n\tenvKey := fmt.Sprintf(\"%s_%s\", symbol, timeframe)\n\te, ok := envMap[envKey]\n\tif !ok {\n\t\te = \u0026ta.BarEnv{\n\t\t\tTimeFrame: timeframe,\n\t\t\tBarNum:    1,\n\t\t}\n\t\tenvMap[envKey] = e\n\t}\n\te.OnBar(bar)\n\tma5 := ta.SMA(e.Close, 5)\n\tma30 := ta.SMA(e.Close, 30)\n\tatr := ta.ATR(e.High, e.Low, e.Close, 14).Get(0)\n\txnum := ta.Cross(ma5, ma30)\n\tif xnum == 1 {\n\t\t// ma5 cross up ma30\n\t\tcurPrice := e.Close.Get(0) // or bar.Close\n\t\tstopLoss := curPrice - atr\n\t\tfmt.Printf(\"open long at %f, stoploss: %f\", curPrice, stopLoss)\n\t} else if xnum == -1 {\n\t\t// ma5 cross down ma30\n\t\tcurPrice := e.Close.Get(0)\n\t\tfmt.Printf(\"close long at %f\", curPrice)\n\t}\n\tkdjRes := ta.KDJ(e.High, e.Low, e.Close, 9, 3, 3).Cols\n\tk, d := kdjRes[0], kdjRes[1]\n}\n```\n### 核心理念\n传统的技术分析库比如`ta-lib`和`pandas-ta`这些库非常流行，也进行了很多性能优化，在一次性计算几百上千跟K线时速度非常快。  \n但在你的机器人实盘运行使用这些库时，每次收到新的K线都要同时传入前面几百个K线数据，如果运行的标的更多一些，或者如果在1m甚至1s上运行时，计算延迟就会大到无法忍受。  \n很多人都用过TradingView，它使用的PineScript就是一个基于事件的技术分析引擎；它不会在收到一根新K线时重新运行前面的K线，而是使用前面缓存的结果。  \n这也是`BanTA`的设计理念，基于事件，每收到一个K线运行一次，使用前面缓存的结果。\n\n#### BanTA中状态缓存是如何实现的\n在`BanTA`中，状态缓存的核心是`Series`序列类型，基本上所有返回值都是序列，`Series`中有`Data []float64`字段，它记录了当前指标在每个K线的值。  \n最常用的`e.Close`就是收盘价序列，`e.Close.Get(0)`就是`float64`类型的当前收盘价；  \n计算均值也很简单：`ma5 := ta.SMA(e.Close, 5)`，返回的ma5也是一个序列；  \n有些指标如KDJ一般返回k和d两个字段`kdjRes := ta.KDJ(e.High, e.Low, e.Close, 9, 3, 3).Cols`，可以从`Cols`中得到两个序列组成的数组。\n\n\n## 如何使用（并行计算）\n```go\nimport (\n\t\"github.com/banbox/banta/tav\"\n)\n\nfunc main(){\n\thighArr := []float64{1.01, 1.01, 1.02, 0.996, 0.98, 0.993, 0.99, 1.0, 1.02}\n    lowArr := []float64{0.99, 1.0, 1.0, 0.98, 0.965, 0.98, 0.98, 0.984, 1.0}\n\tcloseArr := []float64{1.0, 1.01, 1.0, 0.99, 0.97, 0.981, 0.988, 0.992, 1.002}\n\tsma := tav.SMA(closeArr, 5)\n    ma30 := tav.SMA(closeArr, 30)\n    atr := tav.ATR(highArr, lowArr, closeArr, 14)\n    xArr := tav.Cross(ma5, ma30)\n\t\n}\n```\n### 提示\n建议您在进行研究时使用并行计算一次性获取指标计算结果，在实盘或基于事件驱动回测时使用带状态指标，效率更高。\n\n## Python安装\n```shell\npip install bbta\n```\n仅支持python8及以上版本，暂不支持macos和windows下python13\n\n## Python使用带状态指标\n\n```python\nfrom bbta import ta\n\n# 1. 创建环境\n# BarEnv 用于管理状态，在每个时间周期/交易对上创建一个即可\nenv = ta.BarEnv(TimeFrame=\"1m\")\n\n# 2. 准备K线数据\n# (时间戳ms, 开, 高, 低, 收, 交易量)\nklines = [\n    (1672531200000, 100, 102, 99, 101, 1000), (1672531260000, 101, 103, 100, 102, 1200),\n    (1672531320000, 102, 105, 101, 104, 1500), (1672531380000, 104, 105, 103, 103, 1300),\n    (1672531440000, 103, 104, 102, 103, 1100), (1672531500000, 103, 106, 103, 105, 1600),\n    (1672531560000, 105, 107, 104, 106, 1800), (1672531620000, 106, 106, 102, 103, 2000),\n    (1672531680000, 103, 104, 101, 102, 1700), (1672531740000, 102, 103, 100, 101, 1400),\n]\n\n# 3. 模拟K线推送\n# 在实盘中，每收到一根新K线就调用一次 OnBar\nfor kline in klines:\n    ts, o, h, l, c, v = kline\n    env.OnBar(ts, o, h, l, c, v, 0)\n\n    # 4. 计算指标\n    ma5 = ta.Series(ta.SMA(env.Close, 5))\n    ma30 = ta.Series(ta.SMA(env.Close, 30))\n\n    # 获取最新值\n    ma5_val = ma5.Get(0)\n    ma30_val = ma30.Get(0)\n    print(f\"Close={c:.2f}, MA5={ma5_val:.2f}, MA30={ma30_val:.2f}\")\n\n```\n\n## Python使用并行计算指标\n```python\nfrom bbta import tav, go\n\n# 1. 准备数据\n# 并行计算模式的函数接收 go.Slice_float64 类型\n# 我们可以从python list创建\nhigh_py = [102.0, 103.0, 105.0, 105.0, 104.0, 106.0, 107.0, 106.0, 104.0, 103.0]\nlow_py = [99.0, 100.0, 101.0, 103.0, 102.0, 103.0, 104.0, 102.0, 101.0, 100.0]\nclose_py = [101.0, 102.0, 104.0, 103.0, 103.0, 105.0, 106.0, 103.0, 102.0, 101.0]\n\nhigh = go.Slice_float64(high_py)\nlow = go.Slice_float64(low_py)\nclose = go.Slice_float64(close_py)\n\n# 2. 一次性计算所有指标\n# 返回结果也是 go.Slice 类型\nma5 = tav.SMA(close, 5)\natr = tav.ATR(high, low, close, 14)\n\n# 3. 查看结果\n# 可以转为python list查看\nprint(f\"Close: {list(close)[-5:]}\")\nprint(f\"MA5:   {[f'{x:.2f}' for x in list(ma5)[-5:]]}\")\nprint(f\"ATR:   {[f'{x:.2f}' for x in list(atr)[-5:]]}\")\n\n# 对于多返回值指标，比如KDJ\nkdj_result = tav.KDJ(high, low, close, 9, 3, 3)\nk_line = kdj_result[0]\nd_line = kdj_result[1]\nj_line = kdj_result[2]\nprint(f\"K-line: {[f'{x:.2f}' for x in list(k_line)[-5:]]}\")\n```\n\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbanbox%2Fbanta","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbanbox%2Fbanta","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbanbox%2Fbanta/lists"}