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()
相關文章
相關標籤/搜索