บทความที่แล้วผมได้แนะนำระบบ Settrade OpenAPI Version 2 กันไปบ้างแล้ว คราวนี้เราจะมาเขียนโปรแกรมเพิ่มเติมในการตรวจสอบออเดอร์กันบ้าง สำหรับวันนี้ทีเฟค S50Z22 ที่เป็นซีรี่ย์หลักในปัจจุบัน บนโปรแกรม Straming Sandbox ไม่มีการเคลื่อนไหวต้องไปใช้ตัวอื่นทดสอบการส่งคำสั่งแทน ในที่นี้ผมขอใช้ทีเฟค S50H23 ในการรันโปรแกรม place-order.py
import time
from settrade_v2 import Investor
def my_message(result):
data = result['data']
hi_price = data['high']
low_price = data['low']
last_price = data['last']
place_order = deri.place_order(
pin="000000",
symbol=code,
side=side,
position="Auto",
price_type="Limit",
price=last_price,
volume=volume,
)
order_no = place_order["orderNo"]
print("Place order",code,"side =",side,"order no. =",order_no,"volume =",volume)
print("Stock info",code,"Hi = ",hi_price,"Low =",low_price,"Last =",last_price)
volume = 1
side = "Long"
code = "S50H23"
investor = Investor(
app_id="xxx",
app_secret="xxx",
broker_id="SANDBOX",
app_code="SANDBOX",
is_auto_queue=False)
deri = investor.Derivatives(account_no="xxx")
realtime = investor.RealtimeDataConnection()
sub = realtime.subscribe_price_info(code, on_message=my_message)
sub.start()
while True:
time.sleep(1)
break
หมายเหตุ xxx – ข้อมูลบัญชีในระบบ Sandbox OpenAPI
หลังจากรันโปรแกรม 2 ครั้งเราจะได้ออเดอร์บน Straming Sandbox มา 2 ออเดอร์
ต่อไปเราจะทำการตรวจสอบออเดอร์ที่ Python ยิงออกไปก่อนหน้านี้ ด้วยโปรแกรม get-order.py
from settrade_v2 import Investor
investor = Investor(
app_id="xxx",
app_secret="xxx",
broker_id="SANDBOX",
app_code="SANDBOX",
is_auto_queue=False)
deri = investor.Derivatives(account_no = "xxx")
while True :
order = int(input('Enter order no. (0-Exit) : '))
if order != 0:
order_info = deri.get_order(order_no=order)
no = order_info['orderNo']
time = order_info['entryTime']
code = order_info['symbol']
side = order_info['side']
position = order_info['position']
price = order_info['price']
volume = order_info['qty']
cancel = order_info['cancelTime']
print("Stock info",code,"order no.",no,time,side,position,"price =",price, \
"volume =",volume,"cancel time",cancel)
else:
break
เพื่อทดสอบโปรแกรมเราจะทำการยกเลิกรายการบนโปรแกรม Straming ออกไป 1 ออเดอร์ จากนั้นสั่งรันโปรแกรม
C:\Users\Python\SettradeV2Project>get-order.py
Enter order no. (0-Exit) : 100011 Stock info S50H23 order no. 100011 2022-12-13T11:56:06 Long Auto price = 899.5 volume = 1 cancel time 2022-12-13T11:58:43 Enter order no. (0-Exit) : 100012 Stock info S50H23 order no. 100012 2022-12-13T11:57:33 Long Auto price = 899.6 volume = 1 cancel time None Enter order no. (0-Exit) :
หลังจากนั้นลองสังเกตความแตกต่างของ 2 ออเดอร์ด้านบนครับ