31.07.2021 Views

Ultimate Algorithmic Trading System

Using automated systems for trading in stock markets

Using automated systems for trading in stock markets

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

260

GENETIC OPTIMIZATION, WALK FORWARD

Our new alternate list of trades would look like this:

AlternateList[0] = (20011222,+500)

AlternateList[1] = (20011015,−150)

AlternateList[2] = (20011105,+200)

AlternateList[3] = (20011222,+500)

Simple, right? All we are doing is jumbling the order of the original list to create an

alternate path the trading system might have followed. Did you notice that the RNG

generated the number 3 twice? So AlternateList[3] is the same as AlternateList[0].

Does this seem right? This is correct and exactly what we want our RNG to do.

This is called sampling with replacement. Remember how the piece of paper was

to be put back into the bag after recording it? This method almost guarantees that

we never recreate the original trade listing. This is important because we want to

create as many unique alternative paths as we can.

Python provides, to its programmers, some very useful data structures. However,

it does not provide the array structure like other languages (you can use arrays in

Python if you import them with the NumPy library). Arrays are very powerful and

can be multidimensional and this is the structure I would have used if I had used a

different language. But Python has this really cool structure called a tuple. A tuple

is simply a collection or a list of like data. Here are examples of a classic car tuple:

antiqueCarTuple[0] = (1967, ’Ford’ , ’Mustang’ , 289)

antiqueCarTuple[1] = (1971, ‘‘Chevy’, ‘Chevelle’, 402)

As you can see each tuple element holds the various properties of a classic car.

If I need to know the model of the second antique car, I can access it by using the

following notation: modelName = antiqueCarTuple[1][2]

The first bracket following the name of the tuple selects which tuple you are

wanting to access from the tuple list. Since Python is 0 based the [1] in this example

tells the computer to select the second tuple or car. The next bracket is used

to determine which element in the tuple to access. I wanted the model, or third

element, of the antique car so I used the number 2 to access it.

In our Monte Carlo example each tuple will hold the date of the trade exit and

the associated profit or loss. Here is the very lengthy and elaborate code for creating

1000 alternate paths of our trading algorithm:

mcTradeTuple = list()

for x in range(0,1000): # number of alternate histories

for y in range(0,len(tradeTuple)):

randomTradeNum = random.randint(0,len(tradeTuple)-1)

tradeDate = tradeTuple[randomTradeNum][0]

tradePL = tradeTuple[randomTradeNum][1]

mcTradeTuple += ((x,y,tradeDate,tradePL),)

www.rasabourse.com

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!