บทความที่แล้วผมได้แนะนำระบบ 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 ออเดอร์