Welcome, guest | Sign In | My Account | Store | Cart

It simulates an automated trading strategy against a simulated stock.

I think the results are interesting. It seems number of wins are always higher than number of losses but average amount of loss is also always higher than average amount of win!

Python, 62 lines
 ``` 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62``` ```# Automated Stock Market Trading Simulation # FB - 20140515 import random initialMoneyOwned = 1000.0 initialStocksOwned = 0.1 initialStockPrice = 10000.0 tradingDays = 1000 tp = 5.0 # buy/sell percentage threshold of the investor maxVolatilityPercent = 5.0 # of the stock numTrials = 1000 initialInvestment = initialMoneyOwned + initialStocksOwned * initialStockPrice def SimulateTrading(moneyOwned, stocksOwned, stockPrice, days): stockBuySellPrice = stockPrice for day in range(days): volatility = random.random() * stockPrice * maxVolatilityPercent / 100.0 stockPrice += (random.random() * 2.0 - 1.0) * volatility # trading if stocksOwned > 0.0: if stockPrice >= stockBuySellPrice * (100.0 + tp) / 100.0: # sell moneyOwned += stocksOwned * stockPrice stocksOwned = 0.0 stockBuySellPrice = stockPrice if moneyOwned > 0.0: if stockPrice <= stockBuySellPrice * (100.0 - tp) / 100.0: # buy stocksOwned += moneyOwned / stockPrice moneyOwned = 0.0 stockBuySellPrice = stockPrice return (moneyOwned, stocksOwned, stockPrice) numWins = 0 numLosses = 0 totalWins = 0.0 totalLosses = 0.0 for i in range(numTrials): (moneyOwned, stocksOwned, stockPrice) = \ SimulateTrading(initialMoneyOwned, initialStocksOwned, initialStockPrice, tradingDays) finalReturn = moneyOwned + stocksOwned * stockPrice - initialInvestment if finalReturn > 0.0: numWins += 1 totalWins += finalReturn elif finalReturn < 0.0: numLosses += 1 totalLosses += finalReturn print "Initial Investment = " + str(initialInvestment) print "Trading Days = " + str(tradingDays) print "Number of Trials = " + str(numTrials) print "Number of Wins = " + str(numWins) print "Average Win Amt = " + str(totalWins / numWins) print "Number of Losses = " + str(numLosses) print "Average Loss Amt = " + str(abs(totalLosses / numLosses)) ```
 Created by FB36 on Fri, 16 May 2014 (MIT)

### Required Modules

• (none specified)