def main(): signalData = SourceDataDao.getSignalData() dailyQuote = SourceDataDao.getDailyQuote() #columns filter #df3 = sourceData.loc[(df['Mom'] <= 4) & (df['Mom'] <= 4), ['Mom']] #index filter #startDate=DateUtils.str2Datetime('20010105'); #endDate=DateUtils.str2Datetime('20010111'); #df4 = df3.ix[startDate:endDate] #select top 5 group by TradingDay order by Mom desc groupedSignalData = signalData.groupby(level='TradingDay').apply( SelectUtil.top, 5, 'Mom', False) #param #period = 5 startDate = '1/8/2001' #endDate = '1/1/2017' endDate = '18/1/2001' #time series dateList = DateUtil.get_date_list2(startDate, endDate) stockHoldDF = {} stockTradeDF = {} lastSignalData = pd.DataFrame() usableVol = 100 netValue = 1 stockStatDaily = pd.DataFrame(index=pd.date_range(startDate, endDate), columns=['netValue', 'changePCT']) for date in dateList.index: #print(date) dateStr = DateUtil.datetime2_str(date) #print(dateStr) #isinstance(date, datetime) #DateUtil.str2Datetime('20010108') # select by single date try: #print(1) currSignalData = groupedSignalData.ix[date] print("currSignalData:" + str(len(currSignalData))) print(currSignalData) except: #假期,双休日,原数据问题 print(DateUtil.datetime2_str(date) + ': no data') continue
def main(): signalData = SourceDataDao.getSignalData() #dailyQuote = SourceDataDao.getDailyQuote() dailyQuote = SourceDataDao.getNewDailyQuote() #columns filter #df3 = sourceData.loc[(df['Mom'] <= 4) & (df['Mom'] <= 4), ['Mom']] #index filter #startDate=DateUtils.str2Datetime('20010105'); #endDate=DateUtils.str2Datetime('20010111'); #df4 = df3.ix[startDate:endDate] numOfDailyHolding = 5 #select top 5 group by TradingDay order by Mom desc groupedSignalData = signalData.groupby(level='TradingDay').apply( SelectUtil.top, numOfDailyHolding, 'Mom', False) #param #period = 5 #startDate = '1/8/2001' startDate = '1/8/2001' #endDate = '1/1/2017' #eendDate = '5/18/2001' endDate = '12/31/2016' #endDate = '12/31/2002' #endDate = '1/9/2001' #time series #dateList = DateUtil.getDateList2(startDate,endDate) #当前持仓 key:innerCode value: currHoldSet = {} stockHoldDF = {} stockTradeList = [] #资金情况 #capitalDict = {'usableCach':1000000} #初始资金 initialMV = 1000000 capitalEntity = CapitalEntity.CapitalEntity(initialMV) lastMV = initialMV #lastSignalData = pd.DataFrame() usableVol = 100 netValue = 1 #初始化每日统计表 stockStatDailyDf = pd.DataFrame(index=pd.date_range(startDate, endDate), columns=[ 'netValue', 'changePCT', 'buyCnt', 'sellCnt', 'prevHoldCnt', 'currHoldCnt', 'cannotSellCnt', 'cannotBuyCnt', 'usableCach', 'mv' ]) #初始化每日持仓表 stockHoldDailyList = [] #从信号表中取得唯一性日期 dateList = SourceDataDao.select_date_from_signal(signalData, startDate, endDate) for date in dateList: dateStr = DateUtil.datetime2_str(date) #print(dateStr) #isinstance(date, datetime) # select by single date #try: #print(1) currSignalData = groupedSignalData.ix[date] if StockConst.IS_DEBUG: print("currSignalData:" + str(len(currSignalData))) #print(2) buyList = getBuyList(currSignalData, currHoldSet) #print(3) sellList = getSellList(currSignalData, currHoldSet) #print(4) prevHoldList = getPrevHoldList(currSignalData, currHoldSet) #print(currSignalData) """ if (dateStr >= '2015-01-05') & (dateStr <= '2015-01-05'): print('-----'+dateStr+'-----') #print(currSignalData) #print(buyList) print(list(currHoldSet.keys())) for key in currHoldSet.keys(): print(str(key) + ':' + currHoldSet.get(key).openDate) print('----------------') """ #except: #假期,双休日,原数据问题 #if StockConst.isDebug: #print(DateUtil.datetime2Str(date) + ': no data') #continue dailyChangePCT = 0 actualSellList = [] actualBuyList = [] cannotSellList = [] cannotBuyList = [] #实际买入个数 numOfActualBuy = getNumOfActualBuy(date, buyList, dailyQuote) #1.sell changePCTSell handleSellList(date, sellList, dailyQuote, usableVol, stockHoldDF, stockTradeList, currHoldSet, actualSellList, cannotSellList, capitalEntity, numOfActualBuy, signalData, prevHoldList, numOfDailyHolding) #usableVol = calculateUsableVol(currHoldSet) #2.buy changePCTBuy handleBuyList(date, buyList, dailyQuote, usableVol, stockHoldDF, stockTradeList, currHoldSet, actualBuyList, cannotBuyList, capitalEntity) #3.hold #changePCTHold = handleHoldList(date,prevHoldList,dailyQuote,usableVol,stockHoldDF,stockTradeDF,currHoldSet) #changePCTHold = 0 currMV = calculateDailyMV(currHoldSet, capitalEntity, dailyQuote, date, stockHoldDailyList, initialMV) #handleStockHoldDaily(currHoldSet, stockHoldDailyList, date) buyCnt = len(actualBuyList) sellCnt = len(actualSellList) prevHoldCnt = len(prevHoldList) currHoldCnt = len(currHoldSet) cannotSellCnt = len(cannotSellList) cannotBuyCnt = len(cannotBuyList) #if StockConst.isDebug: #print("dateStr:" + dateStr + " changePCTBuy:" + str(changePCTBuy) + " changePCTSell:" + str(changePCTSell) + #" changePCTHold:" + str(changePCTHold)) #dailyChangePCT = changePCTBuy+changePCTSell+changePCTHold #print("dailyChangePCT:"+str(dailyChangePCT)) #netValue = netValue * (1 + dailyChangePCT / 100) #每日净值 netValue = currMV / initialMV #print("netValue:" + str(netValue)) #每日收益 dailyChangePCT = NumUtil.get_change_pct(lastMV, currMV, 2) #每日可用现金 usableCach = capitalEntity.get_usable_cash() stockStatDailyDf.ix[ dateStr] = netValue, dailyChangePCT, buyCnt, sellCnt, prevHoldCnt, currHoldCnt, cannotSellCnt, cannotBuyCnt, usableCach, currMV lastMV = currMV #debug 行情数据 debugDailyQuote(groupedSignalData) # 信号数据 #groupedSignalData.to_csv(StockConst.root + '\export\groupedSignalData.csv') # 每日交易 # print('每日交易:') stockTradeDailyDf = pd.DataFrame(stockTradeList) stockTradeDailyDf.sort_values(by=['tradingDate'], ascending=True) stockTradeDailyDf.to_csv(StockConst.ROOT + '\export\stockTradeDaily.csv') # 每日持仓 #print('每日持仓:') stockHoldDailyDf = pd.DataFrame(stockHoldDailyList) stockHoldDailyDf.sort_values(by=['tradingDate'], ascending=True) stockHoldDailyDf.to_csv(StockConst.ROOT + '\export\stockHoldDaily.csv') #每日统计(收益,净值,买入数,卖出数,持有数) stockStatDailyDf = stockStatDailyDf.dropna(how='all') print('每日统计:') print(stockStatDailyDf) stockStatDailyDf.to_csv(StockConst.ROOT + '\export\stockStatDaily.csv') # 每年统计 yearDf = StockYearService.main(stockStatDailyDf) print('每年统计:') print(yearDf) # 最大回撤 maxdrop = StockMaxDropNewService.get_max_drop(stockStatDailyDf) print('最大回撤:') print(maxdrop) #每年的最大回撤 maxdropDf = StockMaxDropNewService.get_max_drop_for_each_year( stockStatDailyDf) maxdropDf.sort_values(by=["year"]) print('每年的最大回撤:') print(maxdropDf) #夏普比率 sharpRatio = SharpRatioNewService.get_sharp_ratio(stockStatDailyDf) print('夏普比率:') print(sharpRatio) #每年的夏普比率 sharpRatioDf = SharpRatioNewService.get_sharp_ratio_for_each_year( stockStatDailyDf) sharpRatioDf.sort_values(by=["year"]) print('每年的夏普比率:') print(sharpRatioDf)
def main(): signalData = SourceDataDao.getSignalData() dailyQuote = SourceDataDao.getDailyQuote() #columns filter #df3 = sourceData.loc[(df['Mom'] <= 4) & (df['Mom'] <= 4), ['Mom']] #index filter #startDate=DateUtils.str2Datetime('20010105'); #endDate=DateUtils.str2Datetime('20010111'); #df4 = df3.ix[startDate:endDate] #select top 5 group by TradingDay order by Mom desc groupedSignalData = signalData.groupby(level='TradingDay').apply(SelectUtil.top,5,'Mom',False) #param period = 5 startDate = '1/8/2001' #endDate = '1/1/2017' endDate = '1/18/2001' #time series dateList = DateUtil.get_date_list2(startDate, endDate) currHoldSet = {} stockHoldDF = {} stockTradeDF = {} #lastSignalData = pd.DataFrame() usableVol = 100; netValue = 1; stockStatDaily = pd.DataFrame(index=pd.date_range(startDate, endDate), columns=['netValue','changePCT','buyCnt','sellCnt','holdCnt']) for date in dateList.index: #print(date) dateStr = DateUtil.datetime2_str(date) #print(dateStr) #isinstance(date, datetime) #DateUtil.str2Datetime('20010108') # select by single date try: #print(1) currSignalData = groupedSignalData.ix[date] print("currSignalData:"+str(len(currSignalData))) #print(2) buyList = getBuyList(currSignalData,lastSignalData) #print(3) sellList = getSellList(currSignalData,lastSignalData) #print(4) holdList = getHoldList(currSignalData,lastSignalData) #print(currSignalData) #print("len:"+str(len(currSignalData))) except: #假期,双休日,原数据问题 print(DateUtil.datetime2_str(date) + ': no data') continue buyCnt = len(buyList) sellCnt = len(sellList) holdCnt = len(holdList) dailyChangePCT = 0 changePCTBuy = handleBuyList(date,buyList,dailyQuote,usableVol,stockHoldDF,stockTradeDF) releasedVol = getReleasedVol(sellList, stockHoldDF) usableVol = usableVol + releasedVol changePCTSell = handleSellList(date,sellList,dailyQuote,usableVol,stockHoldDF,stockTradeDF) #changePCTHold = handleHoldList(date, sellList, dailyQuote, usableVol, stockHoldDF, stockTradeDF) changePCTHold = 0 print("dateStr:" + dateStr + " changePCTBuy:" + str(changePCTBuy) + " changePCTSell:" + str(changePCTSell) + " changePCTHold:" + str(changePCTHold)) dailyChangePCT = changePCTBuy+changePCTSell+changePCTHold #print("dailyChangePCT:"+str(dailyChangePCT)) netValue = netValue * (1 + dailyChangePCT / 100) #print("netValue:" + str(netValue)) stockStatDaily.ix[dateStr] = netValue,dailyChangePCT,buyCnt,sellCnt,holdCnt #innerCodeList = currSignalData["InnerCode"] #print(innerCodeList) #lastSignalData = currSignalData print(stockStatDaily)
def main(select_stock, numOfDailySignal): signalData = SourceDataDao.getSignalData() #dailyQuote = SourceDataDao.getDailyQuote() dailyQuote = SourceDataDao.getNewDailyQuote() #参数 #每天信号数 #numOfDailySignal = 5 #初始资金 initialMV = 1000000 #1000000 #开始结束日期 #startDate = '1/8/2001' startDate = '1/1/2004' #endDate = '1/1/2017' #eendDate = '5/18/2001' endDate = '12/31/2005' #endDate = '12/31/2002' #endDate = '1/9/2001' #select top 5 group by TradingDay order by Mom desc groupedSignalData = select_stock(signalData, numOfDailySignal) #groupedSignalData = signalData.groupby(level=StockConst.TradingDay).apply(SelectUtil.top,numOfDailySignal,StockConst.Mom,False) #False True #当前持仓 key:innerCode value: currHoldSet = {} stockTradeList = [] # capitalEntity = CapitalEntity.CapitalEntity(initialMV) lastMV = initialMV #netValue = 1; #初始化每日统计表 stockStatDailyDf = pd.DataFrame(index=pd.date_range(startDate, endDate), columns=[ 'netValue', 'changePCT', 'buyCnt', 'sellCnt', 'prevHoldCnt', 'currHoldCnt', 'cannotSellCnt', 'cannotBuyCnt', 'usableCach', 'mv' ]) #初始化每日持仓表 stockHoldDailyList = [] #从信号表中取得唯一性日期 dateList = SourceDataDao.select_date_from_signal(signalData, startDate, endDate) for index, item in enumerate(dateList): #for date in dateList: #print('index:'+str(index)+' len:'+str(len(dateList))) #信号日 signalDay = dateList[index] # 非最后一天 if index < (len(dateList) - 1): #交易日 tradingDay = dateList[index + 1] tradingDayStr = DateUtil.datetime2_str(tradingDay) #print('dateStr:'+dateStr) # 最后一天 else: break #print(dateStr) currSignalData = groupedSignalData.ix[signalDay] if StockConst.IS_DEBUG: print("currSignalData:" + str(len(currSignalData))) #print(currSignalData) # 计划买入列表 planBuyList = getPlanBuyList(currSignalData, currHoldSet) # 计划卖出列表 planSellList = getPlanSellList(currSignalData, currHoldSet) # 昨日持仓部分(在今日持仓中) prevHoldList = getPrevHoldList(currSignalData, currHoldSet) """ if (dateStr >= '2015-01-05') & (dateStr <= '2015-01-05'): print('-----'+dateStr+'-----') #print(currSignalData) #print(buyList) print(list(currHoldSet.keys())) for key in currHoldSet.keys(): print(str(key) + ':' + currHoldSet.get(key).openDate) print('----------------') """ #dailyChangePCT = 0 cannotSellList = [] cannotBuyList = [] # 实际买入列表 actualBuyList = getActualBuyList(planBuyList, dailyQuote, tradingDay, cannotBuyList) # "卖出列表"中要保留的股票数 numOfToKeepInSellList = numOfDailySignal - len(actualBuyList) - len( prevHoldList) # 实际卖出列表 actualSellList = getActualSellList(planSellList, tradingDay, signalData, dailyQuote, numOfToKeepInSellList, cannotSellList) #1.处理实际卖出 handleSellList(tradingDay, dailyQuote, stockTradeList, currHoldSet, capitalEntity, actualSellList) #2.处理实际买入 handleBuyList(tradingDay, dailyQuote, stockTradeList, currHoldSet, capitalEntity, actualBuyList) #3.计算当日市值 currMV = calculateDailyMV(currHoldSet, capitalEntity, dailyQuote, tradingDay, stockHoldDailyList, initialMV) #4.个数 buyCnt = len(actualBuyList) sellCnt = len(actualSellList) prevHoldCnt = len(prevHoldList) currHoldCnt = len(currHoldSet) cannotSellCnt = len(cannotSellList) cannotBuyCnt = len(cannotBuyList) #if StockConst.isDebug: #print("dateStr:" + dateStr + " changePCTBuy:" + str(changePCTBuy) + " changePCTSell:" + str(changePCTSell) + #" changePCTHold:" + str(changePCTHold)) #当日净值 netValue = currMV / initialMV #print("netValue:" + str(netValue)) #当日收益 dailyChangePCT = NumUtil.get_change_pct(lastMV, currMV, 2) #当日可用现金 usableCach = capitalEntity.get_usable_cash() # stockStatDailyDf.ix[ tradingDayStr] = netValue, dailyChangePCT, buyCnt, sellCnt, prevHoldCnt, currHoldCnt, cannotSellCnt, cannotBuyCnt, usableCach, currMV # lastMV = currMV # 信号数据 #groupedSignalData.to_csv(StockConst.root + '\export\groupedSignalData.csv') # 每日交易 # print('每日交易:') stockTradeDailyDf = pd.DataFrame(stockTradeList) stockTradeDailyDf.sort_values(by=['tradingDate'], ascending=True) stockTradeDailyDf.to_csv(StockConst.ROOT + '\export\stockTradeDaily.csv') # 每日持仓 #print('每日持仓:') stockHoldDailyDf = pd.DataFrame(stockHoldDailyList) stockHoldDailyDf.sort_values(by=['tradingDate'], ascending=True) stockHoldDailyDf.to_csv(StockConst.ROOT + '\export\stockHoldDaily.csv') #每日统计(收益,净值,买入数,卖出数,持有数) stockStatDailyDf = stockStatDailyDf.dropna(how='all') print('每日统计:') print(stockStatDailyDf) stockStatDailyDf.to_csv(StockConst.ROOT + '\export\stockStatDaily.csv') # 每年统计 yearDf = StockYearService.main(stockStatDailyDf) print('每年统计:') print(yearDf) # 最大回撤 maxdrop = StockMaxDropNewService.get_max_drop(stockStatDailyDf) print('最大回撤:') print(maxdrop) #每年的最大回撤 maxdropDf = StockMaxDropNewService.get_max_drop_for_each_year( stockStatDailyDf) maxdropDf.sort_values(by=["year"]) print('每年的最大回撤:') print(maxdropDf) #夏普比率 sharpRatio = SharpRatioNewService.get_sharp_ratio(stockStatDailyDf) print('夏普比率:') print(sharpRatio) #每年的夏普比率 sharpRatioDf = SharpRatioNewService.get_sharp_ratio_for_each_year( stockStatDailyDf) sharpRatioDf.sort_values(by=["year"]) print('每年的夏普比率:') print(sharpRatioDf)
def main(): signalData = SourceDataDao.getSignalData() #dailyQuote = SourceDataDao.getDailyQuote() dailyQuote = SourceDataDao.getNewDailyQuote()
def main(): signalData = SourceDataDao.getSignalData() dailyQuote = SourceDataDao.getDailyQuote() #columns filter #df3 = sourceData.loc[(df['Mom'] <= 4) & (df['Mom'] <= 4), ['Mom']] #index filter #startDate=DateUtils.str2Datetime('20010105'); #endDate=DateUtils.str2Datetime('20010111'); #df4 = df3.ix[startDate:endDate] #select top 5 group by TradingDay order by Mom desc groupedSignalData = signalData.groupby(level='TradingDay').apply( SelectUtil.top, 5, 'Mom', False) #param #period = 5 #startDate = '1/8/2001' startDate = '1/8/2001' #endDate = '1/1/2017' endDate = '2/18/2002' #endDate = '12/31/2016' #endDate = '12/31/2002' #time series #dateList = DateUtil.getDateList2(startDate,endDate) currHoldSet = {} stockHoldDF = {} stockTradeDF = {} #lastSignalData = pd.DataFrame() usableVol = 100 netValue = 1 #初始化每日统计表 stockStatDaily = pd.DataFrame(index=pd.date_range(startDate, endDate), columns=[ 'netValue', 'changePCT', 'buyCnt', 'sellCnt', 'prevHoldCnt', 'currHoldCnt', 'cannotSellCnt', 'cannotBuyCnt' ]) #从信号表中取得唯一性日期 dateList = SourceDataDao.select_date_from_signal(signalData, startDate, endDate) for date in dateList: #for date in dateList.index: #print(date) dateStr = DateUtil.datetime2_str(date) #print(dateStr) #isinstance(date, datetime) # select by single date #try: #print(1) currSignalData = groupedSignalData.ix[date] if StockConst.IS_DEBUG: print("currSignalData:" + str(len(currSignalData))) #print(2) buyList = getBuyList(currSignalData, currHoldSet) #print(3) sellList = getSellList(currSignalData, currHoldSet) #print(4) prevHoldList = getPrevHoldList(currSignalData, currHoldSet) #print(currSignalData) #except: #假期,双休日,原数据问题 #if StockConst.isDebug: #print(DateUtil.datetime2Str(date) + ': no data') #continue dailyChangePCT = 0 actualSellList = [] actualBuyList = [] cannotSellList = [] cannotBuyList = [] #1.sell changePCTSell = handleSellList(date, sellList, dailyQuote, usableVol, stockHoldDF, stockTradeDF, currHoldSet, actualSellList, cannotSellList) usableVol = calculateUsableVol(currHoldSet) #2.buy changePCTBuy = handleBuyList(date, buyList, dailyQuote, usableVol, stockHoldDF, stockTradeDF, currHoldSet, actualBuyList, cannotBuyList) #3.hold changePCTHold = handleHoldList(date, prevHoldList, dailyQuote, usableVol, stockHoldDF, stockTradeDF, currHoldSet) #changePCTHold = 0 buyCnt = len(actualBuyList) sellCnt = len(actualSellList) prevHoldCnt = len(prevHoldList) currHoldCnt = len(currHoldSet) cannotSellCnt = len(cannotSellList) cannotBuyCnt = len(cannotBuyList) if StockConst.IS_DEBUG: print("dateStr:" + dateStr + " changePCTBuy:" + str(changePCTBuy) + " changePCTSell:" + str(changePCTSell) + " changePCTHold:" + str(changePCTHold)) dailyChangePCT = changePCTBuy + changePCTSell + changePCTHold #print("dailyChangePCT:"+str(dailyChangePCT)) netValue = netValue * (1 + dailyChangePCT / 100) #print("netValue:" + str(netValue)) stockStatDaily.ix[ dateStr] = netValue, dailyChangePCT, buyCnt, sellCnt, prevHoldCnt, currHoldCnt, cannotSellCnt, cannotBuyCnt #innerCodeList = currSignalData["InnerCode"] #print(innerCodeList) #lastSignalData = currSignalData #每日统计(收益,净值,买入数,卖出数,持有数) stockStatDaily = stockStatDaily.dropna(how='all') print('每日统计:') print(stockStatDaily) stockStatDaily.to_csv('F:\export\stockStatDaily.csv') # 每年统计 yearList = StockYearService.main(stockStatDaily) print('每年统计:') print(yearList) # 最大回撤 maxdrop = StockMaxDropNewService.get_max_drop(stockStatDaily) print('最大回撤:') print(maxdrop) #每年的最大回撤 maxdropList = StockMaxDropNewService.get_max_drop_for_each_year( stockStatDaily) maxdropList.sort_values(by=["year"]) print('每年的最大回撤:') print(maxdropList) #夏普比率 sharpRatio = SharpRatioNewService.get_sharp_ratio(stockStatDaily) print('夏普比率:') print(sharpRatio) #每年的夏普比率 sharpRatioList = SharpRatioNewService.get_sharp_ratio_for_each_year( stockStatDaily) sharpRatioList.sort_values(by=["year"]) print('每年的夏普比率:') print(sharpRatioList)