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.

portfolioClass. These classes are named equityClass and marketSystemClass.

The equityClass is used to store the daily equity for each individual market. A new

equityClass object is instantiated for each different market in the market list.

Here’s what the data containers in the equityClass class look like:

def __init__(self):

self.equityDate = list()

self.equityItm = list()

self.clsTrdEquity = list()

self.openTrdEquity = list()

self.cumuClsEquity = 0

self.dailyEquityVal = list()

self.peakEquity = 0

self.minEquity = 0

self.maxDD = 0

188

USING PYTHON TO BACKTEST YOUR ALGORITHM

After you open a trading account and start trading something happens every day to

your account. It either stays flat, goes up, or goes down. These daily fluctuations

are caused by the P/L from a closed trade or the change in market value of an open

trade. Cumulative equity reflects the growth of your account. This equityClass

keeps track of P/L from closed trades, change in daily open trade equity, and the

cumulative equity for any given time period. If you buy Apple today at $100 a share

and buy just one share, then today’s equity simply reflects the difference between

the closing price and $100. Let’s pretend Apple closed at $105, so your closed trade

equity is equal to zero, but your open trade equity (OTE) is $5. Today’s equity in

your account is $5. Let’s pretend a little further and say Apple moves up to $110,

and you decide to cover your long position. Today’s equity is derived from a closed

trade profit of $10. Since there is no open position, then there is no OTE. Tracking

your two-day performance, you show daily equity of $5 for yesterday and $10 for

today. Your cumulative equity stands at $10. Feeling a little more bullish, you buy

Apple again on the third day at $112, but unfortunately, some bad news comes out

of China and the stock drops back to $105. So today’s equity reflects the cumulative

closed out equity $10 and today’s OTE that stands at −$7. Your cumulative equity

drops by $7 and now stands at $3. The next day Apple drops further and you start

feeling bearish so you sell short at $100. Unfortunately, China states their GDP is

growing a little faster than the analysts predicted and Apple rebounds and closes

at $110. What is your cumulative equity now?

Day 1 : Cum.E ($5) = Cum.Cls.TE (0) + OpenTE (5)

Day 2 : Cum.E ($10) = Cum.Cls.TE (10) + OpenTE (0)

Day 3 : Cum.E ($3) = Cum.Cls.TE(10) + OpenTE(−7)

Day4:Cum.E(−$12) = Cum.Cls.TE(−2) + OpenTE(−10)

www.rasabourse.com

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

Saved successfully!

Ooh no, something went wrong!