forked from gejyn14/pyheroapi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path03_trading_orders.py
More file actions
410 lines (330 loc) · 15.3 KB
/
Copy path03_trading_orders.py
File metadata and controls
410 lines (330 loc) · 15.3 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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
"""
PyHero API - Trading & Order Management Example
This example demonstrates:
1. Basic stock trading (buy/sell)
2. Credit trading (margin trading)
3. Order modifications and cancellations
4. Order status tracking
5. Different order types
6. Account management
7. Trading safety practices
"""
import os
import time
from datetime import datetime
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 account_status_example(client):
"""Check account status and balances before trading"""
print("=== Account Status & Balance Check ===\n")
try:
# Get deposit details (available balance)
print("💰 Account Balance:")
deposit_details = client.get_deposit_details()
print(f" Total Evaluation Amount: {deposit_details.tot_evla_amt}")
print(f" Securities Evaluation Amount: {deposit_details.scts_evla_amt}")
print(f" Total Deposit Amount: {deposit_details.tot_dncl_amt}")
print(f" Order Possible Cash: {deposit_details.ord_psbl_cash}")
# Get estimated assets
print("\n📊 Asset Evaluation:")
assets = client.get_estimated_assets()
print(f" Evaluation Amount: {assets.evla_amt}")
print(f" Previous Day Evaluation: {assets.bfdy_evla_amt}")
print(f" Profit/Loss Amount: {assets.evla_pfls_amt}")
print(f" Profit/Loss Rate: {assets.evla_pfls_rt}%")
# Get current positions
print("\n📈 Current Positions:")
positions = client.get_execution_balance()
if positions:
print(" Symbol | Name | Quantity | Avg Price | Current | P&L")
print(" " + "-" * 55)
for pos in positions[:5]: # Show first 5 positions
print(f" {pos.symbol:<6} | {pos.name[:8]:<8} | {pos.quantity:<8} | "
f"{pos.average_price:<9} | {pos.current_price:<7} | {pos.profit_loss}")
else:
print(" No current positions")
# Get account evaluation status
print("\n📋 Account Evaluation Status:")
evaluation = client.get_account_evaluation_status()
print(f" Account evaluation completed at: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}")
except Exception as e:
print(f"✗ Error getting account status: {e}")
def basic_trading_example(client):
"""Basic buy and sell operations"""
print("\n=== Basic Trading Operations ===\n")
# Example with a stable, liquid stock
symbol = "005930" # Samsung Electronics
test_quantity = 1 # Small quantity for testing
print("🛒 Basic Stock Trading:")
print("⚠️ NOTE: This is a SANDBOX example - no real money involved!")
print(f" Trading symbol: {symbol} (Samsung Electronics)")
print(f" Test quantity: {test_quantity} shares\n")
try:
# Get current price first
quote = client.get_quote(symbol)
current_price = quote.sel_fpr_bid # Use ask price for buying
print(f"📊 Current Ask Price: {current_price}")
# Example 1: Market Buy Order
print("\n1️⃣ Market Buy Order:")
try:
buy_response = client.buy_stock(
symbol=symbol,
quantity=test_quantity,
order_type="3", # Market order
market="KRX"
)
order_number = buy_response.get('ord_no')
print(f"✓ Market buy order placed successfully")
print(f" Order Number: {order_number}")
print(f" Symbol: {symbol}")
print(f" Quantity: {test_quantity}")
print(f" Order Type: Market")
# Wait a moment and check order status
time.sleep(2)
unfilled_orders = client.get_unfilled_orders(symbol=symbol)
print(f" Unfilled orders for {symbol}: {len(unfilled_orders) if unfilled_orders else 0}")
except Exception as e:
print(f"✗ Market buy order failed: {e}")
# Example 2: Limit Buy Order
print("\n2️⃣ Limit Buy Order:")
try:
# Place limit order below current price
limit_price = int(float(current_price) * 0.95) # 5% below market
limit_buy_response = client.buy_stock(
symbol=symbol,
quantity=test_quantity,
price=limit_price,
order_type="0", # Limit order
market="KRX"
)
limit_order_number = limit_buy_response.get('ord_no')
print(f"✓ Limit buy order placed successfully")
print(f" Order Number: {limit_order_number}")
print(f" Limit Price: {limit_price}")
print(f" Current Price: {current_price}")
print(f" Order Type: Limit")
except Exception as e:
print(f"✗ Limit buy order failed: {e}")
# Example 3: Market Sell Order (if we have positions)
print("\n3️⃣ Market Sell Order:")
try:
positions = client.get_execution_balance()
# Check if we have the stock to sell
target_position = None
if positions:
for pos in positions:
if pos.symbol == symbol and int(pos.available_quantity or 0) > 0:
target_position = pos
break
if target_position:
sell_quantity = min(1, int(target_position.available_quantity))
sell_response = client.sell_stock(
symbol=symbol,
quantity=sell_quantity,
order_type="3", # Market order
market="KRX"
)
sell_order_number = sell_response.get('ord_no')
print(f"✓ Market sell order placed successfully")
print(f" Order Number: {sell_order_number}")
print(f" Quantity: {sell_quantity}")
print(f" Order Type: Market")
else:
print("⚠ No available shares to sell for demonstration")
except Exception as e:
print(f"✗ Market sell order failed: {e}")
except Exception as e:
print(f"✗ Error in basic trading example: {e}")
def credit_trading_example(client):
"""Credit (margin) trading example"""
print("\n=== Credit Trading (Margin Trading) ===\n")
symbol = "005930" # Samsung Electronics
test_quantity = 1
print("💳 Credit Trading Operations:")
print("⚠️ NOTE: Credit trading involves borrowing - use with extreme caution!")
print(f" Symbol: {symbol}")
print(f" Quantity: {test_quantity}\n")
try:
# Example 1: Credit Buy (Margin Buy)
print("1️⃣ Credit Buy Order:")
try:
credit_buy_response = client.credit_buy_stock(
symbol=symbol,
quantity=test_quantity,
order_type="3", # Market order
market="KRX"
)
credit_order_number = credit_buy_response.get('ord_no')
print(f"✓ Credit buy order placed successfully")
print(f" Order Number: {credit_order_number}")
print(f" Type: Credit (Margin) Buy")
except Exception as e:
print(f"✗ Credit buy order failed: {e}")
# Example 2: Credit Sell (Short Sell)
print("\n2️⃣ Credit Sell Order:")
try:
credit_sell_response = client.credit_sell_stock(
symbol=symbol,
quantity=test_quantity,
order_type="3", # Market order
market="KRX",
credit_deal_type="99" # Margin combined
)
credit_sell_order_number = credit_sell_response.get('ord_no')
print(f"✓ Credit sell order placed successfully")
print(f" Order Number: {credit_sell_order_number}")
print(f" Type: Credit (Short) Sell")
except Exception as e:
print(f"✗ Credit sell order failed: {e}")
# Check margin requirements
print("\n3️⃣ Margin Requirements:")
try:
margin_info = client.get_margin_rate_order_quantity(
symbol=symbol,
margin_rate="40" # 40% margin rate
)
print(f" Available quantity at 40% margin: {margin_info.get('ord_psbl_qty', 'N/A')}")
except Exception as e:
print(f"✗ Error getting margin info: {e}")
# Check credit guarantee requirements
print("\n4️⃣ Credit Guarantee:")
try:
guarantee_info = client.get_credit_guarantee_rate_order_quantity(
symbol=symbol,
guarantee_rate="40" # 40% guarantee rate
)
print(f" Available quantity at 40% guarantee: {guarantee_info.get('ord_psbl_qty', 'N/A')}")
except Exception as e:
print(f"✗ Error getting guarantee info: {e}")
except Exception as e:
print(f"✗ Error in credit trading example: {e}")
def order_management_example(client):
"""Order modification and cancellation example"""
print("\n=== Order Management (Modify & Cancel) ===\n")
symbol = "005930"
test_quantity = 1
try:
# First, place a limit order that won't execute immediately
print("1️⃣ Placing Limit Order for Modification:")
# Get current price and place order well below market
quote = client.get_quote(symbol)
current_price = float(quote.sel_fpr_bid)
limit_price = int(current_price * 0.8) # 20% below market - unlikely to execute
original_order = client.buy_stock(
symbol=symbol,
quantity=test_quantity,
price=limit_price,
order_type="0", # Limit order
market="KRX"
)
original_order_number = original_order.get('ord_no')
print(f"✓ Original limit order placed")
print(f" Order Number: {original_order_number}")
print(f" Original Price: {limit_price}")
print(f" Quantity: {test_quantity}")
# Wait a moment
time.sleep(1)
# Example 1: Modify Order (change price and quantity)
print("\n2️⃣ Modifying Order:")
try:
new_price = int(current_price * 0.85) # Slightly higher price
new_quantity = 2 # Increase quantity
modify_response = client.modify_order(
original_order_number=original_order_number,
symbol=symbol,
new_quantity=new_quantity,
new_price=new_price,
market="KRX"
)
modified_order_number = modify_response.get('ord_no')
print(f"✓ Order modified successfully")
print(f" New Order Number: {modified_order_number}")
print(f" New Price: {new_price} (was {limit_price})")
print(f" New Quantity: {new_quantity} (was {test_quantity})")
# Update order number for cancellation
order_to_cancel = modified_order_number or original_order_number
except Exception as e:
print(f"✗ Order modification failed: {e}")
order_to_cancel = original_order_number
# Wait a moment
time.sleep(1)
# Example 2: Cancel Order
print("\n3️⃣ Cancelling Order:")
try:
cancel_response = client.cancel_order(
original_order_number=order_to_cancel,
symbol=symbol,
cancel_quantity=new_quantity if 'new_quantity' in locals() else test_quantity,
market="KRX"
)
cancelled_order_number = cancel_response.get('ord_no')
print(f"✓ Order cancelled successfully")
print(f" Cancelled Order Number: {cancelled_order_number}")
except Exception as e:
print(f"✗ Order cancellation failed: {e}")
# Example 3: Check unfilled orders
print("\n4️⃣ Checking Unfilled Orders:")
try:
unfilled_orders = client.get_unfilled_orders(symbol=symbol)
if unfilled_orders:
print(f" Found {len(unfilled_orders)} unfilled orders for {symbol}:")
for order in unfilled_orders[:3]: # Show first 3
print(f" Order #{order.ord_no}: {order.ord_qty} shares at {order.ord_uv}")
else:
print(f" No unfilled orders for {symbol}")
except Exception as e:
print(f"✗ Error checking unfilled orders: {e}")
# Example 4: Check filled orders
print("\n5️⃣ Checking Filled Orders:")
try:
filled_orders = client.get_filled_orders(symbol=symbol)
if filled_orders:
print(f" Found {len(filled_orders)} filled orders for {symbol}:")
for order in filled_orders[:3]: # Show first 3
print(f" Order #{order.ord_no}: {order.cntr_qty} shares at {order.cntr_uv}")
else:
print(f" No filled orders for {symbol}")
except Exception as e:
print(f"✗ Error checking filled orders: {e}")
except Exception as e:
print(f"✗ Error in order management example: {e}")
def main():
"""Main function demonstrating all trading features"""
print("🚀 PyHero API - Comprehensive Trading Example")
print("⚠️ IMPORTANT: This example uses SANDBOX environment!")
print("⚠️ Always test thoroughly before using in production!\n")
# Setup client
client = setup_client()
if not client:
return
try:
# Run all trading examples
account_status_example(client)
basic_trading_example(client)
credit_trading_example(client)
order_management_example(client)
print("\n" + "="*60)
print("✓ Trading examples completed successfully!")
print("\n🔴 REMEMBER:")
print(" - This was SANDBOX trading (no real money)")
print(" - Always use proper risk management")
print(" - Test extensively before live trading")
print(" - Never trade money you can't afford to lose")
except KiwoomAPIError as e:
print(f"✗ Kiwoom API Error: {e}")
except Exception as e:
print(f"✗ Unexpected error: {e}")
if __name__ == "__main__":
main()