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

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

That’s it! These seven lines of code is all it took to create 1000 different and mostly

unique alternate trade histories. I won’t bore you with all of the details, but I will

step through the code quickly to demonstrate how easy this was to do with Python.

Initially I stored all of the trade dates and trade P/L in a tuple named tradeTuple.

From this list I culled all of the trades to fill up the 1000 alternate lists. Basically I

started with the source list and for each trade in the list I created a random number

between 0 and the number of trades:

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

Python generates random numbers through its module: random.randint. I then

used the randomTradeNum as an index into the tradeTuple to extract the

tradeDate and tradePL:

tradeDate = tradeTuple[randomTradeNum][0]

tradePL = tradeTuple[randomTradeNum][1]

Remember the first bracketed number following the tuple selects which tuple.

The second bracketed number selects the element in that particular tuple. In the

tradeTuple, [0] is the trade date and [1] is the trade P/L. While stepping through

the alternate histories sequentially, trades were randomly selected from the original

trade history and inserted. All of the alternate histories were then stored in a list

of tuples named mcTradeTuple. The += operand simply appends the current

alternate trade history to the list. In the end, you have one huge list that includes all

of the alternate histories.

AmiBroker’s Monte Carlo Simulation The Python Monte Carlo simulation is

included in the Python System Backtester. I programmed the simulation because I

wanted to fully understand the mechanism. Fortunately, you don’t need to program

these types of tools because they are usually included in your favorite testing platform.

This is the case for AmiBroker and TradeStation. AmiBroker’s Monte Carlo analysis

is so simple all you have to do is flip a switch. Click the Settings button in an Analysis

window and hit the Monte Carlo tab (see Figure 8.9). You have seen this dialog before

but this is the first time we have discussed the Monte Carlo settings. The default values

are the most popular. The position sizing parameters offers four different options:

(1) keep the sizing the same as the initial test, (2) use a fixed size of contracts or shares,

(3) use a fixed dollar value for size computation, and (4) use a percentage of equity in

fixed fractional approach. The fourth option can introduce serial dependency into the

Monte Carlo simulation due to the fact that the size of the current trade is dependent

upon the success of all of the prior trades. Utilizing this option might muddy the

waters a bit when trying to determine algorithm robustness. Before we move on

let’s discuss the idea of serial correlation among the trades of a trading algorithm.

This concept basically implies there is a relationship or connection between a trade

and the subsequent trade or trades. Critics of Monte Carlo simulation on actual

261

GENETIC OPTIMIZATION, WALK FORWARD

www.rasabourse.com

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

Saved successfully!

Ooh no, something went wrong!