Rss & SiteMap

金字塔客服中心 - 专业程序化交易软件提供商 http://www.weistock.com/bbs/

专业程序化软件提供商
共2 条记录, 每页显示 10 条, 页签: [1]
[浏览完整版]

标题:pandas

1楼
z7c9 发表于:2017/7/11 17:37:41
[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编辑过]
2楼
fantasynew 发表于:2017/7/11 21:49:54
能支持python就好玩了,现在不少搞机器学习的,回测结果不错
共2 条记录, 每页显示 10 条, 页签: [1]


Powered By Dvbbs Version 8.3.0
Processed in .01367 s, 2 queries.