Exemplo n.º 1
0
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
Exemplo n.º 2
0
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)
Exemplo n.º 3
0
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)