Simulating trading at random
In the previous recipe, we tried out a trading idea. However, we have no benchmark that can tell us if the result we got was any good. It is common in such cases to trade at random, under the assumption that we should be able to beat a random process. We will simulate trading by taking some random days from a trading year. This should illustrate working with random numbers using NumPy.
Getting ready
If necessary, install Matplotlib. Refer to the See Also section for the corresponding recipe.
How to do it...
First, we need an array filled with random integers.
Generate random indices.
Generate random integers with the NumPy
randint
function. This will be linked to random days of a trading year:return numpy.random.randint(0, high, size)
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:buys = numpy.take(close, get_indices(len(close), nbuys)) sells = numpy...