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.

trades = tradeInfo('buy',myDate[i],tradeName,

entryPrice[-1],numShares,1)

barsSinceEntry = 1

totProfit += profit

listOfTrades.append(trades)

That’s all the code you have to concern yourself with. There is a lot of code, but

most off it stays the same from one algorithm to the next.

In the case of this system, all entries and exits are executed as stop orders. Since

we are dealing with stops, we must test the myHigh[i] (today’s high) against the

buyLevel. If the high of the day is greater than or equal to our buy stop, then we

can safely assume the order was filled. Did the market gap above our buyLevel—you

must test for this as well. The code is already in place: price = max(myOpen[i],

buyLevel). If the market did indeed gap above our buyLevel, then the fill price

is moved to the open of the day. This line of code is only necessary if a gap open

could impact the fill price on a stop order. The only other thing you need to change

is the tradeName variable. In this case, we change the name inside the quotes to

Boll Buy. That’s all you have to change in the code. The rest of the code is for

internal use only. If IDLE had a collapsible text feature, I would just collapse this

down so you wouldn’t have to see it. You can edit your Python code in a more

sophisticated text editor such as NotePad ++ (https://notepad-plus-plus.org/).

Figure 6.1 demonstrates the collapsible text feature.

We would all agree this is a much cleaner interface. NotePad ++ is available for

download for a small donation. The reason I didn’t use NotePad++ in this book is

because it is not exactly easy to integrate into IDLE and I wanted to keep things as

simple as possible.

197

USING PYTHON TO BACKTEST YOUR ALGORITHM

FIGURE 6.1

NotePad++’s collapsible text feature.

www.rasabourse.com

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

Saved successfully!

Ooh no, something went wrong!