0% found this document useful (0 votes)
49 views27 pages

Implementation of MACD Trend Reversal Trading System - Specification 2023

This document provides specifications for implementing a MACD trading system using historical S&P 500 daily closing price data from 2016 to 2021. The specifications include using a 12-day and 26-day exponential moving average for the MACD calculations, an initial capital of $100,000, and buying and selling at each MACD trend reversal signal while accounting for commission costs. The assignment is to design, implement, and report on the MACD trading system, including comparing the results to a simple buy-and-hold strategy over the same period. The submission should include design documentation, source code, and a run script.

Uploaded by

Trash Bin
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
49 views27 pages

Implementation of MACD Trend Reversal Trading System - Specification 2023

This document provides specifications for implementing a MACD trading system using historical S&P 500 daily closing price data from 2016 to 2021. The specifications include using a 12-day and 26-day exponential moving average for the MACD calculations, an initial capital of $100,000, and buying and selling at each MACD trend reversal signal while accounting for commission costs. The assignment is to design, implement, and report on the MACD trading system, including comparing the results to a simple buy-and-hold strategy over the same period. The submission should include design documentation, source code, and a run script.

Uploaded by

Trash Bin
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 27

Implementation

assignment - MACD
trading system
specificaton

Introduction to Computational Thinking 1


Equities

Stock – S and P 500 daily closing price 2016 to 2021


600
Close

500

400

300

200

100

Introduction to Computational Thinking 2


MACD Model

What is a moving average?


This is the average of a time series over a specific window size which slides along as
we move along the time series.

Introduction to Computational Thinking 3


Standard Simple Moving Average

However, we are unable to have access to future value in a time


series hence we look at simple moving average using past data.

A window {t-T, t) is now used as basis of the computation of SMA. This


gives a lag to the actual movement average value. The computation with
the (t-T, t) sliding window is effectively the MA at t-T/2. Essentially this is
the arithmetic moving average computed by summing the values of the
series over the window period and averaging this using the size of the
window.

SSMA = (p(t-T) + p(t-T+1) + p(t-T+2) ... + p(t-2) + p(t-1) + p(t))/(T+1)

Introduction to Computational Thinking 4


MACD Model

To address the lag due to lack of foresight data, an exponential moving


average that weighs the more on recent data is used.

The formula for the Exponential moving average is:


EMA = (today’s closing price *K) + (Previous EMA * (1 – K))
N = number of days in EMA
K (Smoothing Factor) = 2/(N+1)

For a long EMA of 26 Days, N = 26 and for a short EMA of 12


Days, N=12.

Introduction to Computational Thinking 5


Queue – special data structure that can be used as a FIFO queue
or FILO - stack

The Rear represents the point where the items are inserted
inside the queue. The Front represents the point where the
items from the queue will be removed. This forms a FIFO queue

Python Queue: FIFO, LIFO Example (guru99.com)

Introduction to Computational Thinking 6


Queue – special data structure that can be used as a FIFO queue
or FILO - stack

FILO/LIFO queue - last in the first out queue, the element that
is entered last will be the first to come out. - STACK

Introduction to Computational Thinking 7


Sample Queue

Add and item in a queue Remove an item from the queue

Introduction to Computational Thinking 8


Standard Simple Moving Average

The MA window is shifted 1 day at a time and the MA is computed accordingly.


How to use a queue to implement the SMA computation in each window frame?

Introduction to Computational Thinking 9


Standard SMA

SSMA(t) = (p(t-T) + p(t-T+1) + p(t-T+2) ... + p(t-2) + p(t-1) + p(t))/(T+1)

For a( T+1 ) day moving average

SSMA(t+1) = (SSMA(t)*(T+1) - p(t-T) + p(t+1))/(T+1)

For a( T+1 ) day moving average

Introduction to Computational Thinking 10


What should you do first on the queue to
implement the computation of SSMA at
time (t+1)?

ⓘ Start presenting to display the poll results on this slide.


Introduction to Computational Thinking 11
EMA – Initialisation - computation

Introduction to Computational Thinking 12


EMA-D(t) computation – EMA-12/EMA26

Introduction to Computational Thinking 13


MACD Model

MACD line = S-MA – L-MA (MA can be SMA or EMA of the time series)
9-MACD-MA is the signal line (MA can be SMA or EMA of the MACD)
MACD Histogram = MACD – 9-MACD-MA (+ve = bullish and –ve =
bearish)

1) When the signal line crosses over the MACD line (That is the
histogram changes from +ve to –ve) it represents a trend reversal
turning bearish

2) When the signal line crosses under the MACD line (that is the
histogram changes from –ve to +ve) it represents a trend reversal
turning bullish
Introduction to Computational Thinking 14
MACD and MACDH

Introduction to Computational Thinking 15


MACD Model

Each trend reversal can be accompanied by a Buy for Bullish market –


condition 2 or a Sell for Bearish market – condition 1.

Each pair forms a BUY-HOLD-SELL action and incur commission loss at


the BUY and SELL position determined by the cross over of the signal
line.

In a BUY and HOLD investment, the Long position is held over the
duration of investment and no opportunistic BUY and SELL are
considered during this investment holding period. Hence only 2
commission loss are incurred in standard BUY and HOLD strategy.

Introduction to Computational Thinking 16


MACD and MACDH

Introduction to Computational Thinking 17


How effective is MACD trading during a
fully bullish market with minor market
correction?

ⓘ Start presenting to display the poll results on this slide.


Introduction to Computational Thinking 18
S and P 500

600
Close

500

400

300

200

100

Introduction to Computational Thinking 19


Implementation Specification
MACD trend reversal model using the exponential Moving average for Long
and Short can be used within the MACD trend reversal model.

Based on your design in assignment 1 implement the MACD trading model


using the S and P 500 daily closed data set provided as an excel file.

The following are to be used:


S-MA = 12 days
L-MA = 26 days
Commission = 1/8 percent per trade.
Initial capital = US$100,000.00
Consider cumulative investment at each trade where profit is reinvested in
full.
Duration of trading is over the period of the whole S and P 500 dataset
provided.
Introduction to Computational Thinking 20
Design Specification – cont’d
At each trend reversal:
1. The Signal line crossing under the MACD line - a buy signal is given
to initiate a BUY and when
2. The Signal line cross over the MACD line a sell signal is given to
initiate a SELL

Compute the BUY and SELL pair through the trading cycle with a
commission at each trade done during each of the trend reversal
BUY/SELL position. Assume all the holdings are bought or sold with all
the accumulated fund. The stock series is stored in a text or excel file.

Also in your design compare your profit/loses against a simple BUY-


HOLD-SELL position over the entire period of the trend reversal trading

Introduction to Computational Thinking 21


S and P 500

600
Close

500

400

300

200

Hold
100

Buy Sell

Introduction to Computational Thinking 22


Design Implementation – cont’d
Prepare a report 5-6 pages on the design and implementation of such a
trading system with block diagram using flow chart and pseudo code
and highlighting key functions and data structures

Readability and maintainability of your code is a key component in the


assessment

Include a script of the run of your code which allows one to select which
financial excel data file to be used – generality of code.

Introduction to Computational Thinking 23


Design Implementation – cont’d

It should include the following in the summary computed in the run :


total number of trades made using MACD
average return per trade using MACD
relative gain/loss against the long term Buy-Hold-Sell strategy
using the MACD model with SSMA and EMA models.

Optional:
If we intend to reduce unnecessary trade due to weak trend reversal
what must be added to your design? Explain this with your result
with this additional filter for unnecessary trades

Introduction to Computational Thinking 24


Submission:

Send design report to:

[email protected]

Report Filename: name_assign2.docx or pdf # report


Src_code_Filename: name_assign2_code.docx /pdf/txt
# src code – this naming avoid the check via mail
Run-Script Filename: name_assign2-script.docx or pdf.

Deadline: 24th March 2023 (3 weeks)

Introduction to Computational Thinking 25


Reading and Writing to an Excel file

How to read and write from/to a text file

https://www.pythontutorial.net/python-basics/python-read-text-file/

https://www.pythontutorial.net/python-basics/python-write-text-file/

How to read and writ from/to an Excel file

https://www.pythontutorial.net/python-basics/python-read-csv-file/

https://www.pythontutorial.net/python-basics/python-write-csv-file/

https://pythonbasics.org/read-excel/

https://pythonbasics.org/write-excel/

Introduction to Computational Thinking 26


End

Introduction to Computational Thinking 27

You might also like