Python 获取任意周期开盘日
作者:互联网
import json import requests import datetime import tushare as ts cal_dates = ts.trade_cal() today=datetime.date.today().strftime("%Y%m%d") #获取今日日期 url='http://xxxx:xx/api/market/ontradingpool' #默认url #获取任意周期交易日数据 def opendate(n,date12): stocklist=[] for i in cal_dates.index: str_date = cal_dates.loc[i]['calendarDate'] isOpen = cal_dates.loc[i]['isOpen'] if not isOpen: continue date = datetime.datetime.strptime(str_date, '%Y-%m-%d').date() #获取格式为2019-02-22 date1=date.strftime("%Y%m%d") #处理日期格式为20190222 stocklist.append(date1) start=int(stocklist.index(date12)-n+1) #往前倒推n天 end=int(stocklist.index(date12)+1) #结束日期 opendate=stocklist[start:end] return opendate #获取单策略单日期对应code数据 def getStock(url,key,date): #使用requests.get 拼接url数据 date={ 'key':key, 'date':date } res=requests.get(url,params=date) all_info=json.loads(res.text) #result = json.loads(all_info) stocklist = [] for h in range(len(all_info)): code = all_info[h]['code'] # print(code) result3 = str(code) if code > 1000000: code = result3[1:7] else: code = result3[0:6] stocklist.append(code) stocklist = list(set(stocklist)) # set去重 return stocklist # def Boduan(date,key):#波段 # api2 = 'http://xxxx:xx/api/market/ontradingpool?key={key}&date={date}'.format(key=key, date=date) # print(api2) # all_info = requests.get(api2).text # result = json.loads(all_info) # stocklist = [] # for h in range(len(result)): # code=result[h]['code'] # #print(code) # result3 = str(code) # if code>1000000: #处理股票代码格式统一化 # code=result3[1:7] # else: # code = result3[0:6] # stocklist.append(code) # stocklist=list(set(stocklist))#set去重 # #print(len(stocklist)) # return stocklist #多日数据生成list def tongji(): listBoduan = [] #两个参数同时for循环 for i in range(len(opendate1)): listLine=getStock(url,strategyG,opendate1[i]) listBoduan += listLine print(len(set(listBoduan))) if __name__ == '__main__': opendate1 = opendate(20,'20190225') strategyG = '100005' tongji()
标签:info,code,stocklist,Python,result3,获取,key,date,任意 来源: https://www.cnblogs.com/soloate/p/10455407.html