개요
함수 OrderSend
함수정의.
bool OrderSend(
MqlTradeRequest& request, // query struct
MqlTradeResult& result // struct of the answer
);
반환값.
함수 실행성공시 : true
- 실행성공의 의미는 거래업체 서버에서 정상적으로 주문 "접수" 했다는 의미. <- 거래(=체결) 되었다는 의미 아님.
함수 실행 실패시 : false
- OrderSend fail 시 다양한 원인및 해결책 : http://igotit.tistory.com/1833
- 실패의 사유는 아래 함수인자의 구조체 MqlTradeResult 로 제공됨.
함수인자.
구조체 MqlTradeRequest
struct MqlTradeRequest
{
ENUM_TRADE_REQUEST_ACTIONS action; // Trade operation type
ulong magic; // Expert Advisor ID (magic number)
ulong order; // Order ticket
string symbol; // Trade symbol
double volume; // Requested volume for a deal in lots
double price; // Price
double stoplimit; // StopLimit level of the order
double sl; // Stop Loss level of the order
double tp; // Take Profit level of the order
ulong deviation; // Maximal possible deviation from the requested price
ENUM_ORDER_TYPE type; // Order type
ENUM_ORDER_TYPE_FILLING type_filling; // Order execution type
ENUM_ORDER_TYPE_TIME type_time; // Order expiration type
datetime expiration; // Order expiration time (for the orders of ORDER_TIME_SPECIFIED type)
string comment; // Order comment
ulong position; // Position ticket
ulong position_by; // The ticket of an opposite position
};
Identifier | Description |
TRADE_ACTION_DEAL | Place a trade order for an immediate execution with the specified parameters (market order) |
TRADE_ACTION_PENDING | Place a trade order for the execution under specified conditions (pending order) |
TRADE_ACTION_SLTP | Modify Stop Loss and Take Profit values of an opened position |
TRADE_ACTION_MODIFY | Modify the parameters of the order placed previously |
TRADE_ACTION_REMOVE | Delete the pending order placed previously |
TRADE_ACTION_CLOSE_BY | Close a position by an opposite one |
magic
sl 손절가격, tp 익절 가격 설정시 작동 방식.
- 매수 포지션 : Bid 가격 이 sl, tp 가격이 되었을 때 포지션 청산용 매도 주문 자동 실행된다.
- 매도 포지션 : Ask 가격이 sl, tp 가격이 되었을때 포지션 청산용 매수 주문 자동 실행된다.
Identifier | Description |
ORDER_TYPE_BUY | Market Buy order |
ORDER_TYPE_SELL | Market Sell order |
ORDER_TYPE_BUY_LIMIT | Buy Limit pending order |
ORDER_TYPE_SELL_LIMIT | Sell Limit pending order |
ORDER_TYPE_BUY_STOP | Buy Stop pending order |
ORDER_TYPE_SELL_STOP | Sell Stop pending order |
ORDER_TYPE_BUY_STOP_LIMIT | Upon reaching the order price, a pending Buy Limit order is placed at the StopLimit price |
ORDER_TYPE_SELL_STOP_LIMIT | Upon reaching the order price, a pending Sell Limit order is placed at the StopLimit price |
ORDER_TYPE_CLOSE_BY | Order to close a position by an opposite one |
type_filling
주문 체결 모드 . 아래 값 중 1개 지정.
ENUM_ORDER_TYPE_FILLING (체결모드)
Identifier | Description |
ORDER_FILLING_FOK | This filling policy means that an order can be filled only in the specified amount. If the necessary amount of a financial instrument is currently unavailable in the market, the order will not be executed. The required volume can be filled using several offers available on the market at the moment. |
ORDER_FILLING_IOC | This mode means that a trader agrees to execute a deal with the volume maximally available in the market within that indicated in the order. In case the the entire volume of an order cannot be filled, the available volume of it will be filled, and the remaining volume will be canceled. |
ORDER_FILLING_RETURN | This policy is used only for market orders (ORDER_TYPE_BUY and ORDER_TYPE_SELL), limit and stop limit orders (ORDER_TYPE_BUY_LIMIT, ORDER_TYPE_SELL_LIMIT, ORDER_TYPE_BUY_STOP_LIMIT and ORDER_TYPE_SELL_STOP_LIMIT ) and only for the symbols with Market or Exchange execution. In case of partial filling a market or limit order with remaining volume is not canceled but processed further. For the activation of the ORDER_TYPE_BUY_STOP_LIMIT and ORDER_TYPE_SELL_STOP_LIMIT orders, a corresponding limit order ORDER_TYPE_BUY_LIMIT/ORDER_TYPE_SELL_LIMIT with the ORDER_FILLING_RETURN execution type is created. |
상세.
- 모든 MT5 브로커는 4가지 중 1개의 모드만 지원함.
- 바이비트 MT5 의 경우 ORDER_FILLING_IOC 임.
type_time
주문 expiration 타입. 아래 값 중 1개 지정. 주문유효기간.
ENUM_ORDER_TYPE_TIME
Identifier | Description |
ORDER_TIME_GTC | Good till cancel order |
ORDER_TIME_DAY | Good till current trade day order |
ORDER_TIME_SPECIFIED | Good till expired order |
ORDER_TIME_SPECIFIED_DAY | The order will be effective till 23:59:59 of the specified day. If this time is outside a trading session, the order expires in the nearest trading time. |
상세
- 바이비트 MT5 의 경우 ORDER_TIME_GTC 임. 모든 브로커가 GTC 는 지원한다고 보면됨.
expiration
주문 expiration 시간. 앞의 type_time 에서 지정한 형식의 시간을 기록해야함.
comment
주문 관련 커멘트. 사용자 필요에 맞게 아무거나 기록해도됨. 기록 가능 최대문자수 31
- comment 는 주문 초기에만 기록 가능하며, 한번 기록된 comment 수정 불가.
position
포지션의 티켓(고유아이디) . 포지션 수정시 혹은 청산시 필수 기록되어야 함. 변수 order 의 티켓(고유아이디)과 동일하다.
position_by
반대 포지션의 티켓(고유아이디). 어떤 포지션을 동일종목의 반대 포지션으로 청산하려고 할 때의 필요함.
주문 타입별 필수 구조체 멤버변수.
Request Execution
This is a trade order to open a position in the Request Execution mode (trade upon requested prices). It requires to specify the following 9 fields:
- action
- symbol
- volume
- price
- sl
- tp
- deviation
- type
- type_filling
Also it is possible to specify the "magic" and "comment" field values.
Instant Execution
This is a trade order to open a position in the Instant Execution mode (trade by current prices). It requires specification of the following 9 fields:
- action
- symbol
- volume
- price
- sl
- tp
- deviation
- type
- type_filling
Also it is possible to specify the "magic" and "comment" field values.
Market Execution
This is a trade order to open a position in the Market Execution mode. It requires to specify the following 5 fields:
- action
- symbol
- volume
- type
- type_filling
Also it is possible to specify the "magic" and "comment" field values.
Exchange Execution
This is a trade order to open a position in the Exchange Execution mode. It requires to specify the following 5 fields:
- action
- symbol
- volume
- type
- type_filling
Also it is possible to specify the "magic" and "comment" field values.
SL & TP Modification
Trade order to modify the StopLoss and/or TakeProfit price levels. It requires to specify the following 4 fields:
- action
- symbol
- sl
- tp
- position
Trade order to place a pending order. It requires to specify the following 11 fields:
- action
- symbol
- volume
- price
- stoplimit
- sl
- tp
- type
- type_filling
- type_time
- expiration
Trade order to modify the prices of a pending order. It requires to specify the following 7 fields:
- action
- order
- price
- sl
- tp
- type_time
- expiration
Trade order to delete a pending order. It requires to specify the following 2 fields:
- action
- order
구조체 MqlTradeResult
struct MqlTradeResult
{
uint retcode; // Operation return code
ulong deal; // Deal ticket, if it is performed
ulong order; // Order ticket, if it is placed
double volume; // Deal volume, confirmed by broker
double price; // Deal price, confirmed by broker
double bid; // Current Bid price
double ask; // Current Ask price
string comment; // Broker comment to operation (by default it is filled by description of trade server return code)
uint request_id; // Request ID set by the terminal during the dispatch
uint retcode_external; // Return code of an external trading system
};
구조체 멤버변수.
Field | Description |
retcode | Return code of a trade server |
deal | Deal ticket, if a deal has been performed. It is available for a trade operation of TRADE_ACTION_DEAL type |
order | Order ticket, if a ticket has been placed. It is available for a trade operation of TRADE_ACTION_PENDING type |
volume | Deal volume, confirmed by broker. It depends on the order filling type |
price | Deal price, confirmed by broker. It depends on the deviation field of the trade request and/or on the trade operation |
bid | The current market Bid price (requote price) |
ask | The current market Ask price (requote price) |
comment | The broker comment to operation (by default it is filled by description of trade server return code) |
request_id | Request ID set by the terminal when sending to the trade server |
retcode_external | The code of the error returned by an external trading system. The use and types of these errors depend on the broker and the external trading system, to which trading operations are sent. |
함수 OrderSendAsync
함수 OrderSelect
함수 OrderGetTicket
https://www.mql5.com/en/docs/trading/ordergetticket
코드예
void OnStart()
{
//--- variables for returning values from order properties
ulong ticket;
double open_price;
double initial_volume;
datetime time_setup;
string symbol;
string type;
long order_magic;
long positionID;
//--- number of current pending orders
uint total=OrdersTotal();
//--- go through orders in a loop
for(uint i=0;i<total;i++)
{
//--- return order ticket by its position in the list
if((ticket=OrderGetTicket(i))>0)
{
//--- return order properties
open_price =OrderGetDouble(ORDER_PRICE_OPEN);
time_setup =(datetime)OrderGetInteger(ORDER_TIME_SETUP);
symbol =OrderGetString(ORDER_SYMBOL);
order_magic =OrderGetInteger(ORDER_MAGIC);
positionID =OrderGetInteger(ORDER_POSITION_ID);
initial_volume=OrderGetDouble(ORDER_VOLUME_INITIAL);
type =EnumToString(ENUM_ORDER_TYPE(OrderGetInteger(ORDER_TYPE)));
//--- prepare and show information about the order
printf("#ticket %u %s %G %s at %G was set up at %s",
ticket, // order ticket
type, // type
initial_volume, // placed volume
symbol, // symbol
open_price, // specified open price
TimeToString(time_setup)// time of order placing
);
}
}
//---
}
상위 정리
메타트레이더 5 체계정리. 활용법/지식 : http://igotit.tistory.com/1775
첫 등록 : 2018.0813
최종 수정 : 2024.09.09
단축 주소 : https://igotit.tistory.com/1815
'트레이딩 > 메타트레이더 코딩' 카테고리의 다른 글
메타트레이더 5. MQL5. 클래스 CTrade. 주문처리. (2) | 2018.08.25 |
---|---|
메타트레이더 . MQL5 Storage. 원격저장소. 버전 관리. (0) | 2018.08.25 |
메타트레이더 5. MQL5.Code Example. OnChartEvent. mouse, key. (0) | 2018.08.24 |
메타트레이더 5. MQL5 문자열 출력함수들. (0) | 2018.08.23 |
메타트레이더 5. MetaEditor. MQL5 EA 생성. (0) | 2018.08.19 |
댓글