def updateAllDayKLine(): '''从新浪接口获取所有存在的商品的K线图数据,按品种保存到__DATA_DIR中,耗时较长,慎用''' futurelogger.log("开始从新浪网更新最新的商品K线数据,updateAllDayKLine() begins.") commodityList = getCommodityList() for commodityCode in commodityList: __establishCommodityDataFromInternet(commodityCode) futurelogger.log("从新浪网更新最新的商品K线数据完毕,updateAllDayKLine() ends.")
def test06(self): '''测试ExpectedIndex数学期望方差指标''' futurecommodity.setDataDirAfterVerified() theKLineList = futurecommodity.getContractKLineList("AP1901", True) futurelogger.log(theKLineList) fiMacd = futureindex.ExpectedIndex(26) futurelogger.log(fiMacd.cal(theKLineList))
def test05(self): '''测试macd指标''' futurecommodity.setDataDirAfterVerified() theKLineList = futurecommodity.getContractKLineList("AP1901", True) futurelogger.log(theKLineList) fiMacd = futureindex.MacdIndex(12, 26, 9) futurelogger.log(fiMacd.cal(theKLineList))
def testName(self): theKLineList = futurecommodity.getContractKLineList("AP1901") #scs = futurestrategy.SimpleChannelCloseStrategy(20) mcs = futurestrategy.MaChannelStrategy(3, 18) signalList = mcs.signal(theKLineList) combineList = futureindex.combineIndexLists( [theKLineList.getTimeList(), signalList]) futurelogger.log(combineList)
def updateMainContractDict(): '''扫描所有合约数据文件,生成主力合约信息,并写入__DATA_MAIN_CONTRACT_FILENAME,虽然不是网络函数,但也耗时较长''' futurelogger.log("开始扫描所有K线数据文件并生成主力合约数据文件,updateMainContractDict() begins.") commodityList = getCommodityList() mainContractDict = {}#存放主力合约的字典 for commodityCode in commodityList:#遍历所有品种 #现将该品种的所有合约数据一次性读入到theContractDict theContractDict = __getCommodityDataToDict(commodityCode) #按照时间遍历theContractDict中的所有合约,找出主力合约 beginDate = datetime.datetime(__BEGIN_YEAR, 1, 1) endDate = datetime.datetime(__getNowYear(), __getNowMonth(), 28) theDaysList = futuretools.getDaysList(beginDate, endDate) mainContractInTheCommodity = {} lastContractName = None for theDate in theDaysList: contractVolDict = {} #读出该品种所有合约在当天的成交量 for theContractName in theContractDict.keys(): sub = theContractDict[theContractName].getSubscript(theDate) if sub is None: continue contractVolDict[theContractName] = theContractDict[theContractName].getKLine( sub).vol() #若在该品种所有合约中未找到该日期,则略过 if len(contractVolDict) == 0: continue #找出成交量最大的合约,加到mainContractDict[commodityCode]中 maxVol = __MIN_VOL_IN_MAIN_CONTRACT maxVolContractName = "" for theContractName in contractVolDict.keys(): if maxVol < contractVolDict[theContractName]: maxVol = contractVolDict[theContractName] maxVolContractName = theContractName if maxVol == __MIN_VOL_IN_MAIN_CONTRACT: #当天成交量太小的话不算数 continue #把主力合约的时间也记上 if maxVolContractName not in mainContractInTheCommodity.keys(): mainContractInTheCommodity[maxVolContractName] = [theDate, theDate] if lastContractName is not None: theLastContractBeginDate = mainContractInTheCommodity[ lastContractName][0] mainContractInTheCommodity[lastContractName] = [ theLastContractBeginDate, theDate] lastContractName = maxVolContractName theLastContractBeginDate = mainContractInTheCommodity[lastContractName][0] mainContractInTheCommodity[lastContractName] = [theLastContractBeginDate, endDate] mainContractDict[commodityCode] = mainContractInTheCommodity futuretools.writeListToFile(__DATA_MAIN_CONTRACT_FILENAME, mainContractDict) futurelogger.log("扫描所有K线数据文件并生成主力合约数据文件完毕,updateMainContractDict() ends.")
def updateAllAvailableCommodityCode(): '''从新浪接口检测获取所有存在的商品代码,并写入__DATA_COMMODITY_FILENAME,耗时较长,非出现新品种的情况下不要使用''' futurelogger.log("开始从新浪网更新最新的商品代码,updateAllAvailableCommodityCode() begins.") ALPHABET = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' theList = [] for commodityCode in ALPHABET: if __isCommodityExist(commodityCode): theList.append(commodityCode) for commodityCode2 in ALPHABET: if __isCommodityExist(commodityCode + commodityCode2): theList.append(commodityCode + commodityCode2) futuretools.writeListToFile(__DATA_COMMODITY_CODE_FILENAME, theList) futurelogger.log("从新浪网更新最新的商品代码完毕,updateAllAvailableCommodityCode() ends.")
def __readList2FromInternet(theUrl): '''从新浪数据接口读入日线图,如果内部数据能够解析成list则返回list类型结果,如果含null数据则直接返回None''' req=urllib.request.Request(theUrl) resp=urllib.request.urlopen(req) data=resp.read().decode('utf-8') if data == "null": return None try: theList = eval(data) except NameError: futurelogger.log("在解析以下url时发生了NameError异常,不能返回list类型,直接返回None:" + theUrl) futurelogger.log("原型string如下:" + data + "\n") return None else: return theList
def test04(self): '''测试rsi指标''' futurecommodity.setDataDirAfterVerified() theKLineList = futurecommodity.getContractKLineList("AP1901", True) futurelogger.log(theKLineList) fiRsi6 = futureindex.RsiIndex(6) futurelogger.log(fiRsi6.cal(theKLineList)) fiRsi12 = futureindex.RsiIndex(3) futurelogger.log(fiRsi12.cal(theKLineList)) fiRsi24 = futureindex.RsiIndex(24) futurelogger.log(fiRsi24.cal(theKLineList))
def checkAllDayKLine(): '''检查所有K线的合法性,可接在updateAllDayKLine之后立即调用为佳,本函数耗时较长,慎用''' futurelogger.log("开始检查所有K线数据文件的合法性并将校正过的数据放入verified文件夹,checkAllDayKLine() begins.") infoDict = {} for commodityCode in getCommodityList(): #仅检查主力合约 dataDict = __getCommodityDataToDict(commodityCode, getMainContractDict()[commodityCode].keys()) for contractName in dataDict.keys(): kLineList = dataDict[contractName] info = kLineList.checkAndCorrectData(contractName) infoDict[contractName] = info setDataDirAfterVerified() commodityDir = __getDataDir() + "/" + commodityCode futuretools.mkdirIfNotExist(commodityDir) futuretools.writeListToFile(commodityDir + "/" + contractName + ".txt", kLineList) setDataDirBeforeVerified() futuretools.writeListToFile("../checkReport.txt", infoDict) setDataDirAfterVerified() futurelogger.log("检查所有K线数据文件的合法性并将校正过的数据放入verified文件夹完毕,checkAllDayKLine() ends.")