所有的形态函数,大部分可以在股票软件中找到,通过Python的talib实现形态识别,可以和股票软件对比一下。
看一下Pattern Recognition有多少函数:
for i in tlb.get_function_groups():
print (i,len(tlb.get_function_groups()[i]))
print (tlb.get_function_groups()[i])
print ()
Pattern Recognition 61
[‘CDL2CROWS’, ‘CDL3BLACKCROWS’, ‘CDL3INSIDE’, ‘CDL3LINESTRIKE’, ‘CDL3OUTSIDE’, ‘CDL3STARSINSOUTH’, ‘CDL3WHITESOLDIERS’, ‘CDLABANDONEDBABY’, ‘CDLADVANCEBLOCK’, ‘CDLBELTHOLD’, ‘CDLBREAKAWAY’, ‘CDLCLOSINGMARUBOZU’, ‘CDLCONCEALBABYSWALL’, ‘CDLCOUNTERATTACK’, ‘CDLDARKCLOUDCOVER’, ‘CDLDOJI’, ‘CDLDOJISTAR’, ‘CDLDRAGONFLYDOJI’, ‘CDLENGULFING’, ‘CDLEVENINGDOJISTAR’, ‘CDLEVENINGSTAR’, ‘CDLGAPSIDESIDEWHITE’, ‘CDLGRAVESTONEDOJI’, ‘CDLHAMMER’, ‘CDLHANGINGMAN’, ‘CDLHARAMI’, ‘CDLHARAMICROSS’, ‘CDLHIGHWAVE’, ‘CDLHIKKAKE’, ‘CDLHIKKAKEMOD’, ‘CDLHOMINGPIGEON’, ‘CDLIDENTICAL3CROWS’, ‘CDLINNECK’, ‘CDLINVERTEDHAMMER’, ‘CDLKICKING’, ‘CDLKICKINGBYLENGTH’, ‘CDLLADDERBOTTOM’, ‘CDLLONGLEGGEDDOJI’, ‘CDLLONGLINE’, ‘CDLMARUBOZU’, ‘CDLMATCHINGLOW’, ‘CDLMATHOLD’, ‘CDLMORNINGDOJISTAR’, ‘CDLMORNINGSTAR’, ‘CDLONNECK’, ‘CDLPIERCING’, ‘CDLRICKSHAWMAN’, ‘CDLRISEFALL3METHODS’, ‘CDLSEPARATINGLINES’, ‘CDLSHOOTINGSTAR’, ‘CDLSHORTLINE’, ‘CDLSPINNINGTOP’, ‘CDLSTALLEDPATTERN’, ‘CDLSTICKSANDWICH’, ‘CDLTAKURI’, ‘CDLTASUKIGAP’, ‘CDLTHRUSTING’, ‘CDLTRISTAR’, ‘CDLUNIQUE3RIVER’, ‘CDLUPSIDEGAP2CROWS’, ‘CDLXSIDEGAP3METHODS’]
一共61个函数,数量大,逐一测试验证工作量不小,计划每个小节10个函数,循序渐进。
为了能清楚展示各类K线形态的效果,使用mplfinance 绘制蜡烛图。
数据周期选择从2000年到2022年,23年的数据,希望能够覆盖各类K线形态。
# -*- coding: utf-8 -*-
import numpy as np
import talib as tlb
import matplotlib.pyplot as plt
import pandas as pd
from sqlalchemy import create_engine
import mplfinance as mpf
import datetime
if __name__ == '__main__':
#matplotlib作图设置
plt.rcParams['font.sans-serif'] = ['SimHei'] # 用来正常显示中文标签
plt.rcParams['axes.unicode_minus'] = False # 用来正常显示负号
#数据获取,为了能取到各类形态,所以选择时间周期大一点
start_date = '2000-01-01'
end_date = '2023-01-01'
#还是以000002,深证指数为测试目标
df = get_data('000002', start_date, end_date)
df_index = get_index('399001', start_date, end_date)