python stock quantitative trading (8) ---K-line chart: hammerhead, shooting star, morning star

A person’s ambition is like a tree with roots. To set this ambition, one must think of modesty and convenience, which naturally touches the world, and the blessing is for me.

Hammer

This article continues the previous article to introduce the candlestick pattern.

First of all, the first K-line pattern we introduce today is the hammer. The method provided by the TA-Lib library is talib.CDLHAMMER(), which is a one-day K-line pattern with short entities and no upper shadows. At the same time The lower shadow is more than twice the length of the entity, indicating a trend reversal.

The complete code for drawing the marking hammer is as follows:

import pandas as pd
import talib
import matplotlib.pyplot as plt
import matplotlib.ticker as ticker
import mpl_finance as mpf
fig = plt.figure(figsize=(12, 8))
plt.rcParams['font.sans-serif'] = ['SimHei']
ax = fig.add_subplot(111)
df = pd.read_excel("歌尔股份year.xlsx")
df['date'] = pd.to_datetime(df['date'])
df['date'] = df['date'].apply(lambda x: x.strftime('%Y-%m-%d'))
df['hammer_head'] = talib.CDLHAMMER(df['open'].values, df['high'].values, df['low'].values, df['close'].values)

pattern = df[(df['hammer_head'] == 100) | (df['hammer_head'] == -100)]
mpf.candlestick2_ochl(ax, df["open"], df["close"], df["high"], df["low"], width=0.6, colorup='r',
                          colordown='green',
                          alpha=1.0)
for key, val in df.items():
    for index, today in pattern.iterrows():
        x_posit = df.index.get_loc(index)
        ax.annotate("{}\n{}".format("锤头", today["date"]), xy=(x_posit, today["high"]),
                    xytext=(0, pattern["close"].mean()), xycoords="data",
                    fontsize=18, textcoords="offset points", arrowprops=dict(arrowstyle="simple", color="r"))


ax.xaxis.set_major_locator(ticker.MaxNLocator(20))

def format_date(x, pos=None):
    if x < 0 or x > len(df['date']) - 1:
        return ''
    return df['date'][int(x)]


ax.xaxis.set_major_formatter(ticker.FuncFormatter(format_date))
plt.setp(plt.gca().get_xticklabels(), rotation=45, horizontalalignment='right')
plt.show()

There are many stocks in the form of hammerheads. You can find stocks to test at will. Goertek shares are still used here. After running, the display effect is as shown in the figure below:
Insert picture description here

Inverted hammer

Since there is a positive hammer, there must be an inverted hammer. The method provided by the TA-Lib library is talib.CDLINVERTEDHAMMER(), which is also a one-day candlestick, which is defined as the upper shadow line is longer, and the length is twice that of the entity Above, there is no lower shadow, and at the bottom of the downtrend, it indicates a trend reversal.

Since the above complete code is available, here is the same as the previous blog post, only two lines of code need to be replaced:

df['Inverted_hammer_head'] = talib.CDLHAMMER(df['open'].values, df['high'].values, df['low'].values, df['close'].values)

pattern = df[(df['Inverted_hammer_head'] == 100) | (df['Inverted_hammer_head'] == -100)]

At the same time, change the text mark "hammer head" to "inverted hammer head", the effect after operation is as shown in the figure below:
Insert picture description here

Shooting star

Shooting Star is a one-day K-line mode, defined as the upper shadow line is at least twice the length of the entity, and there is no lower shadow line, which indicates that the stock will fall. The method provided to us by the TA-Lib library is talib.CDLSHOOTINGSTAR().

Similarly, the code of Shooting Star only needs to replace 2 lines:

df['shoot_star'] = talib.CDLSHOOTINGSTAR(df['open'].values, df['high'].values, df['low'].values, df['close'].values)

pattern = df[(df['shoot_star'] == 100) | (df['shoot_star'] == -100)]

After running, the displayed effect is as shown in the figure below:
Insert picture description here

Morning star

The morning star is a three-day K-line pattern, which is defined as a downtrend. The first day is a Yinxian, the second day's price amplitude is small, and the third day's Yangxian indicates that there may be a reversal at the bottom. The method provided to us by the TA-Lib library is talib.CDLMORNINGSTAR().

Similarly, the code of Morning Star only needs to replace 2 lines:

df['morning_star'] = talib.CDLMORNINGSTAR(df['open'].values, df['high'].values, df['low'].values, df['close'].values)

pattern = df[(df['morning_star'] == 100) | (df['morning_star'] == -100)]

After running, the displayed effect is as shown in the figure below:
Insert picture description here
Of course, there are actually many indicators for the K-line chart, such as the hanging line method CDLHANGINGMAN, the inverted T cross CDLGRAVESTONEDOJI, the up/down gap and the parallel Yang line CDLGAPSIDESIDEWHITE, etc., the use method is the same as this 2 The blog posts are exactly the same, except that the method is replaced. Therefore, in order not to explain the quantitative trading of unnutritious stocks, other indicator bloggers are omitted here. For specific indicators you need, you can query the development documents yourself and directly follow these two calls.

Guess you like

Origin blog.csdn.net/liyuanjinglyj/article/details/113426867
Recommended