def createFakeSignalData(addr): signalData = SourceDataDao.load_signal_data(addr) startDate = signalData.index[0][0] endDate = signalData.index[-1][0] startYear = DateUtil.datetime2_year_str(startDate) endYear = DateUtil.datetime2_year_str(endDate) FORMAT = "%d-%d-%d" # c = time.strftime('%Y%m%d', time.strptime(y, '%Y-%m-%d')) for year in range(int(startYear), int(endYear) + 1, 1): for month in range(1, 13): d = calendar.monthrange(year, month) exportStartDate = FORMAT % (year, month, 1) exportEndDate = FORMAT % (year, month, d[1]) # exportStartDate = str(year)+'-'+addZero(month)+'-'+'01' # exportEndDate = str(year) + '-' + addZero(month) + '-' + getMonthEndDay(month) subSignalData = SourceDataDao.select_by_date( signalData, exportStartDate, exportEndDate) print('exportStartDate:' + exportStartDate) print('exportEndDate:' + exportEndDate) print('subSignalData:' + str(len(subSignalData))) if (len(subSignalData) > 0): newSubSignalData = subSignalData.copy() newSubSignalData['Mom2'] = subSignalData['Mom'] newSubSignalData['Mom3'] = subSignalData['Mom'] SourceDataDao.export_to_hdfstore( newSubSignalData, StockConst.ROOT + StockConst.FAKE_SIGNAL_DATA_H5)
def testSelectSignalByDateAndInnerCode(self): signalData = SourceDataDao.load_signal_data('') signalData = SourceDataDao.select_signal_by_date_and_inner_code(signalData, '2004-01-31', '000049.SZ') # self.assertEqual(self.sum(1, 2), 3) print('signalData:'+str(len(signalData))) #print(signalData) self.assertNotEqual(len(signalData), 0)
def testSelectByPrevTradingDay(self): dailyQuote = SourceDataDao.load_new_daily_quote('') df = SourceDataDao.select_by_prev_tradingday(dailyQuote, '2004-09-12', '000049.SZ') print(df) #print(signalData) self.assertNotEqual(len(df), 0)
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 create_h5(): ## ms = MSSQL(host="localhost",user="******",pwd="123456",db="PythonWeiboStatistics") ## #返回的是一个包含tuple的list,list的元素是记录行,tuple的元素是每行记录的字段 ## ms.ExecNonQuery("insert into WeiBoUser values('2','3')") # ms = MSSQLHelper.getMSSQLInstance() # # #连接 # sqlserverCon = ms.getConnect() # sql = 'SELECT NatureDay,WindCode,IfTradingDay FROM AsIndexComponentZZ500 order by NatureDay,WindCode' # # # resList = ms.ExecQuery(sql) # # for (NatureDay,WindCode,IfTradingDay) in resList: # # print(str(NatureDay) + ' ' + str(WindCode) + ' ' + str(IfTradingDay)) # # #sqlserrver转成h5 # df = pd.read_sql(sql, con=sqlserverCon) # print('df:') # print(df) # exportToHDFStore(df, StockConst.root + '/' + targetAddr + '.h5') hs300df = SourceDataDao.load_h5(StockConst.ROOT + StockConst.HS300H5) zz500df = SourceDataDao.load_h5(StockConst.ROOT + StockConst.ZZ500H5) zz800df = hs300df.append(zz500df) zz800df = zz800df.sort_values(by=['NatureDay', 'WindCode'], ascending=True) SourceDataDao.export_to_hdfstore(zz800df, StockConst.ROOT + StockConst.ZZ800H5)
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 init_signal_data_info(signalDataAddr): # 取得技术指标 techListOrigin = SourceDataDao.get_colums_from_h5(signalDataAddr) techList.clear() for x in techListOrigin: techList.append(x) # 删除已有指标控件 for widget in techframe.winfo_children(): widget.destroy() # 新增指标控件 for index, item in enumerate(techListOrigin): R1 = Radiobutton(techframe, text=item, variable=techRadioVar, value=index, command=select_tech_radio) R1.pack(anchor=W) # 信号文件地址 signalDataAddrVal.set(signalDataAddr) # originStartDate = SourceDataDao.get_first_record_from_h5( signalDataAddr).index[0][0] originEndDate = SourceDataDao.get_last_record_from_h5( signalDataAddr).index[0][0] originStartDate = DateUtil.datetime2_str(originStartDate) originEndDate = DateUtil.datetime2_str(originEndDate) originStartDateVar.set(originStartDate) originEndDateVar.set(originEndDate) #开始时间 结束时间 startDateVal.set(originStartDate) endDateVal.set(originEndDate)
def testCheckIfCannotBuy(self): dailyQuote = SourceDataDao.load_new_daily_quote('') #双休日 checkResult = SourceDataDao.check_if_cannot_buy(dailyQuote, '2004-01-31', '000049.SZ') print('checkResult:'+str(checkResult)) #print(signalData) self.assertTrue(checkResult) #双休日 checkResult = SourceDataDao.check_if_cannot_buy(dailyQuote, '2004-02-01', '000049.SZ') print('checkResult:' + str(checkResult)) # print(signalData) self.assertTrue(checkResult) #一字涨停 checkResult = SourceDataDao.check_if_cannot_buy(dailyQuote, '2004-02-02', '000049.SZ') print('checkResult:' + str(checkResult)) # print(signalData) self.assertTrue(checkResult) #正常 checkResult = SourceDataDao.check_if_cannot_buy(dailyQuote, '2004-02-05', '000049.SZ') print('checkResult:' + str(checkResult)) # print(signalData) self.assertFalse(checkResult)
def printByInnerCode(innerCode,addr): dailyQuote = SourceDataDao.load_new_daily_quote(addr) # dailyQuote = selectByDate(dailyQuote,'2004-01-05','2004-01-06') # dailyQuote = selectByInnerCodeAndDate(dailyQuote,'2004-01-05','000049.SZ') #'000049.SZ' dailyQuote = SourceDataDao.select_by_inner_code(dailyQuote, innerCode) dailyQuote.to_csv(StockConst.ROOT + '\export\dailyQuoteDebug.csv') print(dailyQuote)
def getActualSellList(planSellList, tradingDate, signalData, dailyQuote, numOfToKeepInSellList, cannotSellList): if len(planSellList) == 0: return planSellList #创建2列DF,innerCode,Mom(空) dic = {StockConst.INNERCODE: planSellList} sortedSellDf = pd.DataFrame(data=dic) sortedSellDf.insert(1, StockConst.MOM, Series()) #print(sortedSellList) #从信号数据中获取Mom用于排序 for innerCode in planSellList: signalEntity = SourceDataDao.select_signal_by_date_and_inner_code( signalData, tradingDate, innerCode) #entity = signalData.ix[(tradingDate, innerCode)] sortedSellDf.loc[(sortedSellDf[StockConst.INNERCODE] == innerCode), [StockConst.MOM]] = signalEntity[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] #try: isCannotSell = SourceDataDao.check_if_cannot_sell( dailyQuote, tradingDate, innerCode) #except: #print('getToSellInSellList '+DateUtil.datetime2Str(tradingDate)+' '+str(innerCode)) #print(sortedSellDf) #print(sellList) #print(currHoldSet) #print(dailyQuoteRow) #退市的股票:卖出 #不能卖 if isCannotSell: 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) #去掉需要保留的股票 actualSellList = sortedSellDf[~( sortedSellDf[StockConst.INNERCODE].isin(toKeepInSellList))][ StockConst.INNERCODE].values #print('toSellInSellList') #print(toSellInSellList) return actualSellList
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 createSignalDataHill(): # dateList = ['2010-01-01','2010-01-02','2010-01-03','2010-01-04'] dateList1 = pd.date_range('2004-01-01', '2004-06-30') dateList2 = pd.date_range('2004-07-01', '2004-12-31') df1 = createSignalDataHillMain(dateList1, True) df2 = createSignalDataHillMain(dateList2, False) df = df1.append(df2) # print(df) SourceDataDao.export_to_hdfstore( df, StockConst.ROOT + '/export/SignalDataHill.h5')
def createFakeIndexQuote2(): dateRange = pd.date_range(start='2005-01-01', end='2005-12-31') codeList = [ StockConst.HS300_CODE, StockConst.ZZ500_CODE, StockConst.ZZ800_CODE ] multi_index = pd.MultiIndex.from_product([dateRange, codeList], names=['TradingDay', 'WindCode']) indexDF = pd.DataFrame(index=multi_index, columns=['ChangePCT']) indexDF['ChangePCT'] = 1 #百分之 # print(indexDF) SourceDataDao.export_to_hdfstore( indexDF, StockConst.ROOT + StockConst.FAKE_INDEX_QUOTE_H5_2) indexDF.to_csv(StockConst.ROOT + '\\export\\fakeIndexQuoteH5_2.csv')
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 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 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 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 addBuySellFlgToSignalData2(signalDataAddr, dailyQuoteAddr): signalData = SourceDataDao.load_signal_data(signalDataAddr) dailyQuote = SourceDataDao.load_new_daily_quote(dailyQuoteAddr) groupedSignalData = signalData.groupby(level=StockConst.TRADINGDAY, as_index=False).apply( SelectUtil.top, 5, StockConst.MOM, False) groupedSignalData[StockConst.BUY_FLG] = dailyQuote[StockConst.BUY_FLG] groupedSignalData[StockConst.SELL_FLG] = dailyQuote[StockConst.SELL_FLG] # groupedSignalData.insert(1, StockConst.buyFlg, Series()) # groupedSignalData.insert(2, StockConst.sellFlg, Series()) # for index, row in groupedSignalData.iterrows(): # # print('tradingDate:'+str(index[0])+',innerCode:'+str(index[2])) # groupedSignalData.ix[index, [StockConst.buyFlg]] = dailyQuote.ix[(index[0], index[2]), [StockConst.buyFlg]] # groupedSignalData.ix[index, [StockConst.sellFlg]] = dailyQuote.ix[(index[0], index[2]), [StockConst.sellFlg]] groupedSignalData.to_csv(StockConst.ROOT + '\export\groupedSignalData.csv')
def mergeSignalWithIndexMain(signalDataDf, indexDf, addr): # print(signalDataDf) # print(indexDf) # newIndexDf = indexDf.apply(convertDate, axis=1) # # indexDf['NatureDay'] = DateUtil.str2Datetime(indexDf['NatureDay']) # print(newIndexDf) # 取交集 df = pd.merge(indexDf, signalDataDf, left_on=['NatureDay','WindCode'], right_index=True) # NatureDay,WindCode # 取3列 df = df[['NatureDay','WindCode','Mom']] # 重命名 df. rename(columns={'NatureDay': 'TradingDay'}, inplace=True) # 设置索引 df = df.set_index(['TradingDay','WindCode']) # 导出h5 SourceDataDao.export_to_hdfstore(df, StockConst.ROOT + addr)
def printSingalDataByDate(date,addr): signalData = SourceDataDao.load_signal_data(addr) t1 = pd.Timestamp(date) #t2 = pd.Timestamp(endDate) #signalData = signalData.loc[t1:t2, :] signalData = signalData[signalData.index.get_level_values(0) == t1] signalData = signalData.sort_values(by=[StockConst.MOM], ascending=False) print(signalData)
def read_func1(signalDataAddr, table_name): if signalDataAddr is not None: signalData = SourceDataDao.read_file_set_index(signalDataAddr) elif table_name is not None: signalData = SignalDataMSSQLDao.select_signal_data(table_name) else: raise ValueError('signal data address and table name is unknown!') # print('signalData') # print(signalData) return signalData
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 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 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 main(): signalDataDf = SourceDataDao.read_file_set_index('D:\\0_Data\\Mom_basedon_HS300.h5') techList = [StockConst.MOM] sliceIdx = None numOfDailySignal = 4 sliceTotalNum = None sliceDict = {'sliceIdx': sliceIdx, 'numOfDailySignal': numOfDailySignal, 'sliceTotalNum': sliceTotalNum} allSelectStockDf = select_func_6(signalDataDf,techList,sliceDict)
def sort_by_tech(planSellList, signalData, tradingDay, tech): techDict = {} for innerCode in planSellList: signalEntity = SourceDataDao.select_signal_by_date_and_inner_code(signalData, tradingDay, innerCode) techDict.setdefault(innerCode, signalEntity[tech]) # TODO dict = sorted(techDict.items(), key=lambda d: d[1], reverse=True) # print(dict) planSellList2 = [] for x in dict: planSellList2.append(x[0]) return planSellList2
def testCheckIfCannotSell(self): dailyQuote = SourceDataDao.load_new_daily_quote('') #停牌 checkResult = SourceDataDao.check_if_cannot_sell(dailyQuote, '2004-09-12', '000049.SZ') print('checkResult:'+str(checkResult)) #print(signalData) self.assertTrue(checkResult) #一字跌停 checkResult = SourceDataDao.check_if_cannot_sell(dailyQuote, '2004-09-13', '000049.SZ') print('checkResult:' + str(checkResult)) # print(signalData) self.assertTrue(checkResult) #一字跌停 checkResult = SourceDataDao.check_if_cannot_sell(dailyQuote, '2004-09-14', '000049.SZ') print('checkResult:' + str(checkResult)) # print(signalData) self.assertTrue(checkResult) #一字跌停 checkResult = SourceDataDao.check_if_cannot_sell(dailyQuote, '2004-09-15', '000049.SZ') print('checkResult:' + str(checkResult)) # print(signalData) self.assertTrue(checkResult) #正常 checkResult = SourceDataDao.check_if_cannot_sell(dailyQuote, '2004-09-16', '000049.SZ') print('checkResult:' + str(checkResult)) # print(signalData) self.assertFalse(checkResult)
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 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 addBuySellFlgAndExport(addr): dailyQuote = SourceDataDao.load_daily_quote(addr) startDate = dailyQuote.index[0][0] endDate = dailyQuote.index[-1][0] startYear = DateUtil.datetime2_year_str(startDate) endYear = DateUtil.datetime2_year_str(endDate) FORMAT = "%d-%d-%d" # c = time.strftime('%Y%m%d', time.strptime(y, '%Y-%m-%d')) for year in range(int(startYear), int(endYear) + 1, 1): for month in range(1 ,13): d = calendar.monthrange(year, month) exportStartDate = FORMAT % (year, month, 1) exportEndDate = FORMAT % (year, month, d[1]) # exportStartDate = str(year)+'-'+addZero(month)+'-'+'01' # exportEndDate = str(year) + '-' + addZero(month) + '-' + getMonthEndDay(month) subDailyQuote = SourceDataDao.select_by_date(dailyQuote, exportStartDate, exportEndDate) print('exportStartDate:' + exportStartDate) print('exportEndDate:' + exportEndDate) print( 'dailyQuote:' +str(len(subDailyQuote))) if(len(subDailyQuote) > 0): newSubDailyQuote = addBuySellFlg(subDailyQuote) SourceDataDao.export_to_hdfstore(newSubDailyQuote, StockConst.ROOT + StockConst.NEW_DAILY_QUOTE_H5)