Jun 6

一个读股票文件和生成移动平均线的python程序 不指定

adminleon , 01:24 , python , 评论(0) , 引用(0) , 阅读(160) , Via 本站原创 | |


用python读股票文件都几方便,看来可以用python构造自己的交易程序了,
以下是读文件和生成移动平均线值的python程序


#-*- encoding: gbk -*-
from   struct   import   *
import time

# 读取股票文件, 使用是大智慧股票软件的数据文件格式
fd=file("500018.day",'rb')


# 读取文件到数组xx
xx=[]

try:
  buf = fd.read()
  icount=len(buf)/40
  k=0
  

  while True:
    j=k*40
    (a1,a2,a3,a4,a5,a6,a7)=unpack('IIIIIII',buf[j:(j+28)])

    
    idate=a1
    iopen=float(a2) / 1000
    ihigh=float(a3) / 1000
    ilow=float(a4) / 1000
    iclose=float(a5) / 1000
    ivol=a6
    # 一列的数组z
    z=[idate,iopen,ihigh,ilow,iclose,ivol,a7]

    # 一列数据再插入数组xx
    xx.append(z)
    k=k+1
    if k==icount:
      break

finally:
  fd.close;



# 位置是结尾
weizhi=len(xx)-1

# 周期 5
zou=5

# zoux是周期 weizhix是计算位置 data是输入数组文件
def NewMa(zoux,weizhix,data):
   zclose=0
   aclose=0

   for i in range(0,zoux):
     aa=data[weizhix-i]
    # print aa
     aclose=aa[4]
     zclose=zclose+aclose

   maclose=zclose / zou
   return maclose


# 显示 5日均线的值
print NewMa(zou,weizhi,xx)

发表评论
表情
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
打开HTML
打开UBB
打开表情
隐藏
记住我
昵称   密码   游客无需密码
网址   电邮   [注册]