From 2baf82b74828a6991430b01aa208e0ddae5f31d2 Mon Sep 17 00:00:00 2001 From: aszerW Date: Sun, 19 Oct 2025 10:47:41 +0800 Subject: [PATCH] =?UTF-8?q?=E4=B8=8D=E5=90=8C=E5=B8=82=E5=9C=BA=E7=94=BB?= =?UTF-8?q?=E5=9B=BE=E7=9A=84=E6=95=B0=E6=8D=AE=E5=8D=95=E7=8B=AC=E8=8E=B7?= =?UTF-8?q?=E5=8F=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- crypto_chart.py | 12 ++++++++++++ index_chart.py | 44 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+) create mode 100644 crypto_chart.py create mode 100644 index_chart.py diff --git a/crypto_chart.py b/crypto_chart.py new file mode 100644 index 0000000..1eb5ff9 --- /dev/null +++ b/crypto_chart.py @@ -0,0 +1,12 @@ +import pandas as pd +from chart import plot_chart + +if __name__ == "__main__": + symbol = "ETH_USDT" + timeframe = "1d" + data_path = f"/Users/aszer/Documents/vscode/cta/user_data/data/okx/{symbol}-{timeframe}.feather" + df = pd.read_feather(data_path) + + df.rename(columns={"date": "time"}, inplace=True) + print(df.head()) + plot_chart(df, symbol=symbol, name=symbol, timeframe=timeframe) diff --git a/index_chart.py b/index_chart.py new file mode 100644 index 0000000..e0ecff3 --- /dev/null +++ b/index_chart.py @@ -0,0 +1,44 @@ +import pandas as pd +from loguru import logger +from chart import plot_chart, resample_data +from db_config import DatabaseManager, DatabaseConfig + + +def get_kline(code: str) -> list: + """ + 获取所有指数代码 + :return: + """ + env = "online" + env = "daily" + db_config = DatabaseConfig(env=env) + logger.info(f"数据库连接: {db_config.connection_string}") + + db_manager = DatabaseManager(db_config) + sql = f"SELECT date as time, open, high, low, close, volume FROM public.index_kline where code='{code}' order by date;" + res = db_manager.execute_query(sql) + data_list = [dict(item) for item in res] + df = pd.DataFrame(data_list) + df["time"] = pd.to_datetime(df["time"]) + num_cols = ["open", "high", "low", "close", "volume"] + for col in num_cols: + if col in df.columns: + df[col] = pd.to_numeric(df[col], errors="coerce").astype(float) + return df + + +if __name__ == "__main__": + symbol = "399998" + timeframe = "1D" + + df = pd.read_csv( + "/Users/aszer/Documents/vscode/etf/data/index_all_stock.csv", + encoding="utf-8-sig", + ) + name = df.loc[df["代码"] == symbol, "名称"].values[0] + + df = get_kline(code=symbol) + df = resample_data(df, 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)