Ejemplo n.º 1
0
    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)
Ejemplo n.º 2
0
    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)
Ejemplo n.º 3
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)
Ejemplo n.º 4
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)
Ejemplo n.º 5
0
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)
Ejemplo n.º 6
0
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')
Ejemplo n.º 7
0
def main(select_stock_1day_func,trade_1day_func,techList,signalDataAddr = '', dailyQuoteAddr = '', startDate = '2004-01-01', endDate = '2005-12-31'):
    start = time.clock()

    signalData = SourceDataDao.load_signal_data(signalDataAddr) #ByDate
    #dailyQuote = SourceDataDao.loadDailyQuote()
    dailyQuote = SourceDataDao.load_new_daily_quote(dailyQuoteAddr) #ByDate

    start_1 = time.clock()
    signalData = signalData[(signalData.index.get_level_values(0) >= startDate) & (signalData.index.get_level_values(0) <= endDate) ]
    end_1 = time.clock()
    print("signalData花费时间:%f s" % (end_1 - start_1))

    # print(startDate)
    # print(endDate)
    dateList = SourceDataDao.select_date_from_signal(signalData, startDate, endDate)
    # print(dateList)

    # 统计结果
    stockStatDailyDf = pd.DataFrame(index=pd.date_range(startDate, endDate),
                                    columns=['netValue', 'changePCT', 'buyCnt', 'sellCnt', 'prevHoldCnt',
                                             'currHoldCnt', 'cannotSellCnt', 'cannotBuyCnt',
                                         'usableCach', 'mv'])
    # 每日持仓
    stockHoldDailyList = []
    # 交易结果
    stockTradeList = []

    start_trade = time.clock()
    # 交易
    t1d = process1day(startDate, endDate, dailyQuote, signalData, dateList, stockStatDailyDf, stockHoldDailyList, stockTradeList)
    res = signalData.groupby(level=StockConst.TRADINGDAY).apply(t1d.deal, techList, select_stock_1day_func, trade_1day_func)
    end_trade = time.clock()
    print("交易花费时间:%f s" % (end_trade - start_trade))


    result = ''

    start_stat = time.clock()
    # 信号数据
    #groupedSelectStock.to_csv(StockConst.root + '\export\groupedSelectStock.csv')

    # 每日交易
    # print('每日交易:')
    # print(stockTradeList)
    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')
    result += '--' * 70 + '\n'
    result += '每日统计:' + '\n'
    result += stockStatDailyDf.to_string() + '\n'
    # stockStatDailyDf.to_csv(StockConst.root + '\export\stockStatDaily.csv')

    # 每年统计
    result += '--' * 70 + '\n'
    yearDf = StockYearService.main(stockStatDailyDf)
    result += '每年统计:' + '\n'
    result += yearDf.to_string() + '\n'

    # 最大回撤
    result += '--' * 70 + '\n'
    maxdrop = StockMaxDropNewService.get_max_drop(stockStatDailyDf)
    result += '最大回撤:' + '\n'
    result += SetUtil.dict_to_string(maxdrop) + '\n'
    #每年的最大回撤
    maxdropDf = StockMaxDropNewService.get_max_drop_for_each_year(stockStatDailyDf)
    maxdropDf.sort_values(by=["year"])
    result += '每年的最大回撤:' + '\n'
    result += maxdropDf.to_string() + '\n'

    #夏普比率
    result += '--' * 70 + '\n'
    sharpRatio = SharpRatioNewService.get_sharp_ratio(stockStatDailyDf)
    result += '夏普比率:' + '\n'
    result += str(sharpRatio) + '\n'
    #每年的夏普比率
    sharpRatioDf = SharpRatioNewService.get_sharp_ratio_for_each_year(stockStatDailyDf)
    sharpRatioDf.sort_values(by=["year"])
    result += '每年的夏普比率:' + '\n'
    result += sharpRatioDf.to_string() + '\n'

    end_stat = time.clock()
    print("统计花费时间:%f s" % (end_stat - start_stat))

    # result += '回测完成'
    end = time.clock()
    print("main花费时间:%f s" % (end - start))

    # 净值曲线图
    stockStatDailyDf['netValue'].plot()
    plt.show()

    resultDic={'stockStatDailyDf':stockStatDailyDf,'result':result}

    print(result)
    return resultDic
Ejemplo n.º 8
0
 def testLoadNewDailyQuote(self):
     newDailyQuote = SourceDataDao.load_new_daily_quote('')
     # self.assertEqual(self.sum(1, 2), 3)
     print('newDailyQuote:'+str(len(newDailyQuote)))
     self.assertNotEqual(len(newDailyQuote), 0)
Ejemplo n.º 9
0
def main(select_stock_func,
         trade_func,
         techList,
         signalDataAddr='',
         dailyQuoteAddr='',
         startDate='1/1/2004',
         endDate='12/31/2005'):
    start = time.clock()

    signalData = SourceDataDao.load_signal_data(signalDataAddr)
    #dailyQuote = SourceDataDao.loadDailyQuote()
    dailyQuote = SourceDataDao.load_new_daily_quote(dailyQuoteAddr)

    #参数
    #每天信号数
    #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_func(signalData, techList)
    #当前持仓 key:innerCode value:
    currHoldSet = {}
    #资金情况
    capitalEntity = CapitalEntity.CapitalEntity(initialMV)
    lastMV = initialMV
    #初始化每日统计表
    stockStatDailyDf = pd.DataFrame(index=pd.date_range(startDate, endDate),
                                    columns=[
                                        'netValue', 'changePCT', 'buyCnt',
                                        'sellCnt', 'prevHoldCnt',
                                        'currHoldCnt', 'cannotSellCnt',
                                        'cannotBuyCnt', 'usableCach', 'mv'
                                    ])
    #初始化每日持仓表
    stockHoldDailyList = []
    stockTradeList = []

    #从信号表中取得唯一性日期
    dateList = SourceDataDao.select_date_from_signal(signalData, startDate,
                                                     endDate)
    # print(dateList)

    start_loop = time.clock()
    for index, item in enumerate(dateList):
        #for date in dateList:
        #print('index:'+str(index)+' len:'+str(len(dateList)))
        start_inloop0 = time.clock()

        #信号日
        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]
        # print(currSignalData)
        if StockConst.IS_DEBUG:
            print("currSignalData:" + str(len(currSignalData)))
            #print(currSignalData)

        end_inloop0 = time.clock()
        print("inloop0花费时间:%f s" % (end_inloop0 - start_inloop0))

        #交易方法(外部传入)
        start_inloop1 = time.clock()
        dict = trade_func(currSignalData, currHoldSet, dailyQuote, tradingDay,
                          signalData, capitalEntity)
        end_inloop1 = time.clock()
        print("trade_func花费时间:%f s" % (end_inloop1 - start_inloop1))

        actualBuyList = dict['actualBuyList']
        actualSellList = dict['actualSellList']
        prevHoldList = dict['prevHoldList']
        cannotBuyList = dict['cannotBuyList']
        cannotSellList = dict['cannotSellList']
        stockTradeListDaily = dict['stockTradeList']

        if len(stockTradeListDaily) > 0:
            stockTradeList.extend(stockTradeListDaily)

        # print(currHoldSet)

        #3.计算当日市值
        start_inloop2 = time.clock()
        currMV = calculateDailyMV(currHoldSet, capitalEntity, dailyQuote,
                                  tradingDay, stockHoldDailyList, initialMV)
        end_inloop2 = time.clock()
        print("calculateDailyMV花费时间:%f s" % (end_inloop2 - start_inloop2))

        start_inloop3 = time.clock()
        #4.个数
        buyCnt = len(actualBuyList)
        sellCnt = len(actualSellList)
        prevHoldCnt = len(prevHoldList)
        currHoldCnt = len(currHoldSet)
        cannotSellCnt = len(cannotSellList)
        cannotBuyCnt = len(cannotBuyList)

        # print(currMV)

        #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
        end_inloop3 = time.clock()
        print("inloop3花费时间:%f s" % (end_inloop3 - start_inloop3))

        pass

    end_loop = time.clock()
    print("for循环花费时间:%f s" % (end_loop - start_loop))

    result = ''

    start_stat = time.clock()
    # 信号数据
    #groupedSignalData.to_csv(StockConst.root + '\export\groupedSignalData.csv')

    # 每日交易
    # print('每日交易:')
    # print(stockTradeList)
    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')
    result += '--' * 70 + '\n'
    result += '每日统计:' + '\n'
    result += stockStatDailyDf.to_string() + '\n'
    # stockStatDailyDf.to_csv(StockConst.root + '\export\stockStatDaily.csv')

    # 每年统计
    result += '--' * 70 + '\n'
    yearDf = StockYearService.main(stockStatDailyDf)
    result += '每年统计:' + '\n'
    result += yearDf.to_string() + '\n'

    # 最大回撤
    result += '--' * 70 + '\n'
    maxdrop = StockMaxDropNewService.get_max_drop(stockStatDailyDf)
    result += '最大回撤:' + '\n'
    result += SetUtil.dict_to_string(maxdrop) + '\n'
    #每年的最大回撤
    maxdropDf = StockMaxDropNewService.get_max_drop_for_each_year(
        stockStatDailyDf)
    maxdropDf.sort_values(by=["year"])
    result += '每年的最大回撤:' + '\n'
    result += maxdropDf.to_string() + '\n'

    #夏普比率
    result += '--' * 70 + '\n'
    sharpRatio = SharpRatioNewService.get_sharp_ratio(stockStatDailyDf)
    result += '夏普比率:' + '\n'
    result += str(sharpRatio) + '\n'
    #每年的夏普比率
    sharpRatioDf = SharpRatioNewService.get_sharp_ratio_for_each_year(
        stockStatDailyDf)
    sharpRatioDf.sort_values(by=["year"])
    result += '每年的夏普比率:' + '\n'
    result += sharpRatioDf.to_string() + '\n'

    end_stat = time.clock()
    print("统计花费时间:%f s" % (end_stat - start_stat))

    # result += '回测完成'
    end = time.clock()
    print("main花费时间:%f s" % (end - start))

    # 净值曲线图
    stockStatDailyDf['netValue'].plot()
    plt.show()

    resultDic = {'stockStatDailyDf': stockStatDailyDf, 'result': result}

    print(result)
    return resultDic