def testSelectByInnerCodeAndDate(self): dailyQuote = SourceDataDao.load_new_daily_quote('') dailyQuote = SourceDataDao.select_by_inner_code_and_date(dailyQuote, '2004-09-14', '000049.SZ') # self.assertEqual(self.sum(1, 2), 3) print('dailyQuote:'+str(len(dailyQuote))) print(dailyQuote) self.assertNotEqual(len(dailyQuote), 0)
def get_plan_sell_list_mv(planSellList, tradingDay, signalData, dailyQuote, cannotSellList, dailyQuote1day, currHoldSet): planSellListMV = [] if len(planSellList) == 0: return planSellListMV # 排序 planSellList = sort_by_tech(planSellList, signalData, tradingDay, StockConst.MOM) #TODO 排序 for innerCode in planSellList: isCannotSell = SourceDataDao.check_if_cannot_sell_1day(dailyQuote1day, innerCode) #能卖 if not isCannotSell: dailyQuoteRow = SourceDataDao.select_by_inner_code_and_date(dailyQuote, tradingDay, innerCode) stockHoldEntity = currHoldSet.get(innerCode) # 当前vwap sellPrice = BackTestHelper.get_vwap(dailyQuoteRow) # 当前市值 sellMV = BackTestHelper.get_sell_mv(stockHoldEntity.buyPrice, stockHoldEntity.buyMV, sellPrice) # mVEntity = MVEntity.MVEntity(innerCode, currMV) planDict = {StockConst.INNERCODE: innerCode, planDictMV: sellMV, planDictPrice:sellPrice} planSellListMV.append(planDict) else: cannotSellList.append(innerCode) return planSellListMV
def handleBuyList(tradingDate, buyList, dailyQuote, usableVol, stockHoldDF, stockTradeDF): vol = usableVol / len(buyList) #print('vol:'+vol) partChangePCT = 0 for innerCode in buyList: dailyQuoteRow = SourceDataDao.select_by_inner_code_and_date( dailyQuote, tradingDate, innerCode) #print('innerCode:'+str(innerCode)) #print(dailyQuoteRow) turnoverValue = dailyQuoteRow["TurnoverValue"] turnoverVolume = dailyQuoteRow["TurnoverVolume"] closePrice = dailyQuoteRow["ClosePrice"] cost = turnoverValue / turnoverVolume changePCT = NumUtil.get_change_pct(cost, closePrice, 2) realChangePCT = changePCT * vol / 100.0 partChangePCT = partChangePCT + realChangePCT hold = StockHoldEntity.StockHoldEntity(tradingDate, innerCode, vol) trade = StockTradeEntityBak.StockTradeEntity(tradingDate, innerCode, 1, vol, cost, changePCT, realChangePCT) stockHoldDF.setdefault(innerCode, hold) #TODO trade return partChangePCT
def calculateDailyMV(currHoldSet, capitalEntity, dailyQuote, tradingDate, stockHoldDailyList, initialMV): usableCach = capitalEntity.get_usable_cash() dailyMV = 0 for innerCode in currHoldSet: dailyQuoteRow = SourceDataDao.select_by_inner_code_and_date( dailyQuote, tradingDate, innerCode) stockHoldEntity = currHoldSet[innerCode] vol = stockHoldEntity.vol # print('innerCode:' + str(innerCode)) closePrice = dailyQuoteRow["ClosePrice"] mv = vol * closePrice dailyMV += mv #stockHoldEntity.setClosePrice(closePrice) #stockHoldEntity.setMV(mv) cost = stockHoldEntity.cost profit = (closePrice - cost) * vol holdDict = { 'tradingDate': tradingDate, 'innerCode': innerCode, 'vol': vol, 'buyPrice': stockHoldEntity.buyPrice, 'cost': cost, 'closePrice': closePrice, 'mv': mv, 'profit': profit, 'profitPCT': NumUtil.get_round(profit / initialMV * 100, 5) } stockHoldDailyList.append(holdDict) #print('dailyMV:' + str(dailyMV)) #print('usableCach:' + str(usableCach)) dailyMV = dailyMV + usableCach return dailyMV
def get_plan_buy_list_mv(planBuyList, dailyQuote, tradingDay, cannotBuyList, dailyQuotePn, dailyQuote1day, lastCapitalEntity, currSelectStock, preHoldList, currHoldSet): planBuyListMV = [] if len(planBuyList) == 0: return planBuyListMV #无需排序,因为planBuyList已经排过序 # 昨日总现金 lastTotalCash = get_last_total_cash(lastCapitalEntity) for innerCode in planBuyList: # print('dailyQuote:' + str(len(dailyQuote))) # print('dailyQuote1day:'+str(len(dailyQuote1day))) # isCannotBuy = SourceDataDao.checkIfCannotBuy(dailyQuote,tradingDay,innerCode) isCannotBuy = SourceDataDao.check_if_cannot_buy_1day(dailyQuote1day, innerCode) # isCannotBuy = SourceDataDao.checkIfCannotBuyPn(dailyQuotePn, tradingDay, innerCode) # isCannotBuy = False #buyFlg = dailyQuoteRow[StockConst.buyFlg] #可买 if not isCannotBuy: dailyQuoteRow = SourceDataDao.select_by_inner_code_and_date(dailyQuote, tradingDay, innerCode) # 当前vwap buyPrice = BackTestHelper.get_vwap(dailyQuoteRow) volWeight = currSelectStock.ix[innerCode][StockConst.VOL_WEIGHT] # 计划买入现金=昨日总现金*仓位权重 buyCash = lastTotalCash * volWeight # [处理调整仓位2017-03-28]###################################### # 在昨日持有列表中:调整仓位 isAdjust = None partialFlg = None if innerCode in preHoldList: stockHoldEntity = currHoldSet.get(innerCode) # 当前vwap sellPrice = BackTestHelper.get_vwap(dailyQuoteRow) # 当前市值 sellMV = BackTestHelper.get_sell_mv(stockHoldEntity.closePrice, stockHoldEntity.closeMV, sellPrice) # 不买的情况 if buyCash <= sellMV: buyCash = 0 # 调整仓位 else: buyCash = buyCash - sellMV isAdjust = 1 partialFlg = 1 ################################################# # 有买入 if buyCash > 0: planDict = {StockConst.TRADINGDAY: tradingDay, StockConst.INNERCODE: innerCode, planDictMV:buyCash, planDictPrice:buyPrice, planDictIsAdjust:isAdjust, 'partialFlg': partialFlg} planBuyListMV.append(planDict) else: cannotBuyList.append(innerCode) return planBuyListMV
def getToSellInSellList(sellList, tradingDate, signalData, dailyQuote, numOfToKeepInSellList, cannotSellList, currHoldSet): if len(sellList) == 0: return sellList #创建2列DF,innerCode,Mom(空) dic = {StockConst.INNERCODE: sellList} sortedSellDf = pd.DataFrame(data=dic) sortedSellDf.insert(1, StockConst.MOM, Series()) #print(sortedSellList) #从信号数据中获取Mom for innerCode in sellList: entity = signalData.ix[(tradingDate, innerCode)] sortedSellDf.loc[(sortedSellDf[StockConst.INNERCODE] == innerCode), [StockConst.MOM]] = entity[StockConst.MOM] #按Mom倒序排序 sortedSellDf = sortedSellDf.sort_values(by=StockConst.MOM, ascending=False) #卖出列表中保留的股票 toKeepInSellList = [] #保留的股票: 停牌或一字跌停股 #if len(toKeepInSellList) < numOfToKeepInSellList: for index, row in sortedSellDf.iterrows(): innerCode = row[StockConst.INNERCODE] sellFlg = 0 try: dailyQuoteRow = SourceDataDao.select_by_inner_code_and_date( dailyQuote, tradingDate, innerCode) except: #print('tradingDate:'+DateUtil.datetime2Str(tradingDate)) #print(sortedSellDf) #print(sellList) #print(currHoldSet) #print(dailyQuoteRow) sellFlg = 0 sellFlg = dailyQuoteRow[StockConst.SELL_FLG] #不能卖 if sellFlg == -1: toKeepInSellList.append(innerCode) cannotSellList.append(innerCode) # 保留的股票:按sortedSellDf的顺序加入 for index, row in sortedSellDf.iterrows(): if len(toKeepInSellList) < numOfToKeepInSellList: innerCode = row[StockConst.INNERCODE] if (innerCode not in toKeepInSellList): toKeepInSellList.append(innerCode) #去掉需要保留的股票 toSellInSellList = sortedSellDf[~( sortedSellDf[StockConst.INNERCODE].isin(toKeepInSellList))][ StockConst.INNERCODE].values #print('toSellInSellList') #print(toSellInSellList) return toSellInSellList
def handleSellList(tradingDate,dailyQuote,stockTradeList,currHoldSet,capitalEntity,actualSellList): #partChangePCT = 0 dateStr = DateUtil.datetime2_str(tradingDate) if len(actualSellList) == 0: return #sellList全部可以卖出 for innerCode in actualSellList: #print('innerCode:' + str(innerCode)) #try: dailyQuoteRow = SourceDataDao.select_by_inner_code_and_date(dailyQuote, tradingDate, innerCode) #except: #退市的股票: 查询不到行情,取最后一个交易日 #dailyQuoteRow = SourceDataDao.selectByLastDay(dailyQuote,innerCode) # print(dailyQuoteSingle) #可卖 #if sellFlg != -1: #actualSellList.append(innerCode) stockHoldEntity = currHoldSet.pop(innerCode) #vol = stockHoldEntity.vol sellPrice = BackTestHelper.get_vwap(dailyQuoteRow) #卖出市值(没有扣佣金) #turnover = sellPrice * vol sellMV = BackTestHelper.get_sell_mv(stockHoldEntity.buyPrice, stockHoldEntity.buyMV, sellPrice) #佣金(元) commission = BackTestHelper.get_sell_commission(sellMV, StockConst.SELL_COMMISSION / 100) #卖出后所得资金(扣掉佣金后所得金额) sellCach = sellMV - commission # 更新可用金额(增加) capitalEntity.increase_usable_cash(sellCach) # 开仓日 openDate = stockHoldEntity.openDate # 加入交易表 stockTradeDict = {'tradingDate': dateStr, 'innerCode': innerCode, 'type': -1, 'price': sellPrice, 'buyMV': '', 'sellMV': sellMV, 'commission': commission, 'sellCach': sellCach,'buyCach':'','openDate':openDate} stockTradeList.append(stockTradeDict) """ if DateUtil.datetime2Str(tradingDate) == '2014-12-09': print('-----handleSellList-----') print(DateUtil.datetime2Str(tradingDate)) print(innerCode) print(vol) print(sellPrice) print(turnoverValue) print(dailyQuoteRow) print('----------') """ #changePCT = NumUtil.getChangePCT(prevClosePrice, sellPrice, 2) #realChangePCT = (changePCT - StockConst.sellCommission) * vol / 100.0 #partChangePCT = partChangePCT + realChangePCT """
def getToBuyInBuyList(buyList, dailyQuote, tradingDate, cannotBuyList): toBuyInBuyList = [] for innerCode in buyList: dailyQuoteRow = SourceDataDao.select_by_inner_code_and_date( dailyQuote, tradingDate, innerCode) buyFlg = dailyQuoteRow[StockConst.BUY_FLG] #可买 if buyFlg != -1: toBuyInBuyList.append(innerCode) else: cannotBuyList.append(innerCode) return toBuyInBuyList
def debugDailyQuote(groupedSignalData,date,dailyQuote): """ """ # 行情数据Debug #dailyQuoteToDebug = pd.DataFrame() signalDataToDebug = groupedSignalData.ix[date] print('行情数据:') #print('signalDataToDebug:'+str(len(signalDataToDebug))) for index, row in signalDataToDebug.iterrows(): tradingDate = index[0] innerCode = index[1] dailyQuoteRow = SourceDataDao.select_by_inner_code_and_date(dailyQuote, tradingDate, innerCode) #dailyQuoteToDebug.append(dailyQuoteRow) print(dailyQuoteRow)
def get_plan_sell_list_mv(planSellList, tradingDay, signalData, dailyQuote, cannotSellList, dailyQuote1day, currHoldSet, preHoldList, currSelectStock, lastCapitalEntity): planSellListMV = [] if len(planSellList) == 0: return planSellListMV # 昨日总现金 lastTotalCash = get_last_total_cash(lastCapitalEntity) # 排序 planSellList = sort_by_tech(planSellList, signalData, tradingDay, StockConst.MOM) #TODO 排序 for innerCode in planSellList: isCannotSell = SourceDataDao.check_if_cannot_sell_1day(dailyQuote1day, innerCode) #能卖 if not isCannotSell: dailyQuoteRow = SourceDataDao.select_by_inner_code_and_date(dailyQuote, tradingDay, innerCode) stockHoldEntity = currHoldSet.get(innerCode) # 当前vwap sellPrice = BackTestHelper.get_vwap(dailyQuoteRow) # 当前市值 sellMV = BackTestHelper.get_sell_mv(stockHoldEntity.closePrice, stockHoldEntity.closeMV, sellPrice) #stockHoldEntity.buyPrice, stockHoldEntity.buyMV # [处理调整仓位2017-03-28]###################################### # 在昨日持有列表中:调整仓位 isAdjust = None partialFlg = None if innerCode in preHoldList: volWeight = currSelectStock.ix[innerCode][StockConst.VOL_WEIGHT] # 计划买入现金=昨日总现金*仓位权重 buyCash = lastTotalCash * volWeight # 不卖的情况 if sellMV <= buyCash: sellMV = 0 # 调整仓位 else: sellMV = sellMV - buyCash isAdjust = 1 partialFlg = 1 ################################################# # 有卖出 if sellMV > 0: planDict = {StockConst.TRADINGDAY: tradingDay, StockConst.INNERCODE: innerCode, planDictMV: sellMV, planDictPrice: sellPrice, planDictIsAdjust:isAdjust, 'partialFlg': partialFlg} planSellListMV.append(planDict) else: cannotSellList.append(innerCode) return planSellListMV
def getNumOfActualBuy(tradingDate, buyList, dailyQuote): if len(buyList) == 0: return 0 numOfActualBuy = 0 for innerCode in buyList: dailyQuoteRow = SourceDataDao.select_by_inner_code_and_date( dailyQuote, tradingDate, innerCode) buyFlg = dailyQuoteRow[StockConst.BUY_FLG] # 可买 if buyFlg != -1: numOfActualBuy += 1 return numOfActualBuy
def calculate_daily_mv(currHoldSet, capitalEntity, dailyQuote, tradingDay, stockHoldDailyList, initialCash): usableCash = capitalEntity.get_usable_cash() stockMV = 0 for innerCode in currHoldSet: #try: dailyQuoteRow = SourceDataDao.select_by_inner_code_and_date(dailyQuote, tradingDay, innerCode) #except: #缺少数据,取最近一个交易日的数据 #dailyQuoteRow = SourceDataDao.selectByPrevTradingDay(dailyQuote, tradingDay, innerCode) #print('calculateDailyMV '+DateUtil.datetime2Str(tradingDay)+' '+str(innerCode)) stockHoldEntity = currHoldSet[innerCode] #vol = stockHoldEntity.vol # print('innerCode:' + str(innerCode)) closePrice = BackTestHelper.get_close_price(dailyQuoteRow) #mv = vol * closePrice #收盘市值 closeMV = BackTestHelper.get_sell_mv(stockHoldEntity.lastTradePrice, stockHoldEntity.lastTradeMV, closePrice) #stockHoldEntity.buyPrice, stockHoldEntity.buyMV # stockHoldEntity.set_close_price(closePrice) stockHoldEntity.set_close_mv(closeMV) #每日市值 stockMV += closeMV #该股收益(已扣佣金)(仅用于查看) TODO(部分卖出时的算法) profit = (closeMV - stockHoldEntity.lastTradeMV) * (1 - StockConst.BUY_COMMISSION / 100) holdDict = {'tradingDay': tradingDay, 'innerCode': innerCode, 'lastTradePrice': stockHoldEntity.lastTradePrice, 'lastTradeMV': stockHoldEntity.lastTradeMV, 'cost': stockHoldEntity.cost, 'closePrice': closePrice, 'closeMV': closeMV, 'profit': profit, 'openDate': stockHoldEntity.openDate, 'profitPCT': NumUtil.get_round(profit / initialCash * 100, 5), 'changeDate': stockHoldEntity.changeDate } stockHoldDailyList.append(holdDict) #print('dailyMV:' + str(dailyMV)) #print('usableCash:' + str(usableCash)) #总资产=股票市值+现金 totalAsset = stockMV + usableCash capitalEntity.set_total_asset(totalAsset) #股票市值 capitalEntity.set_stock_mv(stockMV)
def handleBuyList(tradingDate,dailyQuote,stockTradeList,currHoldSet,capitalEntity,actualBuyList): usableCach = capitalEntity.get_usable_cash() if usableCach == 0: return if len(actualBuyList) == 0: return buyCach = usableCach/len(actualBuyList) #print('vol:'+vol) #partChangePCT = 0 # buyList全部可以买入 for innerCode in actualBuyList: dailyQuoteRow = SourceDataDao.select_by_inner_code_and_date(dailyQuote, tradingDate, innerCode) #print('innerCode:'+str(innerCode)) #print(dailyQuoteRow) #可买 #if buyFlg != -1: #平均买入价(没有算佣金) buyPrice = BackTestHelper.get_vwap(dailyQuoteRow) #成本价(算佣金) cost = buyPrice*(1 + StockConst.BUY_COMMISSION / 100) #仓位(股) #vol = NumUtil.getRound(turnover/cost,StockConst.volScale) #佣金(元) commission = BackTestHelper.get_buy_commission(buyCach, StockConst.BUY_COMMISSION / 100) #买入市值 buyMV = buyCach - commission #更新可用金额(减少) capitalEntity.reduce_usable_cash(buyCach) #changePCT = NumUtil.getChangePCT(cost, closePrice, 2) #realChangePCT = (changePCT - StockConst.buyCommission) * vol / 100.0 #partChangePCT = partChangePCT + realChangePCT # #stockHold = StockHoldEntity.StockHoldEntity(tradingDate,innerCode,vol) #stockTrade = StockTradeEntity.StockTradeEntity(tradingDate, innerCode, 1, vol, cost, '', '') dateStr = DateUtil.datetime2_str(tradingDate) stockTradeDict = {'tradingDate':dateStr,'innerCode':innerCode,'type':1, 'price':buyPrice,'buyMV':buyMV,'sellMV':'','commission': commission, 'sellCach':'','buyCach':buyCach,'openDate':''} stockTradeList.append(stockTradeDict) #插入表 #stockHoldDF.setdefault(innerCode,stockHold) #当前持仓 stockHoldEntity = StockHoldEntity.StockHoldEntity(innerCode, buyPrice, cost, DateUtil.datetime2_str(tradingDate), buyMV) currHoldSet.setdefault(innerCode,stockHoldEntity) """
def handleBuyList(tradingDate, buyList, dailyQuote, usableVol, stockHoldDF, stockTradeDF, currHoldSet, actualBuyList, cannotBuyList): if usableVol == 0: return 0 if len(buyList) == 0: return 0 vol = usableVol / len(buyList) #print('vol:'+vol) partChangePCT = 0 for innerCode in buyList: dailyQuoteRow = SourceDataDao.select_by_inner_code_and_date( dailyQuote, tradingDate, innerCode) #print('innerCode:'+str(innerCode)) #print(dailyQuoteRow) turnoverValue = dailyQuoteRow["TurnoverValue"] turnoverVolume = dailyQuoteRow["TurnoverVolume"] prevClosePrice = dailyQuoteRow["PrevClosePrice"] closePrice = dailyQuoteRow["ClosePrice"] highPrice = dailyQuoteRow["HighPrice"] lowPrice = dailyQuoteRow["LowPrice"] # 非停牌 且 非一字涨停: 可以卖出 check = not BackTestHelper.isYiZiZhangTing(highPrice, lowPrice, prevClosePrice) if (closePrice != 0) & (turnoverValue != float(0)) & check: actualBuyList.append(innerCode) cost = turnoverValue / turnoverVolume changePCT = NumUtil.get_change_pct(cost, closePrice, 2) realChangePCT = (changePCT - StockConst.BUY_COMMISSION) * vol / 100.0 partChangePCT = partChangePCT + realChangePCT # stockHold = StockHoldEntity.StockHoldEntity( tradingDate, innerCode, vol) stockTrade = StockTradeEntityBak.StockTradeEntity( tradingDate, innerCode, 1, vol, cost, changePCT, realChangePCT) #插入表 #stockHoldDF.setdefault(innerCode,stockHold) #当前持仓 currHoldSet.setdefault(innerCode, vol) #TODO trade else: cannotBuyList.append(innerCode) return partChangePCT
def calculateDailyMV(currHoldSet, capitalEntity, dailyQuote, tradingDate, stockHoldDailyList, initialMV): usableCach = capitalEntity.get_usable_cash() dailyMV = 0 for innerCode in currHoldSet: #try: dailyQuoteRow = SourceDataDao.select_by_inner_code_and_date( dailyQuote, tradingDate, innerCode) #except: #缺少数据,取最近一个交易日的数据 #dailyQuoteRow = SourceDataDao.selectByPrevTradingDay(dailyQuote, tradingDate, innerCode) #print('calculateDailyMV '+DateUtil.datetime2Str(tradingDate)+' '+str(innerCode)) stockHoldEntity = currHoldSet[innerCode] #vol = stockHoldEntity.vol # print('innerCode:' + str(innerCode)) closePrice = BackTestHelper.get_close_price(dailyQuoteRow) #mv = vol * closePrice #收盘市值 closeMV = BackTestHelper.get_sell_mv(stockHoldEntity.buyPrice, stockHoldEntity.buyMV, closePrice) #每日市值 dailyMV += closeMV #该股收益(已扣佣金)(仅用于查看) profit = (closeMV - stockHoldEntity.buyMV) * ( 1 - StockConst.BUY_COMMISSION / 100) holdDict = { 'tradingDate': tradingDate, 'innerCode': innerCode, 'buyPrice': stockHoldEntity.buyPrice, 'buyMV': stockHoldEntity.buyMV, 'cost': stockHoldEntity.cost, 'closePrice': closePrice, 'closeMV': closeMV, 'profit': profit, 'openDate': stockHoldEntity.openDate, 'profitPCT': NumUtil.get_round(profit / initialMV * 100, 5) } stockHoldDailyList.append(holdDict) #print('dailyMV:' + str(dailyMV)) #print('usableCach:' + str(usableCach)) dailyMV = dailyMV + usableCach return dailyMV
def handleSellList(tradingDate, sellList, dailyQuote, usableVol, stockHoldDF, stockTradeDF): partChangePCT = 0 for innerCode in sellList: dailyQuoteSingle = SourceDataDao.select_by_inner_code_and_date( dailyQuote, tradingDate, innerCode) #print('innerCode:' + str(innerCode)) hold = stockHoldDF.pop(innerCode) vol = hold.vol # print(dailyQuoteSingle) turnoverValue = dailyQuoteSingle["TurnoverValue"] turnoverVolume = dailyQuoteSingle["TurnoverVolume"] prevClosePrice = dailyQuoteSingle["PrevClosePrice"] sellPrice = turnoverValue / turnoverVolume changePCT = NumUtil.get_change_pct(prevClosePrice, sellPrice, 2) realChangePCT = changePCT * vol / 100.0 partChangePCT = partChangePCT + realChangePCT return partChangePCT
def get_plan_buy_list_mv(planBuyList, dailyQuote, tradingDay, cannotBuyList, dailyQuotePn, dailyQuote1day, lastCapitalEntity, currSelectStock): planBuyListMV = [] if len(planBuyList) == 0: return planBuyListMV #无需排序,因为planBuyList已经排过序 lastStockMV = lastCapitalEntity.get_stock_mv() #昨日股票市值 lastUsableCash = lastCapitalEntity.get_usable_cash() #昨日现金 # 佣金(元) commission = BackTestHelper.get_sell_commission(lastStockMV, StockConst.SELL_COMMISSION / 100) # 昨日市值扣掉佣金后可得现金 lastMVToCash = lastStockMV - commission # 昨日总现金 lastTotalCash = lastUsableCash + lastMVToCash for innerCode in planBuyList: # print('dailyQuote:' + str(len(dailyQuote))) # print('dailyQuote1day:'+str(len(dailyQuote1day))) # isCannotBuy = SourceDataDao.checkIfCannotBuy(dailyQuote,tradingDay,innerCode) isCannotBuy = SourceDataDao.check_if_cannot_buy_1day(dailyQuote1day, innerCode) # isCannotBuy = SourceDataDao.checkIfCannotBuyPn(dailyQuotePn, tradingDay, innerCode) # isCannotBuy = False #buyFlg = dailyQuoteRow[StockConst.buyFlg] #可买 if not isCannotBuy: dailyQuoteRow = SourceDataDao.select_by_inner_code_and_date(dailyQuote, tradingDay, innerCode) # 当前vwap buyPrice = BackTestHelper.get_vwap(dailyQuoteRow) volWeight = currSelectStock.ix[innerCode][StockConst.VOL_WEIGHT] # 计划买入现金=昨日总现金*仓位权重 buyCash = lastTotalCash * volWeight # mVEntity = MVEntity.MVEntity(innerCode,buyMV) planDict = {StockConst.INNERCODE: innerCode, planDictMV:buyCash, planDictPrice:buyPrice} planBuyListMV.append(planDict) else: cannotBuyList.append(innerCode) return planBuyListMV
def handleSellList(tradingDate, sellList, dailyQuote, usableVol, stockHoldDF, stockTradeDF, currHoldSet, actualSellList, cannotSellList): partChangePCT = 0 dateStr = DateUtil.datetime2_str(tradingDate) if len(sellList) == 0: return 0 for innerCode in sellList: dailyQuoteRow = SourceDataDao.select_by_inner_code_and_date( dailyQuote, tradingDate, innerCode) #print('innerCode:' + str(innerCode)) # print(dailyQuoteSingle) turnoverValue = dailyQuoteRow["TurnoverValue"] turnoverVolume = dailyQuoteRow["TurnoverVolume"] prevClosePrice = dailyQuoteRow["PrevClosePrice"] closePrice = dailyQuoteRow["ClosePrice"] highPrice = dailyQuoteRow["HighPrice"] lowPrice = dailyQuoteRow["LowPrice"] #非停牌 且 非一字跌停: 可以卖出 check = not BackTestHelper.isYiZiDieTing(highPrice, lowPrice, prevClosePrice) if (closePrice != 0) & (turnoverValue != float(0)) & check: actualSellList.append(innerCode) vol = currHoldSet.pop(innerCode) sellPrice = turnoverValue / turnoverVolume changePCT = NumUtil.get_change_pct(prevClosePrice, sellPrice, 2) realChangePCT = (changePCT - StockConst.SELL_COMMISSION) * vol / 100.0 partChangePCT = partChangePCT + realChangePCT """ if dateStr == '2001-01-17': print("vol:"+str(vol)+" turnoverValue:"+str(turnoverValue)+" turnoverVolume:"+str(turnoverVolume)+ " prevClosePrice:"+str(prevClosePrice)+" closePrice:"+str(closePrice)+" sellPrice:"+str(sellPrice)+ " changePCT:"+str(changePCT)+ " realChangePCT:"+str(realChangePCT)+" partChangePCT:"+str(partChangePCT)) """ else: cannotSellList.append(innerCode) return partChangePCT
def get_force_sell_list_mv(prevHoldList, tradingDay, signalData, dailyQuote, dailyQuote1day, currHoldSet): forceSellListMV = [] if len(prevHoldList) == 0: return forceSellListMV for innerCode in prevHoldList: dailyQuoteRow = SourceDataDao.select_by_inner_code_and_date(dailyQuote, tradingDay, innerCode) delistFlg = dailyQuoteRow['delistFlg'] #退市 if delistFlg == '1': stockHoldEntity = currHoldSet.get(innerCode) # 当前vwap sellPrice = BackTestHelper.get_vwap(dailyQuoteRow) # 当前市值 sellMV = BackTestHelper.get_sell_mv(stockHoldEntity.buyPrice, stockHoldEntity.buyMV, sellPrice) # mVEntity = MVEntity.MVEntity(innerCode, currMV) planDict = {StockConst.INNERCODE: innerCode, planDictMV: sellMV, planDictPrice:sellPrice} forceSellListMV.append(planDict) return forceSellListMV
def handleSellList(tradingDate,sellList,dailyQuote,usableVol,stockHoldDF,stockTradeDF): partChangePCT = 0 dateStr = DateUtil.datetime2_str(tradingDate) for innerCode in sellList: dailyQuoteSingle = SourceDataDao.select_by_inner_code_and_date(dailyQuote, tradingDate, innerCode) #print('innerCode:' + str(innerCode)) hold = stockHoldDF.pop(innerCode) vol = hold.vol # print(dailyQuoteSingle) turnoverValue = dailyQuoteSingle["TurnoverValue"] turnoverVolume = dailyQuoteSingle["TurnoverVolume"] prevClosePrice = dailyQuoteSingle["PrevClosePrice"] sellPrice = turnoverValue / turnoverVolume changePCT = NumUtil.get_change_pct(prevClosePrice, sellPrice, 2) realChangePCT = changePCT * vol / 100.0 partChangePCT = partChangePCT + realChangePCT; if dateStr == '2001-01-17': print("vol:"+str(vol)+" turnoverValue:"+str(turnoverValue)+" turnoverVolume:"+str(turnoverVolume)+ " prevClosePrice:"+str(prevClosePrice)+" sellPrice:"+str(sellPrice)+" changePCT:"+str(changePCT)+ " realChangePCT:"+str(realChangePCT)+" partChangePCT:"+str(partChangePCT)) return partChangePCT
def handleHoldList(tradingDate, holdList, dailyQuote, usableVol, stockHoldDF, stockTradeDF, currHoldSet): partChangePCT = 0 #dateStr = DateUtil.datetime2Str(tradingDate) if len(holdList) == 0: return 0 for innerCode in holdList: dailyQuoteRow = SourceDataDao.select_by_inner_code_and_date( dailyQuote, tradingDate, innerCode) #print('innerCode:' + str(innerCode)) # print(dailyQuoteSingle) turnoverValue = dailyQuoteRow["TurnoverValue"] #turnoverVolume = dailyQuoteRow["TurnoverVolume"] prevClosePrice = dailyQuoteRow["PrevClosePrice"] closePrice = dailyQuoteRow["ClosePrice"] #非停牌 if (closePrice != 0) & (turnoverValue != float(0)): vol = currHoldSet.get(innerCode) changePCT = NumUtil.get_change_pct(prevClosePrice, closePrice, 2) realChangePCT = changePCT * vol / 100.0 partChangePCT = partChangePCT + realChangePCT return partChangePCT
def printByInnerCodeAndDate(addr): dailyQuote = SourceDataDao.load_daily_quote(addr) dailyQuote = SourceDataDao.select_by_inner_code_and_date(dailyQuote, '2004-01-17', '000049.SZ') print(dailyQuote)
def handleBuyList(tradingDate, buyList, dailyQuote, usableVol, stockHoldDF, stockTradeList, currHoldSet, actualBuyList, cannotBuyList, capitalEntity): usableCach = capitalEntity.get_usable_cash() if usableCach == 0: return #if usableVol == 0: #return 0 if len(buyList) == 0: return #可买列表 buyList = getToBuyInBuyList(buyList, dailyQuote, tradingDate, cannotBuyList) if len(buyList) == 0: return turnover = usableCach / len(buyList) #print('vol:'+vol) partChangePCT = 0 for innerCode in buyList: dailyQuoteRow = SourceDataDao.select_by_inner_code_and_date( dailyQuote, tradingDate, innerCode) #print('innerCode:'+str(innerCode)) #print(dailyQuoteRow) turnoverValue = dailyQuoteRow[StockConst.TURNOVER_VALUE] turnoverVolume = dailyQuoteRow[StockConst.TURNOVER_VOLUME] #buyFlg = dailyQuoteRow[StockConst.buyFlg] #可买 #if buyFlg != -1: #actualBuyList.append(innerCode) #平均买入价(没有算佣金) buyPrice = turnoverValue / turnoverVolume #成本价(算佣金) cost = buyPrice * (1 + StockConst.BUY_COMMISSION / 100) #仓位(股) #vol = NumUtil.getRound(turnover/cost,StockConst.volScale) #佣金(元) commission = BackTestHelper.get_buy_commission( turnover, StockConst.BUY_COMMISSION / 100) #买入市值 buyMV = turnover - commission #更新可用金额(减少) capitalEntity.reduce_usable_cash(turnover) #changePCT = NumUtil.getChangePCT(cost, closePrice, 2) #realChangePCT = (changePCT - StockConst.buyCommission) * vol / 100.0 #partChangePCT = partChangePCT + realChangePCT # #stockHold = StockHoldEntity.StockHoldEntity(tradingDate,innerCode,vol) #stockTrade = StockTradeEntity.StockTradeEntity(tradingDate, innerCode, 1, vol, cost, '', '') stockTradeDict = { 'tradingDate': tradingDate, 'innerCode': innerCode, 'type': 1, 'vol': 0, 'price': buyPrice, 'turnover': turnover, 'commission': commission, 'MV': buyMV } stockTradeList.append(stockTradeDict) #插入表 #stockHoldDF.setdefault(innerCode,stockHold) #当前持仓 stockHoldEntity = StockHoldEntity.StockHoldEntity( innerCode, vol, buyPrice, cost, DateUtil.datetime2_str(tradingDate)) #tradingDate, currHoldSet.setdefault(innerCode, stockHoldEntity) #vol """
def handleBuyList(tradingDate, buyList, dailyQuote, usableVol, stockHoldDF, stockTradeList, currHoldSet, actualBuyList, cannotBuyList, capitalEntity): usableCach = capitalEntity.get_usable_cash() if usableCach == 0: return 0 #if usableVol == 0: #return 0 if len(buyList) == 0: return 0 turnover = usableCach / len(buyList) #print('vol:'+vol) partChangePCT = 0 for innerCode in buyList: dailyQuoteRow = SourceDataDao.select_by_inner_code_and_date( dailyQuote, tradingDate, innerCode) #print('innerCode:'+str(innerCode)) #print(dailyQuoteRow) turnoverValue = dailyQuoteRow["TurnoverValue"] turnoverVolume = dailyQuoteRow["TurnoverVolume"] buyFlg = dailyQuoteRow[StockConst.BUY_FLG] #可买 if buyFlg != -1: actualBuyList.append(innerCode) #平均买入价(没有算佣金) buyPrice = turnoverValue / turnoverVolume #成本价(算佣金) cost = buyPrice * (1 + StockConst.BUY_COMMISSION / 100) #仓位(股) vol = NumUtil.get_round(turnover / cost, StockConst.VOL_SCALE) #佣金(元) commission = BackTestHelper.get_buy_commission( turnover, StockConst.BUY_COMMISSION / 100) #更新可用金额(减少) capitalEntity.reduce_usable_cash(turnover) #changePCT = NumUtil.getChangePCT(cost, closePrice, 2) #realChangePCT = (changePCT - StockConst.buyCommission) * vol / 100.0 #partChangePCT = partChangePCT + realChangePCT # #stockHold = StockHoldEntity.StockHoldEntity(tradingDate,innerCode,vol) #stockTrade = StockTradeEntity.StockTradeEntity(tradingDate, innerCode, 1, vol, cost, '', '') stockTradeDict = { 'tradingDate': tradingDate, 'innerCode': innerCode, 'type': 1, 'vol': vol, 'price': buyPrice, 'turnover': turnover, 'commission': commission } stockTradeList.append(stockTradeDict) #插入表 #stockHoldDF.setdefault(innerCode,stockHold) #当前持仓 stockHoldEntity = StockHoldEntity.StockHoldEntity( innerCode, vol, buyPrice, cost, DateUtil.datetime2_str(tradingDate)) #tradingDate, currHoldSet.setdefault(innerCode, stockHoldEntity) #vol """ if (DateUtil.datetime2Str(tradingDate) == '2014-12-08') | (DateUtil.datetime2Str(tradingDate) == '2014-12-09'): print('-----handleBuyList-----') print(DateUtil.datetime2Str(tradingDate)) print(innerCode) print(vol) print(buyPrice) print(cost) print(turnoverValue) print(dailyQuoteRow) print('----------') """ else: cannotBuyList.append(innerCode)
def handleSellList(tradingDate, sellList, dailyQuote, usableVol, stockHoldDF, stockTradeList, currHoldSet, actualSellList, cannotSellList, capitalEntity, numOfActualBuy, signalData, prevHoldList, numOfDailyHolding): partChangePCT = 0 dateStr = DateUtil.datetime2_str(tradingDate) if len(sellList) == 0: return #卖出列表中要保留的股票数 numOfToKeepInSellList = numOfDailyHolding - numOfActualBuy - len( prevHoldList) #卖出数 > 实际买入数:对卖出列表进行排序 #if len(sellList) > numOfActualBuy: sellList = getToSellInSellList(sellList, tradingDate, signalData, dailyQuote, numOfToKeepInSellList, cannotSellList, currHoldSet) if len(sellList) == 0: return #numOfActualSell = 0 for innerCode in sellList: #print('innerCode:' + str(innerCode)) try: dailyQuoteRow = SourceDataDao.select_by_inner_code_and_date( dailyQuote, tradingDate, innerCode) except: #退市的股票: 查询不到行情,取最后一个交易日 dailyQuoteRow = SourceDataDao.selectByLastDay( dailyQuote, innerCode) #print('innerCode:' + str(innerCode)) # print(dailyQuoteSingle) #try: turnoverValue = dailyQuoteRow["TurnoverValue"] #except: #print('tradingDate:'+dateStr) #print('innerCode:' + str(innerCode)) #print(dailyQuoteRow) turnoverVolume = dailyQuoteRow["TurnoverVolume"] #prevClosePrice = dailyQuoteRow["PrevClosePrice"] #closePrice = dailyQuoteRow["ClosePrice"] #highPrice = dailyQuoteRow["HighPrice"] #lowPrice = dailyQuoteRow["LowPrice"] #sellFlg = dailyQuoteRow[StockConst.sellFlg] #buyFlg = dailyQuoteRow["buyFlg"] #sellFlg = dailyQuoteRow["sellFlg"] #非停牌 #check1 = not BackTestHelper.isStop(closePrice, turnoverValue) #非一字跌停: 可以卖出 #check2 = not BackTestHelper.isYiZiDieTing(highPrice,lowPrice,prevClosePrice) #可卖 #if sellFlg != -1: #实际卖出数不能大于实际买入数 #if numOfActualSell >= numOfActualBuy: #continue actualSellList.append(innerCode) stockHoldEntity = currHoldSet.pop(innerCode) vol = stockHoldEntity.vol sellPrice = turnoverValue / turnoverVolume #卖出后所得资金(没有扣佣金) turnover = sellPrice * vol # 佣金(元) commission = BackTestHelper.get_sell_commission( turnover, StockConst.SELL_COMMISSION / 100) # 扣掉佣金后所得金额 turnover = turnover - commission # 更新可用金额(增加) capitalEntity.increase_usable_cash(turnover) # 加入交易表 stockTradeDict = { 'tradingDate': tradingDate, 'innerCode': innerCode, 'type': -1, 'vol': vol, 'price': sellPrice, 'turnover': turnover, 'commission': commission } stockTradeList.append(stockTradeDict) #实际卖出数 #numOfActualSell += 1 """ if DateUtil.datetime2Str(tradingDate) == '2014-12-09': print('-----handleSellList-----') print(DateUtil.datetime2Str(tradingDate)) print(innerCode) print(vol) print(sellPrice) print(turnoverValue) print(dailyQuoteRow) print('----------') """ #changePCT = NumUtil.getChangePCT(prevClosePrice, sellPrice, 2) #realChangePCT = (changePCT - StockConst.sellCommission) * vol / 100.0 #partChangePCT = partChangePCT + realChangePCT """