Python stock quantitative trading (7) ---K-line chart: crow, dark cloud topping, doji

Follow the conditions to help the people, the kind is to the complicated, and the outline is about ten: first, be kind to others; second, love and respect; third, the beauty of adults; fourth, persuade others to be kind; fifth, save others is critical; Sixth, to build great profits; seventh, to sacrifice money for blessing; eighth, to protect the righteousness; ninth, to respect and respect the long; tenth, to cherish fate.

Preface

In stock trading, the graphs we use the longest are actually K-line graphs. For example, what dojis, what crows, etc. are commonly used reference indicators. These patterns play a very important role in judging the market trend. . Therefore, this article will explain in detail the various forms of K-line recognition by the TA-Lib library.

Two crows

First of all, we will introduce the simplest K-line pattern: Two Crows, which is based on the 3-day K-line, the first day is Changyang, the second day is high, the second day is high, the second day is high and the third day is high again. At the same time, the closing price is lower than the previous day's closing price. The appearance of this pattern indicates that the stock price will fall.

To determine the shape of two crows, we use the talib.CDL2CROWS() method of the TA-Lib library. Next, let's get the candlestick pattern that marks two crows through a stock. The specific code 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['tow_crows'] = talib.CDL2CROWS(df['open'].values, df['high'].values, df['low'].values, df['close'].values)

pattern = df[(df['tow_crows'] == 100) | (df['tow_crows'] == -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()

After running, the displayed effect is as shown in the figure below:
Insert picture description here
Of course, this figure is a K-line chart of GoerTek. Since the two crows are in the form of rising, hahaha, this may be a great irony for people who look at technical indicators in the stock market. . Here, the blogger just teaches everyone to use the tools. If the tools are accurate, there will be no loss-making people. To make a digression, in fact, bloggers do not look at indicators. I teach you to read indicators here. I always feel that I have taken you to the pit.

Three crows

There are not only two crows in our candlestick chart, but also three. The definition of Three Black Crows also looks at the three-day K-line, which means that there are three consecutive Yin-lines, and the daily closing price has fallen and is close to the lowest price. At the same time, the daily opening price is within the entity of the previous K-line. , Which also indicates a decline in stock prices.

The TA-Lib library provides us with a method for judging the three crows as talib.CDL3BLACKCROWS(). To use the method, you only need to replace the crow method with CDL3BLACKCROWS, as follows:

df['three_crows'] = talib.CDL3BLACKCROWS(df['open'].values, df['high'].values, df['low'].values, df['close'].values)
pattern = df[(df['three_crows'] == 100) | (df['three_crows'] == -100)]

At present, the marked text "and only crows" has been changed to "three crows". After running, the displayed effect is as shown in the figure below: It
Insert picture description here
should be noted that the stock acquisition here is changed to sz000789 million young, because most stocks rarely have the three-crow pattern, and the blogger has tested 20 stocks before finding a three-crow pattern Of stocks.

Dark clouds

TA-Lib library provides us with the calculation method of dark cloud pressure top pattern: talib.CDLDARKCLOUDCOVER(). It is a two-day K-line indicator, defined as Changyang on the first day, the opening price of the second day is higher than the highest price of the previous day, and the closing price is below the center of the entity the previous day, indicating a decline in stocks.

The same here also only needs to replace the method, just change two lines of code:

df['dark_cloud'] = talib.CDLDARKCLOUDCOVER(df['open'].values, df['high'].values, df['low'].values, df['close'].values)

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

Similarly, you need to replace the marked text "three crows" with "black clouds over the top". After running, the displayed effect is as shown in the following figure:
Insert picture description here
The test here is replaced with Goertek shares, the previous Wannianqing did not have this form in recent years.

Doji

The doji method provided by the TA-Lib library is talib.CDLDOJISTAR(). It is a one-day candlestick pattern, defined as the opening price and closing price are basically equal, and the upper and lower shadow lines will not be very long, indicating the current trend reversal.

The same here also only needs to replace the method, just change two lines of code:

df['star'] = talib.CDLDOJISTAR(df['open'].values, df['high'].values, df['low'].values, df['close'].values)

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

Goertek is also used for drawing here, and the above text "Dark Clouds Pressing on the Top" should be changed to "Doji Star". After running, the displayed effect is as shown in the figure below:
Insert picture description here

Guess you like

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