Skip to main content

Financial Data Analysis IX Simulation of gains arising from stock trading with MACD indicator buy-sell signals

· 2 Minutes to read
Allen Ma

Case (3) Simple financial data analysis

Project 3: Calculate the return generated by trading stocks with the MACD indicator buy-sell signal over a one-year period

A program is designed to calculate the return generated by trading stocks with the MACD indicator buy and sell signals over a period of one year. The MACD trading signals are: a fast line crossing the slow line from bottom to top is a buy signal for that day, and a fast line crossing the slow line from top to bottom is a sell signal for that day. Assume that the buy and sell prices are the closing prices on the day of the trade signal.

# -*- coding: utf-8 -*-
"""
Created on Sun Sept 20 9:04:59 2020

@author: mly
"""
import numpy as np
import datetime
import pandas_datareader.data as web

start = datetime.datetime(2018, 6, 1)
end = datetime.datetime.today()
stock_name = '601318.ss'
df = web.DataReader(stock_name, 'yahoo', start, end)


def df_EMA(prices, N):
ema = []
k = len(prices)
if k > 0:
for i in range(k):
if i == 0:
ema.append(prices[i])
else:
ema.append((2 * prices[i] + (N - 1) * ema[i - 1]) / (N + 1))
return (ema)


def df_MACD(df, short=12, long=26, M=9):
fast = df_EMA(df['Adj Close'].values, short)
slow = df_EMA(df['Adj Close'].values, long)
if len(fast) > 0: # & len(slow)>0:
df['Fast'] = np.round(np.array(fast), 2)
df['Slow'] = np.round(np.array(slow), 2)
df['DIF'] = df['Fast'] - df['Slow']
df['DEA'] = np.round(np.array(df_EMA(df['DIF'].values, M)), 2)
df['MACD'] = 2 * (df['DIF'] - df['DEA'])
df['tim'] = df['Close'] / df['Open']
return (df)
else:
print('no data,no MACD')


times0 = 1
marker = 0
df_MACD(df, 12, 26, 9)
for i in df.itertuples(index=True, name='df'):
if getattr(i, 'DIF') > 0 and marker == 0:
times0 = times0 * getattr(i, 'tim')
marker = 1
elif getattr(i, 'DIF') > 0 and marker == 1:
times0 = times0 * getattr(i, 'tim')
marker = 1
elif getattr(i, 'DIF') == 0 and marker == 1:
times0 = times0 * getattr(i, 'tim')
marker = 0
elif getattr(i, 'DIF') < 0 and marker == 1:
times0 = times0 * getattr(i, 'tim')
marker = 0
else:
continue

print(times0)

Results of the run.

在这里插入图片描述 It seems that historically if you follow this method of buying shares, at least you won't lose qwq,23333333333...