以文本方式查看主题

-  金字塔客服中心 - 专业程序化交易软件提供商  (http://222.73.7.161/bbs/index.asp)
--  期货人生  (http://222.73.7.161/bbs/list.asp?boardid=7)
----  pandas  (http://222.73.7.161/bbs/dispbbs.asp?boardid=7&id=155880)

--  作者:z7c9
--  发布时间:2017/7/11 17:37:41
--  pandas
[Code] import pandas as pd %matplotlib inline df = pd.read_csv(\'/Volumes/homes/admin/CloudStation/Backup/WIN-86HQB8IN67K/D/data/futureday/SHFE_rb1801.csv\', encoding=\'gbk\') df = df.rename(columns={\'tradeDate\': \'date\', \'openPrice\': \'open\', \'highestPrice\': \'high\', \'lowestPrice\': \'low\', \'closePrice\': \'close\'}) df[\'range\'] = (df[\'high\'] - df[\'low\']).shift(1) df[\'upper\'] = df[\'open\'] + df[\'range\'] df[\'lower\'] = df[\'open\'] - df[\'range\'] trades = list() position = 0 n = 0 for i, x in df.iterrows(): if df.loc[i, \'high\'] >= df.loc[i, \'upper\']: if position<0: trade="trade" = {\'date\': df.loc[i, \'date\'], \'action\': \'平空\', \'qty\': 1, \'price\': df.loc[i, \'upper\']} trades.append(trade) position="position" = 0="0" if="if" position="=0: " trade="trade" = {\'date\': df.loc[i, \'date\'], \'action\': \'开多\', \'qty\': 1, \'price\': df.loc[i, \'upper\']} trades.append(trade) position="position" = 1 if="if" df.loc[i, \'low\'] <= df.loc[i, \'lower\']: if="if" position="position" >0: trade = {\'date\': df.loc[i, \'date\'], \'action\': \'平多\', \'qty\': 1, \'price\': df.loc[i, \'lower\']} trades.append(trade) position = 0 if position==0: trade = {\'date\': df.loc[i, \'date\'], \'action\': \'开空\', \'qty\': 1, \'price\': df.loc[i, \'lower\']} trades.append(trade) position = -1 if n == df.shape[0] - 1: if position>0: trade = {\'date\': df.loc[i, \'date\'], \'action\': \'强制平多\', \'qty\': 1, \'price\': df.loc[i, \'close\']} trades.append(trade) position = 0 if position<0: trade = {\'date\': df.loc[i, \'date\'], \'action\': \'强制平空\', \'qty\': 1, \'price\': df.loc[i, \'close\']} trades.append(trade) position = 0 n += 1 trade_df = pd.DataFrame(trades) trade_df openPos = trade_df[trade_df.index % 2 == 0].reset_index(drop=True) closePos = trade_df[trade_df.index % 2 != 0].reset_index(drop=True) ndf = pd.merge(openPos, closePos, left_index="True," right_index="True) ndf.loc[ndf[\'action_x\']" == \'开多\', \'profit\'] = (ndf[\'price_y\'] - ndf[\'price_x\']) * 10 ndf.loc[ndf[\'action_x\'] == \'开空\', \'profit\'] = (ndf[\'price_x\'] - ndf[\'price_y\']) * 10 ndf[\'cum_profit\'] = ndf[\'profit\'].cumsum() ndf[\'cum_profit\'].plot(figsize=(20, 5)) [Code]
[此贴子已经被作者于2017/7/11 17:38:20编辑过]

--  作者:fantasynew
--  发布时间:2017/7/11 21:49:54
--  
能支持python就好玩了,现在不少搞机器学习的,回测结果不错