def testStrategies(strategies, numTests, sDate=(2003, 1, 1), eDate=(2013, 10, 10)): """Main Function""" maxDaysHeld = 365 * 7 # Make sure our allocations are well formed if not checkAllocationStrats(): raise Exception("Allocations are malformed.") # Colors for plotting colors = plt.cm.rainbow(linspace(0, 1, numTests)) # Test each investment strategy for strategy in strategies.keys(): print "\n===%s===" % strategy for n in xrange(numTests): # Test different versions of the same portfolio portfolio = createPortfolio(strategies[strategy]) print "Test portfilo %d: %s" % (n, portfolio) testPortfilo( portfolio, sDate, eDate, maxDaysHeld, outputFilename=r"data\%s_%d.dat" % (strategy, n), silent=True ) # Plot the results plt.figure() for n in xrange(numTests): f = shelve.open(r"data\%s_%d.dat" % (strategy, n)) pltAgainst(f, "Portfolio %d" % n, colors[n]) f.close() plt.legend(loc=2) plt.title("Strategy: %s" % strategy) plt.show()
def plotResults(strategiesToPlot, numTests, which=None): plt.xkcd() plt.figure() if which is None: # Which tells us to plot a subset of the results which = range(numTests) if len(strategiesToPlot) > 1: colors = plt.cm.rainbow(linspace(0, 1, len(strategiesToPlot))) else: colors = plt.cm.rainbow(linspace(0, 1, len(which))) colors = {a: b for (a, b) in zip(which, colors)} for nstrat, strategy in enumerate(strategiesToPlot): for n in xrange(numTests): if n not in which: continue if n == 0 or len(strategiesToPlot) == 1: title = '%s #%d' % (strategy, n) else: title = None f = shelve.open(r'data\%s_%d.dat' % (strategy, n)) if len(strategiesToPlot) > 1: pltAgainst(f, title, colors[nstrat]) else: pltAgainst(f, title, colors[n]) f.close() plt.legend(loc='upper left') plt.title('All Strategies') plt.show()
def plotResults(strategiesToPlot, numTests, which=None): plt.xkcd() plt.figure() if which is None: # Which tells us to plot a subset of the results which = range(numTests) if len(strategiesToPlot) > 1: colors = plt.cm.rainbow(linspace(0, 1, len(strategiesToPlot))) else: colors = plt.cm.rainbow(linspace(0, 1, len(which))) colors = {a: b for (a, b) in zip(which, colors)} for nstrat, strategy in enumerate(strategiesToPlot): for n in xrange(numTests): if n not in which: continue if n == 0 or len(strategiesToPlot) == 1: title = "%s #%d" % (strategy, n) else: title = None f = shelve.open(r"data\%s_%d.dat" % (strategy, n)) if len(strategiesToPlot) > 1: pltAgainst(f, title, colors[nstrat]) else: pltAgainst(f, title, colors[n]) f.close() plt.legend(loc="upper left") plt.title("All Strategies") plt.show()
def testStrategies(strategies, numTests, sDate=(2003, 1, 1), eDate=(2013, 10, 10)): """Main Function""" maxDaysHeld = 365 * 7 # Make sure our allocations are well formed if not checkAllocationStrats(): raise Exception('Allocations are malformed.') # Colors for plotting colors = plt.cm.rainbow(linspace(0, 1, numTests)) # Test each investment strategy for strategy in strategies.keys(): print '\n===%s===' % strategy for n in xrange(numTests): # Test different versions of the same portfolio portfolio = createPortfolio(strategies[strategy]) print "Test portfilo %d: %s" % (n, portfolio) testPortfilo(portfolio, sDate, eDate, maxDaysHeld, outputFilename=r'data\%s_%d.dat' % (strategy, n), silent=True) # Plot the results plt.figure() for n in xrange(numTests): f = shelve.open(r'data\%s_%d.dat' % (strategy, n)) pltAgainst(f, 'Portfolio %d' % n, colors[n]) f.close() plt.legend(loc=2) plt.title('Strategy: %s' % strategy) plt.show()