Example #1
0
def runVisualise():
    fileName = 'data_/AMERICAN_MANAGEMENT_SYSTEMS_INC.csv'
    #fileName = 'table.csv'
    targetGroup = 89

    #global data, headers
    data, headers = para.readFile(fileName)
    dates = data['Date']
    close = data['Close']
    groups = grouping.groupUp(data, data['Close'])

    target = targetGroup
    targetNext = target+const.ma
    results = testalgos.compareAllGroupsBefore(groups, target)
    results2 = testalgos.compareAllGroupsBefore(groups, targetNext)
    #results2 = compareAllGroupsTo(groups, targetNext)
    
    results.reverse()
    results.sort(key=lambda x : x[2])
    results2.sort(key=lambda x : x[2])

    print('Target ' + str(target))
    display.plotgroup(data, groups[target])
    plt.show()

    for v in results[0:10]:
        print(v)

    totalRank = 0
    ranks = []
    for v in results[0:10]:
        rank = testalgos.getRank(results2, v[0]+const.ma)
        totalRank += rank
        ranks.append(rank)
    print('Total Rank = ' + str(totalRank))
    print(ranks)

    display.plotAverageHighLow(groups, results[0:10], groups[targetNext][2])
    return

    for v in results[0:10]:
        print()
        print('Chosen group:')
        print('Number ' + str(v[0]) + ', SimilarityScore = ' + str(v[2]))
        print('Matching Group:')
        print('Number ' + str(v[0]+const.ma) + ', Rank = ' + str(testalgos.getRank(results2, v[0]+const.ma)))
        print('Plotting Target Next Group (blue)')
        display.plotnormalizedWith(data, groups[targetNext], [similarity.byFirst])
        print('Plotting Predicted Next Group (green)')
        display.plotnormalizedWith(data, groups[v[0]+const.ma], [similarity.byFirst])
        plt.show()
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)