Reprint the original text: http://www.popoding.club/post/31/
In manual or automatic trading, the one click closing function is commonly used, especially when it involves risk control management, which can quickly stop loss and effectively control risk. This script function is not difficult to implement, but in the process of programmed implementation, there will be some different application scenarios.
Traverse position orders
Before executing the position closing instruction, you need to traverse all the position orders. After finding the specific order, you can realize the position closing function according to the position order number. The function you need is PositionsTotal(), get the total number of position orders, and then use the for loop to realize the traversal function.
for(int i = 0; i < PositionsTotal(); i++) { //Circulatory body }
Get order properties
In the loop body, you need to get the order number according to the order sequence. The function used is PositionGetTicket(). This function can get the order number and locate the current order before you can get the relevant order attribute information.
ulong pos_ticket = PositionGetTicket(i); //order number ulong pos_magic = PositionGetInteger(POSITION_MAGIC); //Order magic number string pos_symbol = PositionGetString(POSITION_SYMBOL); //Order variety double pos_profit = PositionGetDouble(POSITION_PROFIT); //Order profit
Closing conditions
Here you can get all the order attribute information displayed in MT5 platform, including order number, magic number, variety, profit amount, type, etc. These attributes will involve different application scenarios, such as closing all orders of a certain variety, closing all profit orders, closing all purchase orders, etc., which are realized through conditional judgment after obtaining the corresponding attributes.
In the multi variety and multi-mode complex trading system, orders need to be classified according to different mode categories for more adaptable and complex warehouse management. Here, the magic number of orders needs to be used. It can be realized by specifying the magic number coding rules when opening a warehouse. For example, 11010001, "11" represents variety EURUSD, "01" represents mode 1, "0001" represents the first order of the current variety under the current mode. Here is just an example. The coding rules can be formulated according to needs. With this magic number, you can close positions with more complex requirements by intercepting the specified contents, such as closing all loss orders in EURUSD mode 1.
Closing function
The closing function of MT5 is realized through reverse order request, that is, if the holding order is a purchase order, the closing can be realized by sending a sell order instruction and specifying the same order number, variety, transaction volume and other request information; Closing a sell order is to send a buy order.
request.position = pos_ticket; //Specify closing order number
It should be mentioned that some platform brokers have different request modes. If the mode is incorrect, the request will fail. The mode has ORDER_FILLING_FOK,ORDER_FILLING_IOC,ORDER_ FILLING_ There are three types of return. The difference is that the processing methods for part of the trading volume are different. Through the SymbolInfoInteger() function, symbol_ FILLING_ After obtaining the value of mode attribute, you can adapt in different ways.
int filling=(int)SymbolInfoInteger(Symbol(),SYMBOL_FILLING_MODE); if(filling == 1) { request.type_filling = ORDER_FILLING_FOK; //All transaction mode } else if(filling == 2) { request.type_filling = ORDER_FILLING_IOC; //Partial transaction mode } else if(filling == 3) { request.type_filling == ORDER_FILLING_RETURN; //Limit order execution mode }
Implementation source code
#property copyright "official account Luyuanmw WeChat wentxiong" #property link "http://www.popoding.club/" #property version "1.00" //+------------------------------------------------------------------+ //| Script program start function | //+------------------------------------------------------------------+ void OnStart() { //--- //Traverse warehouse receipt for(int i = 0; i < PositionsTotal(); i++) { //Circulatory body ulong pos_ticket = PositionGetTicket(i); //order number ulong pos_magic = PositionGetInteger(POSITION_MAGIC); //Order magic number string pos_symbol = PositionGetString(POSITION_SYMBOL); //Order variety double pos_profit = PositionGetDouble(POSITION_PROFIT); //Order profit double pos_volume = PositionGetDouble(POSITION_VOLUME); //Trading volume ENUM_POSITION_TYPE pos_type = (ENUM_POSITION_TYPE)PositionGetInteger(POSITION_TYPE);//Order type //close a position MqlTradeRequest request = {0}; MqlTradeResult result = {0}; double ask = SymbolInfoDouble(Symbol(),SYMBOL_ASK); double bid = SymbolInfoDouble(Symbol(),SYMBOL_BID); //Request structure request.action = TRADE_ACTION_DEAL; request.position = pos_ticket; request.symbol = pos_symbol; request.volume = pos_volume; request.deviation = 50; request.magic = pos_magic; //Specify order closing mode int filling=(int)SymbolInfoInteger(Symbol(),SYMBOL_FILLING_MODE); if(filling == 1) { request.type_filling = ORDER_FILLING_FOK; //All transaction mode } else if(filling == 2) { request.type_filling = ORDER_FILLING_IOC; //Partial transaction mode } else if(filling == 3) { request.type_filling = ORDER_FILLING_RETURN; //Limit order execution mode } //Specify magic number, variety and condition if(pos_magic == 0 && pos_symbol == Symbol()) { if(pos_type == POSITION_TYPE_SELL) { request.type = ORDER_TYPE_BUY; request.price = ask; } else if(pos_type == POSITION_TYPE_BUY) { request.type = ORDER_TYPE_SELL; request.price = bid; } //Send request command if(!OrderSend(request,result)) { printf("Ordersend Error : %d",GetLastError()); } } } } //+------------------------------------------------------------------+