MT5 Trading API

คู่มือใช้งาน MT5 Trading API

เอกสารประกอบการเชื่อมต่อระบบเทรดและดึงข้อมูลผ่านรูปแบบ RESTful API

Overview & Basics (ภาพรวมระบบ)

MT5 Trading API เป็นบริการ API สำหรับเชื่อมต่อกับระบบซื้อขายบน MT5 โดยเครื่องมือหรือระบบภายนอกสามารถสื่อสารกับ MT5 ผ่านรูปแบบ RESTful API ด้วย HTTP Requests เพื่อดำเนินการต่างๆ ได้ เช่น การดึงข้อมูลบัญชี ข้อมูลสถานะการซื้อขาย และการส่งคำสั่งซื้อขายได้

Base URL API: https://mt5api.toptrader.co.th/web-api/v1/

Data Structures (โครงสร้างข้อมูลหลัก)

รูปแบบโครงสร้างโมเดลข้อมูลหลัก (Models จาก Swagger UI) ที่ใช้งานและรับส่งข้อมูลภายในระบบ มีดังนี้:

Trading Request Data Model

โมเดลข้อมูลสำหรับส่งคำสั่งซื้อขายและการปรับปรุงคำสั่งซื้อ (พารามิเตอร์ทั้งหมดเป็นประเภท string)

{ 
  Action, Comment, DigitsSet, Login, PriceOrder, 
  PriceSL, PriceTP, PriceTrigger, Symbol, Type, 
  TypeFill, TypeTime, Volume 
}

Order Update Data Model

โมเดลข้อมูลโครงสร้างสัญญาสถานะและตั๋วบันทึกของออเดอร์ (พารามิเตอร์ทั้งหมดเป็นประเภท string)

{ 
  Comment, Login, Order, PriceOrder, PriceSL, PriceTP, PriceTrigger, Symbol
}

Position Update Data Model

โมเดลข้อมูลสำหรับการอัปเดตและถือครองสถานะสัญญาล่าสุดในพอร์ตปัจจุบัน (พารามิเตอร์ทั้งหมดเป็นประเภท string)

{ 
  Comment, Login, Position, PriceSL, PriceTP, Symbol
}

รายละเอียดฟิลด์พารามิเตอร์และเงื่อนไขเพิ่มเติม

Action

  • 200 (MP) - Execution of a trade operation by a dealer
    • Required Fields: Login, Symbol, Volume, PriceOrder, Type[0 or 1], TypeFill
    • Optional Fields: Digits, Comment, PriceSL, PriceTP
  • 201 (Pending) - Placing of a pending order by a dealer
    • Required Fields: Login, Symbol, Volume, Type, PriceOrder, TypeTime, TypeFill, PriceTrigger
    • Optional Fields: Digits, Comment, PriceSL, PriceTP, TypeFill
  • 202 - (Modify Position) Position modification by a dealer
    • Required Fields: Login, Symbol
    • Optional Fields: Digits, Comment
  • 203 - (Modify Order) Order modification by a dealer
    • Required Fields: Login, Order, Symbol, Type[2,3,4,5,6,7], PriceOrder, PriceTrigger, TypeFill, PriceSL, PriceTP, TypeTime
    • Optional Fields: Digits, Comment, Volume
    หมายเหตุ: ต้องใช้ API วาง Pending Order เท่านั้นถึงจะ Modify Order ด้วย API ได้
  • 204 - (Cancel Pending) Pending order deletion by a dealer
    • Required Fields: Login, Symbol, Order, Type[2,3,4,5,6,7]
    • Optional Fields: Digits, Comment, PriceSL, PriceTP

PriceSL

= The Stop Loss level in the trade request. (ประเภทฟิลด์คือ string)

PriceTP

= The Take Profit level in the trade request. (ประเภทฟิลด์คือ string)

PriceTrigger

= Price at which a limit order is placed upon triggering of a stop limit order. (stop price, only for stop-limit orders) (ประเภทฟิลด์คือ string)

Type

(ประเภทฟิลด์คือ string - Note: สามารถใส่ชื่อ Type แทนตัวเลขได้)

  • 0 - Buy
  • 1 - Sell
  • 2 - Buy Limit
  • 3 - Sell Limit
  • 4 - Buy Stop
  • 5 - Sell Stop
  • 6 - Buy Stop Limit
  • 7 - Sell Stop Limit
  • 8 - Close By (only used in the hedging mode)

TypeFill

(ประเภทฟิลด์คือ string)

  • 0 - Fill or Kill. An order must be filled completely or canceled.
  • 1 - Immediate or Cancel. An order can be filled partially and the residual volume is canceled.
  • 2 - Return the remainder to the queue. This mode is used for pending orders.

1. Login (การเข้าสู่ระบบ)

POST api/login

ทำคำขอเข้าสู่ระบบเพื่อยืนยันตัวตนและรับสิทธิ์ใช้งาน Bearer Token สำหรับส่งร่วมกับ API เส้นอื่นๆ ถัดไป

Query Parameters

Parameter Type Description
loginstringเลขล็อกอินบัญชีซื้อขายของคุณ (เช่น 99000009)
passwordstringรหัสผ่านของบัญชีซื้อขาย

Example Request URL

POST https://mt5api.toptrader.co.th/web-api/v1/login?login=99000009&password=password123

Example Response (200 OK)

{
  "login": "99000009",
  "login_time": "2024-05-30 11:55:03.685",
  "retcode": "0 Done",
  "token": "eyJhbGciOiJIUzI1NiIsInR5..."
}
คำแจ้งเตือน: Token ที่ได้รับจะนำไปใช้ในการ Request อื่นๆ โปรดบันทึกเก็บไว้

2. User Info (ดึงข้อมูลผู้ใช้)

GET api/user/info

ใช้สำหรับเรียกดูข้อมูลรายละเอียดพื้นฐานและกลุ่มบัญชีของตัวผู้ใช้งานเอง

Query Parameters

ParameterTypeDescription
loginstringระบุเลขล็อกอินของบัญชี

Required Header

Authorization: Bearer [ใส่เลข Token ที่ได้จาก Login]

Example Request URL

GET https://mt5api.toptrader.co.th/web-api/v1/api/user/info?login=99000009
Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5...

Example Response

{ 
  "retcode" : "0 Done", 
  "answer" : {
    "Login" : "99000009",
    "Group" : "real\\TT_0.2",
    "Name" : "Trader Name"
  } 
}

3. Symbol (ดึงข้อมูลผลิตภัณฑ์ซื้อขาย)

GET api/symbol

ใช้เรียกข้อมูลพารามิเตอร์โครงสร้าง ตัวคูณสัญญา จำนวนทศนิยม และสถานะของสินทรัพย์หรือหลักทรัพย์ที่ต้องการเปิดสัญญาซื้อขาย

Query Parameters

ParameterTypeDescription
symbolstringชื่อผลิตภัณฑ์ย่อ *สำคัญ: ต้องเป็นตัวพิมพ์ใหญ่เท่านั้น เช่น PTT, AOT
loginstringระบุเลขล็อกอินบัญชี

Required Header

Authorization: Bearer [ใส่เลข Token ที่ได้จาก Login]

Example Request URL

GET https://mt5api.toptrader.co.th/web-api/v1/api/symbol?symbol=PTT&login=99000009
Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5...

Example Response

{ 
  "retcode" : "0 Done", 
  "answer" : {
    "Symbol" : "PTT",
    "Path" : "SET\\Common Stock\\PTT",
    "Description" : "PTT PUBLIC COMPANY LIMITED",
    "Digits" : "2",
    "Point" : "0.01000000",
    "Multiply" : "100.00000000"
  }
}

4. Orders (เรียกดูคำสั่งซื้อขาย)

รวมกลุ่ม Endpoint สำหรับตรวจสอบปริมาณและรายละเอียดคำสั่งซื้อที่ค้างอยู่ในกระดาน (Open Orders) รวมถึงคำสั่งซื้อในประวัติย้อนหลัง (Order History)

4.1 Get Total Orders (จำนวนคำสั่งที่ยังเปิดอยู่ปัจจุบัน)

GET api/order/get_total

Query Parameters

ParameterTypeDescription
loginstringระบุเลขล็อกอินของบัญชี

Required Header

Authorization: Bearer [ใส่เลข Token ที่ได้จาก Login]

Example Request URL

GET https://mt5api.toptrader.co.th/web-api/v1/api/order/get_total?login=99000009
Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5...

Example Response

{ "retcode" : "0 Done", "answer" : { "total" : "5" } }

4.2 Get Open Orders (รายละเอียดคำสั่งที่เปิดอยู่)

GET api/order/get_open_orders

Query Parameters

ParameterTypeDescription
loginstringระบุเลขล็อกอินของบัญชี

Required Header

Authorization: Bearer [ใส่เลข Token ที่ได้จาก Login]

Example Request URL

GET https://mt5api.toptrader.co.th/web-api/v1/api/order/get_open_orders?login=99000009
Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5...

Example Response

{ 
  "retcode" : "0 Done", 
  "answer" : [
    { "Order" : "3920016", "Symbol" : "PTT", "Login" : "99000009", "ContractSize" : "1.00" }
  ] 
}

4.3 Get Total Orders History (จำนวนประวัติคำสั่งซื้อทั้งหมด)

GET web-api/v1/order/history/get_total

Query Parameters

ParameterTypeDescription
loginstringเลขบัญชีผู้ใช้
fromstring(ไม่บังคับ) วันเวลาเริ่มต้น รูปแบบ YYYY-MM-DD hh:mm:ss (ค่าเริ่มต้นคือ 1970-01-01 00:00:00)
tostring(ไม่บังคับ) วันเวลาสิ้นสุด รูปแบบ YYYY-MM-DD hh:mm:ss (ค่าเริ่มต้นคือเวลาปัจจุบัน)

Required Header

Authorization: Bearer [ใส่เลข Token ที่ได้จาก Login]

Example Request URL

GET https://mt5api.toptrader.co.th/web-api/v1/order/history/get_total?login=99000009&from=2026-01-01 00:00:00&to=2026-01-31 23:59:59
Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5...

Example Response

{ "retcode": "0 Done", "answer": { "total": "4" } }

4.4 Get Orders History (รายละเอียดประวัติคำสั่งซื้อ)

GET web-api/v1/order/history/get

Query Parameters

ParameterTypeDescription
loginstringเลขบัญชีผู้ใช้
fromstring(ไม่บังคับ) วันเวลาเริ่มต้น รูปแบบ YYYY-MM-DD hh:mm:ss
tostring(ไม่บังคับ) วันเวลาสิ้นสุด รูปแบบ YYYY-MM-DD hh:mm:ss
symbolstring(ไม่บังคับ) ชื่อย่อหลักทรัพย์ (เช่น PTT หรือคั่นด้วยคอมมา PTT,TRUE) **หากระบุ จำเป็นต้องระบุ from และ to ด้วยเสมอ
typestring(ไม่บังคับ) คัดกรองประเภทออเดอร์ เช่น BUY STOP หรือใส่เป็นตัวเลข 4

Required Header

Authorization: Bearer [ใส่เลข Token ที่ได้จาก Login]

Example Request URL

GET https://mt5api.toptrader.co.th/web-api/v1/order/history/get?login=99000009&from=2026-01-01 00:00:00&to=2026-01-31 23:59:59&symbol=PTT
Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5...

Example Response

{
  "retcode": "0 Done",
  "answer": [{
    "Order": "6856981",
    "Login": "99000009",
    "Symbol": "PTT",
    "State": "2",
    "TimeSetup": "2026-01-22 15:45:57",
    "TimeDone": "2026-01-26 15:54:01"
  }]
}

5. Deals (ข้อมูลประวัติการจับคู่ซื้อขายสำเร็จ)

ใช้ดึงข้อมูลตั๋ว Deal หรือข้อมูลการ Matching สำเร็จที่เกิดขึ้นในพอร์ตตามช่วงเวลาที่ระบุ

5.1 Get Total Deals (จำนวน Deal ทั้งหมดในช่วงเวลาที่กำหนด)

GET web-api/v1/deal/get_total

Query Parameters

ParameterTypeDescription
loginstringเลขบัญชีผู้ใช้
fromstring(ไม่บังคับ) วันเวลาเริ่มต้น รูปแบบ YYYY-MM-DD hh:mm:ss (ค่าเริ่มต้นคือ 1970-01-01 00:00:00)
tostring(ไม่บังคับ) วันเวลาสิ้นสุด รูปแบบ YYYY-MM-DD hh:mm:ss (ค่าเริ่มต้นคือเวลาปัจจุบัน)

Required Header

Authorization: Bearer [ใส่เลข Token ที่ได้จาก Login]

Example Request URL

GET https://mt5api.toptrader.co.th/web-api/v1/deal/get_total?login=99000009&from=2026-01-01 00:00:00&to=2026-01-31 23:59:59
Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5...

Example Response

{ "retcode": "0 Done", "answer": { "total": "1" } }

5.2 Get Deals (รายละเอียดประวัติข้อมูลธุรกรรม Deal)

GET web-api/v1/deal/get

Query Parameters

ParameterTypeDescription
loginstringเลขบัญชีผู้ใช้
fromstring(ไม่บังคับ) วันเวลาเริ่มต้น รูปรูปแบบ YYYY-MM-DD hh:mm:ss
tostring(ไม่บังคับ) วันเวลาสิ้นสุด รูปแบบ YYYY-MM-DD hh:mm:ss
symbolstring(ไม่บังคับ) ชื่อย่อหลักทรัพย์ (เช่น PTT หรือคั่นด้วยคอมมา PTT,TRUE) **หากระบุ จำเป็นต้องระบุ from และ to ด้วยเสมอ
typestring(ไม่บังคับ) คัดกรองประเภทดีล เช่น BUY หรือใส่เป็นตัวเลข 0

Required Header

Authorization: Bearer [ใส่เลข Token ที่ได้จาก Login]

Example Request URL

GET https://mt5api.toptrader.co.th/web-api/v1/deal/get?login=99000009&from=2026-01-01 00:00:00&to=2026-01-31 23:59:59&symbol=PTT
Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5...

Example Response

{
  "retcode": "0 Done",
  "answer": [{
    "Deal": "1094943",
    "Login": "99000009",
    "Symbol": "PTT",
    "Entry": "IN",
    "Type": "BUY",
    "Volume": "100",
    "Price": "33.25",
    "Commission": "-7.12",
    "Profit": "0.00",
    "Time": "2026-01-27 11:30:31"
  }]
}

6. Positions (ข้อมูลสถานะถือครองที่เปิดอยู่ปัจจุบัน)

6.1 Get Total Positions (ตรวจสอบจำนวน Position ทั้งหมดในพอร์ต)

GET api/position/get_total

Query Parameters

ParameterTypeDescription
loginstringระบุเลขล็อกอินของบัญชี

Required Header

Authorization: Bearer [ใส่เลข Token ที่ได้จาก Login]

Example Request URL

GET https://mt5api.toptrader.co.th/web-api/v1/api/position/get_total?login=99000009
Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5...

Example Response

{ "retcode" : "0 Done", "answer" : { "total" : "1" } }

6.2 Get Open Positions (รายละเอียด Position ที่ถือครองอยู่)

GET api/position/get_open_positions

Query Parameters

ParameterTypeDescription
loginstringระบุเลขล็อกอินของบัญชี
symbolstring(ไม่บังคับ) สามารถส่งแบบกลุ่มได้ เช่น PTT,TRUE

Required Header

Authorization: Bearer [ใส่เลข Token ที่ได้จาก Login]

Example Request URL

GET https://mt5api.toptrader.co.th/web-api/v1/api/position/get_open_positions?login=99000009&symbol=PTT
Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5...

Example Response

{ 
  "retcode" : "0 Done", 
  "answer" : [
    { "Position" : "3920174", "Login" : "99000009", "Symbol" : "PTT", "Volume" : "100" }
  ] 
}

7. Trade (การส่งคำสั่งซื้อขายและตรวจสอบผลลัพธ์)

7.1 Send Trade Request (ส่งชุดคำสั่งซื้อขายเข้าสู่ Server)

POST api/trade/send_request

Query Parameters

ParameterTypeDescription
loginstringระบุเลขล็อกอินของบัญชี

Required Header

Authorization: Bearer [ใส่เลข Token ที่ได้จาก Login]

Example Request URL

POST https://mt5api.toptrader.co.th/web-api/v1/api/trade/send_request?login=99000009
Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5...

โครงสร้าง Response เบื้องต้น (ทุก Action)

เมื่อระบบรับข้อมูลคำขอสำเร็จ จะส่งค่า id กลับมาเพื่อให้คุณนำไปใช้สืบค้นผลลัพธ์สุดท้ายในข้อ 7.2

{
  "id": "43",
  "result": "Please try requesting the trading result again later using the /web-api/v1/trade/get_request_result API",
  "retcode": "0 Done"
}

7.1.1 Action = 200 (Market Price)

ส่งคำสั่งแมตช์ราคาตลาดปัจจุบันทันทีเพื่อทำการเปิดสัญญา (Open Position)

ตัวอย่าง JSON Body:

{
  "Action" : "200",
  "Login" : "99000009",
  "Symbol" : "PTT",
  "Volume" : "100",
  "Type" : "0", 
  "PriceOrder" : "0",
  "TypeFill": "1",
  "Digits" : "2",
  "Comment" : "API Market Order"
}

7.1.2 Action = 201 (Pending Order)

สร้างสัญญารอเข้าซื้อหรือขายล่วงหน้าตามเงื่อนไขราคาเทริกเกอร์ที่ตั้งไว้

* หมายเหตุประเภทคำสั่ง (Type): 2 = Buy Limit, 3 = Sell Limit, 4 = Buy Stop, 5 = Sell Stop

ตัวอย่าง JSON Body:

{
  "Action" : "201",
  "Login" : "99000009",
  "Symbol" : "PTT",
  "Volume" : "100",
  "Type" : "2",
  "PriceOrder" : "33.00",
  "TypeFill": "1",
  "Digits" : "2",
  "Comment" : "API Pending"
}

7.1.3 Action = 202 (Modify Position)

แก้ไขหรือตั้งจุดตัดขาดทุน (PriceSL) และจุดทำกำไร (PriceTP) สำหรับ Position หรือสัญญาที่มีการถือครองอยู่แล้วในกระพอร์ตปัจจุบัน

ตัวอย่าง JSON Body:

{
  "Action" : "202",
  "Login" : "99000009",
  "Symbol" : "PTT",
  "PriceSL" : "31.50",
  "PriceTP" : "36.00",
  "Digits" : "2"
}

7.1.4 Action = 203 (Modify Order)

แก้ไขข้อมูลราคาและเงื่อนไขของตั๋วออเดอร์ล่วงหน้า (Pending Order) ที่ยังไม่ถูก Matching ในตลาด สัญญานี้ต้องสร้างจากระบบ MT5 Trading APII เท่านั้น

ตัวอย่าง JSON Body:

{
  "Action" : "203",
  "Login" : "99000009",
  "Symbol" : "PTT",
  "PriceOrder" : "32.75",
  "Volume" : "100",
  "Digits" : "2"
}

7.1.5 Action = 204 (Cancel Pending)

สั่งยกเลิกสัญญาและถอดถอนตั๋วคำสั่งซื้อขายล่วงหน้า (Pending Order) ออกจากกระดานจับคู่

ตัวอย่าง JSON Body:

{
  "Action" : "204",
  "Login" : "99000009",
  "Symbol" : "PTT"
}

7.2 Get Request Result (ตรวจสอบผลลัพธ์การทำงานของคำสั่งซื้อขาย)

GET api/trade/get_request_result

นำเลข id ที่ได้รับจาก Response ของข้อ 7.1 มาใส่ใน Parameter id เพื่อรับผลลัพธ์ของคำสั่งซื้อขาย

Query Parameters

ParameterTypeDescription
idstringเลข ID อ้างอิงที่ได้รับกลับมาจากขั้นตอนการ Send Request
loginstringระบุเลขล็อกอินของบัญชี

Required Header

Authorization: Bearer [ใส่เลข Token ที่ได้จาก Login]

Example Request URL

GET https://mt5api.toptrader.co.th/web-api/v1/api/trade/get_request_result?id=43&login=99000009
Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5...

Inner Retcode ที่สำคัญภายในระบบ

Retcodeความหมายและสถานะผลลัพธ์คำสั่ง
10006Request rejected - คำขอถูกระบบปฏิเสธเนื่องจากพารามิเตอร์ไม่สมบูรณ์
10008An order placed - ระบบดำเนินการออกคำสั่งซื้อขายสำเร็จเรียบร้อย
10018Market is closed - ตลาดหลักทรัพย์ปิดทำการในช่วงเวลาดังกล่าว
10019Not enough money - ยอดเงินหรือหลักประกัน (Margin) ในบัญชีไม่เพียงพอ

Limitations & Rules (ข้อจำกัดและเงื่อนไขทางเทคนิค)

  • หากวาง Pending Order ด้วยระบบไหน ต้องใช้ระบบนั้น Modify Pending เท่านั้น เช่น ถ้าวาง Pending Order ด้วย Terminal ก็ต้องใช้ Terminal Modify Pending เท่านั้น จะไม่สามารถใช้ API Modify Pending ได้
  • ชื่อ Symbol ต้องใช้ตัวพิมพ์ใหญ่เท่านั้น
  • ยังไม่รองรับการเพิ่ม Token บนเว็บ