def test_get_growth_data(self): mt = ts.Master() df = mt.TradeCal(exchangeCD='XSHG', beginDate='20150928', endDate='20151010', field='calendarDate,isOpen,prevTradeDate') print(df)
def load_Internet_data(self): mt = ts.Master() df = mt.SecID(assetClass='E', field=','.join(FETCH_FIELDS)) df[FETCH_FIELDS[0]] = df[FETCH_FIELDS[0]].astype('string') df.rename(columns={ FETCH_FIELDS[0]: TICKER_FIELD, FETCH_FIELDS[1]: NAME_FIELD, FETCH_FIELDS[2]: EXCHANGE_FIELD, FETCH_FIELDS[3]: STATUS_FIELD, FETCH_FIELDS[4]: LIST_DATE_FIELD }, inplace=True) df = df[df[EXCHANGE_FIELD].isin( [gs.SHANGHAI_EXCHANGE, gs.SHENZHEN_EXCHANGE])] df.index = Series(range(1, len(df) + 1)) return df
def load_data(self): mt = ts.Master() # Fetch data from tushare df = mt.SecID(assetClass='E', field=','.join(self.fields_map.keys())) # Change dtype of ticker to string ticker_field = self.fields_map.keys()[0] df[ticker_field] = df[ticker_field].astype('string') # Filter stocks not in Shanghai exchagne and Shenzhen exchange exchange_field = self.fields_map.keys()[2] df = df[df[exchange_field].isin([gs.SHANGHAI_EXCHANGE, gs.SHENZHEN_EXCHANGE])] # Recount index from 1 to len(df) + 1 df.index = Series(range(1, len(df) + 1)) self.data = df
# -*- coding: utf-8 -*- from datetime import datetime, timedelta print(datetime.now()) import tushare as ts import pandas as pd import numpy as np import os ts.set_token( '1075d9a4eb51461266520905807f7d68806f68511ad0c90d938dc81af9ea6dee') ts.Master().TradeCal(exchangeCD='XSHG,XSHE', beginDate='20130101').to_csv('TradeCal.csv')
import tushare as tus from pandas import DataFrame import datetime from talib import SMA import traceback tus.set_token( '4b35b6b552ea66ef08186d267e693c86ed4726227979720aac8fb2d42a1c242a') tl = tus.Master() maPeriods = [10, 20, 60] def mas_are_thrusted(ticker, date=datetime.datetime.now().strftime("%Y-%m-%d")): headDay = datetime.datetime.strftime( datetime.datetime.strptime(date, "%Y-%m-%d") - datetime.timedelta(days=365), "%Y-%m-%d") dMktD = tus.get_k_data(ticker, headDay, date) if dMktD.empty: return False closed = dMktD['close'].values maVals = ma_vals(maPeriods, closed) todayOpen = dMktD['open'].values.tolist()[-1] todayClose = closed.tolist()[-1] return todayClose > max(maVals) and todayOpen < min(maVals) def ma_vals(maperiods, closed):
client = pymongo.MongoClient("localhost", 27017) # DB_GETRICH = client.GETRICH db_SecID_coll = DB_GETRICH.SecID ## 证券编码及基本上市信息 db_TradeCal_coll = DB_GETRICH.TradeCal ## 交易所交易日历 db_SecTypeRel_coll = DB_GETRICH.SecTypeRel ## 证券板块成分 db_EquInfo_coll = DB_GETRICH.EquInfo ## 沪深股票键盘精灵 db_SecTypeRegionRel_coll = DB_GETRICH.SecTypeRegionRel ## 沪深股票地域分类 db_SecType_coll = DB_GETRICH.SecType ## 证券板块 db_SecTypeRegion_coll = DB_GETRICH.SecTypeRegion ## 地域分类 # 得到交易日 tlMaster = ts.Master() # 证券编码及基本上市信息 stocksType = ['E','B','F','IDX','FU','OP'] # E 股票,B 债券,F 基金,IDX 指数,FU 期货,OP 期权; for types in stocksType: df = tlMaster.SecID(assetClass=types) js_df = df[1:].to_json(orient="records") db_json = json.loads(js_df) db_SecID_coll.insert(db_json) # 交易所交易日历 # XSHG表示上海证券交易所,XSHE表示深圳证券交易所,CCFX表示中国金融期货交易所,XDCE表示大连商品交易所,XSGE表示上海期货交易所,XZCE表示郑州商品交易所,XHKG表示香港证券交易所 #exchangeType = ['XSHG','XSHE','CCFX','XDCE','XSGE','XZCE','XHKG'] exchangeType = ['XSHG','XSHE'] for types in exchangeType: df = tlMaster.TradeCal(exchangeCD=types, beginDate='20150928', endDate='20151010') #df = df['calendarDate'].apply(lambda x: x.replace('-', ''))
opener.addheaders = [] for key, ele in headers.items(): opener.addheaders += [(key, ele)] data = opener.open(url).read() indexSoup = BeautifulSoup(data) statusSoup = BeautifulSoup(indexSoup.prettify().decode("utf-8")) import tushare as ts ts.set_token( '6cbc132dcf304322dc7b6f1d714d792fe224049b2038abf29e7711bf71334b52') ''' 通联数据 没有分时数据 也没有黄金延期 ''' mt = ts.Master() code_info = mt.SecID( assetClass='FU', field='secID,ticker,secShortName,assetClass,exchangeCD,transCurrCD') st = ts.Market() AGTD = st.MktFutd( tradeDate='20160229', field='secID,tradeDate,ticker,secShortName,openPrice,closePrice') F_product = st.MktMFutd( tradeDate='20160229', field='secID,tradeDate,ticker,secShortName,openPrice,closePrice')
# -*- coding: utf-8 -*- from datetime import datetime, timedelta print(datetime.now()) import tushare as ts import pandas as pd import numpy as np import os ts.set_token( '1075d9a4eb51461266520905807f7d68806f68511ad0c90d938dc81af9ea6dee') #get all stock code, name ticker,secShortName,exchangeCD,listDate b01 = ts.Master().SecID(assetClass='E', field='ticker,exchangeCD') b01 = b01[((b01['exchangeCD'] == 'XSHE') | (b01['exchangeCD'] == 'XSHG')) & (b01['ticker'].str.len() == 6)].rename(columns={u'ticker': 'code'}) b02 = ts.get_stock_basics().loc[:, ['name', 'outstanding', 'timeToMarket']] b02['code'] = b02.index.values b0 = pd.merge(b02, b01, how='left', on='code') b1 = list(b0.code) #get Adj factor adj0 = pd.DataFrame() for i in list(range(len(b1)))[::451]: adj1 = ts.Market().MktAdjf(ticker=','.join(b1[i:min(i + 450, len(b1))]), field='ticker,exDivDate,adjFactor') if adj0.empty: adj0 = adj1 else: