How to Modify Orders in MQL4
In earlier articles you realized how to open orders, shut orders and scan the present orders. As you need to already know, orders will be additionally modified, for instance for pending orders you might have considered trying to change the open worth, or, for market orders you might have considered trying to change the cease loss or take revenue worth. Update an order is sort of simple utilizing MQL4, very related to the usage of the OrderShut() perform, to modify an order MQL4 has the OrderModify() perform. We will see some examples right here of how to modify present orders utilizing MQL4 capabilities. We can see the main points of this perform
1 2 3 4 5 6 7 8 | bool OrderModify( int ticket, // ticket double price, // price double stoploss, // stop loss
double takeprofit, // take profit
datetime expiration, // expiration color arrow_color // color ); |
The parameters are:
- ticket to establish the ticket to modify
- worth is used to change the open worth of a pending order
- stoploss is the brand new cease loss worth
- takeprofit is the brand new take revenue worth
- expiration is to change the expiration date/time for pending orders
- arrow_color is to change the colour of the “update” arrow on the chart, or to cover it
Like once you open a brand new order keep in mind to contemplate that worth, cease loss worth and take revenue worth can’t be too shut to the present worth or the operation will fail. Why would you need to change an order? Here some examples:
Trailing cease is possibly one of the crucial standard cause, you progress the cease loss worth when the value strikes in the course of your order
Move the take revenue worth, often accomplished along with a trailing cease
change the expiration date of a pending order, to prolong it or anticipate the validity
change the open worth of a pending order in case of a distinct market situation
In the instance you’re going to see we’ll set a cease loss and take revenue worth for every order of a forex pair with no set cease loss, the code is
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 | #property copyright “Luca Spinello” #property link "https://mql4tradingautomation.com"
#property version "1.00"
#property strict #property show_inputs //Allowed Slippage extern double Slippage=3; //Allowed Slippage extern int TakeProfit=40; //Take profit in pips extern int StopLoss=20; //Stop loss in pips //As recommended we need to normalize the digits to properly calculate take profit and stop loss double CalculateNormalizedDigits() { if(Digits<=3){ return(0.01); } else if(Digits>=4){ return(0.0001); } else return(0); } //We declare a function UpdateOpenOrders of type int and we want to return //the number of orders that have been updated int UpdateOpenOrders(){ int TotalUpdated=0; //We want to count how many orders have been closed double nDigits=CalculateNormalizedDigits(); //Normalization of the slippage if(Digits==3 || Digits==5){ Slippage=Slippage*10; } //We scan all the orders backwards for( int i=OrdersTotal()–1;i>=0;i— ) { //We select the order of index i selecting by position and from the pool of market trades//If the selection is successful we proceed with the updateif(OrderSelect( i, SELECT_BY_POS, MODE_TRADES )){//We check if the order is for the same currency pair of the chart where the script is run if(OrderSymbol()==Symbol()){ double OpenPrice=0; double StopLossPrice=0; double TakeProfitPrice=0; //We get the open price OpenPrice=OrderOpenPrice(); //We calculate the stop loss and take profit price depending on the type of order if(OrderType()==OP_BUY){ StopLossPrice=NormalizeDouble(OpenPrice–StopLoss*nDigits,Digits); TakeProfitPrice=NormalizeDouble(OpenPrice+TakeProfit*nDigits,Digits); } if(OrderType()==OP_SELL){ StopLossPrice=NormalizeDouble(OpenPrice+StopLoss*nDigits,Digits); TakeProfitPrice=NormalizeDouble(OpenPrice–TakeProfit*nDigits,Digits); } if(OrderModify(OrderTicket(),OpenPrice,StopLossPrice,TakeProfitPrice,CLR_NONE)){ TotalUpdated++; } else{ Print(“Order failed to update with error – “,GetLastError()); } } //If the order is updated correcly we increment the counter of updated orders //If the order fails to be updated we print the error } //If the OrderSelect() fails we return the cause else{ Print(“Failed to select the order – “,GetLastError()); } //We can have a delay if the execution is too fast, Sleep will wait x milliseconds before proceed with the code //Sleep(300); } //If the loop finishes it means there were no more open orders for that pair return(TotalUpdated); } void OnStart() { Print(“How many orders have been modified ? “,UpdateOpenOrders()); } |
If now we have some orders open in completely different pairs we will see that solely the orders of the identical forex pair of the place the script is operating are modified.When the script is run it revealsIf we run it in the EURUSD chart the result’sWhile in the USDJPYBeing in a position to replace orders is often a elementary function for an Expert Advisor,
there are literally Expert Advisors that solely try this. Take a while to perceive the code and take a look at it together with your orders.
I keep in mind once I first began to study MQL coding,
though I had some data of coding many idea have been nonetheless unclear.
To velocity up your studying course of I might advocate to observe a fast MQL course that gives you
the basics you want to create useful Trading Robot. You can take a look at the next two course,
Udemy usually has gross sales on to make the programs much more inexpensive, and when you subscribe to my e-newsletter (high and backside of the web page) I’ll let you already know when this occurs.
MQL4 Programming for Traders: Build Robust Trading Robots!
Black Algo Trading: Build Your Trading Robot
You can discover a examples of scripts that replace orders in the obtain part and in this text. Please depart a remark in case you have any suggestions or doubt and keep in mind to like us on Social Media!
If you need to save hours of analysis and coding and also you need to see some skilled code examine this out! You can use it to construct your personal EA!
How to Modify Orders in MQL4