Error : cannot modify order and error 4051

 
Hi, I have the error message when I backtest my code. I wanted to write a code which move the SL to breakeven the pair moves 5.1 pips in the good direction. But so far it does not work, actually sometimes the modifcation works (really few times) and usually I receive the error 0 and 4051.
Does someone knows something about this kind of error. I tried to change the SL, the TP and the trailling stop but so far nothing is working. Here is my code :

//+------------------------------------------------------------------+
//| HAtrend.mq4 |
//| Copyright fxinvjp |
//| http://www.fxinvjp.com |
//+------------------------------------------------------------------+
#property copyright "Copyright fxinvjp"
#property link "http://www.fxinvjp.com"

//---- input parameters
extern int ATRperiod=20;
extern int ATRmedium=10;
extern int ATRfast=5;
extern int MaMethod=2 ;
extern int MaPeriod= 6;
extern int MaMethod2= 3;
extern int MaPeriod2= 2;
extern int FisherPeriod=10;
extern int magic = 01;
extern double Lots = 0.2;
extern double CloseLots = 0.1;
extern double TrailingStop = 5.0;

double CoeffTP= 3.0;
double CoeffSL= 2.5 ;

extern double MoneyManagpercent = 0.05;



// magique number

#define magicscalping 10
#define magicH4 240
#define magicH1 60
#define magicClassic 0

//Historic ATR

#define HATRname "ATRDiff"
#define bearish 1
#define bullish 2 //break HA parameter

//Historic Volume

#define HVolumename "HistVolume"


//Daily Pivot

#define DPname "DailyPivot"

//HA parameter

#define HAname "Heiken_Ashi_Smoothed"

//time parameter

int oldTime;

//Pivot data

double pivot;
double S1;
double S2;
double R1;
double R2;

//exit strategy

#define ExHelp "0: Fixed SL/TP, 1: Close Half and Fixed Trailing Stop, 2: Close Half and Trailing stop on closed Bars"
#define ExStrategy 0

#define FixedTPSL 0
#define HalfFixTrailingStop 1
#define HalfClosedBar 2

//MoneyManagement

#define MoneyHelp "0:Fixed Lots, 1:Percent of the account"
#define MoneyManagMethos 0

#define FixedLots 0
#define PercentMang 1


//pair

#define PairHelp "0: GBP/USD, 1: GBP/JPY, 2: AUD/USD. 3: EUR/USD, 4: EUR/JPY"

#define GBPUSD 0
#define GBPJPY 1
#define AUDUSD 2
#define EURUSD 3
#define EURJPY 4

#define Pairchoisie 0
//Status

#define StatusEnable 0
#define StatusDisable 1

int EAStatus;

//Extras

datetime prevtime;


//EA info

#define EA_NAME "HATrading"
#define EA_VERSION "1.0"


//+------------------------------------------------------------------+
//| expert initialization function |
//+------------------------------------------------------------------+
int init()
{


EAStatus = StatusEnable;
Comment("HATrend activate");

switch(ExStrategy)
{
case FixedTPSL:
break;
case HalfFixTrailingStop:
break;
case HalfClosedBar:
break;
default :
Print("Invalid Exit Strategy");
Comment("HATrend inactivate");
EAStatus = StatusDisable;
return(0);
}

switch(MoneyManagMethos)
{
case FixedLots:
break;
case PercentMang:
break;
default :
Print("Invalid Money Management");
Comment("EA inactivate");
EAStatus = StatusDisable;
return(0);
}

switch(Pairchoisie)
{
case GBPUSD:
CoeffTP = 3.0;
CoeffSL = 2.5;
break;
case GBPJPY:
CoeffTP = 3.0;
CoeffSL = 2.5;
break;
case AUDUSD:
CoeffTP = 1.5;
CoeffSL = 1.0;
break;
case EURJPY:
CoeffTP = 2.5;
CoeffSL = 2.0;
break;
case EURUSD:
CoeffTP = 2.5;
CoeffSL = 2.0;
break;
default:
Print("Error choice of pair");
Comment("EA inactivate");
EAStatus = StatusDisable;
return(0);
}
}
//+------------------------------------------------------------------+
//| expert deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
//----

//----
return(0);
}
//+------------------------------------------------------------------+
//| expert start function |
//+------------------------------------------------------------------+
int start()
{

if(EAStatus==StatusDisable)
{
Comment("EA disable");
return(1);
}

if (Bars<=10)
{
Print("Not Enough Bars");
return(0);
}


double SL,TP;
int shift=1;
int ticket, ticketTS;
int total= OrdersTotal();

if(prevtime==Time[0])
return(0);
prevtime=Time[0];



if(total<1)
{


if(iClose(NULL,PERIOD_M30,shift)>iCustom(NULL,PERIOD_M30,HAname,MaMethod,MaPeriod,MaMethod2,MaPeriod2,0,shift)&&ReverseHA(shift,PERIOD_M30)==bearish&&GetHATR(shift,PERIOD_M30)>0)
{
if(iStochastic(NULL,PERIOD_M30,15,3,3,MODE_SMMA,1,MODE_MAIN,1)<=20.0||iStochastic(NULL,PERIOD_M30,15,3,3,MODE_SMMA,1,MODE_MAIN,2)<=20.0||iStochastic(NULL,PERIOD_M30,15,3,3,MODE_SMMA,1,MODE_SIGNAL,2)<=20.0||iStochastic(NULL,PERIOD_M30,15,3,3,MODE_SMMA,1,MODE_SIGNAL,1)<=20.0)
{
RefreshRates();


SL = NormalizeDouble(iLow(NULL,PERIOD_M30,shift),Digits);
if(SL<=200.0*Point)
{
Print("Stop loss < 10 pips");
return(0);
}
else
{
// TP = NormalizeDouble(2*Ask-SL,Digits);
TP = Ask+200.0*Point;
ticket = OrderSend(Symbol(),OP_BUY,Lots,Ask,3,SL,TP,"HAtrend M30",magicscalping,0,Cyan);

if(ticket<=0)
{
Print("Error cannot open the trade: ",GetLastError());
return(0);
}
else if(ticketTS<=0)
{
Print("Error cannot open the trade: ",GetLastError());
return(0);
}
else
{
if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES))
Alert("Buy Order Open : "+OrderOpenPrice());
Comment("Buy Order Open : "+OrderOpenPrice()+" TP = "+TP);
return(0);
}
}
}
}


if(iClose(NULL,PERIOD_M15,shift)>iCustom(NULL,PERIOD_M15,HAname,MaMethod,MaPeriod,MaMethod2,MaPeriod2,0,shift)&&ReverseHA(shift,PERIOD_M15)==bearish&&GetHATR(shift,PERIOD_M15)>0&&iATR(NULL,PERIOD_M15,5,0)>iATR(NULL,PERIOD_M15,20,0))
{
if(iStochastic(NULL,PERIOD_M15,15,3,3,MODE_SMMA,1,MODE_MAIN,1)<=20.0||iStochastic(NULL,PERIOD_M15,15,3,3,MODE_SMMA,1,MODE_MAIN,2)<=20.0||iStochastic(NULL,PERIOD_M15,15,3,3,MODE_SMMA,1,MODE_SIGNAL,2)<=20.0||iStochastic(NULL,PERIOD_M15,15,3,3,MODE_SMMA,1,MODE_SIGNAL,1)<=20.0)
{
RefreshRates();


SL = NormalizeDouble(iLow(NULL,PERIOD_M15,shift),Digits);
if(SL<=200.0*Point)
{
Print("Stop loss < 10 pips");
return(0);
}
else
{
// TP = NormalizeDouble(2*Ask-SL,Digits);
TP = Ask+200.0*Point;
ticket = OrderSend(Symbol(),OP_BUY,Lots,Ask,3,SL,TP,"HAtrend M15 rebound",magicscalping,0,Cyan);

if(ticket<=0)
{
Print("Error cannot open the trade: ",GetLastError());
return(0);
}
else if(ticketTS<=0)
{
Print("Error cannot open the trade: ",GetLastError());
return(0);
}
else
{
if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES))
Alert("Buy Order Open : "+OrderOpenPrice());
Comment("Buy Order Open : "+OrderOpenPrice()+" TP = "+TP);
return(0);
}
}
}
}

if(iClose(NULL,PERIOD_M30,shift)<iCustom(NULL,PERIOD_M30,HAname,MaMethod,MaPeriod,MaMethod2,MaPeriod2,0,shift)&&ReverseHA(shift,PERIOD_M30)==bullish&&GetHATR(shift,PERIOD_M30)>0)
{
if(iStochastic(NULL,PERIOD_M30,15,3,3,MODE_SMMA,1,MODE_MAIN,1)>=80.0||iStochastic(NULL,PERIOD_M30,15,3,3,MODE_SMMA,1,MODE_MAIN,2)>=80.0||iStochastic(NULL,PERIOD_M30,15,3,3,MODE_SMMA,1,MODE_SIGNAL,2)>=80.0||iStochastic(NULL,PERIOD_M30,15,3,3,MODE_SMMA,1,MODE_SIGNAL,1)>=80.0)
{
RefreshRates();


SL = NormalizeDouble(iHigh(NULL,PERIOD_M30,shift),Digits);
if(SL<=200.0*Point)
{
Print("Stop loss < 10 pips");
return(0);
}
else
{
// TP = NormalizeDouble(2*Bid-SL,Digits);
TP = Bid-200.0*Point;
ticket = OrderSend(Symbol(),OP_SELL,Lots,Bid,3,SL,TP,"HAtrend M30",magicscalping,0,Orange);

if(ticket<=0)
{
Print("Error cannot open the trade: ",GetLastError());
return(0);
}
else if(ticketTS<=0)
{
Print("Error cannot open the trade: ",GetLastError());
return(0);
}
else
{
if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES))
Alert("SELL Order Open : "+OrderOpenPrice());
Comment("SELL Order Open : "+OrderOpenPrice()+" TP = "+TP);
return(0);
}
}

}
}


if(iClose(NULL,PERIOD_M15,shift)<iCustom(NULL,PERIOD_M15,HAname,MaMethod,MaPeriod,MaMethod2,MaPeriod2,0,shift)&&ReverseHA(shift,PERIOD_M15)==bullish&&GetHATR(shift,PERIOD_M15)>0&&iATR(NULL,PERIOD_M15,5,0)>iATR(NULL,PERIOD_M15,20,0))
{
if(iStochastic(NULL,PERIOD_M15,15,3,3,MODE_SMMA,1,MODE_MAIN,1)>=80.0||iStochastic(NULL,PERIOD_M15,15,3,3,MODE_SMMA,1,MODE_MAIN,2)>=80.0||iStochastic(NULL,PERIOD_M15,15,3,3,MODE_SMMA,1,MODE_SIGNAL,2)>=80.0||iStochastic(NULL,PERIOD_M15,15,3,3,MODE_SMMA,1,MODE_SIGNAL,1)>=80.0)
{
RefreshRates();


SL = NormalizeDouble(iHigh(NULL,PERIOD_M15,shift),Digits);
if(SL<=200.0*Point)
{
Print("Stop loss < 10 pips");
return(0);
}
else
{
// TP = NormalizeDouble(2*Bid-SL,Digits);
TP = Bid-200.0*Point;
ticket = OrderSend(Symbol(),OP_SELL,Lots,Bid,3,SL,TP,"HAtrend M15 rebound",magicscalping,0,Orange);

if(ticket<=0)
{
Print("Error cannot open the trade: ",GetLastError());
return(0);
}
else if(ticketTS<=0)
{
Print("Error cannot open the trade: ",GetLastError());
return(0);
}
else
{
if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES))
Alert("SELL Order Open : "+OrderOpenPrice());
Comment("SELL Order Open : "+OrderOpenPrice()+" TP = "+TP);
return(0);
}
}
}
}
}

int counter=OrdersTotal();

for(int m=0;m<counter;m++)
{
if(OrderSelect(m,SELECT_BY_POS,MODE_TRADES))
{
if(OrderType()==OP_BUY&&OrderSymbol()==Symbol()&&OrderMagicNumber()==magicscalping)
{
if(Bid>=OrderOpenPrice()+51.0*Point)
{
bool check = OrderModify(OrderTicket(),OrderOpenPrice(),Bid-Point*51.0,OrderTakeProfit()+51.0*Point,0);

if(!check)
{
Print("Cannot Modify the Order "+GetLastError());
Alert("Cannot Modify the Order "+GetLastError());
return(0);
}
else
{
Print("Cannot Modify the Order "+GetLastError());
Alert("Cannot Modify the Order "+GetLastError());
return(0);
}


}
}

if(OrderType()==OP_SELL&&OrderSymbol()==Symbol()&&OrderMagicNumber()==magicscalping)
{
if(OrderOpenPrice()-Ask>=51.0*Point)
{

bool checkModify = OrderModify(OrderTicket(),OrderOpenPrice(),Ask-51.0*Point,OrderTakeProfit()-51.0+Point,0);

if(!checkModify)
{
Print("Cannot Modify the Order "+GetLastError());
Alert("Cannot Modify the Order "+GetLastError());
return(0);
}
else
{
Print("Cannot Modify the Order "+GetLastError());
Alert("Cannot Modify the Order "+GetLastError());
return(0);
}


}
}


}

}



return(0);
}
//+------------------------------------------------------------------+


double GetHATR(int shift,int timeFrame)
{
return (iCustom(NULL,timeFrame,HATRname,ATRperiod,0,shift));
}

double GetHVolume(int shift,int timeFrame)
{
return(iCustom(NULL,timeFrame,HVolumename,0,shift));
}


int ReverseHA(int shift, int timeFrame)
{
double haOpen,haClose;
haOpen = iCustom(NULL,timeFrame,HAname,MaMethod,MaPeriod,MaMethod2,MaPeriod2,2,shift);
haClose = iCustom(NULL,timeFrame,HAname,MaMethod,MaPeriod,MaMethod2,MaPeriod2,3,shift);


if (haOpen-haClose>0)
{
Comment("Bearish");
return(bearish);
}
if (haOpen-haClose<0)
{
Comment("Bullish");
return(bullish);
}
else
return(0);

}



In red is the part where I use OrderModify. any references or comments are welcome.

Thanks
Files:
hatrendtest.mq4  25 kb
 

J
You are short of a parameter for
OrderModify(OrderTicket(),OrderOpenPrice(),Bid-Point*51.0,OrderTakeProfit()+51.0*Point,0);
should be something like
OrderModify(OrderTicket(),OrderOpenPrice(),Bid-Point*51.0,OrderTakeProfit()+51.0*Point,0, Red);

Also you need to trap the value of GetLastError as successive calls to it will give different results

Print("Cannot Modify the Order "+GetLastError());
Alert("Cannot Modify the Order "+GetLastError()); // Reverts to 0

// should be

// Order stuff here that goes wrong

int iError = GetLastError());  // Catch the actual number

Print("Cannot Modify the Order ", i);
Alert("Cannot Modify the Order ", i); // both show correct number


Good Luck
-BB-

 
Hi,

The color is by default, but I changed and choose a color but I receive the same error message. I really do not get what:s wrong. Do you think it comes from the trailling stop (5.1 pips).

Actually I wanted to do something like that :

1) when it moves in the good direction and the Bid(or Ask)-OpenOrderPrice()>5.1, move stop to break evev
2) then the stop loss is atrialing stop with trailling 5.1

But there is huge difference between what I have and what I wanted.....
 

J


Do you think it comes from the trailling stop (5.1 pips).

Could be a bit tight - depends on the minimum distance as in MarketInfo for MODE_STOPLEVEL
-BB-