上证指数(优化一)
1.自制模块
- sh.py
# -*- coding: utf-8 -*- class sh: """数据处理模块 sh """ #---------------------------------------------------------------------- def __init__(self, user='sa', password='123456', dsn='XiTongDSN'): """ SQL 信息初始化""" self.user = user self.password = password self.dsn = dsn import sqlalchemy self.engine = sqlalchemy.create_engine('mssql+pyodbc://'+self.user+':'+self.password+'@'+self.dsn) #---------------------------------------------------------------------- def get_url(self): """从 tushare.org 下载 sh 数据""" import tushare self.URLsh = tushare.get_hist_data('sh') #---------------------------------------------------------------------- def save_url(self): """把网络 sh 数据存入到本地 SQL """ import pandas index = list(self.URLsh['open'].index) self.URLsh['date'] = pandas.Series(index, index) pandas.DataFrame(self.URLsh, index) self.URLsh.drop(['price_change', 'p_change', 'ma5', 'ma10', 'ma20', 'v_ma5', 'v_ma10', 'v_ma20'], axis=1, inplace=True) self.URLsh.to_sql('sh', self.engine, if_exists='replace', index=False) #---------------------------------------------------------------------- def get_sql(self): """调取本地 SQL 数据""" import pandas self.connection = self.engine.connect() self.SQLsh = pandas.read_sql_table('sh', self.connection) self.connection.close() #---------------------------------------------------------------------- def save_sql(self, dataframe): """本地修改过的 sh 数据存回 SQL """ import pandas dataframe.to_sql('sh', self.engine, if_exists='replace', index=False)
- bs.py
# -*- coding: utf-8 -*- class bs: """买卖信号模块 bs """ #---------------------------------------------------------------------- def __init__(self, index, short, long): """数据初始化:索引,短线,长线""" self.index = index self.short = short self.long = long #---------------------------------------------------------------------- def buy(self): """买点""" self.bi = [] self.bs = [] self.bl = [] self.bbuy = [0] for i in range(1, len(self.index) - 1): if (self.short[i] > self.long[i]) and (self.short[i-1] < self.long[i-1]): self.bi.append(self.index[i]) self.bs.append(self.short[i]) self.bbuy.append(1) else: self.bbuy.append(0) #---------------------------------------------------------------------- def sell(self): """卖点""" self.si = [] self.ss = [] self.sl = [] self.ssell = [0] for i in range(1, len(self.index) - 1): if (self.short[i] < self.long[i]) and (self.short[i-1] > self.long[i-1]): self.si.append(self.index[i]) self.ss.append(self.short[i]) self.ssell.append(1) else: self.ssell.append(0)
- tr.py
# -*- coding: utf-8 -*- class tr: """交易测试模块 tr """ #---------------------------------------------------------------------- def __init__(self, money = 1000000, rate = 0.1): """信号初始化:本金,比例""" self.money = money self.rate = rate self.use = money * rate self.stock = 0 self.k = 0 self.final = [money] #---------------------------------------------------------------------- def trade(self, index, buy, sell, price): """交易函数""" for i in range(self.k, len(index) - 1): if buy[i] == 1: self.money = self.money - self.use self.stock = self.use / price[i] while self.stock: for j in range(self.k + 1, len(index) - 1): if sell[j] == 1: self.money = self.money + self.stock * price[j] self.stock = 0 self.final.append(self.money) self.k = self.k + 2 break
2.调用模块
# -*- coding: utf-8 -*- '''添加文件路径''' import sys sys.path.append('D:\360data\重要数据\桌面') """调用 sh.py 模块""" import sh SH = sh.sh() SH.get_sql() SQLsh = SH.SQLsh Index = list(SQLsh['open'].index) Open = SQLsh['open'] Close = SQLsh['close'] """调用 bs.py 模块""" import bs BS = bs.bs(Index, Open, Close) index = BS.index BS.buy() buy = BS.bbuy BS.sell() sell = BS.ssell """调用 tr.py 模块""" import tr ''' rate = 0.5 ''' TR1 = tr.tr(money=100000, rate=0.1) TR1.trade(index, buy, sell, Open) money1 = TR1.final ''' rate = 0.01 ''' TR2 = tr.tr(money=100000, rate=0.05) TR2.trade(index, buy, sell, Open) money2 = TR2.final '''画出资金曲线图''' import matplotlib.pyplot as plot plot.plot(money1, 'g-') plot.plot(money2, 'r-') plot.title('sh') plot.xlabel('index') plot.ylabel('money') plot.legend(('rate = 0.1', 'rate = 0.05')) plot.axis('auto') plot.grid(True)


浙公网安备 33010602011771号