示例#1
0
r = r[idxTradesOverLapped, :]

# In[34]:

tradeArea = [inTime, otTime]
result = rschLib.getTradeAnalysisSampleGroups(r, tradeArea)

# In[35]:

strategy_name = q['strategy_name']
offStart = strategy_off_start

# In[36]:

rschLib.drawPriceChange(r, strategy_name, timeLabels=timeLabels, tp=tradeArea)

# In[37]:

importlib.reload(rschLib)
rschLib.drawPriceChange(r, strategy_name, timeLabels=timeLabels, tp=tradeArea)
rschLib.drawPriceChange(result['rGood10'],
                        strategy_name,
                        timeLabels=timeLabels,
                        title='盈利前10%交易',
                        tp=tradeArea)
rschLib.drawPriceChange(result['rGood20'],
                        strategy_name,
                        timeLabels=timeLabels,
                        title='盈利前20%交易',
                        tp=tradeArea)
示例#2
0
# get trades
R = open_mtx[:, 1:] / close_mtx[:, :-1] - 1  #使用收盘到开盘的回报率来修正分红和拆股
R = np.hstack((np.zeros((R.shape[0], 1)), R))
tradesUsed, r_withnan = rschLib.getTradesFast(strategy_name, name, tkrs, dtes,
                                              maxD, dayTimeAsFloat, R)

# get trade samples by good/bad trades
tradeArea = [inTime, otTime]
r = r_withnan.copy()
r[np.isfinite(r) == False] = 0

# draw price change
idxTradable = np.isfinite(r_withnan[:, tradeArea[0]])
result = rschLib.getTradeAnalysisSampleGroups(r, idxTradable, tradeArea)
rschLib.drawPriceChange(r[idxTradable, :],
                        strategy_name,
                        timeLabels=timeLabels,
                        tp=tradeArea)
rschLib.drawPriceChange(result['rGood10'],
                        strategy_name,
                        timeLabels=timeLabels,
                        title='盈利前10%交易',
                        tp=tradeArea)
rschLib.drawPriceChange(result['rGood30'],
                        strategy_name,
                        timeLabels=timeLabels,
                        title='盈利前30%交易',
                        tp=tradeArea)
rschLib.drawPriceChange(result['rBad10'],
                        strategy_name,
                        timeLabels=timeLabels,
                        title='亏损前10%交易',
示例#3
0
l = np.quantile(p,0.1)
l2 = np.quantile(p,0.2)
l3 = np.quantile(p,0.3)
rGood = r[p>=u,:]
rGood2 = r[p>=u2,:]
rGood3 = r[p>=u3,:]
rBad = r[p<=l, :]
rBad2 = r[p<=l2, :]
rBad3 = r[p<=l3, :]
#for (i,x) in enumerate(tradesUsed):
#    if p[i]>u:
#        print('good trade:', x['name'], x['dateIn'],p[i],isZhangtingBeforeTradeArea[i])
#    if p[i]<l:
#        print('bad trade:', x['name'], x['dateIn'], p[i],isZhangtingBeforeTradeArea[i])
importlib.reload(rschLib)
rschLib.drawPriceChange(r, strategy_name, timeLabels=timeLabels, tp=tradeArea)
rschLib.drawPriceChange(rGood, strategy_name, timeLabels=timeLabels, title='盈利前10%交易', tp=tradeArea)
rschLib.drawPriceChange(rGood2, strategy_name, timeLabels=timeLabels, title='盈利前20%交易', tp=tradeArea)
rschLib.drawPriceChange(rGood3, strategy_name, timeLabels=timeLabels, title='盈利前30%交易', tp=tradeArea)
rschLib.drawPriceChange(rBad, strategy_name, timeLabels=timeLabels, title='亏损前10%交易', tp=tradeArea)
rschLib.drawPriceChange(rBad2, strategy_name, timeLabels=timeLabels, title='亏损前20%交易', tp=tradeArea)
rschLib.drawPriceChange(rBad3, strategy_name, timeLabels=timeLabels, title='亏损前30%交易', tp=tradeArea)


# In[83]:


importlib.reload(rschLib)
[dtesPnl,pnl, numTrades]=rschLib.getPnl(dtes,tkrs, name, trades, inTime, otTime, dayOff, timeAsFloat, toDatabase='yes')

def analyzeStrategy(strategy_name, offStart, dtes, name, tkrs):
    timeAsFloat, timeLabels, maxM, dayOff, dayTimeAsFloat = rschLib.getTimeLabels(
        maxD)
    R = open_mtx[:, 1:] / close_mtx[:, :-1] - 1  #使用收盘到开盘的回报率来修正分红和拆股
    R = np.hstack((np.zeros((R.shape[0], 1)), R))
    tradesUsed, r_withnan = rschLib.getTradesFast(strategy_name, name, tkrs,
                                                  dtes, maxD, dayTimeAsFloat,
                                                  R)
    # get trade samples by good/bad trades
    tradeArea = [inTime, otTime]
    idxTradable = np.isfinite(r_withnan[:, tradeArea[0]])
    r = r_withnan.copy()
    r[np.isfinite(r) == False] = 0
    result = rschLib.getTradeAnalysisSampleGroups(r, idxTradable, tradeArea)

    # draw price change
    rschLib.drawPriceChange(r[idxTradable, :],
                            strategy_name,
                            timeLabels=timeLabels,
                            tp=tradeArea)
    rschLib.drawPriceChange(result['rGood10'],
                            strategy_name,
                            timeLabels=timeLabels,
                            title='盈利前10%交易',
                            tp=tradeArea)
    #rschLib.drawPriceChange(result['rGood20'], strategy_name, timeLabels=timeLabels, title='盈利前20%交易', tp=tradeArea)
    rschLib.drawPriceChange(result['rGood30'],
                            strategy_name,
                            timeLabels=timeLabels,
                            title='盈利前30%交易',
                            tp=tradeArea)
    rschLib.drawPriceChange(result['rBad10'],
                            strategy_name,
                            timeLabels=timeLabels,
                            title='亏损前10%交易',
                            tp=tradeArea)
    #rschLib.drawPriceChange(result['rBad20'], strategy_name, timeLabels=timeLabels, title='亏损前20%交易',  tp=tradeArea)
    rschLib.drawPriceChange(result['rBad30'],
                            strategy_name,
                            timeLabels=timeLabels,
                            title='亏损前30%交易',
                            tp=tradeArea)

    # analyze tags
    #rschLib.analyzeTradeTags(tradesUsed, result['rGood10'], result['idxGood10'], '盈利前10%交易',strategy_name, dtes, tkrs, offStart)
    #rschLib.analyzeTradeTags(tradesUsed, result['rGood20'], result['idxGood20'], '盈利前20%交易',strategy_name, dtes, tkrs, offStart)
    #rschLib.analyzeTradeTags(tradesUsed, result['rGood30'], result['idxGood30'], '盈利前30%交易',strategy_name, dtes, tkrs, offStart)
    #rschLib.analyzeTradeTags(tradesUsed, result['rBad10'], result['idxBad10'], '亏损前10%交易',strategy_name, dtes, tkrs, offStart)
    #rschLib.analyzeTradeTags(tradesUsed, result['rBad20'], result['idxBad20'], '亏损前20%交易',strategy_name, dtes, tkrs, offStart)
    #rschLib.analyzeTradeTags(tradesUsed, result['rBad30'], result['idxBad30'], '亏损前30%交易',strategy_name, dtes, tkrs, offStart)

    #get tag names
    tnames, tagNamesEn, t2 = rschLib.getTagNames()
    idxOverLapTagList = rschLib.analyzeTradeTags(tradesUsed, r,
                                                 list(range(len(tradesUsed))),
                                                 '所有交易', strategy_name, dtes,
                                                 tkrs, offStart,
                                                 "d:\\pklWeeklyUpdate\\")

    #draw pnl and tag pnl
    importlib.reload(rschLib)
    [dtesByTrade, pnlByTrade] = rschLib.getPnlFast(r,
                                                   dtes,
                                                   tkrs,
                                                   name,
                                                   tradesUsed,
                                                   inTime,
                                                   otTime,
                                                   dayOff,
                                                   timeAsFloat,
                                                   toDatabase='yes',
                                                   strategy_name=strategy_name)
    [dtesPnlAggr, pnlAggr,
     numTrades] = rschLib.aggregatePnlAndDtes(dtesByTrade, pnlByTrade)
    rschLib.drawPNL(dtesPnlAggr,
                    pnlAggr,
                    dtes,
                    strategy_name,
                    showFigure='no',
                    toDatabase='yes')
    for i in range(len(tnames)):
        tagName = tnames[i]
        [dtesWithTag, pnlWithTag,
         n] = rschLib.aggregatePnlAndDtes(dtesByTrade[idxOverLapTagList[i]],
                                          pnlByTrade[idxOverLapTagList[i]])
        rschLib.drawPNL(dtesWithTag,
                        pnlWithTag,
                        dtes,
                        strategy_name,
                        showFigure='no',
                        toDatabase='yes',
                        dateStart=dtesPnlAggr[0],
                        pnlType=tagName)
        rschLib.drawPNL(dtesWithTag,
                        pnlWithTag,
                        dtes,
                        strategy_name + '+' + tagNamesEn[i],
                        showFigure='no',
                        toDatabase='yes',
                        dateStart=dtesPnlAggr[0],
                        pnlType='pnl')

    #analysis of number of trades vs performance
    importlib.reload(rschLib)
    rschLib.pnlVsNumtrades(pnlAggr, numTrades, strategy_name, toDatabase='yes')
    rschLib.saveOffStart(strategy_name, offStart)
示例#5
0
def analyzeStrategy(strategy_name, offStart, dtes, name, tkrs):
    timeAsFloat, timeLabels, maxM, dayOff = rschLib.getTimeLabels(maxD)
    trades, tradesUsed, Po, r = rschLib.getTradesWithPklCache(
        strategy_name, name, tkrs, dtes, maxD, maxM)
    # get trade samples by good/bad trades
    tradeArea = [inTime, otTime]
    result = rschLib.getTradeAnalysisSampleGroups(r, tradeArea)

    # draw price change
    rschLib.drawPriceChange(r,
                            strategy_name,
                            timeLabels=timeLabels,
                            tp=tradeArea)
    rschLib.drawPriceChange(result['rGood10'],
                            strategy_name,
                            timeLabels=timeLabels,
                            title='盈利前10%交易',
                            tp=tradeArea)
    rschLib.drawPriceChange(result['rGood20'],
                            strategy_name,
                            timeLabels=timeLabels,
                            title='盈利前20%交易',
                            tp=tradeArea)
    rschLib.drawPriceChange(result['rGood30'],
                            strategy_name,
                            timeLabels=timeLabels,
                            title='盈利前30%交易',
                            tp=tradeArea)
    rschLib.drawPriceChange(result['rBad10'],
                            strategy_name,
                            timeLabels=timeLabels,
                            title='亏损前10%交易',
                            tp=tradeArea)
    rschLib.drawPriceChange(result['rBad20'],
                            strategy_name,
                            timeLabels=timeLabels,
                            title='亏损前20%交易',
                            tp=tradeArea)
    rschLib.drawPriceChange(result['rBad30'],
                            strategy_name,
                            timeLabels=timeLabels,
                            title='亏损前30%交易',
                            tp=tradeArea)
    # analyze tags
    #rschLib.analyzeTradeTags(tradesUsed, result['rGood10'], result['idxGood10'], '盈利前10%交易',strategy_name, dtes, tkrs, offStart)
    #rschLib.analyzeTradeTags(tradesUsed, result['rGood20'], result['idxGood20'], '盈利前20%交易',strategy_name, dtes, tkrs, offStart)
    #rschLib.analyzeTradeTags(tradesUsed, result['rGood30'], result['idxGood30'], '盈利前30%交易',strategy_name, dtes, tkrs, offStart)
    #rschLib.analyzeTradeTags(tradesUsed, result['rBad10'], result['idxBad10'], '亏损前10%交易',strategy_name, dtes, tkrs, offStart)
    #rschLib.analyzeTradeTags(tradesUsed, result['rBad20'], result['idxBad20'], '亏损前20%交易',strategy_name, dtes, tkrs, offStart)
    #rschLib.analyzeTradeTags(tradesUsed, result['rBad30'], result['idxBad30'], '亏损前30%交易',strategy_name, dtes, tkrs, offStart)

    #get tag names
    tnames, tagNamesEn, t2 = rschLib.getTagNames()
    idxOverLapTagList = rschLib.analyzeTradeTags(tradesUsed, r,
                                                 list(range(len(tradesUsed))),
                                                 '所有交易', strategy_name, dtes,
                                                 tkrs, offStart)

    #draw pnl and tag pnl
    importlib.reload(rschLib)
    [dtesByTrade, pnlByTrade] = rschLib.getPnl(dtes,
                                               tkrs,
                                               name,
                                               tradesUsed,
                                               inTime,
                                               otTime,
                                               dayOff,
                                               timeAsFloat,
                                               toDatabase='yes',
                                               strategy_name=strategy_name)
    [dtesPnlAggr, pnlAggr,
     numTrades] = rschLib.aggregatePnlAndDtes(dtesByTrade, pnlByTrade)
    rschLib.drawPNL(dtesPnlAggr,
                    pnlAggr,
                    dtes,
                    strategy_name,
                    toDatabase='yes')
    for i in range(len(tnames)):
        tagName = tnames[i]
        [dtesWithTag, pnlWithTag,
         n] = rschLib.aggregatePnlAndDtes(dtesByTrade[idxOverLapTagList[i]],
                                          pnlByTrade[idxOverLapTagList[i]])
        rschLib.drawPNL(dtesWithTag,
                        pnlWithTag,
                        dtes,
                        strategy_name,
                        toDatabase='yes',
                        dateStart=dtesPnlAggr[0],
                        pnlType=tagName)
        rschLib.drawPNL(dtesWithTag,
                        pnlWithTag,
                        dtes,
                        strategy_name + '+' + tagNamesEn[i],
                        toDatabase='yes',
                        dateStart=dtesPnlAggr[0],
                        pnlType='pnl')

    #analysis of number of trades vs performance
    importlib.reload(rschLib)
    rschLib.pnlVsNumtrades(pnlAggr, numTrades, strategy_name, toDatabase='yes')
    rschLib.saveOffStart(strategy_name, offStart)