Simulated Random Trading

This entry is part 7 of 23 in the series Numpy Strategies

NumPy Strategies 0.1.6

Our Product Owner and Product Manager got talking about our latest strategy. They decided that our Fantasy Hedge Fund needs to have simulations of these strategies. The new User Story:

  • Simulate periodic trading with random buying and selling.

We will simulate trading by taking some random days from a trading year.

1. Get close prices

Let’s start by downloading historical data for a stock. For instance, AAPL.

1
2
3
4
5
today = date.today()
start = (today.year - 1, today.month, today.day)
 
quotes = quotes_historical_yahoo(sys.argv[1], start, today)
close =  numpy.array([q[4] for q in quotes])

2. Generate random indices

Generate random integers with the NumPy randint function.

1
   return numpy.random.randint(0, high, size)

3. Simulate trades

Simulate trades with the random indices from the previous step. Use the NumPy take function to extract random close prices from the array of step 1.

1
2
3
   buys = numpy.take(close, get_indices(len(close), nbuys))
   sells = numpy.take(close, get_indices(len(close), nbuys))
   profits[i] = sells.sum() - buys.sum()

4. Plot a histogram of the profits

Let’s plot a histogram of the profits for a large number of simulations.

1
2
matplotlib.pyplot.hist(profits)
matplotlib.pyplot.show()

The resulting histogram of 2000 simulations for AAPL with 5 buys and sells in a year:

Random Periodic Simulation

What just happened?

This is the Retrospective of Sprint 16. One thing we noticed is that the profits variance is HUGE! So it will be hard to beat a random strategy. The mean is close to 0, which was expected.

Below is the complete code or get it from Github.

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
from matplotlib.finance import quotes_historical_yahoo
from datetime import date
import numpy
import sys
import matplotlib.pyplot
 
def get_indices(high, size):
   #2. Generate random indices
   return numpy.random.randint(0, high, size)
 
#1. Get close prices.
today = date.today()
start = (today.year - 1, today.month, today.day)
 
quotes = quotes_historical_yahoo(sys.argv[1], start, today)
close =  numpy.array([q[4] for q in quotes])
 
nbuys = int(sys.argv[2])
N = int(sys.argv[3])
profits = numpy.zeros(N)
 
for i in xrange(N):
   #3. Simulate trades
   buys = numpy.take(close, get_indices(len(close), nbuys))
   sells = numpy.take(close, get_indices(len(close), nbuys))
   profits[i] = sells.sum() - buys.sum()
 
print "Mean", profits.mean() 
print "Std", profits.std()
 
#4. Plot a histogram of the profits
matplotlib.pyplot.hist(profits)
matplotlib.pyplot.show()

If you liked this post and are interested in NumPy check out NumPy Beginner’s Guide by yours truly.

Series Navigation
By the author of NumPy Beginner's Guide, NumPy Cookbook and Instant Pygame. If you enjoyed this post, please consider leaving a comment or subscribing to the RSS feed to have future articles delivered to your feed reader.
Share
This entry was posted in programming and tagged , , , . Bookmark the permalink.