Thanks deysmacro
I am now hav¡ng the following message when I backtest my EA: Ocurred Error 4051 and after that it shows OrderModify Error 1
I tried to find where am I sending the same values for the ordermodify function but can't figure it out
This has ocurred after the EA placed its first trade so it is related to the trailing function in my code
my best guess is that something is happening with this sentence:
double Min_Dist=MarketInfo(Symbol(),MODE_STOPLEVEL);
This where the error begins, just after the init section has been checked
2014.10.03 01:09:21.932 2014.01.02 01:00 POWER PRO V11 OCT 2014 EURUSD,H4: Alert: Occurred error 4051 2014.10.03 01:09:21.932 2014.01.02 01:00 POWER PRO V11 OCT 2014 EURUSD,H4: 10000.0 Maximum Lot Size 2014.10.03 01:09:21.932 2014.01.02 01:00 POWER PRO V11 OCT 2014 EURUSD,H4: 0.01 Minimum Lot Step 2014.10.03 01:09:21.932 2014.01.02 01:00 POWER PRO V11 OCT 2014 EURUSD,H4: 0.01 Minimum Lot Size 2014.10.03 01:09:21.932 2014.01.02 01:00 POWER PRO V11 OCT 2014 EURUSD,H4: 100000.0 Lot Size 2014.10.03 01:09:21.932 2014.01.02 01:00 POWER PRO V11 OCT 2014 EURUSD,H4: Alert: FOR EURUSD ONLY! 2014.10.03 01:09:21.929 POWER PRO V11 OCT 2014 inputs: ECN=1; Slip=2; Chk_Trend=1; EMA_Fast=21; EMA_Slow=55; EMA_Very_Slow=100; Chk_Ranging=1; Range=20; Pop_Out_Zone=1; EMA_High=20; EMA_Low=20; EMA2_5_Crossover=1; Confirm=1; MACD_Confirm=0; MACD_Fast=12; MACD_Slow=26; MACD_Signal=9; Stochastic_Confirm=0; PK=8; PD=3; RAL=5; RSI_Confirm=1; RSI_Period=10; RSI_Upper_Level=55; RSI_Lower_Level=45; RSI_SMA_Period=10; Chk_HTF=0; HTF=1440; HTF_Chk_Trend=0; HTF_Macd=0; HTF_Stochastic=0; HTF_RSI=0; Risk=2; Set_SLTP=1; Count_bars=5; Delta_pi 2014.10.03 01:09:19.134 TestGenerator: current spread 4 used
I don't know how to correct error 4051
if(Use_Fixed_TS==true) { if(Trailing_Stop(Fixed_Trail_Stop)==1) Comment("Trail Stop ok"); else { Comment("Error in Trailing Stop"); //-------------------------------------------------------------------------------- 7 -- // Error handling section //-------------------------------------------------------------------------------------- int Error=GetLastError(); // Failed :( switch(Error) // Overcomable errors { case 130:Alert("Wrong stops. Retrying."); RefreshRates(); // Update data break; // At the next iteration case 136:Alert("No prices. Waiting for a new tick.."); while(RefreshRates()==false) // To the new tick Sleep(1); // Cycle delay break; // At the next iteration case 146:Alert("Trading subsystem is busy. Retrying "); Sleep(500); // Simple solution RefreshRates(); // Update data break; // At the next iteration // Critical errors case 2 : Alert("Common error."); break; // Exit 'switch' case 5 : Alert("Old version of the client terminal."); break; // Exit 'switch' case 64: Alert("Account is blocked."); break; // Exit 'switch' case 133:Alert("Trading is prohibited"); break; // Exit 'switch' default: Alert("Occurred error ",Error); //Other errors } } // End of If Ans==false } // End of Trail Stop section return(0); // End of start function } //--------------------------------------------------------------------------------- // function Trailing Stop //--------------------------------------------------------------------------------- bool Trailing_Stop(int Stop) { bool Answer=false; string Text; int Tipo=OrderType(); // Order type int i; for(i=0; i<=OrdersTotal(); i++) // Cycle searching in orders { if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==true) // Select if the next is available { // Analysis of orders: if(OrderSymbol()!=Symbol()||Tipo>1)continue; // The order is not "ours" continue with next OrderSelect double SL=OrderStopLoss(); // SL of the selected order while(true) // Modification cycle { double TS=Stop*pips2dbl; // Trail Stop x 10 because I use a 5 digit broker double Min_Dist=MarketInfo(Symbol(),MODE_STOPLEVEL); // Min. distance Comment(MarketInfo(Symbol(),MODE_STOPLEVEL)); // if(TS<Min_Dist) TS=Min_Dist; // If less than allowed New value of TS bool Modify=false; // start with a Not to be modified instruction switch(Tipo) // Evaluate By order type { case 0 : // Case Buy Order SL=NormalizeDouble(Bid-TS,Digits); // calculate new SL if(OrderStopLoss()<SL) // If Order Stop Loss is lower than what we want { Text=" Buy "; // Text for Buy Modify=true; // To be modified } break; // Exit 'switch' case 1 : // Case Sell Order SL=NormalizeDouble(Ask+TS,Digits); // calculate new SL if(OrderStopLoss()>SL) // If Order Stop Loss is higher than what we want { Text=" Sell "; // Text for Sell Modify=true; // To be modified } } // End of 'switch' if(Modify==false)break; // If it is not modified exit while Alert("Modification to",Text,"#",OrderTicket()," - Awaiting response.."); Answer=OrderModify(OrderTicket(),OrderOpenPrice(),SL,OrderTakeProfit(),0); //Modify it! if(Answer==true) // Got it! :) { Alert("Order ",Text,OrderTicket()," is modified:)"); break; // From modification cycle. } } // End of modification cycle while (true) continue; // continue with next OrderSelect } // End of select operator } // End of order search return(Answer); } //+------------------------- the end ------------------------------------------------+
Will you please check my code and see if you can find where the error is?
best regards
roberto
You probably really don't want to be using
if(OrderStopLoss()<SL)
Better to use
if(SL-OrderStopLoss()>Point)
And then, do you really want to modify every Point?
Maybe
if(SL-OrderStopLoss()>pips2dbl)
Also this
double TS=Stop*pips2dbl; // double Min_Dist=MarketInfo(Symbol(),MODE_STOPLEVEL); // // if(TS<Min_Dist) TS=Min_Dist;
In your case, you say that STOPLEVEL =0, but if it didn't it would be a value in points. Ie if 2 pips it could be 20 points
Maybe use
double Min_Dist=MarketInfo(Symbol(),MODE_STOPLEVEL)*Point;
Thanks GumRai
I will work on your suggestions and will post my results. It looks like you have hit the nail on the head.
Thanks very much I really appreciate your help.
best regards
JHF
Hi Sir
I was able to correct OrderModify Error 1, the EA is taking trades , buy and sell with no problema at all.
But once I activate the Trailing Stop feature, Error 4051 shows up and the trailing stop is not working.
Since I have done some changes to my code I would like if someone can check it again and maybe find what is wrong in it.
I am trying to use a fixed size (let´s say 30 pips) for the Trail Stop but it is not working.
Another important piece of information is that my broker has a Min_Dist=MarketInfo(Symbol(),MODE_STOPLEVEL) that is 0.0
Don´t know if this could be part of the problem
//-------------------------------------------------------------------------------------------+ // Set Trailing Stop //-------------------------------------------------------------------------------------------+ double TS_EMAH0=iMA(NULL,0,TS_EMA,0,1,PRICE_HIGH,0); double TS_EMAL0=iMA(NULL,0,TS_EMA,0,1,PRICE_LOW,0); if(Use_Fixed_TS==true) { if(Trailing_Stop(Fixed_Trail_Stop)==1) Comment("Trail Stop ok"); else { Comment("Error in Trailing Stop"); //-------------------------------------------------------------------------------- 7 -- // Error handling section //-------------------------------------------------------------------------------------- int Error=GetLastError(); // Failed :( switch(Error) // Overcomable errors { case 130:Alert("Wrong stops. Retrying."); RefreshRates(); // Update data break; // At the next iteration case 136:Alert("No prices. Waiting for a new tick.."); while(RefreshRates()==false) // To the new tick Sleep(1); // Cycle delay break; // At the next iteration case 146:Alert("Trading subsystem is busy. Retrying "); Sleep(500); // Simple solution RefreshRates(); // Update data break; // At the next iteration // Critical errors case 2 : Alert("Common error."); break; // Exit 'switch' case 5 : Alert("Old version of the client terminal."); break; // Exit 'switch' case 64: Alert("Account is blocked."); break; // Exit 'switch' case 133:Alert("Trading is prohibited"); break; // Exit 'switch' default: Alert("Occurred error ",Error); // Other errors } } // End of If Ans==false } // End of Trail Stop section return(0); // End of start function } //--------------------------------------------------------------------------------- // function Trailing Stop //--------------------------------------------------------------------------------- bool Trailing_Stop(int Stop) { bool Answer=false; string Text; int Tipo=OrderType(); // Order type int i; for(i=0; i<=OrdersTotal(); i++) // Cycle searching in orders { if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==true) // Select if the next is available { // Analysis of orders: if(OrderSymbol()!=Symbol()||Tipo>1)continue; // The order is not "ours" continue with next OrderSelect double SL=OrderStopLoss(); // SL of the selected order while(true) // Modification cycle { double TS=Stop*pips2points; // Trail Stop in 0.00010 format double Min_Dist=MarketInfo(Symbol(),MODE_STOPLEVEL)*Point; // Min. distance in 0.00010 format not in Points (10) if(TS<Min_Dist) TS=Min_Dist; // If less than allowed New value of TS bool Modify=false; // start with a Not to be modified instruction switch(Tipo) // Evaluate By order type { case 0 : // Case Buy Order SL=NormalizeDouble(Bid-TS*pips2dbl,Digits); // calculate new SL if(SL-OrderStopLoss()>pips2dbl) // If Order Stop Loss is lower than what we want { Text=" Buy "; // Text for Buy Modify=true; // To be modified } break; // Exit 'switch' case 1 : // Case Sell Order SL=NormalizeDouble(Ask+TS*pips2dbl,Digits); // calculate new SL if(OrderStopLoss()-SL>pips2dbl) // If Order Stop Loss is higher than what we want { Text=" Sell "; // Text for Sell Modify=true; // To be modified } } // End of 'switch' if(Modify==false)break; // If it is not modified exit while Alert("Modification to",Text,"#",OrderTicket()," - Awaiting response.."); Answer=OrderModify(OrderTicket(),OrderOpenPrice(),SL,OrderTakeProfit(),0); //Modify it! if(Answer==true) // Got it! :) { Alert("Order ",Text,OrderTicket()," is modified:)"); break; // From modification cycle. } } // End of modification cycle while (true) continue; // continue with next OrderSelect } // End of select operator } // End of order search return(Answer); }
In your code
double TS=Stop*pips2points; // Trail Stop in 0.00010 format double Min_Dist=MarketInfo(Symbol(),MODE_STOPLEVEL)*Point; // Min. distance in 0.00010 format not in Points (10) if(TS<Min_Dist) TS=Min_Dist; // If less than allowed New value of TS bool Modify=false; // start with a Not to be modified instruction switch(Tipo) // Evaluate By order type { case 0 : // Case Buy Order SL=NormalizeDouble(Bid-TS*pips2dbl,Digits); // calculate new SL if(SL-OrderStopLoss()>pips2dbl) // If Order Stop Loss is lower than what we want { Text=" Buy "; // Text for Buy Modify=true; // To be modified }
SL=NormalizeDouble(Bid-TS*pips2dbl,Digits);
equates to
SL=NormalizeDouble(Bid-Stop*pips2points*pips2dbl,Digits);
Hi Sir
I checked the code and did some changes, will you please check again?
My Screen is showing the comment " Error in Trailing Stop"
I am receiving the same alert on every ticket: Ocurred error 4051
I checked the code and did some changes following your last message, I was confusing points with pips, but once I changed the code the Trailing stop started to work just fine, the only problem is that I am getting this very annoying alert on every ticket.
There must be something else that I am not seeing so I have copied almost all my code, I just left out the openbuy, opensell and close trades section and a couple of special funtions, but otherwise from that the code is complete, so I must have an error somewhere else, hope you can find it.
Thanks in advance for your help. JHF
//+-------------------------------------------------------------------+ //| News Trading EA | //| Copyright © 2014, XXXXXXXXXXXXX | //| | //+-------------------------------------------------------------------+ #property copyright "News Trading EA" #property version "3.2" #property description "EA based on trend confirmation, 10-20 EMA crossover" #property description "and RSI as entry signal" #property strict //---- HEADER input parameters
//---many input parameters, I think there are not the problem here //--- variables globales int Contador=0; double pips2points, // slippage 3 pips 3=points 30=points pips2dbl; // Stoploss 15 pips 0.0015 0.00150 //int Digits2pips; // DoubleToStr(dbl/pips2dbl, Digitspips) //+------------------------------------------------------------------+ //| expert initialization function | //+------------------------------------------------------------------+ int init() { if (Digits == 5 || Digits == 3) { // Adjust for five (5) digit brokers. pips2dbl=Point*10; pips2points=10; // Digits2pips=1; } else { pips2dbl=Point; pips2points=1; // Digits2pips=0; } Alert("FOR EURUSD ONLY!"); Print(MarketInfo(Symbol(), MODE_LOTSIZE)," Lot Size"); Print(MarketInfo(Symbol(), MODE_MINLOT)," Minimum Lot Size"); Print(MarketInfo(Symbol(), MODE_LOTSTEP)," Minimum Lot Step"); Print(MarketInfo(Symbol(), MODE_MAXLOT)," Maximum Lot Size"); Print(MarketInfo(Symbol(), MODE_STOPLEVEL)," Minimum Distance"); return(0); } //+------------------------------------------------------------------+ //| expert start function | //+------------------------------------------------------------------+ int start() { //----inicializa variables locales int Slippage=Slip*10; int i,haltb=0,halts=0,haltbx=0,haltsx=0; //-------------------------------------------------------------------+ // time check //-------------------------------------------------------------------+ int TradeTimeOk=0; if((DayOfWeek()==StartDay)&&(Hour()>=StartHour))TradeTimeOk=1; if((DayOfWeek()>StartDay)&&(DayOfWeek()<EndDay))TradeTimeOk=1; if((DayOfWeek()==EndDay)&&(Hour()<=EndHour))TradeTimeOk=1; //-------------------------------------------------------------------+ //Check open orders and count them //-------------------------------------------------------------------+ if(OrdersTotal()>0) { for(i=0; i<=OrdersTotal(); i++) // Cycle searching in orders { if(OrderSelect(i,SELECT_BY_POS)==true) // If there is an order { if(OrderMagicNumber()==MagicNumber1) {haltb=haltb+1;} //if there is an open buy order haltb=increase on 1 if(Modify_other_orders==true && OrderType()==OP_BUY) haltbx=haltbx+1; //count all buy orders, not only "ours" if(OrderMagicNumber()==MagicNumber2) {halts=halts+1;} //if there is an open sell order halts=increase on 1 if(Modify_other_orders==true && OrderType()==OP_SELL) haltsx=haltsx+1; //count all buy orders, not only "ours" } } } //-------------------------------------------------------------------+ // Parameters needed //-------------------------------------------------------------------+ //--- Moving Average values double EmaT1=iMA(NULL,0,EMA_Fast,0,1,PRICE_CLOSE,0); double EmaT2=iMA(NULL,0,EMA_Slow,0,1,PRICE_CLOSE,0); double EmaT3=iMA(NULL,0,EMA_Very_Slow,0,1,PRICE_CLOSE,0); double EMAHS1=NormalizeDouble(iMA(NULL,0,EMA_Signal,0,1,PRICE_HIGH,1),Digits)+Extra_pips*pips2dbl; double EMALS1=NormalizeDouble(iMA(NULL,0,EMA_Signal,0,1,PRICE_LOW,1),Digits)-Extra_pips*pips2dbl; double EMA2=NormalizeDouble(iMA(NULL,0,2,0,1,PRICE_CLOSE,1),Digits); double EMA5=NormalizeDouble(iMA(NULL,0,5,0,1,PRICE_CLOSE,1),Digits); double OPEN1=NormalizeDouble(iOpen(NULL,0,1),Digits); double CLOSE1=NormalizeDouble(iClose(NULL,0,1),Digits); double LOW1=NormalizeDouble(iLow(NULL,0,1),Digits); double HIGH1=NormalizeDouble(iHigh(NULL,0,1),Digits); //-------------------------------------------------------------------+ //trades //-------------------------------------------------------------------+ double SLb=0,SLs=0,TPb=0,TPs=0,SLpips=0,TPpips=0; double R0,R1,R2,R3; int T=0,Z=0; double VarLot; bool openbuy=0,opensell=0; bool Trade=false; // start with Trade=false then check if all conditions are met //--- Open buy: Call Trend function, Check HOUR, Check Open buy Trades, Check all buy conditions //-----------confidential code // Open sell //-----------confidential code //--- Close open trades //-----------confidential code //--- Modify Open Orders bool OM=0; if(OrdersTotal()>0) // Check if there are open orders { if(Set_SL_TP==true) { if(opensell==true || openbuy==true) // If a new Order has been Opened { for(i=0; i<=OrdersTotal(); i++) // Cycle searching in orders { if(OrderSelect(i,SELECT_BY_POS)==true) // If the next is available { if((OrderMagicNumber()==MagicNumber1)&&(OrderSymbol()==Symbol())) //openbuy order { if(OrderTakeProfit()==0) { OM=OrderModify(OrderTicket(),OrderOpenPrice(),OrderStopLoss(),TPb,0,Blue); if(OM==true) Alert("Order Buy - Initial TP Modified"); } if(OrderStopLoss()==0) { OM=OrderModify(OrderTicket(),OrderOpenPrice(),SLb,TPb,0,Red); if(OM==true) Alert("Order Buy - Initial SL Modified"); } } if((OrderMagicNumber()==MagicNumber2)&&(OrderSymbol()==Symbol())) //opensell order { if(OrderTakeProfit()==0) { OM=OrderModify(OrderTicket(),OrderOpenPrice(),OrderStopLoss(),TPs,0,Blue); if(OM==true) Alert("Order Sell - Initial TP Modified"); } if(OrderStopLoss()==0) { OM=OrderModify(OrderTicket(),OrderOpenPrice(),SLs,TPs,0,Red); if(OM==true) Alert("Order Sell - Initial SL Modified"); } } } } } } // Stop if Set_SLTP==true } // End of If OrdersTotal >0 //-------------------------------------------------------------------------------------------+ // Set Trailing Stop //-------------------------------------------------------------------------------------------+ if(OrdersTotal()>0) // Check if there are Open Orders { if(Use_Fixed_TS==true || Use_Variable_TS==true) // Check if Trail Stop is needed { double TS_EMAH0=iMA(NULL,0,TS_EMA,0,1,PRICE_HIGH,0); double TS_EMAL0=iMA(NULL,0,TS_EMA,0,1,PRICE_LOW,0); int X=0,F=0,V=0; if(Use_Fixed_TS==true) {F=1; X=1;} if(Use_Variable_TS==true) {V=1; X=2;} if(F==1&&V==1) X=3; bool A=false; if(haltb>0||haltbx>0) // One or more Open Buy Orders { switch(X) { case 0: // Case 0 is Not possible, any way, just get out of the switch loop break; case 1: // Call the Trailing Stop Function - fixed pips trail stop A=Trailing_Stop(Fixed_Trail_Stop); break; case 2: // Call the Trailing Stop Function - variable pips trail stop A=Trailing_Stop((Bid-TS_EMAL0)*10000); break; case 3: Alert(" Wrong setting , choose one type of trailing stop only"); } } if(halts>0||haltsx>0) { switch(X) { case 0: // Case 0 is Not possible, any way, just get out of the switch loop break; case 1: // Call the Trailing Stop Function - fixed pips A=Trailing_Stop(Fixed_Trail_Stop); break; case 2: // Call the Trailing Stop Function - variable pips A=Trailing_Stop((TS_EMAH0-Ask)*10000); break; case 3: Alert(" Wrong setting, choose one type of trailing stop only"); } } if(A==1) Comment("Trail Stop ok"); else { Comment("Error in Trailing Stop"); //-------------------------------------------------------------------------------- 7 -- // Error handling section //-------------------------------------------------------------------------------------- int Error=GetLastError(); // Failed :( switch(Error) // Overcomable errors { case 130: Alert("Wrong stops. Retrying."); RefreshRates(); // Update data break; // At the next iteration case 136: Alert("No prices. Waiting for a new tick.."); while(RefreshRates()==false) // To the new tick Sleep(1); // Cycle delay break; // At the next iteration case 146: Alert("Trading subsystem is busy. Retrying "); Sleep(500); // Simple solution RefreshRates(); // Update data break; // At the next iteration // Critical errors case 2: Alert("Common error."); break; // Exit 'switch' case 5: Alert("Old version of the client terminal."); break; // Exit 'switch' case 64: Alert("Account is blocked."); break; // Exit 'switch' case 133: Alert("Trading is prohibited"); break; // Exit 'switch' default: Alert("Occurred error ",Error); // Other errors } } // End of "If A==true else" condition } // End of if Use_TS Section } // End of Modify Open Orders section return(0); } // End of start function //-----------------------------------------------------------------------------------+ // function Trailing Stop //-----------------------------------------------------------------------------------+ bool Trailing_Stop(int Stop) { bool Answer=false; string Text; int i; for(i=0; i<=OrdersTotal(); i++) // Cycle searching in orders { if(OrderSelect(i,SELECT_BY_POS)==true) // Select if the next is available { // Analysis of orders: int Tipo=OrderType(); if(OrderSymbol()!=Symbol()||Tipo>1)continue; // The order is not "ours" continue with next OrderSelect double SL=OrderStopLoss(); // SL of the selected order while(true) // Modification cycle { double TS_Dis=Stop*pips2points; // Trail Stop in points (*10 if ECN Broker) format if(Send_Alerts==true) Alert("TS_Dis=",TS_Dis); double Min_Dist=MarketInfo(Symbol(),MODE_STOPLEVEL); // Min. distance is in Points (*10 if ECN Broker) format if(Send_Alerts==true) Alert("Min_Dis=",Min_Dist); if(TS_Dis<Min_Dist) TS_Dis=Min_Dist; // If less than allowed New value of TS bool Modify=false; // start with a Not to be modified instruction switch(Tipo) // Evaluate By order type { case OP_BUY : // Case Buy Order SL=NormalizeDouble(Bid-TS_Dis*Point,Digits); // calculate new SL if(Send_Alerts==true) Alert("SL=",SL); if(SL-OrderStopLoss()>pips2dbl) // If Order Stop Loss is lower than what we want { if(Send_Alerts==true) Alert("SL-OrderStopLoss=",SL-OrderStopLoss()); Text=" Buy "; // Text for Buy Modify=true; // To be modified } break; // Exit 'switch' case OP_SELL : // Case Sell Order SL=NormalizeDouble(Ask+TS_Dis*Point,Digits); // calculate new SL if(Send_Alerts==true) Alert("SL=",SL); if(OrderStopLoss()-SL>pips2dbl) // If Order Stop Loss is higher than what we want { if(Send_Alerts==true) Alert("OrderStopLoss-SL=",OrderStopLoss()-SL); Text=" Sell "; // Text for Sell Modify=true; // To be modified } } // End of 'switch' if(Modify==false)break; // If it is not modified exit while Alert("Modification to",Text,"#",OrderTicket()," - Awaiting response.."); Answer=OrderModify(OrderTicket(),OrderOpenPrice(),SL,OrderTakeProfit(),0,Green); //Modify it! if(Answer==true) // Got it! :) { Alert("Order ",Text,OrderTicket()," is modified:)"); break; // From modification cycle. } } // End of modification cycle while (true) continue; // continue with next OrderSelect } // End of select operator } // End of order search return(Answer); } // End of Function Trailing Stop
- 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 am trying to adjust my EA to a 5 digit broker but can't get it done.
I am using this code (see SRC below) to convert my Digits to a 5 digit broker but when I compile the code I get the following errors
'.' - semicolon expected
pips - struct or class type expected
I really don't know what is this part of the code supposed to do (int Digits.pip;) never seen such an expression, can anyone tell me what is wrong here?