Skip to content

Instantly share code, notes, and snippets.

@zhangz
Last active September 9, 2019 01:17
Show Gist options
  • Save zhangz/9af1a7ea91dfc8f37e93a9f0b622297d to your computer and use it in GitHub Desktop.
Save zhangz/9af1a7ea91dfc8f37e93a9f0b622297d to your computer and use it in GitHub Desktop.
# coding: utf8
import tushare as ts
import pandas as pd
def calcOneMinuteBar():
# data_frame = pd.read_csv('AUDJPY-2016-01.csv', names=['Symbol', 'Date_Time', 'Bid', 'Ask'],
# index_col=1, parse_dates=True)
df = ts.get_tick_data('002743', date='2016-10-28')
# df.tail(10)
df['time'] = '2016-10-28 ' + df['time']
df['time'] = pd.to_datetime(df['time'])
df = df.set_index(['time'])
# TICK数据转成开、高、低、收
price_df = df['price'].resample('1min').ohlc()
price_df = price_df.dropna()
# 成交量
vols = df['volume'].resample('1min').sum()
vols = vols.dropna()
vol_df = pd.DataFrame(vols, columns=['volume'])
# 成交额
amounts = df['amount'].resample('1min').sum()
amount = amounts.dropna()
amount_df = pd.DataFrame(amounts, columns=['amount'])
# 合并
result_df = price_df.merge(vol_df, left_index=True, right_index=True).merge(
amount_df, left_index=True, right_index=True)
result_df.head()
# 一分钟转N分钟
d_dict = {'open': 'first', 'high': 'max',
'close': 'last', 'low': 'min', 'amount': 'sum'}
newdf = pd.DataFrame()
for col in newdf.columns:
newdf[col] = newdf[col].resample('5min', how=d_dict[col])
newdf.tail(20)
if __name__ == "__main__":
calcOneMinuteBar()
# http://mp.weixin.qq.com/s?__biz=MzAwOTgzMDk5Ng==&mid=2650833965&idx=1&sn=e3e74639c068e7a1e41a35bb1decd313
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment