def show_random_walks(): "plot 3 random walks of each type" from models import test_sequences iMinutesPerDay = 24*60 iDays = 1 iLen = iDays * iMinutesPerDay lHours = [i/60.0 for i in range(iLen)] lWalk_norm = test_sequences.random_walk(iLen, sTrend="normal") lWalk_bull = test_sequences.random_walk(iLen, sTrend="bullish") lWalk_bear = test_sequences.random_walk(iLen, sTrend="bearish") import matplotlib.pyplot as plt plt.plot(lHours, lWalk_bull, 'g', label="bull " + "{0:.2f}".format(lWalk_bull[-1])) plt.plot(lHours, lWalk_norm, 'b', label="norm " + "{0:.2f}".format(lWalk_norm[-1])) plt.plot(lHours, lWalk_bear, 'r', label="bear " + "{0:.2f}".format(lWalk_bear[-1])) plt.legend(loc=3) plt.title("normal, bullish, bearish random walks") plt.xlabel("steps (hours)") plt.ylabel("simulated stock price") plt.show()
def trade(): "buy low, sell high" from models import strategies, meta iMinutesPerDay = 24*60 iDays = 1 iLen = iDays * iMinutesPerDay iInitialAmount = 10000 oAccount = meta.Account(iInitialAmount, {}) from models import test_sequences lWalk_norm_hist = test_sequences.random_walk(iLen, sTrend="normal", iStart=100) oData = meta.Data("NormWalk", lData=lWalk_norm_hist) oActiveTrading = strategies.Active(oData, oAccount) #build the data as we go for every minute for one day for i in range(iLen): oActiveTrading.buy_low_sell_high(99, 101, 90, 0.1, "simple") oData.add_random_walk("normal") print oAccount.dStock[oData.sName], oData.lData[-1] iAllStock = oAccount.dStock[oData.sName]['iNr'] oActiveTrading.sell(iAllStock, oData) iProfit = oAccount.iCash - iInitialAmount print "Profit made = {}".format(iProfit) import matplotlib.pyplot as plt plt.plot(oData.lData) plt.title(iProfit) plt.show()