Exemple #1
0
def testAlgo(algo, target):
#    global data
#    testAlgoOnData(algo, target, data['Close'])
#
#def testAlgoOnData(algo, target, dataList):
    dates = data['Date']
    groups = grouping.groupUp(data, data['Close'])

    targetNext = target+const.ma
    if targetNext >= len(groups):
        return None
    similarity._normalizeFuns = [similarity.byMean]
    similarity._measureFun = algo
    results = compareAllGroupsBefore(groups, target)
    results2 = compareAllGroupsBefore(groups, targetNext)
    
    results.reverse()
    results.sort(key=lambda x : x[2])
    results2.sort(key=lambda x : x[2])

    ### Uses Average Data: useOnlyAverageData = True
    #tradePolicy = tradingmeasure.dontSell
    #tradePolicy = tradingmeasure.sellOrKeep
    #tradePolicy = tradingmeasure.riskAverseSellOrKeep
    #tradePolicy = tradingmeasure.largestReturn

    ### Doesn't use Average Data: useOnlyAverageData = False
    tradePolicy = tradingmeasure.confidenceFilter(0.2, tradingmeasure.sellOrKeep)

    useOnlyAverageData = False

    totalRank = 0
    lpScore = 0
    nResults = 10
    for v in results[0:nResults]:
        rank = getRank(results2, v[0]+const.ma)
        totalRank += rank
        lpScore += similarity.computeWith(groups[v[0]+const.ma], groups[targetNext], [similarity.byFirst], similarity.lpNorms(2))
    
    dataLists = getDataLists(groups, results[0:nResults], const.ma)
    if usingOnlyAverageData:
        dataLists = tradingmeasure.averageData(dataLists)

    money = tradingmeasure.computeWithFunOn(dataLists, groups[targetNext][2], tradePolicy)
    #print(money)
    totalRank *= 100        # normalize totalRank for equal weightage.
    totalRank /= len(results2) # normalize totalRank for equal weightage.

    return (lpScore/nResults, totalRank/nResults, money)
def testAlgoWeighted(algo, target, weightDataFun, data):
    dates = data['Date']
    groupsWeighted = grouping.groupUp(data, weightDataFun(data))
    groupsClose = grouping.groupUp(data, data['Close'])

    targetNext = target+const.ma
    if targetNext >= len(groupsWeighted):
        return None
    similarity._normalizeFuns = [similarity.byMean]
    similarity._measureFun = algo
    results = testalgos.compareAllGroupsBefore(groupsWeighted, target)
    results2 = testalgos.compareAllGroupsBefore(groupsClose, targetNext)
    
    results.reverse()
    results.sort(key=lambda x : x[2])
    results2.sort(key=lambda x : x[2])

    tradePolicy = tradingmeasure.sellOrKeep
    usingOnlyAverageData = True

    totalRank = 0
    lpScore = 0
    nResults = 10

    for v in results[0:nResults]:
        rank = testalgos.getRank(results2, v[0]+const.ma)
        totalRank += rank
        lpScore += similarity.computeWith(groupsClose[v[0]+const.ma], groupsClose[targetNext], [similarity.byFirst], similarity.lpNorms(2))
    
    dataLists = testalgos.getDataLists(groupsClose, results[0:nResults], const.ma)
    if usingOnlyAverageData:
            dataLists = tradingmeasure.averageData(dataLists)

    money = tradingmeasure.computeWithFunOn(dataLists, groupsClose[targetNext][2], tradePolicy)
    #totalRank *= 100        # normalize totalRank for equal weightage.
    #totalRank /= len(results2) # normalize totalRank for equal weightage.

    return (lpScore/nResults, totalRank/nResults, money)