python输入n行图形*_如何用Python找出TRIX金叉的股票?

话不多说先上代码,此代码经过反复验证,确认计算结果与通达信完全一致,这里周期取12日和9日:

#请使用前复权的日K线数据import os#缓存数据class SecurityData:  code = '' #股票代码  name = '' #股票名称  value = 0 #数值#定义列表lst = []#打开日志文件fs = open(r'C:\Py\result.txt','a+')#循环遍历所有的日K线文件for root, dirs, files in os.walk(r'C:\Py\day'):  for file in files:    #写日志    print('正在计算' + file)    #打开日K线文件    fs2 = open(os.path.join(root,file), 'r', True)    #索引    pos = 0    #数据索引    dataPos = 0    #股票名称    sName = ''    #交叉类型    isCross = 0    #定义周期    n = 12    m = 9    #上次的指标值    lastEMA1 = 0    lastEMA2 = 0    lastTr = 0    lastTrixMa = 0    #保存数据的集合    lstTrix = []    #循环遍历每一行    while True:      #读取该行      line = fs2.readline()      #没有行的时候退出      if not line: break      #去除前2行和尾行      if pos > 1 and len(line) > 20:        #重置多头数量        isCross = 0        #分割字符串        strs = line.split(',')            #收盘价        closePrice = float(strs[4])        if dataPos == 0:          lastEMA1 = closePrice        #计算指标        ema1 = (closePrice * 2 + lastEMA1 * (n - 1)) / (n + 1)        if dataPos == 0:          lastEMA2 = ema1        ema2 =  (ema1 * 2 + lastEMA2 * ( n - 1)) / (n + 1)        if dataPos == 0:          lastTr = ema2        tr =  (ema2 * 2 + lastTr * ( n - 1)) / (n + 1)        #计算TRIX        trix = 0        if dataPos > 0:          if lastTr != 0:            trix = (tr - lastTr) / lastTr * 100        lstTrix.append(trix)        #真实计算周期        realN = m        if dataPos < m:          realN = dataPos  + 1        #数值的和        startIndex = dataPos - m + 1        if startIndex < 0:          startIndex = 0        sum = 0        if dataPos > m:          #高性能求和          sum = lastTrixMa * m + trix - lstTrix[startIndex - 1]        else:          #计算和          thisLst = lstTrix[startIndex:dataPos + 1]          for cVal in thisLst:            sum = sum + cVal        #计算MA        trixMa = sum / realN        if dataPos > 0:          if trix > trixMa and lstTrix[dataPos - 1] < lastTrixMa:            isCross = 1        #保存上次的数据        lastEMA1 = ema1        lastEMA2 = ema2        lastTr = tr        lastTrixMa = trixMa        #累加数据索引        dataPos = dataPos + 1      elif pos == 0:        sName = line[line.find(' ') + 1 : line.find(' 日线')]      #累加索引      pos = pos + 1    #保存到列表中    securityData = SecurityData()    securityData.code = file    securityData.value = isCross    securityData.name = sName    lst.append(securityData)    #关闭文件流    fs2.close()#给列表排序from operator import attrgetterlst2 = sorted(lst, key=attrgetter('value'), reverse=True)#输出结果count = 0for val in lst2:  if val.value == 1:    print(str(count + 1) + '.' + val.name + ','  + val.code + ',TRIX金叉')    fs.write(str(count + 1) + '.' + val.name + ','  + val.code + ',TRIX金叉\r\n')    count = count + 1#关闭文件流fs.close()

新建一个文件,命名为TRIX.py,并将上述代码粘贴到你的文件中。

fb17081b87f0092b1c7cbc75a85aabf6.png

eb06803da8faac77139f52e6f45815cb.png

按照教程下载所有A股的前复权数据,并放到一个文件夹中:

如何免费轻松获得最完整可靠的股票期货等历史数据?

9e810143cf3e5c58eaef4c0a50604cca.png

修改Python中的文件和文件夹路径为你的路径:

6638623b7ec02a7b6a51766642673eed.png

如果没有安装Python,就到这个地址下载安装一下:

https://www.python.org/ftp/python/3.9.0/python-3.9.0-amd64.exe

注意第一个界面的Add to Path一定要勾上。

打开命令提示行,输入python C:\PY\TRIX.py

a091bfe819764892d168a64bc4df409a.png

输入回车运行脚本,得到如下结果:

483f7509c71f2ccfecefb035930d2f30.png

result.txt中也输出了结果:

9b9b49a1702e446289488e0d968d7b3e.png

打开图形K线验证结果:

第一个,皖通高速,SH600012,的确是金叉

a0071f6683ed5583e66a85821450d3dd.png

第二个,上海家化,SH600315,的确是金叉

4c2f71a147293f9abd6c30f621813e10.png

第三个,福耀玻璃,SH600660,的确是金叉

9e8688f0b4a281c6bace26f786677a75.png

结论:

1.计算结果完全正确,而且是完全对应通达信的;

2.可以直接运行,得到结果文件result.txt,怎么用看你的;

3.可以修改代码,例如修改输入结果文件的格式;

4.不止用于A股,什么品种,数据,包括1分钟,5分钟,60分钟线也都可以;

5.可以用来做交易回测,超卖和超买只是把trix > trixMa and lstTrix[dataPos - 1] < lastTrixMa:改成trix < trixMa and lstTrix[dataPos - 1] > lastTrixMa:。

6.可以把代码嵌入你的Python中,用来做实时监控。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Python中,可以使用talib库来计算TRIX指标。首先,你需要导入talib库并读取你的股票数据。引用中给出了一个示例代码来计算TRIX指标。然后,你可以使用talib.TRIX函数来计算TRIX指标的值,指定适当的参数,如使用的数据列和时间周期。最后,你可以将计算得到的TRIX指标值存储在一个新的列中。 例如,以下代码片段展示了如何使用talib库计算TRIX指标: ```python import pandas as pd import talib # 读取股票数据 df = pd.read_csv('E:/temp005/600660.csv', encoding='utf-8') # 删除停牌的数据 df = df.loc[df['openPrice']>0].copy() # 调整价格数据 df['openPrice'] = df['openPrice'] * df['accumAdjFactor'] df['closePrice'] = df['closePrice'] * df['accumAdjFactor'] df['highestPrice'] = df['highestPrice'] * df['accumAdjFactor'] df['lowestPrice'] = df['lowestPrice'] * df['accumAdjFactor'] # 计算TRIX指标 df['trix'] = talib.TRIX(df['closePrice'], timeperiod=12) # 打印计算结果 print(df['trix']) ``` 这样,你就可以得到TRIX指标的数值,并根据需要进进一步的分析和处理。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [python 依据某几列累加求和_如何用Python找出TRIX金叉股票?](https://blog.csdn.net/weixin_39755824/article/details/110076151)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"] - *2* *3* [python_计算股票指标](https://blog.csdn.net/m0_37967652/article/details/127579127)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值