From e1bda0a0fa7002bf312813aa41510fd26a2e5046 Mon Sep 17 00:00:00 2001 From: aszerW Date: Sat, 18 Oct 2025 18:56:40 +0800 Subject: [PATCH] =?UTF-8?q?=E6=A0=B9=E6=8D=AEperiod=E9=9A=8F=E6=9C=BA?= =?UTF-8?q?=E7=94=9F=E6=88=90=E9=A2=9C=E8=89=B2=EF=BC=9B=E6=B7=BB=E5=8A=A0?= =?UTF-8?q?=E4=B9=B0=E5=8D=96=E4=BF=A1=E5=8F=B7markers?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- chart.py | 78 +++++++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 66 insertions(+), 12 deletions(-) diff --git a/chart.py b/chart.py index 9b9d159..4a2f86b 100644 --- a/chart.py +++ b/chart.py @@ -5,18 +5,32 @@ import talib as ta from lightweight_charts import Chart +import random + + +def get_fixed_color_based_on_period(period): + # 使用周期值作为随机种子,确保相同周期生成相同颜色 + random.seed(period) + + # 生成随机的RGB值 + r = random.randint(0, 255) + g = random.randint(0, 255) + b = random.randint(0, 255) + + # 将RGB值转换为十六进制颜色代码 + color_code = "#{:02x}{:02x}{:02x}".format(r, g, b) + + # 重置随机种子,避免影响其他随机操作 + random.seed(None) + + return color_code + + def add_ema(df, chart, period: int = 50): - ema = ta.EMA(df["close"], timeperiod=period) name = f"EMA {period}" - df[name] = ema - PERIOD_COLORS = { - 10: "#E42C2A", - 20: "#E49D30", - 30: "#3CFF0A", - 60: "#3275E4", - 200: "#B06CE3", - } - line = chart.create_line(name, color=PERIOD_COLORS[period], width=2) + df[name] = ta.EMA(df["close"], timeperiod=period) + color = get_fixed_color_based_on_period(period) + line = chart.create_line(name, color=color, width=2) line.set(df[["time", name]]) @@ -114,6 +128,38 @@ def add_TD(df, chart): chart.marker_list(markers) +def add_buy_sell_signal_markers(df, chart): + + if "buy" not in df.columns or "sell" not in df.columns: + return + + markers = [] + signals = df[["time", "buy", "sell"]].to_dict(orient="records") + for item in signals: + if item["buy"] == 1: + markers.append( + { + "time": item["time"].strftime("%Y-%m-%d"), + "position": "below", + "shape": "arrow_up", + "color": "#4CAF50", + "text": f"B", + } + ) + elif item["sell"] == 1: + markers.append( + { + "time": item["time"].strftime("%Y-%m-%d"), + "position": "above", + "shape": "arrow_down", + "color": "#F44336", + "text": f"S", + } + ) + + chart.marker_list(markers) + + def get_kline(code: str) -> list: """ 获取所有指数代码 @@ -179,7 +225,12 @@ def resample_data(df: pd.DataFrame, timeframe: str) -> pd.DataFrame: return resampled -def plot_chart(symbol: str, name: str, timeframe: str): +def plot_chart(df, symbol: str, name: str, timeframe: str): + """ + df: DataFrame + time, open, high, low, close, volume, [buy, sell]可选 buy=1 买入信号, sell=1 卖出信号 + + """ chart = Chart(toolbox=True, inner_height=0.7, maximize=True) chart.topbar.textbox("symbol", symbol) @@ -196,6 +247,7 @@ def plot_chart(symbol: str, name: str, timeframe: str): add_macd(df, chart) add_cci(df, chart, period=14) add_TD(df, chart) + add_buy_sell_signal_markers(df, chart) chart.show(block=True) @@ -212,4 +264,6 @@ if __name__ == "__main__": df = get_kline(code=symbol) df = resample_data(df, timeframe) - plot_chart(symbol=symbol, name=name, timeframe=timeframe) + # df['buy'] = df['time'].apply(lambda x: 1 if pd.to_datetime(x).day % 2 == 1 else 0) + # df['sell'] = df['time'].apply(lambda x: 1 if pd.to_datetime(x).day % 2 == 0 else 0) + plot_chart(df=df, symbol=symbol, name=name, timeframe=timeframe)