{"id":13488281,"url":"https://github.com/mpquant/MyTT","last_synced_at":"2025-03-28T00:33:26.871Z","repository":{"id":37402356,"uuid":"368472013","full_name":"mpquant/MyTT","owner":"mpquant","description":"MyTT将通达信,同花顺,文华麦语言等指标公式,最简移植到Python中,核心库单个文件，仅百行代码,十几个核心函数，神奇的实现所有常见技术指标算法（不依赖talib库）的纯python实现和转换通达信MACD,RSI,BOLL,ATR,KDJ,CCI,PSY等公式,全部基于pandas函数计算方法封装，简洁且高性能，能非常方便的应用在股票指标公式,股市期货量化框架分析,自动程序化交易,数字货币量化等领域,它是您最精练的股市量化工具。Python library with most stock market indicators.","archived":false,"fork":false,"pushed_at":"2025-01-14T08:20:33.000Z","size":423,"stargazers_count":2049,"open_issues_count":54,"forks_count":620,"subscribers_count":50,"default_branch":"main","last_synced_at":"2025-03-22T07:01:41.546Z","etag":null,"topics":["atr","boll","btc","cci","indicators","kdj","macd","psy","python","quant","rsi","stock"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/mpquant.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"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}},"created_at":"2021-05-18T09:25:36.000Z","updated_at":"2025-03-20T14:54:59.000Z","dependencies_parsed_at":"2025-02-07T06:02:15.464Z","dependency_job_id":"a84fa76a-b8cb-4fbd-a41d-8148afd96f58","html_url":"https://github.com/mpquant/MyTT","commit_stats":{"total_commits":162,"total_committers":3,"mean_commits":54.0,"dds":"0.024691358024691357","last_synced_commit":"ea4f14857ecc46a3739a75ce2e6974b9057a6102"},"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mpquant%2FMyTT","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mpquant%2FMyTT/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mpquant%2FMyTT/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mpquant%2FMyTT/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mpquant","download_url":"https://codeload.github.com/mpquant/MyTT/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245949254,"owners_count":20698911,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","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":["atr","boll","btc","cci","indicators","kdj","macd","psy","python","quant","rsi","stock"],"created_at":"2024-07-31T18:01:13.001Z","updated_at":"2025-03-28T00:33:21.847Z","avatar_url":"https://github.com/mpquant.png","language":"Python","readme":"# MyTT (My麦语言 T通达信 T同花顺)\nMyTT是您量化工具箱里的瑞士军刀，精炼而高效，它将通达信,同花顺,文华麦语言等指标公式indicators,最简移植到Python中,核心库单个文件，仅百行代码,实现和转换同花顺通达信所有常见指标MACD,RSI,BOLL,ATR,KDJ,CCI,PSY等,全部基于numpy和pandas的函数封装，简洁且高性能，能非常方便的应用在各自股票股市技术分析，股票自动程序化交易,数字货币BTC等量化等领域.Mini Python library with most stock market indicators.\n\n[![license](https://img.shields.io/:license-gpl-blue.svg)](https://badges.gpl-license.org/)\n\n# 功能特点\n* 核心库轻量化： 项目库就一个文件 [MyTT.py](https://github.com/mpquant/MyTT/blob/main/MyTT.py),不用安装设置，可自由裁剪，随用随走 `from MyTT import *` 即可 \n\n* 代码人类化：)  没有什么炫耀的编程花样，初学者也能看懂，自己就能自行增加指标，马上就能用在项目中。\n\n* 不需要安装ta-lib库,是纯python代码实现的的核心逻辑，很多人都有安装ta-lib库的痛苦经历\n\n* 和通达信，同花顺的指标写法完全兼容，一个新的指标基本不用做修改，直接拿来即可使用\n\n* 超高性能，基本不用循环，全是靠numpy,pandas的内置函数实现各种指标\n\n* 和Talib库一样是多天参数进，多天指标出（序列进，序列出），便于画图和观察趋势\n\n* MyTT实现的各种指标和通达信，同花顺，雪球等软件的技术指标一致到小数点后2位\n\n* MyTT高级进阶版本，收录了高级复杂用法的函数和实验验证函数 [MyTT_plus](https://github.com/mpquant/MyTT/blob/main/MyTT_plus.py)\n\n* MyTT也能在python2的老版本pandas中使用，请用此python2版本 [MyTT_python2](https://github.com/mpquant/MyTT/blob/main/MyTT_python2.py)\n\n\n### 先看一个最简单的例子  \n\n```python\n\n#数字货币行情获取和指标计算演示\nfrom  hb_hq_api import *         #数字货币行情库\nfrom  MyTT import *              #myTT麦语言工具函数指标库\n\n#获取btc.usdt交易对120日的数据\ndf=get_price('btc.usdt',count=120,frequency='1d');     #'1d'是1天, '4h'是4小时\n\n#-----------df结果如下表(股市也基本一样)-------------------------------------------\n```\n\n|  |open|\tclose|\thigh\t|low|\tvol|\n|--|--|--|--|--|--|\n|2021-05-16\t|48983.62|\t47738.24|\t49800.00|\t46500.0\t|1.333333e+09 |\n|2021-05-17\t|47738.24|\t43342.50|\t48098.66|\t42118.0\t|3.353662e+09 |\n|2021-05-18\t|43342.50|\t44093.24|\t45781.52|\t42106.0\t|1.793267e+09 |\n\n\n```python\n\n#-------有数据了，下面开始正题 -------------\nCLOSE=df.close.values;  OPEN=df.open.values           #基础数据定义，只要传入的是序列都可以   \nHIGH=df.high.values;    LOW=df.low.values             #例如 CLOSE=list(df.close) 都是一样\n\nMA5=MA(CLOSE,5)                                       #获取5日均线序列\nMA10=MA(CLOSE,10)                                     #获取10日均线序列\n\nprint('BTC5日均线', MA5[-1] )                          # 只取最后一个数   \nprint('BTC10日均线',RET(MA10))                         # RET(MA10) == MA10[-1]\nprint('今天5日线是否上穿10日线',RET(CROSS(MA5,MA10)))\nprint('最近5天收盘价全都大于10日线吗？',EVERY(CLOSE\u003eMA10,5) )\n\n```\n### 安装方法\n* 直接拷贝 MyTT.py到你的项目下 `from MyTT import *` 即可调用文件中的所有函数\n\n* 传统标准库安装 `pip install MyTT`\n\n```python\nfrom  MyTT import *                 #声明调用MyTT， 请注意大小写\nS=np.random.randint(1,99,[10])      #生成1-99内的10个数序列 \nEMA(S,6)                            #对这个序列S进行6周期EMA指数平均计算\n```\n\n### 教程和案例应用\n* [通达信公式转Python神器——MyTT库](https://www.joinquant.com/view/community/detail/a6cc7d1fb73a57dbac4b77044a33b15d)  \n\n* [利用MyTT库整合通达信指标公式](https://www.joinquant.com/view/community/detail/4237ebaa5db39a5a9a2195338e8be588)  \n\n* [MyTT库应用示例及计算精度验证](https://www.joinquant.com/view/community/detail/bd26874654a6f9f1958f23043ca06149)  \n\n* [如何在聚宽研究环境中建立myTT.py库文件](https://www.joinquant.com/view/community/detail/2abf0cc457352b59ef2e873ad7c4e430)  \n\n* [基于MyTT来编写Python版通达信指标](https://www.joinquant.com/view/community/detail/7a0297fb7bd717cfb2be40b4c8062eeb)  \n\n* [MyTT基础函数EMA指数平均的公式推导](https://www.joinquant.com/view/community/detail/ab76489c8fdfd1f201b6df47f11a5360)\n\n\n### MyTT库中的部分工具函数\n* n天前的数据：`REF`\n```python\nREF(CLOSE, 1)              # 截止到昨天收盘价 序列\n```\n\n* 移动平均线计算：MA\n```python\nMA(CLOSE, 5)             # 取得收盘价5日平均线\n```\n\n* 加权移动平均计算：EMA\n```python\nEMA(CLOSE, 5)            # 为了精度 ，  EMA至少需要120周期   \n```\n\n* 中国式的SMA计算：SMA\n```python\nSMA(CLOSE, 5)            # 为了精度 ，  SMA至少需要120周期   \n```\n\n*  返回序列标准差：STD\n```python\nSTD(CLOSE, 5)             # 返回收盘价5日内标准差\n```\n\n*  平均绝对偏差：`AVEDEV`\n```python\nAVEDEV(CLOSE, 5)    # 序列与其平均值的绝对差的平均值\n```\n\n* 金叉判断：CROSS\n```python\nCROSS(MA(CLOSE, 5), MA(CLOSE, 10))       #5日均线上穿10日均线\n```\n\n* 两个序列取最大值,最小值：`MAX`  `MIN`\n```python\nMAX(OPEN, CLOSE )                       #K线实体的最高价\n```\n\n* n天内满足条件的天数：COUNT\n```python\nCOUNT(CLOSE \u003e OPEN, 10)                 #最近10天收阳线的天数\n```\n\n* n天内全部满足条件的天数：EVERY\n```python\nEVERY(CLOSE \u003eOPEN, 5)                   #最近5天都是收阳线\n```\n\n* 从前A日到前B日一直满足条件 ：LAST\n```python\nLAST(CLOSE\u003eOPEN,5,3)                    #5天前到3天前是否都收阳线\n```\n\n* n天内是否至少满足条件一次：EXIST\n```python\nEXIST(CLOSE\u003eOPEN, 5)                   #最近5天是否有一天收阳线\n```\n\n* 上一次条件成立到当前的周期：BARSLAST\n```python\nBARSLAST(CLOSE/REF(CLOSE)\u003e=1.1)         #上一次涨停到今天的天数\n```\n\n* 返回序列的线性回归斜率：`SLOPE`\n```python\nSLOPE(MA(CLOSE,10),5)                   #得到10日平均线最近5天的斜率(其实就是MA均线的方向)\n```\n\n* 取回线性回归后的预测值：`FORCAST`\n```python\nFORCAST(CLOSE,20)                       #根据最近20日的走势预测明天的收盘价\n```\n\n*  n天内最大值：`HHV`\n```python\nHHV(MAX(OPEN, CLOSE), 20)               #最近20天K线实体的最高价\n```\n\n* n天内最小值：`LLV`\n```python\nLLV(MIN(OPEN, CLOSE), 60)              #最近60天K线实体的最低价\n```\n\n* 条件 `IF`\n```python\nIF(OPEN \u003e CLOSE, OPEN, CLOSE)          #如果 开盘\u003e收盘  返回OPEN ，否则返回CLOSE\n```\n\n### 具体指标的实现，全部基于MyTT库中的工具函数 （更多指标可以自行添加）\n\n```python\ndef MACD(CLOSE,SHORT=12,LONG=26,M=9):    # EMA的关系，CLOSE取120日，结果能精确到雪球小数点2位\n    DIF = EMA(CLOSE,SHORT)-EMA(CLOSE,LONG);  \n    DEA = EMA(DIF,M);      MACD=(DIF-DEA)*2\n    return RD(DIF),RD(DEA),RD(MACD)\n```\n\n```python\ndef KDJ(CLOSE,HIGH,LOW, N=9,M1=3,M2=3):   \n    RSV = (CLOSE - LLV(LOW, N)) / (HHV(HIGH, N) - LLV(LOW, N)) * 100\n    K = EMA(RSV, (M1*2-1));    D = EMA(K,(M2*2-1));        J=K*3-D*2\n    return K, D, J\n```\n\n```python\ndef RSI(CLOSE, N=24):                     #RSI指标\n    DIF = CLOSE-REF(CLOSE,1) \n    return RD(SMA(MAX(DIF,0), N) / SMA(ABS(DIF), N) * 100)  \n```\n\n```python\ndef WR(CLOSE, HIGH, LOW, N=10, N1=6):    #W\u0026R 威廉指标\n    WR = (HHV(HIGH, N) - CLOSE) / (HHV(HIGH, N) - LLV(LOW, N)) * 100\n    WR1 = (HHV(HIGH, N1) - CLOSE) / (HHV(HIGH, N1) - LLV(LOW, N1)) * 100\n    return RD(WR), RD(WR1)\n```\n\n```python\ndef BIAS(CLOSE,L1=6, L2=12, L3=24):      #BIAS乖离率\n    BIAS1 = (CLOSE - MA(CLOSE, L1)) / MA(CLOSE, L1) * 100\n    BIAS2 = (CLOSE - MA(CLOSE, L2)) / MA(CLOSE, L2) * 100\n    BIAS3 = (CLOSE - MA(CLOSE, L3)) / MA(CLOSE, L3) * 100\n    return RD(BIAS1), RD(BIAS2), RD(BIAS3)\n```\n\n```python\ndef BOLL(CLOSE,N=20, P=2):                #BOLL布林带    \n    MID = MA(CLOSE, N); \n    UPPER = MID + STD(CLOSE, N) * P\n    LOWER = MID - STD(CLOSE, N) * P\n    return RD(UPPER), RD(MID), RD(LOWER)    \n```\n\n```python\ndef PSY(CLOSE,N=12, M=6):                 #PSY心理线指标\n    PSY=COUNT(CLOSE\u003eREF(CLOSE,1),N)/N*100\n    PSYMA=MA(PSY,M)\n    return RD(PSY),RD(PSYMA)\n```\n\n```python\ndef CCI(CLOSE,HIGH,LOW,N=14):            #CCI顺势指标\n    TP=(HIGH+LOW+CLOSE)/3\n    return (TP-MA(TP,N))/(0.015*AVEDEV(TP,N))\n```\n\n```python\ndef ATR(CLOSE,HIGH,LOW, N=20):           #真实波动N日平均值\n    TR = MAX(MAX((HIGH - LOW), ABS(REF(CLOSE, 1) - HIGH)), ABS(REF(CLOSE, 1) - LOW))\n    return MA(TR, N)\n```\n\n```python\ndef BBI(CLOSE,M1=3,M2=6,M3=12,M4=20):    #BBI多空指标   \n    return (MA(CLOSE,M1)+MA(CLOSE,M2)+MA(CLOSE,M3)+MA(CLOSE,M4))/4  \n```\n\n\n```python\ndef TAQ(HIGH,LOW,N):                         #唐安奇通道(海龟)交易指标，大道至简，能穿越牛熊\n    UP=HHV(HIGH,N);    DOWN=LLV(LOW,N);    MID=(UP+DOWN)/2\n    return UP,MID,DOWN\n```\n\n```python\ndef KTN(CLOSE,HIGH,LOW,N=20,M=10):           #肯特纳交易通道, N选20日，ATR选10日\n    MID=EMA((HIGH+LOW+CLOSE)/3,N)\n    ATRN=ATR(CLOSE,HIGH,LOW,M)\n    UPPER=MID+2*ATRN;   LOWER=MID-2*ATRN\n    return UPPER,MID,LOWER   \n```\n\n```python\ndef TRIX(CLOSE,M1=12, M2=20):                #三重指数平滑平均线\n    TR = EMA(EMA(EMA(CLOSE, M1), M1), M1)\n    TRIX = (TR - REF(TR, 1)) / REF(TR, 1) * 100\n    TRMA = MA(TRIX, M2)\n    return TRIX, TRMA\n```\n\n```python\ndef BRAR(OPEN,CLOSE,HIGH,LOW,M1=26):         #BRAR-ARBR 情绪指标  \n    AR = SUM(HIGH - OPEN, M1) / SUM(OPEN - LOW, M1) * 100\n    BR = SUM(MAX(0, HIGH - REF(CLOSE, 1)), M1) / SUM(MAX(0, REF(CLOSE, 1) - LOW), M1) * 100\n    return AR, BR\n```\n\n```python\ndef MTM(CLOSE,N=12,M=6):                    #动量指标\n    MTM=CLOSE-REF(CLOSE,N);         MTMMA=MA(MTM,M)\n    return MTM,MTMMA\n```\n```python\ndef ROC(CLOSE,N=12,M=6):                     #变动率指标\n    ROC=100*(CLOSE-REF(CLOSE,N))/REF(CLOSE,N);    MAROC=MA(ROC,M)\n    return ROC,MAROC\n```\n```python\ndef EXPMA(CLOSE,N1=12,N2=50):                #EMA指数平均数指标\n    return EMA(CLOSE,N1),EMA(CLOSE,N2);\n``` \n```python\ndef OBV(CLOSE,VOL):                          #能量潮指标\n    return SUM(IF(CLOSE\u003eREF(CLOSE,1),VOL,IF(CLOSE\u003cREF(CLOSE,1),-VOL,0)),0)/10000\n``` \n\n```python\ndef MFI(CLOSE,HIGH,LOW,VOL,N=14):            #MFI指标是成交量的RSI指标\n    TYP = (HIGH + LOW + CLOSE)/3\n    V1=SUM(IF(TYP\u003eREF(TYP,1),TYP*VOL,0),N)/SUM(IF(TYP\u003cREF(TYP,1),TYP*VOL,0),N)  \n    return 100-(100/(1+V1))    \n``` \n\n\n\n* 更多指标看库文件  [MyTT.py](https://github.com/mpquant/MyTT/blob/main/MyTT.py)\n\n### 因为语法的问题 =: 是不能用了，python就是=号 ，条件与是\u0026 ，条件或是|\n```python\n\n#通达信函数 VAR1:=(C\u003eREF(C,1) AND C\u003eREF(C,2));\n python写法： VAR1=( (CLOSE\u003eREF(CLOSE,1)) \u0026 (CLOSE\u003eREF(CLOSE,2)) );\n\n# 收盘价在10日均线上 且10日均线在20日均线上\npython写法： (C \u003e MA(C, 10)) \u0026 (MA(C, 10) \u003e MA(C, 20))\n\n# 收阳线 或 收盘价大于昨收\npython写法： (CLOSE \u003e O) | (CLOSE \u003e REF(CLOSE, 1))\n\n```\n\n\n### BOLL带指标数据获取和做图演示 (上证综指)\n\n```python\nup,mid,lower=BOLL(CLOSE)                                        #获取布林带数据 \n\nplt.figure(figsize=(15,8))  \nplt.plot(CLOSE,label='上证');    plt.plot(up,label='up');        #画图显示 \nplt.plot(mid,label='mid');      plt.plot(lower,label='lower');\n\n```\n\u003cdiv  align=\"center\"\u003e \u003cimg src=\"/img/boll.png\" width = \"960\" height = \"400\" alt=\"Boll线\" /\u003e \u003c/div\u003e\n\n\n### 唐安奇交易通道指标计算和做图演示 (沪深300指数)\n\n```python\nup,mid,down=TAQ(HIGH,LOW,20)                                    #获取唐安奇交易通道数据，大道至简，能穿越牛熊\nplt.figure(figsize=(15,8))  \nplt.plot(CLOSE,label='沪深300指数')                                  \nplt.plot(up,label='唐安奇-上轨');     plt.plot(mid,label='唐安奇-中轨');      plt.plot(down,label='唐安奇-下轨')\n```\n\u003cdiv  align=\"center\"\u003e \u003cimg src=\"/img/taq.jpg\" width = \"960\" height = \"400\" alt=\"taq\" /\u003e \u003c/div\u003e\n\n\n### 需安装第三方库（无需ta-lib库，所有指标实现仅需要安装pandas既可）\n* pandas\n\n\n### 代码贡献者Contributors \n    火焰，jqz1226, stanene, bcq\n\n\n----------------------------------------------------\n### 团队其他开源项目 - 如果本项目能帮助到您，请右上角帮我们点亮 ★star 以示鼓励！\n* [MyTT 通达信,同花顺公式指标，文华麦语言的python实现](https://github.com/mpquant/MyTT)\n\n* [Ashare最简股票行情数据接口API,A股行情完全开源免费](https://github.com/mpquant/Ashare)\n\n\n\n----------------------------------------------------\n\n![加入群聊](https://github.com/mpquant/Ashare/blob/main/img/qrcode.png) \n\n\u003e #### 股市程序化交易大群,数字货币量化交易探讨, 圈内大咖量化策略分享\n\u003e #### 全是干货，无闲聊 ，物以类聚,人以群分，一起感受思维碰撞的力量!\n\n\n\n----------------------------------------------------\n## Star 历史\n[![Star History Chart](https://api.star-history.com/svg?repos=mpquant/MyTT\u0026type=Date)](https://star-history.com/#mpquant/MyTT\u0026Date)\n","funding_links":[],"categories":["Python"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmpquant%2FMyTT","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmpquant%2FMyTT","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmpquant%2FMyTT/lists"}