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.

is the length of the calculation. You will again notice the last two arguments are

the same for all of the function calls: i and 1. The i will never change but you

can change the last argument. If you were to use 2, then the indicator calculation

would be offset by two days. Instead of starting yesterday and going back 10 days,

it would start the day before yesterday and go back 10 days.

RSI. This function returns the Relative Strength Index from the last 14 days of

data. This function requires the data array and the length of the RSI calculation.

As it is programmed in this example, it is utilizing the closing prices for its

calculations. You can pass it high or low or volume data—it doesn’t matter.

Stochastic. This subroutine returns K, D, and Slow D. Here is another example of

using a subroutine in place of a function. Just like the BollingerBand subroutine,

this subroutine returns three values. It requires the length of the K calculation

and the two additional smoothing lengths for the D and Slow D.

Examples of all the indicator functions and subroutines are included in Appendix B.

After you get your indicator values, the real fun begins. The most important

code in the ESB can be found in the Trade subroutine. This subroutine determines

if a trade takes place and then stores the information for later use. The subroutine

requires five arguments (see Figure 5.13).

The first argument informs the subroutine what you are trying to do and can take

on four different values: Buy, Sell, ExitLong, ExitShort. The second argument

is the name of the signal that will show up in the trade listing. It is a string so it needs

to be included in quotes. In this example, the signal name is ‘‘BB-Buy.’’ It can be

anything you want. The third argument is the price you would like to execute the

order. Here we are trying to Buy on the penetration of the Upper Bollinger Band.

The fourth argument informs the subroutine what type of order is being placed. It

can take on four types of orders:

stp—Stop order. The price of the order must be above the market for Buy and

ExitShort or below the market for Sell and ExitLong.

lmt—Limit order. The price of the order must be below the market for Buy

and ExitShort or above the market for Sell and ExitLong.

161

USING MICROSOFT EXCEL TO BACKTEST YOUR ALGORITHM

FIGURE 5.13 This tiny snippet of code contains five arguments, which are all necessary for the

Trade subroutine.

www.rasabourse.com

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

Saved successfully!

Ooh no, something went wrong!