Hi,
the problem lies in vars OSL/NewSL, I think. You Need to change OSL once you determined the new level. Else you end up in an endless loop. Better leave out OSL, make it just SL, like this:
for(int i=1; i<=OrdersTotal(); i++) // Cycle searching in orders { if(OrderSelect(i-1,SELECT_BY_POS)==true) // Select if the next is available { // Analysis of orders: int OTicket=OrderTicket(); // Number of selected order // string OSymbol=OrderSymbol(); // Symbol of selected order int OType=OrderType(); // Type of selected order double OPrice=OrderOpenPrice(); // Price of selected order double SL=OrderStopLoss(); // SL of selected order double OTP=OrderTakeProfit(); // TP of selected order if(OrderSymbol()!=Symbol()||OType>1)continue; // The order is not "ours" continue with next OrderSelect double TS=10*5*Point; // Trail Stop x 10 // if(Use_ATR_as_Trail_Stop==true) TS=iATR(NULL,0,Count_bars,0)*ATR_factor; while(true) // Modification cycle { //int Min_Dist=MarketInfo(Symbol(),MODE_STOPLEVEL); //Min. distance //if (MathAbs(TS-SL)<= Point) // If less than allowed //TS=Min_Dist; // New value of TS bool Modify=false; // start with a Not to be modified instruction switch(OType) // Evaluate By order type { case 0 : // Order Buy double NewSL=NormalizeDouble(Bid-TS,Digits); // calculate new SL if(NewSL-SL>0.00020) // If Order Stop Loss is lower than what we want { SL=NewSL; string Text=" Buy Trade "; // Text for Buy Modify=true; // To be modified } break; // Exit 'switch' case 1 : // Order Sell NewSL=NormalizeDouble(Ask+TS,Digits); // calculate new SL if(SL-NewSL>0.00020) // If Order Stop Loss is higher than what we want { SL=NewSL; Text=" Sell Trade "; // Text for Sell Modify=true; // To be modified } break; }
Don't hard code numbers. Why bother documenting them. case 0 : // Order Buy : case 1 : // Order Sell
Just use the correct constants. case OP_BUY : : case OP_SELL :
if(OrderSelect(i-1,SELECT_BY_POS)==true) // Select if the next is available
You would never write if( (2+2 == 4) == true) would you? if(2+2 == 4) is sufficient. So Don't write if(bool == true), just use if(bool) or if(!bool). Code becomes self documenting when you use meaningful variable names, like bool isLongEnabled. Long_Entry sounds like a trigger price or a ticket number and "if long entry" is an incomplete sentence.if(OrderSelect(i-1,SELECT_BY_POS)==true) // Select if the next is available
Will this EA, never be put on multiple charts, or multiple timeframes, or run in the presence of other EAs or manual trading? Filter order accounting - MQL4 forumfor(int i=1; i<=OrdersTotal(); i++) // Cycle searching in orders
In the presence of multiple orders (one EA or multiple,) you must count down when closing/deleting/modifying in a position loop. Get in the habit of always counting down. Loops and Closing or Deleting Orders - MQL4 forum. Get in the habit of the 1960's paradigm, i=0; i < limit{select(i. Not i=1 i <= limit{ select(i-1).
Don't hard code numbers. Why bother documenting them. Just use the correct constants. - You would never write if( (2+2 == 4) == true) would you? if(2+2 == 4) is sufficient. So Don't write if(bool == true), just use if(bool) or if(!bool). Code becomes self documenting when you use meaningful variable names, like bool isLongEnabled. Long_Entry sounds like a trigger price or a ticket number and "if long entry" is an incomplete sentence.
- Will this EA, never be put on multiple charts, or multiple timeframes, or run in the presence of other EAs or manual trading? Filter order accounting - MQL4 forum
- In the presence of multiple orders (one EA or multiple,) you must count down when closing/deleting/modifying in a position loop. Get in the habit of always counting down. Loops and Closing or Deleting Orders - MQL4 forum. Get in the habit of the 1960's paradigm, i=0; i < limit{select(i. Not i=1 i <= limit{ select(i-1).
So that is why it "should be used for educational purpose only!" : https://book.mql4.com/trading/ordermodify
Just kidding, of course you're right! ;)
Hi Guys
I just wanted to say thankyou for your kindly attention to my last post and your willingness to help. I finally reached a point were I was getting mad with this issue so I tried everything that I am capable of with my current programing knowledge and therefore focused on PomeGranate indications and on Dr. Jack the Reaper principles. I started chopping the code into smaller functions and inserted several Alerts in order to get more information on when and why is the OrderModify Error 1 being generated. I learned curious things, for example on first trade that the EA took I got an Alert stating that it was going to " Modify Ticket #4 " and right after that the Order Modify Error1 showed up and the program crashed. It was clear that with just one trade generated the Ticket # 4 mesage was showing somekind of inconsistency that in my belief was on the For i=1 ; i<=OrdersTotal(); i++ function , the Select function and / or on the While(true) loop.
After changing the code on different parts to narrow the possibilities I finally reached to the conclusion that the while loop was not consistent so I changed it completely to an If construction, identified my trades with the OrderMagicNumber() function and solved the issue.
I have not slept well for the past two days so I am crashing out right now but finally with a big smile on my face because now I have left my MT4 platform with an optimization process running that is finally generating interesting results.
thanks for your help again.
Roberto
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
Hi guys
I have been working hard to get an EA send Alerts and it seems to work just fine.
Now I am working to make the EA place trades but everytime I try to set an automatic Trailing Stop feature it just doesn´t work.
I receiving these codes: OrderModify error 1
I know it means that OrderModify attempts to replace the values already set with the same values. I have tried to change my code in several ways in order to correct it but I just can´t do it alone.
Your help will be really appreciated
Here is the code