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.

if mp >= 1 and myClose[i] < entryPrice[-1] - stopAmt and

barsSinceEntry > 1:

price = myClose[i]

# Short Logic

if (mp !=-1) and avg1 < avg2:

profit = 0

price = myClose[i]

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

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

# Short Exit Loss

if mp <= -1 and myClose[i] >= entryPrice[-1] +

stopAmt and barsSinceEntry > 1:

price = myClose[i]

The only problem with this logic is that the system gets right back in the next

bar after a money management exit if the moving averages are still aligned. Other

testing software might get right back in at the same price it exits if the software

allows multiple entries on a single bar. If you want to force a new crossover, then

just add these lines:

prevAvg1 = sAverage(myClose,19,i,1)

prevAvg2 = sAverage(myClose,39,i,1)

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

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

You create two new variables and assign them the moving average values using

an offset of 1. PrevAvg1 and prevAvg2 are the moving averages from yesterday.

Your long and short entries should look something like this:

#Long Entry

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

price = myClose[i]

#Short Entry

if (mp !=-1)

price = myClose[i]

and avg1 < avg2 and prevAvg1 > prevAvg2:

201

USING PYTHON TO BACKTEST YOUR ALGORITHM

Notice how the comparison of the prior day’s moving average values is

incorporated in the logic. Yesterday’s prevAvg1 must be less than yesterday’s

www.rasabourse.com

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

Saved successfully!

Ooh no, something went wrong!