# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

import tushare as ts

cons = ts.get_apis()

# 1、获取股票行情

#股票日线行情
df = ts.bar('600000', conn=cons, freq='D', start_date='2016-01-01', end_date='')
df.head(5)

# 2、股票因子数据

#带因子的行情
df = ts.bar('600000', conn=cons, start_date='2016-01-01', end_date='', ma=[5, 10, 20], factors=['vr', 'tor'])
df.head(5)

# 3、复权数据

#复权行情, adj=qfq(前复权)， hfq（后复权），默认None不复权
df = ts.bar('600000', conn=cons, adj='qfq', start_date='2016-01-01', end_date='')
df.head(5)

# 4、分钟和其它频度数据

#分钟数据, 设置freq参数，分别为1min/5min/15min/30min/60min，D(日)/W(周)/M(月)/Q(季)/Y(年)
df = ts.bar('600000', conn=cons, freq='1min', start_date='2016-01-01', end_date='')
df.head(15)

# 指数行情

#指数日行情, 设置asset='INDEX'
df = ts.bar('000300', conn=cons, asset='INDEX', start_date='2016-01-01', end_date='')
df.head(5)

# 5、港股数据

#港股数据, 设置asset='X'
df = ts.bar('00981', conn=cons, asset='X', start_date='2016-01-01', end_date='')
df.head(5)

# 6、期货数据

#期货数据, 设置asset='X'
df = ts.bar('CU1801', conn=cons, asset='X', start_date='2016-01-01', end_date='')
df.head(5)

# 7、美股数据

import time
import sys
from datetime import datetime, timedelta,date


def n_days_ago(n):
	#now=datetime.now()
	today=date.today()
	#print("Now:\n"+str(today))
	ndays_ago=today-timedelta(n)
	#print(str(n)+" days ago:\n"+str(ndays_ago))
	return str(ndays_ago)

n=180
while n>0:
    ndate=n_days_ago(n)
    df = ts.tick('300676', conn=cons, date=ndate)
    if df is None:
        print(ndate + ": NULL")
    else:
        print(ndate + ": Valid")
        print(len(df.datetime))
    n=n-1

df=ts.get_tick_data('000050',date="2017-09-16")
print(df)

n=180
while n>0:
    ndate=n_days_ago(n)
    df = ts.get_tick_data('000050',date=ndate)
    if df is None:
        print(ndate + ": NULL")
    else:
        print(ndate + ": Valid")
    n=n-1

#美股数据, 设置asset='X'
df = ts.bar('BABA', conn=cons, asset='X', start_date='2016-01-01', end_date='')
df.head(5)

# 8,股票tick

# +
#股票tick,type:买卖方向，0-买入 1-卖出 2-集合竞价成交
_df = ts.tick('000050', conn=cons, date='2018-02-26')


if _df is None:
    idx=0
else:
    idx=len(_df.datetime)
i=0
while(i<idx):
    print(i)
    if(_df.type[i] == '0'):
        print("Buy")
    else:
        print("Sell")
    i=i+1
# -

# 9,期货tick

#期货tick,type:买卖方向，0:开仓  1:多开   -1:空开
df = ts.tick('CU1712', conn=cons, asset='X', date='2017-10-28')
df.head(20)

# # 沪深港通资金流向
#

#沪/深港通每日资金流向(南向/北向资金)
df = ts.moneyflow_hsgt()
df.sort_values('date', ascending=False)



StarcoderdataJupyterScriptsDedupFiltered