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 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
| import pandas as pd import talib import plotly.graph_objects as go import numpy as np from datetime import datetime, timedelta
# 1. 数据准备(假设df是包含日期索引和OHLC数据的DataFrame) # 如果已有数据,请替换以下示例数据 # def generate_sample_data(days=100): # dates = pd.date_range(end=datetime.today(), periods=days) # open_prices = np.random.uniform(3500, 4000, days) # high_prices = open_prices + np.random.uniform(10, 50, days) # low_prices = open_prices - np.random.uniform(10, 80, days) # 模拟长下影线 # close_prices = np.random.uniform(np.minimum(open_prices, low_prices), # np.maximum(open_prices, high_prices)) # return pd.DataFrame({ # 'Open': open_prices, # 'High': high_prices, # 'Low': low_prices, # 'Close': close_prices # }, index=dates)
# # 生成示例数据(实际使用时替换为你的DataFrame) # df = generate_sample_data()
# 2. 使用TA-Lib识别锤头形态[1,2,4](@ref) signals1 = talib.CDLHAMMER(df['开盘价'], df['最高价'], df['最低价'], df['收盘价'])
# 2. 创建包含信号的DataFrame df = pd.DataFrame({ 'Open': df['开盘价'], 'High': df['最高价'], 'Low': df['最低价'], 'Close': df['收盘价'], 'hammer_signals': signals1 })
# 3. 创建信号标记数据 signals = df['hammer_signals'] > 0 # >0表示识别到锤头形态 print(f"识别到{len(signals)}个锤头形态")
# 4. 创建Plotly K线图 fig = go.Figure()
# 添加K线主图 fig.add_trace(go.Candlestick( x=df.index, open=df['Open'], high=df['High'], low=df['Low'], close=df['Close'], increasing_line_color='red', # 根据中国习惯设置涨跌颜色 decreasing_line_color='green', name='K线' ))
# 5. 标注锤头形态 if not signals.empty: fig.add_trace(go.Scatter( x=df.index[signals], y=df['Low'][signals] * 0.995, # 在最低价下方显示标记 mode='markers', marker=dict( symbol='triangle-up', size=15, color='gold', line=dict(width=2, color='black') ), name='锤头形态', hoverinfo='text', hovertext=[f"日期:{date.strftime('%Y-%m-%d')}<br>最低价:{low:.2f}" for date, low in zip(df.index[signals], df['Low'][signals])] ))
# 6. 添加技术指标增强分析(可选) df['MA20'] = talib.MA(df['Close'], timeperiod=20) # 20日均线 fig.add_trace(go.Scatter( x=df.index, y=df['MA20'], line=dict(color='blue', width=1.5), name='20日均线' ))
# 7. 专业级图表布局配置[7](@ref) fig.update_layout( title='期货K线图 - 锤头形态识别', xaxis_title='日期', yaxis_title='价格', xaxis_rangeslider_visible=False, # 隐藏底部滑动条 template='plotly_dark', height=700, hovermode='x unified', legend=dict(orientation='h', yanchor='bottom', y=1.02, xanchor='right', x=1), annotations=[dict( x=0.5, y=-0.12, xref='paper', yref='paper', showarrow=False, text='数据来源:期货行情数据 | 技术指标:TA-Lib CDLHAMMER' )] )
# 添加专业网格和样式 fig.update_xaxes( gridcolor='rgba(100, 100, 100, 0.2)', rangeslider_thickness=0.03 ) fig.update_yaxes( gridcolor='rgba(100, 100, 100, 0.2)' )
# 8. 显示图表 fig.show()
# 9. 输出信号明细(可选) if not signals.empty: print("\n锤头形态出现日期:") print(df.index[signals].strftime("%Y-%m-%d").values)
|