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 decideStrategy(knownData, groupSize, predictSize, chart = None):
    global tradePolicy, tradingPreprocess, similarityMeasure
    
    dates = knownData['Date']
    fullData = knownData['Close']
    points = getDataselectPoints(knownData)

    if len(fullData)-1 not in points:
        return dontTrade()

    groups = groupByLast(knownData, fullData, groupSize)

    if (len(groups) < 20): return dontTrade()

    target = len(groups) - 1

    """
    matches = dataselect.findMatches(knownData, groups, 2)
    printGroups(matches)
    #printGroup(groups[target])
    if groups[target] not in matches:
        return dontTrade()
    """

    similarity._normalizeFuns = [similarity.byMean]
    similarity._measureFun = similarityMeasure

    results = compareAllGroupsBefore(groups, groupSize, target)
    
    results.reverse()
    results.sort(key=lambda x : x[1])

    nResults = 10
    #print(','.join(map(lambda x : str(x[0]), results[0:nResults])))
    dataLists = getDataLists(fullData, groups, results[0:nResults], predictSize)
    strategy = createStrategyFromPolicy(dataLists)

    if chart != None:
        avgData = tradingmeasure.averageData(dataLists)
        chart.writeChart(avgData)

    return strategy
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)