The Moving Average Convergence Divergence or MACD for short is a popular stock trading indicator that is used to generate buy or sell signals. It shows the momentum that can help detection of shift in trends.
To plot MACD, I need to see the computed MACD, the signal, the histogram, zero line and the actual closing value of the ticker. See below example:
I used Pandas Technical Analysis library(because it is very simple and will make the code clean and I love taking shortcuts) to get the MACD, Signal, Histogram, Zero Lines and got the historical close value from yahoo finance. I generated the above figure using plotly.
After watching several youtube videos regarding the topic "How to Trade using MACD", the most notable video is below(but I think a lot of youtubers produced equally excellent tutorial):
From the above video, I have learned the following trading strategy:
1. A signal should be generated each time the Macd and Signal lines crosses, a sell signal is generated when the signal line is above the Macd line and a buy signal is generated when Macd line is above the signal line.
2. Observe if the market reacts to these generated signal. If the market goes the opposite direction most of the time, then don't trade using the said indicator.
3. Big waves indicates long trends that could last a few days or even years depending on the size.
4. A false signal is generated when the signal line is above the Macd line but the crossing occured below the Zero line or when the Macd line is above the signal line but the crossing occured above the zero line.
In case of Apple(APPL) as an example, it seems that MACD can be used as an indicator, see below figure:
I will soon update this post once I finish the python program to implement my trading strategy.
Here is the code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 | import pandas_ta as ta import yfinance as yf import pandas as pd import numpy as np import plotly.graph_objects as go from plotly.subplots import make_subplots df = yf.Ticker('AAPL').history(period='1y')[map(str.title, ['open', 'close', 'low', 'high', 'volume'])] df.ta.macd(close='close', fast=12, slow=26, append=True) df.columns = [x.lower() for x in df.columns] macd_plot = make_subplots(rows=3, cols=1) macd_plot.append_trace( go.Scatter( x=df.index, y=df['close'], line=dict(color='#ff9900', width=1), name='close', legendgroup='1', ), row=1, col=1 ) macd_plot.append_trace( go.Candlestick( x=df.index, open=df['open'], high=df['high'], low=df['low'], close=df['close'], increasing_line_color='green', decreasing_line_color='red', showlegend=False ), row=1, col=1 ) # (%k) macd_plot.append_trace( go.Scatter( x=df.index, y=df['macd_12_26_9'], line=dict(color='#ff9900', width=2), name='macd', legendgroup='2', ), row=2, col=1 ) # (%d) macd_plot.append_trace( go.Scatter( x=df.index, y=df['macds_12_26_9'], line=dict(color='#000000', width=2), legendgroup='2', name='signal' ), row=2, col=1 ) design = np.where(df['macdh_12_26_9'] < 0, '#000', '#ff9900') macd_plot.append_trace( go.Bar( x=df.index, y=df['macdh_12_26_9'], name='histogram', marker_color=design, ), row=2, col=1 ) layout = go.Layout( plot_bgcolor='#efefef', font_family='Monospace', font_color='#000000', font_size=20, xaxis=dict( rangeslider=dict( visible=False ) ) ) macd_plot.update_layout(layout) macd_plot.show() |
No comments:
Post a Comment