You are here:
  • KB Home
  • Smart Bridge
  • 4. วิธีการเขียนส่งคำสั่งซื้อ-ขาย AFL Language ไปยัง MT4
< Back

How to Smart Bridge on AFL Language

1. ให้ทำการ Copy Code ชุดคำสั่งนี้ไว้บนส่วนหัวของโปรแกรม AFL

#include <SmartBridgeFunction.afl>
SetServer (“127.0.0.1”);
SetPort (5500);
SetKey (“รหัสอนุญาต”);

รูปตัวอย่าง

  • บรรทัดที่ 1 ส่วนหัวโปรแกรมทำหน้าที่ในการประกาศ #include
  • บรรทัดที่ 2 ตั้งค่าหมายเลข IP ที่ใช้เชื่อมต่อกับเซิร์ฟเวอร์ของ Smart Bridge
  • บรรทัดที่ 3 ตั้งค่าหมายเลข Port ที่ใช้เชื่อมต่อกับเซิร์ฟเวอร์ของ Smart Bridge
  • บรรทัดที่ 4 ตั้งค่ารหัสอนุญาต License key ที่ใช้เชื่อมต่อกับเซิร์ฟเวอร์ของ Smart Bridge 

**สามารถขอ License key ได้ที่ Line :  @Toptrader 

ตัวอย่างฟังก์ชัน คำสั่งเกี่ยวกับการซื้อ-ขาย (Trade Function)

OrderSend

คือคำสั่งหลักในการเปิดออร์เดอร์ หากส่งคำสั่งนี้จะทำการเปิดออร์เดอร์ทันที

int OrderSend (string symbol, string cmd, string comment, int volume, double

price, int slippage, double stoploss, double takeprofit,int magicnumber);

int OrderSend( // ฟังก์ชันการเปิด ออเดอร์นี้จะมีค่าส่งคืนเป็น ตัวเลขจำนวนเต็ม 
     string   symbol // Symbol  สินค้าที่ต้องการเปิด
     string   cmd,      // Operation ประเภท ออร์เดอร์
     string comment=NULL, // Comment คำอธิบาย
     int volume // Volume จำนวนสัญญาที่เปิด (lot)
     double price // price ราคาที่เปิด
     int slippage,    // Slippage ความคลาดเคลื่อนสูงสุดขณะกราฟเคลื่อนไหวที่รับได้
     double stoploss, // Stop loss จุดตัดขาดทุน
     double takeprofit // take profit จุดปิดเมื่อกำไร
     int magic=0); // magic number ตัวเลข magic ไว้ระบุออร์เดอร์

Parameter:

– symbol

     สินค้าที่ต้องการส่งคำสั่ง
– cmd
     ประเภทของการส่งคำสั่ง

OP_BUY

Buy operation (คำสั่งซื้อ)

OP_SELL

Sell operation  (คำสั่งขาย)

OP_BUYLIMIT

Buy limit pending order (ตั้งคำสั่งซื้อล่วงหน้า ราคาปัจจุบันอยู่บน)

OP_SELLLIMIT

Sell limit pending order (ตั้งคำสั่งขายล่วงหน้า ราคาปัจจุบันอยู่ล่าง)

OP_BUYSTOP

Buy stop pending order (ตั้งคำสั่งซื้อล่วงหน้า ถึงจุดแล้วขึ้นต่อ กำไร ราคาปัจจุันอยู่ล่าง ตามน้ำ)

OP_SELLSTOP

Sell stop pending order (ตั้งคำสั่งขายล่วงหน้า ถึงจุดแล้วลงต่อ กำไร ราคาปัจจุันอยู่บน ตามน้ำ)

– comment
     ส่วนแสดงคำอธิบาย ถ้าข้อความมีความยาวมากไปส่วนท้ายอาจจะถูกเปลี่ยนโดยระบบ
– volume

     จำนวนสัญญาที่เปิดหรือขนาดของสัญญา (lot)
– price
    ราคาที่เปิด
– slippage
     ความคลาดเคลื่อนสูงสุดขณะกราฟเคลื่อนไหวที่รับได้
– stoploss
     จุดตัดขาดทุน
– take profit 
     จุดทำกำไร
– magic
     ตัวเลข magic number ไว้ระบุ ออร์เดอร์
Returned value:
ฟังก์ชั่นการเปิดออร์เดอร์นี้ จะมีค่าส่งคืนเป็น ตัวเลขจำนวนเต็มของ ticket 
ถ้าแสดง เป็น -1 คือส่งคำสั่งผิด Error

 
Example:

     ตัวอย่างการส่งคำสั่ง เลือกซื้อสินค้า S50_DUMMY ,ซื้อ,แสดงคำอธิบายคว่า Amibroker ,จำนวน 2 สัญญา ,ที่ราคาปัจจุบัน ,ความคลาดเคลื่อนสูงสุดขณะกราฟเคลื่อนไหวที่รับได้ 3 จุด ,ตั้งจุดตัดขาดทุนที่ราคา 950 และ จุดทำกำไร 1000 ตั้งค่าmagic Number 654321

ticket = OrderSend(“S50_DUMMY”, “OP_BUY”, “Amibroker”, 2, 0, 3, 950, 1000, 654321);

รูปตัวอย่าง AFL Language

รูปตัวอย่าง เมื่อส่งคำสั่งคำสั่งเสร็จบน MT4

OrderModify

คำสั่งในการปรับเปลี่ยนค่าต่างๆของออร์เดอร์ที่เปิดอยู่ ทั้ง Market Order  และ Pending Order

int OrderModify (int ticket,double price,double stoploss,double takeprofit);

Parameter:
– ticket
     หมายเลขออร์เดอร์ที่ต้องการแก้ไข
– price
     ราคาของออร์เดอร์นั้นที่ต้องการแก้ไข
– stoploss
     จุดตัดขาดทุน
– takeprofit
     จุดทำกำไร
Returned value:
     ถ้าฟังก์ชั่นทำงานสำเร็จจะส่งค่ากลับมาเป็น true และถ้าส่งค่าผิดจะเป็น false
Example:

     จากรูปตัวอย่าง ต้องการแก้ไขหมายเลข Order ที่ 939464 จากราคา(Price) 920 เป็น 910, จุดตัดขาททุน(S/L) 900 เป็น 890 ,จากจุดทำกำไร(T/P) จาก 960 เป็น 940  

     ตัวอย่าง Code Program ของ Amibroker

ticket = OrderModify(939464,910,890,940);

OrderClose
Closes opened order.

int OrderClose(int ticket, int voulume, int slippage);

Parameter:
– ticket
Unique number of the order ticket.
– voulume
Number of lots.
– slippage
Value of the maximum price slippage in points.
Returned value:
– Returns 1 if successful.
– Otherwise if fail.
Example:

ret = OrderClose(12345, 1, 0);

printf(“Retutn code = ” + ret + “\n”);

OrderDelete
Deletes previously opened pending order.

int OrderDelete(int ticket);

Parameter:
– ticket
Unique number of the order ticket.
Returned value:
– Returns 1 if successful.
– Otherwise if fail.
Example:

ret = OrderDelete(12345);

printf(“Retutn code = ” + ret + “\n”);

ClosePositionAll
Closes all opened order.

int ClosePositionAll();

Parameter:

Returned value:
– Returns 1 if successful.
– Otherwise if fail.
Example:

ret = ClosePositionAll();

printf(“Retutn code = ” + ret + “\n”);

ClosePendingAll
Deletes all opened pending order.

intClosePendingAll();

Parameter:

Returned value:
– Returns 1 if successful.
– Otherwise if fail.
Example:

ret = ClosePendingAll();

printf(“Retutn code = ” + ret + “\n”);

CloseAll
Closes all opened order and deletes all opened pending order.

int CloseAll();

Parameter:

Returned value:
– Returns 1 if successful.
– Otherwise if fail.
Example:

ret = CloseAll();

printf(“Retutn code = ” + ret + “\n”);

AccountInfo
Get all account information.

void AccountInfo();

Parameter:

Returned value:
– No returns value. But can get account info by AFL function StaticVarGet(string varname) and StaticVarGetText(string varname) and varnamecan be any of the following values:

AccountInfo.Server

Trade server name.

AccountInfo.Profit

Current profit of an account in the deposit currency.

AccountInfo.AccNumber

The current account number.

AccountInfo.AccName

Client name.

AccountInfo.Margin

Account margin used in the deposit currency.

AccountInfo.Leverage

Account leverage.

AccountInfo.FreeMargin

Free margin of an account in the deposit currency.

AccountInfo.Equity

Account equity in the deposit currency.

AccountInfo.Company

Name of a company that serves the account.

AccountInfo.Balance

Account balance in the deposit currency.

AccountInfo.TradeAllow

Allowed trade for the current account.

AccountInfo.TradeMode

Account trade mode.

Example:

AccountInfo();

printf(“AccountInfo.Server = “+StaticVarGetText(“AccountInfo.Server”)+”\n”);

printf(“AccountInfo.Profit = “+StaticVarGetText(“AccountInfo.Profit”)+”\n”);

printf(“AccountInfo.AccNumber = “+StaticVarGetText(“AccountInfo.AccNumber”)+”\n”);

printf(“AccountInfo.AccName = “+StaticVarGetText(“AccountInfo.AccName”)+”\n”);

printf(“AccountInfo.Margin = “+StaticVarGetText(“AccountInfo.Margin”)+”\n”);

printf(“AccountInfo.Leverage = “+StaticVarGetText(“AccountInfo.Leverage”)+”\n”);

printf(“AccountInfo.FreeMargin = “+StaticVarGetText(“AccountInfo.FreeMargin”)+”\n”);

printf(“AccountInfo.Equity = “+StaticVarGetText(“AccountInfo.Equity”)+”\n”);

printf(“AccountInfo.Company = “+StaticVarGetText(“AccountInfo.Company”)+”\n”);

printf(“AccountInfo.Balance = “+StaticVarGetText(“AccountInfo.Balance”)+”\n”);

printf(“AccountInfo.TradeAllow = “+StaticVarGetText(“AccountInfo.TradeAllow”)+”\n”);

printf(“AccountInfo.TradeMode = “+StaticVarGetText(“AccountInfo.TradeMode”)+”\n”);

OrderActive
Get all opened order.

void OrderActive();

Parameter:

Returned value:
– No returns value. But can get all opened order by AFL function StaticVarGet(string varname) and StaticVarGetText(string varname) and varnamecan be any of the following values:

OrderActive.Order

Ticket number of the order.

OrderActive.OpenTime

Open time of the order.

OrderActive.Type

Order operation type of the order.

OrderActive.Size

Amount of lots of the order.

OrderActive.Symbol

Symbol name of the order.

OrderActive.OpenPrice

Open price of the order.

OrderActive.SL

Stop loss value of the order.

OrderActive.TP

Take profit value of the order.

OrderActive.CurrentPrice

Current price of the order.

OrderActive.Commission

Calculated commission of the order.

OrderActive.Swap

Swap value of the order.

OrderActive.Profit

Returns profit of the order.

OrderActive.Comment

Comment of the order.

TotalOrderActive

Number of all opened order.

Example:

OrderActive();

printf(“TotalOrderActive = %g\n”, StaticVarGet(“TotalOrderActive”));

total = StaticVarGet(“TotalOrderActive”);

for(i = 0; i< total; i++)

{

       printf(“OrderActive[“+i+”].Order = “+StaticVarGet(“OrderActive[“+i+”].Order”)+”\n”);

       printf(“OrderActive[“+i+”].OpenTime = “+StaticVarGetText(“OrderActive[“+i+”].OpenTime”)+”\n”);

       printf(“OrderActive[“+i+”].Type = “+ StaticVarGet(“OrderActive[“+i+”].Type”)+”\n”);

       printf(“OrderActive[“+i+”].Size = “+ StaticVarGet(“OrderActive[“+i+”].Size”)+”\n”);

       printf(“OrderActive[“+i+”].Symbol = “+ StaticVarGetText(“OrderActive[“+i+”].Symbol”)+”\n”);

       printf(“OrderActive[“+i+”].OpenPrice = “+StaticVarGet(“OrderActive[“+i+”].OpenPrice”)+”\n”);

       printf(“OrderActive[“+i+”].SL = “+StaticVarGet(“OrderActive[“+i+”].SL”)+”\n”);

       printf(“OrderActive[“+i+”].TP = “+StaticVarGet(“OrderActive[“+i+”].TP”)+”\n”);

       printf(“OrderActive[“+i+”].CurrentPrice = “+StaticVarGet(“OrderActive[“+i+”].CurrentPrice”)+”\n”);

       printf(“OrderActive[“+i+”].Commission = “+StaticVarGet(“OrderActive[“+i+”].Commission”)+”\n”);

       printf(“OrderActive[“+i+”].Swap = “+StaticVarGet(“OrderActive[“+i+”].Swap”)+”\n”);

       printf(“OrderActive[“+i+”].Profit = “+StaticVarGet(“OrderActive[“+i+”].Profit”)+”\n”);

       printf(“OrderActive[“+i+”].Comment = “+StaticVarGetText(“OrderActive[“+i+”].Comment”)+”\n”);

}

OrderPending
Get all opened pending order.

voidOrderPending();

Parameter:

Returned value:
– No returns value. But can get all opened pending order by AFL function StaticVarGet(string varname) and StaticVarGetText(string varname) and varnamecan be any of the following values:

OrderPending.Order

Ticket number of the order.

OrderPending.OpenTime

Open time of the order.

OrderPending.Type

Order operation type of the order.

OrderPending.Size

Amount of lots of the order.

OrderPending.Symbol

Symbol name of the order.

OrderPending.OpenPrice

Open price of the order.

OrderPending.SL

Stop loss value of the order.

OrderPending.TP

Take profit value of the order.

OrderPending.CurrentPrice

Current price of the order.

OrderPending.Comment<