def _loadNormalizedCloseValuesForStocks(self, exchangeName, dateFrom, dateTo):
        nasdaqDataRetrieval = HistoricalDataRetrieval(exchangeName)
        stockNames = nasdaqDataRetrieval.getListOfStocks()
        normalizedStocks = []

        timePrinter = RemainingTimePrinter("Loading " + exchangeName + " stocks data", len(stockNames))

        for a in range(len(stockNames)):
            timePrinter.printMessage(a)
            stockName = stockNames[a]
            stockArray = np.array(nasdaqDataRetrieval.loadAdjustedCloseValues(stockName, dateFrom, dateTo), np.float)
            if stockArray.shape[0] > 100 and stockArray[-1]/stockArray[0] > 1.2:
                normalizedStock = stockArray / stockArray[0]
                normalizedStocks.append((stockName, normalizedStock))

        timePrinter.printLastMessage()

        return normalizedStocks
Beispiel #2
0
#
##YahooDataUpdater("nyse").updateStocks(date.today())
#YahooDataUpdater("nasdaq").updateStocks(date.today())


exchangeName = "nasdaq"
stockNameA, stockNameB, proportion, sharpeRatio = SharpeRatioOptimizer().findBestStocksWithHighestSharpeRatio(exchangeName, date(2011, 1, 1), date(2011,12,31))

print "Best sharpe = " + str(sharpeRatio)+ " is for stock "+stockNameA + " and " + stockNameB + " with proportion 1:"+str(proportion)

end = time.clock()
print "All work took " + str(end - start) + " seconds."




stockDataRetrieval = HistoricalDataRetrieval(exchangeName)

stocksValuesA = np.array(stockDataRetrieval.loadAdjustedCloseValues(stockNameA, date(2011, 1, 1), date(2012,12,31)))
normalizedValuesA = stocksValuesA / stocksValuesA[0]
stocksValuesB = np.array(stockDataRetrieval.loadAdjustedCloseValues(stockNameB, date(2011, 1, 1), date(2012,12,31)))
normalizedValuesB = stocksValuesB / stocksValuesB[0]


pylab.plot(normalizedValuesA)
pylab.plot(normalizedValuesB)
pylab.plot((normalizedValuesA + normalizedValuesB * proportion) / (1 + proportion))
pylab.legend([stockNameA, stockNameB, "Portfolio"])
pylab.show()