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.

FIGURE 6.4

brackets.

A Python run-time error resulting from using parentheses instead of square

good thing is it tells the offending module and line number. Unfortunately, Python

doesn’t use line numbers, but if you go up under the Edit menu in the offending

module and select Go To Line, a small dialog will open and you can plug in the

line number and it will take you to the offending line.

Similar to any testing platform or programming language, the more you use it,

the better you will become. Here are a couple more algorithms in the PSB that

utilize market and limit orders.

If you are entering on the close, then you do not need to use a 1 offset in your

indicator calculations. Basically, you are calculating the indicator at the close and

executing at the close—in real time this is somewhat difficult, but if you want to

test it, you can. Here is a dual moving average crossover algorithm in the PSB. For

brevity’s sake, only the important code snippets were included.

200

USING PYTHON TO BACKTEST YOUR ALGORITHM

avg1 = sAverage(myClose,19,i,0)

avg2 = sAverage(myClose,39,i,0)

print(myDate[i],"avg1 ",avg1," avg2 ",avg2)

Notice how a 0 is used as the offset—I want to use today’s data in the calculation.

Since we don’t have graphing capabilities, you can print right out to the Python

Shell using the keyword print. Basically, put everything you want printed out in a

list separated by a string or a space enclosed in quotation marks and a comma. This

print statement will print out the date, the word avg1, the value of avg1, the word

avg2, and the value of valu2. Here’s what the printout looks like.

20150429 avg1 57.15842105263158 avg2 54.38384615384615

#Long Entry Logic

if (mp != 1) and avg1 > avg2:

profit = 0

price = myClose[i]

----------------

----------------

#Long Exit - Loss

www.rasabourse.com

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

Saved successfully!

Ooh no, something went wrong!