From 43f96b1e052d8002642028795e77aa1d36d9d2b9 Mon Sep 17 00:00:00 2001 From: msincenselee Date: Thu, 5 Nov 2015 00:28:12 +0800 Subject: [PATCH] 201501105 --- .gitignore | 5 ++ vn.data/mysql_createtable.py | 52 ++++++++++++ vn.data/test_tushare.py | 80 ++++++++++++++----- vn.datayes/tests.py | 37 ++++++++- vn.strategy/strategydemo/backtestingEngine.py | 12 +-- 5 files changed, 158 insertions(+), 28 deletions(-) create mode 100644 vn.data/mysql_createtable.py diff --git a/.gitignore b/.gitignore index f45052a8..dabaa9a9 100644 --- a/.gitignore +++ b/.gitignore @@ -64,3 +64,8 @@ vn.lts/build/* *.pickle ======= >>>>>>> refs/remotes/vnpy/master +vn.strategy/strategydemo/strategy01.py +tk.csv +vn.strategy/strategydemo/backtestingStrategy02.py +vn.strategy/strategydemo/strategy01.py +vn.strategy/strategydemo/strategy02.py diff --git a/vn.data/mysql_createtable.py b/vn.data/mysql_createtable.py new file mode 100644 index 00000000..e4a66395 --- /dev/null +++ b/vn.data/mysql_createtable.py @@ -0,0 +1,52 @@ +# encoding: UTF-8 + +import MySQLdb + +#---------------------------------------------------------------------- + + + + +def main(): + + try: + #连接数据库 + conn = MySQLdb.connect(host='vnpy.cloudapp.net', user='stockcn', passwd='7uhb*IJN', db='stockcn', port=3306) + + #获取指针 + cur = conn.cursor(MySQLdb.cursors.DictCursor) + + listSymbol = ['ag','al','au','ax','ay','b','bb','bu','c','cf','cs','cu','er','fb','fg','fu','hc','i','j','jd','jm','jr','l','lr','m','ma','me','ni','oi','p','pb','pm','pp','rb','ri','rm','ro','rs','ru','sf','sm','sn','sr9','sr','srx','sry','ta','tc','v','wh','wr','ws9','ws','wt','y','zn'] + + strSQL = 'CREATE TABLE if not exists TB_{0}{1}( ' \ + 'datetime datetime not null,' \ + ' date date Not NULL,' \ + ' time time Not NULL,' \ + ' open numeric(18, 3) NULL,' \ + ' high numeric(18, 3) NULL,' \ + ' low numeric(18, 3) NULL,' \ + ' close numeric(18, 3) NULL,' \ + ' volume int null,' \ + ' CONSTRAINT PK_TB_{0}{1} PRIMARY KEY (datetime)' \ + ' );' + + for symbol in listSymbol: + + print symbol, 'M1' + #执行脚本 + cur.execute(strSQL.format(symbol, 'M1')) + conn.commit + + print symbol, 'M5' + cur.execute(strSQL.format(symbol, 'M5')) + conn.commit + + + #关闭指针,关闭连接 + cur.close() + conn.close() + except MySQLdb.Error, e: + print "Mysql Error %d: %s" % (e.args[0], e.args[1]) + +if __name__ == '__main__': + main() diff --git a/vn.data/test_tushare.py b/vn.data/test_tushare.py index 546809c0..085a5e0c 100644 --- a/vn.data/test_tushare.py +++ b/vn.data/test_tushare.py @@ -1,25 +1,65 @@ # encoding: UTF-8 import tushare as ts import pandas as pd -import ipdb - -data = ts.get_hist_data('600848', start='2015-04-01', end='2015-10-20') #一次性获取全部日k线数据 - -print data.tail(10) - -data.plot() - -data.high.plot() - -with pd.plot_params.use('x_compat',True): - data.open.plot(color = 'g') - data.close.plot(color = 'y') - data.high.plot(color = 'r') - data.low.plot(color = 'b') - - -with pd.plot_params.use('x_compat',True): - data.high.plot(color ='r', figsize = (10,4), grid ='on') - data.low.plot(color ='b', figsize = (10,4), grid ='on') +#import ipdb +import datetime +#data = ts.get_hist_data('600848', start='2015-04-01', end='2015-10-20') #一次性获取全部日k线数据 +# +#print data.tail(10) +# +#data.plot() +# +#data.high.plot() +# +#with pd.plot_params.use('x_compat',True): +# data.open.plot(color = 'g') +# data.close.plot(color = 'y') +# data.high.plot(color = 'r') +# data.low.plot(color = 'b') +# +# +#with pd.plot_params.use('x_compat',True): +# data.high.plot(color ='r', figsize = (10,4), grid ='on') +# data.low.plot(color ='b', figsize = (10,4), grid ='on') +# #ipdb.set_trace() +# + +ts.set_token('21768410ff5c2acb686264fa97635fa917a2ba1ae5122fa187b45849d92cfc70') + +md = ts.Market() + +#data = md.MktFutd(ticker='au1512',beginDate='20151008', endDate='20151028', +# field='tradeDate,ticker,openPrice,highestPrice,lowestPrice,closePrice,preClosePrice,CHG,CHG1,CHGPct') + +data = md.MktFutd(tradeDate='20140201', + field='tradeDate,ticker,openPrice,highestPrice,lowestPrice,closePrice,preClosePrice,CHG,CHG1,CHGPct') + + +#data['TR'] = abs(data['closePrice']-data['openPrice']) + +#atr = data.tail(10)['TR'].sum()/10 + +tradeDate= str(data.tail(1)['tradeDate'].values[0]) + +predate = datetime.datetime.strptime(tradeDate, '%Y-%m-%d') + +preOpen = float(data.tail(1)['openPrice']) +preHigh = float(data.tail(1)['highestPrice']) +preLow = float(data.tail(1)['lowestPrice']) +preClose = float(data.tail(1)['closePrice']) + +print data + +#print atr + +print predate +print preOpen +print preHigh +print preLow +print preClose + +print data.columns + + diff --git a/vn.datayes/tests.py b/vn.datayes/tests.py index 452a8159..7d63dcea 100644 --- a/vn.datayes/tests.py +++ b/vn.datayes/tests.py @@ -1,4 +1,6 @@ +# encoding: UTF-8 from api import * +import matplotlib.pyplot as plt def test_config(): cfig = Config() @@ -99,9 +101,38 @@ def test_mktbar_M1_get_interM(): api = PyApi(Config()) api.get_equity_M1_interMonth(db=db, id=0, tasks=['000001.XSHE','000002.XSHE']) +def test_mktfutd(): + + api = PyApi(Config()) + + data = api.get_future_D1(ticker='au1512',start='20151010', end='20151029',field='tradeDate,ticker,openPrice,highestPrice,lowestPrice,closePrice,preClosePrice,CHG,CHG1,CHGPct').body.tail(10) + + print data + + #data['TR'] = abs(data['closePrice']-data['openPrice']) + + #atr = 0.0 + + #atr = (data['TR'].sum())/10 + + #print atr + +def test_MktEqudGet(): + + api = PyApi(Config()) + + # DataAPI.MktEqudGet返回pandas.DataFrame格式 + MarketEqud = api.get_equity_D1(secID = "000002.XSHE", + field = ["shortNM", "closePrice", "turnoverValue", "capitalInflow"], + start = "20000106", + end = "20140110").body + + # 绘制返回的数据 + plt(MarketEqud, settings = {'x':'tradeDate','y':'closePrice', 'title':u'万科历史收盘价格'}) + if __name__ == '__main__': #test_config() - test_mktbar_D1() + #test_mktbar_D1() #test_bond_D1() #test_fut_D1() #test_fund_D1() @@ -116,4 +147,6 @@ if __name__ == '__main__': #test_mongod_get_all() #test_mktbar_M1_get_drudgery() #test_mktbar_M1_get_all() - #test_mktbar_M1_get_interM() \ No newline at end of file + #test_mktbar_M1_get_interM() + #test_mktfutd() + test_MktEqudGet() diff --git a/vn.strategy/strategydemo/backtestingEngine.py b/vn.strategy/strategydemo/backtestingEngine.py index 3e2f4e07..370887e5 100644 --- a/vn.strategy/strategydemo/backtestingEngine.py +++ b/vn.strategy/strategydemo/backtestingEngine.py @@ -553,15 +553,15 @@ class BacktestingEngine(object): if tradeItem['Direction'] == '0': - if tradeItem['OffsetFlag'] == '0' : + if tradeItem['OffsetFlag'] == '0': # 开多仓 Buy amount = 0-float(tradeItem['Price'])*int(tradeItem['Volume']) - else: + else: # 平空仓 Cover + amount = 0 -float(tradeItem['Price'])*int(tradeItem['Volume']) + else: + if tradeItem['OffsetFlag'] == '0': # 开空 Short amount = float(tradeItem['Price'])*int(tradeItem['Volume']) - else : - if tradeItem['OffsetFlag'] == '0' : + else: # 平多 sell amount = float(tradeItem['Price'])*int(tradeItem['Volume']) - else: - amount = 0 - float(tradeItem['Price'])*int(tradeItem['Volume'])