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.

Shorting occurs when the low of the day exceeds a 60-day two-deviation lower

band. This line of code provides exactly what we need.

buyLevel,shortLevel,exitLevel = bollingerBands(myDate,

myClose,60,2,i,1)

196

USING PYTHON TO BACKTEST YOUR ALGORITHM

Prior to actually programming your algorithm, it is important to know how your

algorithm enters trades: on a stop, market, or on a limit. Unlike AmiBroker, the

ESB, or TradeStation, it is up to the user to program the different if constructs that

pertain to the different types of orders. Don’t worry; it’s not nearly as complicated

as it sounds. The buyLevel, shortLevel, andexitLevel are all calculated by the

BollingerBands function. This algorithm also utilizes a $3,000 protective stop.

Prepared with this information we are now ready to enter battle. With the PSB

you must determine if the daily market action met the requirements for trade entry

and/or exit. In other words, you have to test the bar’s extreme prices to see if an entry

or exit level was exceeded. You might want to follow along in the code box as you

read the following instructions. First, I have to make sure I am not already in a long

position (mp—market position not equal to 1), and then I look at today’s high; I am

peeking into the future here, to see if the market would have exceeded or equaled

the buyLevel. If it did, then I know I should have entered a long position. With

this method of backtesting, you tell the computer a trade has taken place. In other

platforms, the computer tells you a trade has taken place. Once you determine a trade

should have been executed, you only need to change a little bit of code in the Long

Entry Logic. The code that needs to be changed is bolded in the following code listing.

if (mp != 1) and myHigh[i] >= buyLevel:

profit = 0

price = max(myOpen[i],buyLevel)

if mp <= -1:

profit,trades,curShares = exitPos(price,myDate[i],

"RevShrtLiq",curShares)

listOfTrades.append(trades)

mp = 0

todaysCTE = profit

tradeName = "Boll Buy"

mp += 1

marketPosition[i] = mp

numShares = 1

entryPrice.append(price)

entryQuant.append(numShares)

curShares = curShares + numShares

www.rasabourse.com

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

Saved successfully!

Ooh no, something went wrong!