change something

This commit is contained in:
jackyu66git
2025-04-27 18:37:50 +08:00
parent 88701608bd
commit fceb57d2b8
10 changed files with 285 additions and 162 deletions
+39 -4
View File
@@ -12,7 +12,7 @@ import base64
import time
import traceback
from pytz import timezone
import talib.abstract as ta
# 添加父目录到系统路径
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
@@ -142,6 +142,7 @@ def get_kl_data(symbol, timeframe, limit=1000, start_time=None, end_time=None):
ret_df.loc[klc_index] = df_copy.loc[index]
klc_index += 1
"""
df = add_indicators(df)
# 如果过滤后没有数据,返回None
if len(df) == 0:
print("过滤后无数据")
@@ -154,7 +155,35 @@ def get_kl_data(symbol, timeframe, limit=1000, start_time=None, end_time=None):
print(f"获取数据错误: {e}")
traceback.print_exc()
return None
def add_indicators(df):
fast = 8
slow = 16
period = 6
macd = ta.MACD(df, fastperiod=fast, slowperiod=slow, signalperiod=period)
df['macd'] = macd['macd']
df['macdsignal'] = macd['macdsignal']
df['macdhist'] = macd['macdhist']
df['ma5'] = (ta.MA(df, timeperiod=5)).fillna(0)
df['ma10'] = (ta.MA(df, timeperiod=10)).fillna(0)
df['ma30'] = (ta.EMA(df, timeperiod=30)).fillna(0)
df['ma250'] = (ta.MA(df, timeperiod=250)).fillna(0)
df['rsi'] = ta.RSI(df, timeperiod=14)
df['macd'] = df['macd'].fillna(0)
df['macdsignal'] = df['macdsignal'].fillna(0)
df['macdhist'] = df['macdhist'].fillna(0)
df['ma5'] = df['ma5'].fillna(0)
df['ma10'] = df['ma10'].fillna(0)
df['ma30'] = df['ma30'].fillna(0)
df['ma250'] = df['ma250'].fillna(0)
df['rsi'] = df['rsi'].fillna(0)
df['avg_volume'] = df['volume'].rolling(10).mean()
# 计算量比
df['volume_ratio'] = df['volume'] / df['avg_volume']
# 填充缺失值(前N根K线)
df['volume_ratio'] = df['volume_ratio'].fillna(1.0)
df['avg_volume'] = df['avg_volume'].fillna(0)
return df
def calculate_macd(df):
"""计算MACD指标"""
exp1 = df['close'].ewm(span=12, adjust=False).mean()
@@ -182,7 +211,11 @@ def analyze_chan(df):
zs_list = chan.calculate_zs(bi_list, seg_list)
# 添加买卖点识别
buy_sell_points = identify_trade_points(bi_list, seg_list, zs_list)
for bi in bi_list:
bi.cal_macdhist()
for bi in bi_list:
bi.cal_macd_div()
#print(bi.start_time, bi.macd_hist, bi.macd_div)
# 提取K线分型信息
klc_fx_info = []
for klc in klc_list:
@@ -411,7 +444,8 @@ def analyze():
'end_time': (bi.end_klc.end_time if isinstance(bi.end_klc.end_time, str) else bi.end_klc.end_time.astimezone(client_tz).isoformat()) if bi.end_klc else None,
'start_price': bi.start_klc.low if convert_direction(bi.dir) == 1 else bi.start_klc.high,
'end_price': bi.end_klc.high if convert_direction(bi.dir) == 1 else bi.end_klc.low if bi.end_klc else None,
'direction': convert_direction(bi.dir)
'direction': convert_direction(bi.dir),
'macd_div': float(bi.macd_div) if hasattr(bi, 'macd_div') else 0
} for bi in analysis_result['bi_list'] if bi.end_klc],
'seg_list': [{
'start_time': seg.start_bi.start_klc.end_time if isinstance(seg.start_bi.start_klc.end_time, str) else seg.start_bi.start_klc.end_time.astimezone(client_tz).isoformat(),
@@ -470,7 +504,8 @@ def analyze():
'end_time': (bi.end_klc.end_time if isinstance(bi.end_klc.end_time, str) else bi.end_klc.end_time.astimezone(client_tz).isoformat()) if bi.end_klc else None,
'start_price': bi.start_klc.low if convert_direction(bi.dir) == 1 else bi.start_klc.high,
'end_price': bi.end_klc.high if convert_direction(bi.dir) == 1 else bi.end_klc.low if bi.end_klc else None,
'direction': convert_direction(bi.dir)
'direction': convert_direction(bi.dir),
'macd_div': float(bi.macd_div) if hasattr(bi, 'macd_div') else 0
} for bi in element_analysis['bi_list'] if bi.end_klc]
# 添加小周期K线数据
+72 -2
View File
@@ -442,6 +442,7 @@
<th>起始价格</th>
<th>结束价格</th>
<th>方向</th>
<th>MACD背离值</th>
</tr>
</thead>
<tbody></tbody>
@@ -1553,6 +1554,39 @@
color: bi.direction === 1 ? '#dc3545' : '#28a745',
lineWidth: 1
});
// 在笔的末端添加macd_div值标记
if (bi.macd_div && bi.macd_div !== 0) {
console.log(`添加macd_div标记: ${bi.macd_div.toFixed(2)}, 在时间点: ${endTime}`);
const macdDivLabel = mainChart.addLineSeries({
lastValueVisible: false,
priceLineVisible: false,
});
// 确定标记位置
const markerPosition = bi.direction === 1 ? 'aboveBar' : 'belowBar';
const labelValue = bi.direction === 1 ? endPrice + (endPrice * 0.005) : endPrice - (endPrice * 0.005);
const arrayShape = bi.direction === 1 ? 'arrowDown' : 'arrowUp';
// 简化标记显示
macdDivLabel.setData([
{ time: endTime, value: labelValue }
]);
// 使用不同方式添加文本标记
const textColor = bi.macd_div > 0 ? '#dc3545' : '#28a745';
const textSize = Math.min(14, Math.max(10, Math.abs(bi.macd_div) * 2));
macdDivLabel.setMarkers([
{
time: endTime,
position: markerPosition,
color: textColor,
shape: arrayShape,
text: bi.macd_div.toFixed(2),
}
]);
}
} catch (e) {
console.error('主周期笔处理出错:', e);
}
@@ -1599,6 +1633,40 @@
color: bi.direction === 1 ? '#9c27b0' : '#673ab7',
lineWidth: 1
});
// 在笔的末端添加macd_div值标记
if (bi.macd_div && bi.macd_div !== 0) {
console.log(`添加元素周期macd_div标记: ${bi.macd_div.toFixed(2)}, 在时间点: ${endTime}`);
const macdDivLabel = mainChart.addLineSeries({
lastValueVisible: false,
priceLineVisible: false,
});
// 确定标记位置
const markerPosition = bi.direction === 1 ? 'aboveBar' : 'belowBar';
const labelValue = bi.direction === 1 ? endPrice + (endPrice * 0.005) : endPrice - (endPrice * 0.005);
const arrayShape = bi.direction === 1 ? 'arrowDown' : 'arrowUp';
// 简化标记显示
macdDivLabel.setData([
{ time: endTime, value: labelValue }
]);
// 使用不同方式添加文本标记
const textColor = bi.macd_div > 0 ? '#9c27b0' : '#673ab7';
const textSize = Math.min(14, Math.max(10, Math.abs(bi.macd_div) * 2));
macdDivLabel.setMarkers([
{
time: endTime,
position: markerPosition,
color: textColor,
shape: arrayShape,
text: bi.macd_div.toFixed(2),
transparent: true,
}
]);
}
} catch (e) {
console.error('次周期笔处理出错:', e);
}
@@ -3153,7 +3221,8 @@
{ data: 'end_time', render: formatTime },
{ data: 'start_price', render: formatPrice },
{ data: 'end_price', render: formatPrice },
{ data: 'direction', render: formatDirection }
{ data: 'direction', render: formatDirection },
{ data: 'macd_div', render: formatMacdValue }
]
});
@@ -3633,7 +3702,8 @@
{ data: 'end_time', render: formatTime },
{ data: 'start_price', render: formatPrice },
{ data: 'end_price', render: formatPrice },
{ data: 'direction', render: formatDirection }
{ data: 'direction', render: formatDirection },
{ data: 'macd_div', render: formatMacdValue }
]
});