Hello everyone!
For the life of me I can not figure this out. I'm trying to modify the take profit and stop loss of multiple trades in the same pair at different levels. Instead my EA updates all TP and SL in that pair. Can anyone figure this out? I'm giving you an example below.
for (int i = 0; i < OrdersTotal(); i++) {
OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
while(IsTradeAllowed() == false) Sleep(100);
RefreshRates();
//OrderComment();
if(OrderComment()=="Order1Buy15"){OrderModify(OrderTicket(),OrderOpenPrice(),OrderStopLoss(),OrderOpenPrice()+(MarketInfo(OrderSymbol(), MODE_POINT)*300),0,CLR_NONE);}
if(OrderComment()=="Order2Buy15"){OrderModify(OrderTicket(),OrderOpenPrice(),OrderStopLoss(),OrderOpenPrice()+(MarketInfo(OrderSymbol(), MODE_POINT)*500),0,CLR_NONE);}
if(OrderComment()=="Order3Buy15"){OrderModify(OrderTicket(),OrderOpenPrice(),OrderStopLoss(),OrderOpenPrice()+(MarketInfo(OrderSymbol(), MODE_POINT)*2000),0,CLR_NONE);}
if(OrderComment()=="Order1Sell15"){OrderModify(OrderTicket(),OrderOpenPrice(),OrderStopLoss(),OrderOpenPrice()-(MarketInfo(OrderSymbol(), MODE_POINT)*300),0,CLR_NONE);}
if(OrderComment()=="Order1Sell15"){OrderModify(OrderTicket(),OrderOpenPrice(),OrderStopLoss(),OrderOpenPrice()-(MarketInfo(OrderSymbol(), MODE_POINT)*500),0,CLR_NONE);}
if(OrderComment()=="Order1Sell15"){OrderModify(OrderTicket(),OrderOpenPrice(),OrderStopLoss(),OrderOpenPrice()-(MarketInfo(OrderSymbol(), MODE_POINT)*2000),0,CLR_NONE);}
Print(StringConcatenate("Got Here"," ",OrderSymbol(),"== ",OrderOpenPrice()));
}//end for
DO NOT manage your order basket using the OrderComment() field of your tickets.
MT4 servers can be configured to update the comments field on tickets for orders which are closed by EA.
For example, say that you have an order basket as follows:
Ticket #1234 EURUSD BUY 0.01 lots Comment = "created by EA"
Ticket #1235 EURUSD BUY 0.02 lots Comment = "created by EA"
Ticket #1236 EURUSD BUY 0.03 lots Comment = "created by EA"
If an EA attempts to close out the 0.03 ticket automatically, the server will detect that you are trying to close the larger order and will only close it partially leaving a 0.01 ticket in its place.
But the new ticket will look something like:
Ticket #1249 EURUSD BUY 0.01 lots Comment = "from ticket #1236"
The order basket would then look like:
Ticket #1234 EURUSD BUY 0.01 lots Comment = "created by EA"
Ticket #1235 EURUSD BUY 0.02 lots Comment = "created by EA"
Ticket #1249 EURUSD BUY 0.01 lots Comment = "from ticket #1236"
THAT will break your EA and cause it to improperly manage your order basket, because your EA isn't monitoring for partial ticket closures.
Also, that is going to break your EA if your trading account is a US based account, due to the FIFO requirements,
You would then need to close order #1234 before you could close the newly created #1249.
Perhaps use something like:
int target_magic = 123; //magic number that we are going to compare against for(int x=OrdersTotal()-1; x>=0; x--){ //loop through order basket if(OrderSelect(,x,SELECT_BY_POS)==true){ //Select an order via index if(OrderMagicNumber()==target_magic){ //validate the order magic number against the target_magic number variable. //if you made it here the magic number of the ticket matches the target_magic number that you are watching for. //process your order modification as you normally would. } } }
I didn't test that code, but it should be very close to what you are trying to do.
- Jack
Thanks for the response. Maybe I'm not being clear. Let's say I have 3 open orders on GBPUSD. The above code changes ALL 3 TP to 300 at the same time. Then all 3 to 500 at the same time. Then all 3 to 2000 at the same time. Obviously their comments can't match all 3 criteria. It should only match 1 at most. It's like it's picking up the correct trade but changing all open trades in that pair for some reason. I've tried all kinds of incarnations to correct this including not using the comments and each time it does this.
NFA Enforces FIFO Rule, Bans Forex Hedging in US Forex Accounts - Trading Heroes
FAQ: FIFO in the Forex Market - BabyPips.com
Thanks for the response. Maybe I'm not being clear. Let's say I have 3 open orders on GBPUSD. The above code changes ALL 3 TP to 300 at the same time. Then all 3 to 500 at the same time. Then all 3 to 2000 at the same time. Obviously their comments can't match all 3 criteria. It should only match 1 at most. It's like it's picking up the correct trade but changing all open trades in that pair for some reason. I've tried all kinds of incarnations to correct this including not using the comments and each time it does this.
In your code you have the following:
for (int i = 0; i < OrdersTotal(); i++) { OrderSelect(i, SELECT_BY_POS, MODE_TRADES); while(IsTradeAllowed() == false) Sleep(100); RefreshRates(); //OrderComment(); if(OrderComment()=="Order1Buy15"){OrderModify(OrderTicket(),OrderOpenPrice(),OrderStopLoss(),OrderOpenPrice()+(MarketInfo(OrderSymbol(), MODE_POINT)*300),0,CLR_NONE);} if(OrderComment()=="Order2Buy15"){OrderModify(OrderTicket(),OrderOpenPrice(),OrderStopLoss(),OrderOpenPrice()+(MarketInfo(OrderSymbol(), MODE_POINT)*500),0,CLR_NONE);} if(OrderComment()=="Order3Buy15"){OrderModify(OrderTicket(),OrderOpenPrice(),OrderStopLoss(),OrderOpenPrice()+(MarketInfo(OrderSymbol(), MODE_POINT)*2000),0,CLR_NONE);} if(OrderComment()=="Order1Sell15"){OrderModify(OrderTicket(),OrderOpenPrice(),OrderStopLoss(),OrderOpenPrice()-(MarketInfo(OrderSymbol(), MODE_POINT)*300),0,CLR_NONE);} if(OrderComment()=="Order1Sell15"){OrderModify(OrderTicket(),OrderOpenPrice(),OrderStopLoss(),OrderOpenPrice()-(MarketInfo(OrderSymbol(), MODE_POINT)*500),0,CLR_NONE);} if(OrderComment()=="Order1Sell15"){OrderModify(OrderTicket(),OrderOpenPrice(),OrderStopLoss(),OrderOpenPrice()-(MarketInfo(OrderSymbol(), MODE_POINT)*2000),0,CLR_NONE);} Print(StringConcatenate("Got Here"," ",OrderSymbol(),"== ",OrderOpenPrice())); }//end for
STOP using the order comment field for ticket processing and identification!
I'm not kidding here. Stop using that field. That field is subject to modification by your MT4 broker, and as such it should NEVER be used for order basket management.
Rewrite your function to use OrderMagicNumber()
Here is the documentation page for it.
https://docs.mql4.com/trading/ordermagicnumber
Also..... I can tell you from looking at your code, that it's probably only changing values like you described on SELL orders.That's a hint by the way.... Look at your validation code.
Buy orders are probably processing "correctly" using your coding logic.
Also, if you are with a United States broker, you are going to need to take the FIFO restriction into consideration as well.
- Jack
- docs.mql4.com
yes, when you open file, can only see data on past.
but it still write data, re-open file, you can see new data.
OK perfect
will you make a script?
I want to make you work
what should I do ? how does it work ?
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
Hello everyone!
For the life of me I can not figure this out. I'm trying to modify the take profit and stop loss of multiple trades in the same pair at different levels. Instead my EA updates all TP and SL in that pair. Can anyone figure this out? I'm giving you an example below.
for (int i = 0; i < OrdersTotal(); i++) {
OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
while(IsTradeAllowed() == false) Sleep(100);
RefreshRates();
//OrderComment();
if(OrderComment()=="Order1Buy15"){OrderModify(OrderTicket(),OrderOpenPrice(),OrderStopLoss(),OrderOpenPrice()+(MarketInfo(OrderSymbol(), MODE_POINT)*300),0,CLR_NONE);}
if(OrderComment()=="Order2Buy15"){OrderModify(OrderTicket(),OrderOpenPrice(),OrderStopLoss(),OrderOpenPrice()+(MarketInfo(OrderSymbol(), MODE_POINT)*500),0,CLR_NONE);}
if(OrderComment()=="Order3Buy15"){OrderModify(OrderTicket(),OrderOpenPrice(),OrderStopLoss(),OrderOpenPrice()+(MarketInfo(OrderSymbol(), MODE_POINT)*2000),0,CLR_NONE);}
if(OrderComment()=="Order1Sell15"){OrderModify(OrderTicket(),OrderOpenPrice(),OrderStopLoss(),OrderOpenPrice()-(MarketInfo(OrderSymbol(), MODE_POINT)*300),0,CLR_NONE);}
if(OrderComment()=="Order1Sell15"){OrderModify(OrderTicket(),OrderOpenPrice(),OrderStopLoss(),OrderOpenPrice()-(MarketInfo(OrderSymbol(), MODE_POINT)*500),0,CLR_NONE);}
if(OrderComment()=="Order1Sell15"){OrderModify(OrderTicket(),OrderOpenPrice(),OrderStopLoss(),OrderOpenPrice()-(MarketInfo(OrderSymbol(), MODE_POINT)*2000),0,CLR_NONE);}
Print(StringConcatenate("Got Here"," ",OrderSymbol(),"== ",OrderOpenPrice()));
}//end for