forked from gejyn14/pyheroapi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path02_market_data.py
More file actions
305 lines (235 loc) · 11.2 KB
/
02_market_data.py
File metadata and controls
305 lines (235 loc) · 11.2 KB
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
"""
PyHero API - Market Data Example
This example demonstrates:
1. Stock quotes and order book data
2. OHLCV historical data
3. Market performance indicators
4. Intraday and minute data
5. After-hours trading data
6. Program trading data
"""
import os
from datetime import datetime, timedelta
from pyheroapi import KiwoomClient
from pyheroapi.exceptions import KiwoomAPIError
def setup_client():
"""Setup API client"""
appkey = os.getenv("KIWOOM_APPKEY", "your_app_key_here")
secretkey = os.getenv("KIWOOM_SECRETKEY", "your_secret_key_here")
if appkey == "your_app_key_here":
print("Please set KIWOOM_APPKEY and KIWOOM_SECRETKEY environment variables")
return None
return KiwoomClient.create_with_credentials(
appkey=appkey,
secretkey=secretkey,
is_production=False # SANDBOX MODE: set is_production=False explicitly
)
def stock_quotes_example(client):
"""Comprehensive stock quotes and order book example"""
print("=== Stock Quotes & Order Book Data ===\n")
# Major Korean stocks for examples
symbols = ["005930", "000660", "035420", "005380", "051910"] # Samsung, SK Hynix, NAVER, LG Chem, LG Electronics
symbol_names = ["Samsung Electronics", "SK Hynix", "NAVER", "LG Chem", "LG Electronics"]
for symbol, name in zip(symbols, symbol_names):
try:
print(f"📊 {name} ({symbol}):")
# Get current quote/order book
quote = client.get_quote(symbol)
print(f" Best Bid: {quote.buy_fpr_bid} (Qty: {quote.buy_fpr_req})")
print(f" Best Ask: {quote.sel_fpr_bid} (Qty: {quote.sel_fpr_req})")
print(f" Total Buy Orders: {quote.tot_buy_req}")
print(f" Total Sell Orders: {quote.tot_sel_req}")
# Get basic market data
market_data = client.get_market_data(symbol)
print(f" Current Price: {market_data.current_price}")
print(f" Change: {market_data.change_amount} ({market_data.change_rate}%)")
print(f" Volume: {market_data.volume}")
print(f" Value: {market_data.value}")
except Exception as e:
print(f" ✗ Error getting data for {symbol}: {e}")
print()
def historical_data_example(client):
"""OHLCV historical data example"""
print("=== Historical OHLCV Data ===\n")
symbol = "005930" # Samsung Electronics
try:
# Daily OHLCV data
print("📈 Daily OHLCV (Last 30 days):")
daily_data = client.get_stock_ohlcv(
symbol=symbol,
time_type="D", # Daily
count=30
)
if daily_data:
print(" Date | Open | High | Low | Close | Volume")
print(" " + "-" * 65)
for day in daily_data[:5]: # Show first 5 days
print(f" {day.get('date', 'N/A'):<10} | {day.get('open_pric', 'N/A'):<7} | "
f"{day.get('high_pric', 'N/A'):<7} | {day.get('low_pric', 'N/A'):<7} | "
f"{day.get('close_pric', 'N/A'):<7} | {day.get('trde_qty', 'N/A')}")
print(f" ... and {len(daily_data) - 5} more days")
# Weekly OHLCV data
print("\n📈 Weekly OHLCV (Last 12 weeks):")
weekly_data = client.get_stock_ohlcv(
symbol=symbol,
time_type="W", # Weekly
count=12
)
print(f" Retrieved {len(weekly_data) if weekly_data else 0} weeks of data")
# Monthly OHLCV data
print("\n📈 Monthly OHLCV (Last 6 months):")
monthly_data = client.get_stock_ohlcv(
symbol=symbol,
time_type="M", # Monthly
count=6
)
print(f" Retrieved {len(monthly_data) if monthly_data else 0} months of data")
except Exception as e:
print(f"✗ Error getting historical data: {e}")
def intraday_data_example(client):
"""Intraday and minute data example"""
print("\n=== Intraday & Minute Data ===\n")
symbol = "005930" # Samsung Electronics
try:
# Minute data
print("⏰ Minute Data (1-minute intervals):")
minute_data = client.get_stock_minute_data(
symbol=symbol,
minute_type="1" # 1-minute
)
if minute_data:
print(" Time | Price | Volume | Change")
print(" " + "-" * 35)
for minute in minute_data[:5]: # Show first 5 minutes
print(f" {minute.get('time', 'N/A'):<6} | {minute.get('close_pric', 'N/A'):<7} | "
f"{minute.get('trde_qty', 'N/A'):<6} | {minute.get('flu_rt', 'N/A')}")
# Different minute intervals
for interval in ["5", "15", "30", "60"]:
try:
interval_data = client.get_stock_minute_data(symbol=symbol, minute_type=interval)
print(f" {interval}-minute data: {len(interval_data) if interval_data else 0} records")
except Exception as e:
print(f" {interval}-minute data: Error - {e}")
except Exception as e:
print(f"✗ Error getting intraday data: {e}")
def market_performance_example(client):
"""Market performance indicators example"""
print("\n=== Market Performance Indicators ===\n")
symbol = "005930" # Samsung Electronics
try:
# Market performance info
print("📊 Market Performance Info:")
performance = client.get_market_performance_info(symbol)
print(f" Current Price: {performance.get('cur_prc', 'N/A')}")
print(f" Opening Price: {performance.get('open_pric', 'N/A')}")
print(f" High Price: {performance.get('high_pric', 'N/A')}")
print(f" Low Price: {performance.get('low_pric', 'N/A')}")
print(f" Trading Volume: {performance.get('trde_qty', 'N/A')}")
print(f" Trading Value: {performance.get('trde_prica', 'N/A')}")
# Daily stock prices
print("\n📅 Daily Stock Prices (Last 7 days):")
daily_prices = client.get_daily_stock_prices(symbol=symbol, days=7)
if daily_prices:
for day in daily_prices[:3]: # Show first 3 days
print(f" Date: {day.get('date', 'N/A')}, "
f"Close: {day.get('close_pric', 'N/A')}, "
f"Change: {day.get('flu_rt', 'N/A')}%")
# After-hours single price
print("\n🌙 After-Hours Single Price:")
after_hours = client.get_after_hours_single_price(symbol)
print(f" After-hours price: {after_hours.get('pred_close_pric', 'N/A')}")
print(f" Expected volume: {after_hours.get('pred_trde_qty', 'N/A')}")
except Exception as e:
print(f"✗ Error getting market performance: {e}")
def program_trading_example(client):
"""Program trading data example"""
print("\n=== Program Trading Data ===\n")
try:
# Program trading hourly
print("🤖 Program Trading (Hourly):")
hourly_program = client.get_program_trading_hourly()
if hourly_program:
print(f" Retrieved {len(hourly_program)} hourly records")
if hourly_program:
latest = hourly_program[0]
print(f" Latest hour trading value: {latest.get('trde_prica', 'N/A')}")
# Program trading daily
print("\n🤖 Program Trading (Daily):")
daily_program = client.get_program_trading_daily()
if daily_program:
print(f" Retrieved {len(daily_program)} daily records")
# Program trading arbitrage
print("\n🤖 Program Trading (Arbitrage):")
arbitrage = client.get_program_trading_arbitrage()
if arbitrage:
print(f" Retrieved {len(arbitrage)} arbitrage records")
# Symbol-specific program trading
symbol = "005930"
print(f"\n🤖 Program Trading for {symbol}:")
symbol_program = client.get_symbol_program_trading_hourly(symbol)
if symbol_program:
print(f" Retrieved {len(symbol_program)} symbol-specific records")
except Exception as e:
print(f"✗ Error getting program trading data: {e}")
def institutional_trading_example(client):
"""Institutional trading data example"""
print("\n=== Institutional Trading Data ===\n")
symbol = "005930" # Samsung Electronics
try:
# Daily institutional trading
print("🏛️ Daily Institutional Trading:")
institutional = client.get_daily_institutional_trading_stocks(symbol)
if institutional:
print(f" Retrieved {len(institutional)} institutional trading records")
if institutional:
latest = institutional[0]
print(f" Latest institutional net buying: {latest.get('net_buy_qty', 'N/A')}")
# Institutional trading trends
print("\n📈 Institutional Trading Trends:")
trends = client.get_institutional_trading_trends(symbol)
if trends:
print(f" Retrieved {len(trends)} trend records")
# Trading intensity (hourly)
print("\n⚡ Trading Intensity (Hourly):")
intensity_hourly = client.get_trading_intensity_hourly(symbol)
if intensity_hourly:
print(f" Retrieved {len(intensity_hourly)} hourly intensity records")
# Trading intensity (daily)
print("\n⚡ Trading Intensity (Daily):")
intensity_daily = client.get_trading_intensity_daily(symbol)
if intensity_daily:
print(f" Retrieved {len(intensity_daily)} daily intensity records")
# Intraday investor trading
print("\n👥 Intraday Investor Trading:")
intraday_investor = client.get_intraday_investor_trading(symbol)
if intraday_investor:
print(f" Retrieved {len(intraday_investor)} intraday investor records")
# After-hours investor trading
print("\n🌙 After-Hours Investor Trading:")
afterhours_investor = client.get_after_hours_investor_trading(symbol)
if afterhours_investor:
print(f" Retrieved {len(afterhours_investor)} after-hours investor records")
except Exception as e:
print(f"✗ Error getting institutional trading data: {e}")
def main():
"""Main function demonstrating all market data features"""
print("🚀 PyHero API - Comprehensive Market Data Example\n")
# Setup client
client = setup_client()
if not client:
return
try:
# Run all examples
stock_quotes_example(client)
historical_data_example(client)
intraday_data_example(client)
market_performance_example(client)
program_trading_example(client)
institutional_trading_example(client)
print("\n✓ Market data examples completed successfully!")
except KiwoomAPIError as e:
print(f"✗ Kiwoom API Error: {e}")
except Exception as e:
print(f"✗ Unexpected error: {e}")
if __name__ == "__main__":
main()