From f336928c6413ba26eb2b21df8d7fcc2c901a68db Mon Sep 17 00:00:00 2001 From: yilv <874448085@qq.com> Date: Tue, 3 Mar 2026 17:31:19 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=96=B0=E5=A2=9EK=E7=BA=BF=E6=95=B0?= =?UTF-8?q?=E6=8D=AE=E8=8E=B7=E5=8F=96=E4=B8=8E=E5=88=86=E6=9E=90=E7=A4=BA?= =?UTF-8?q?=E4=BE=8B=E8=84=9A=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 添加 demo_kline_analysis.py 示例文件,展示如何使用 SDK 获取股票K线数据并进行基础分析(如计算平均价格)。该示例包含错误处理和数据格式化展示,便于开发者快速上手集成。 --- demo_kline_analysis.py | 53 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 demo_kline_analysis.py diff --git a/demo_kline_analysis.py b/demo_kline_analysis.py new file mode 100644 index 0000000..c9aaee7 --- /dev/null +++ b/demo_kline_analysis.py @@ -0,0 +1,53 @@ +from itick import Client, Environment, KlinePeriod + +from itick.exceptions import ITickError, AuthError + +def main(): + try: + # Initialize client + client = Client(token="e66e496347e34d72a63cce71a4383c358b2c6d28d9d94bd48d2568cdc7b0f0da", environment=Environment.FREE) + + # Get 1-minute Kline data for a stock (e.g., AAPL) + print("Fetching 1-minute Kline data...") + klines = client.stock.get_kline( + region="US", + code="AAPL", + period=KlinePeriod.ONE_MINUTE.value, + limit=10 + ) + + if not klines: + print("No data found.") + return + + print(f"{'Time':<20} {'Open':<10} {'Close':<10} {'Volume':<15} {'Turnover':<15} {'Avg Price':<10}") + print("-" * 85) + + for k in klines: + # Extract fields + ts = k.get('t') # Timestamp + open_p = k.get('o') # Open Price (Raw) + close_p = k.get('c') # Close Price (Raw) + vol = k.get('v') # Volume + turnover = k.get('tu') # Turnover (Transaction Amount) + + # Calculate Average Price (VWAP for this minute) + # Avg Price = Turnover / Volume + avg_price = turnover / vol if vol and vol > 0 else 0 + + # Convert timestamp to readable format (optional) + import datetime + dt = datetime.datetime.fromtimestamp(ts / 1000).strftime('%Y-%m-%d %H:%M:%S') + + print(f"{dt:<20} {open_p:<10} {close_p:<10} {vol:<15} {turnover:<15} {avg_price:<10.2f}") + + except AuthError as e: + print(f"\nAuthentication Error: {e}") + print("Please check if your token is valid and you have permissions for this endpoint.") + except ITickError as e: + print(f"\nSDK Error: {e}") + except Exception as e: + print(f"\nUnexpected Error: {e}") + +if __name__ == "__main__": + main()