Trouble with Trade Functions

 

Hi,

Im new to MQL4 Programming and am working on my first EA.

Overall my EA is working, however once my order is placed I am having trouble with OrderDelete() and OrderModify().

Below is a section of my code that includes my OrderSend, OrderDelete and OrderModify sections.

Can someone please check it out and let me know what Im doing wrong.

I've bolded the areas that are returning errors

Thank You

if (GlobalVariableGet(Trade)==1)

{

if (Lot()==false) //not enough money for min

return(0);

if (Mas_Tip[0]<1 && Mas_Tip[1]<1 && Mas_Tip[4]<1 && Mas_Tip[5]<1)

{

if ((K_S > D_S && ((K_S<75 && D_S<75) || (K_S<25 && D_S D_L && (K_L<25 || D_L<25)))

{

MN=TimeCurrent();

SwingLow = iLowest(NULL,PeriodLong,MODE_LOW,RSI_Length_L,1);

StopLoss = Low[SwingLow]-(10*UsePoint);

SL=StopLoss; // StopLoss

TP=0; // TakeProfit

Price=High[1]+(2*UsePoint);

MyTicket=OrderSend(Symb,OP_BUYSTOP,Lots_New,Price,UseSlippage,SL,TP,"",MN,0,Green);

Accounting();

}

if ((K_S 25 && D_S>25) || (K_S>75 && D_S>75))) && (K_L 75 || D_L>75)))

{

MN=TimeCurrent();

SwingHigh = iHighest(NULL,PeriodLong,MODE_HIGH,RSI_Length_L,1);

StopLoss = High[SwingHigh] +(10*UsePoint);

SL= StopLoss; // StopLoss

TP=0 ; // TakeProfit

Price=Low[1]-2*UsePoint;

MyTicket=OrderSend(Symb,OP_SELLSTOP,Lots_New,Price,UseSlippage,SL,TP,"",MN,0,Green);

Accounting();

}

}

}

//+------------------------------------------------------------------+

//Cancel Pending Orders

//+------------------------------------------------------------------+

int IsCrossedS = Crossed (K_S,D_S);

Accounting();

if (Mas_Tip[0]==0 && Mas_Tip[4]==1 && (MarketInfo(Symb,MODE_ASK)<=StopLoss || IsCrossedS ==2))

{

DelPend=OrderSelect(MyTicket,SELECT_BY_TICKET);

if (DelPend>0) Deleted = OrderDelete(MyTicket);

if(Deleted == false)

Print("Error deleting buy stop order ", OrderTicket(), " Err#:", GetLastError());

else MyTicket=0;

}

if (Mas_Tip[1]==0 && Mas_Tip[5]==1 && (MarketInfo(Symb,MODE_BID)>=StopLoss || IsCrossedS == 1))

{

DelPend=OrderSelect(MyTicket,SELECT_BY_TICKET);

if (DelPend>0) Deleted = OrderDelete(MyTicket);

if(Deleted == false)

Print("Error deleting buy stop order ", OrderTicket(), " Err#:", GetLastError());

else MyTicket=0;

}

//+------------------------------------------------------------------+

//Modify Pending Orders

//+------------------------------------------------------------------+

Accounting();

if (Mas_Tip[0]==0 && Mas_Tip[4]==1 &&

Bid > (High[1]+(10*UsePoint)))

{

Price=High[1]+(10*UsePoint);

ModPend=OrderSelect(MyTicket,SELECT_BY_TICKET);

if (ModPend>0) Open_Modify=OrderModify(MyTicket,Price,SL,0,0,0);

if(Open_Modify == false)

Print("Error modifying long entry ", OrderTicket(), " Err#:", GetLastError());

}

if (Mas_Tip[1]==0 && Mas_Tip[5]==1 &&

Ask < (Low[1]-(10*UsePoint)))

{

Price=Low[1]-(10*UsePoint);

ModPend=OrderSelect(MyTicket,SELECT_BY_TICKET);

if (ModPend>0) Open_Modify=OrderModify(MyTicket,Price,SL,0,0,0);

if(Open_Modify == false)

Print("Error modifying short entry ", OrderTicket(), " Err#:", GetLastError());

}

//+------------------------------------------------------------------+

//Trailing Stops

//+------------------------------------------------------------------+

Accounting();

int isCrossedL = Crossed (K_L,D_L);

if (Mas_Tip[0] == 1 && isCrossedL == 1 )

{

StopLoss = OrderStopLoss();

SwingLow = iLowest(NULL,PeriodLong,MODE_LOW,RSI_Length_L,1);

NextStop = Low[SwingLow]-(10*UsePoint);

if (NextStop > StopLoss)

{

ModTrail= OrderSelect(MyTicket,SELECT_BY_TICKET);

if (ModTrail>0)

Trail_stop= OrderModify(MyTicket,OrderOpenPrice(),NextStop,0,0,Red);

if(Trail_stop == false)

Print("Error modifying Trailing stop ", OrderTicket(), " Err#:", GetLastError());

}

}

else

if (Mas_Tip[1] == 1 &&isCrossedL == 2 )

{

SwingHigh = iHighest(NULL,PeriodLong,MODE_LOW,RSI_Length_L,1);

NextStop = High[SwingHigh]+(10*UsePoint);

StopLoss = OrderStopLoss();

if (NextStop < StopLoss)

{

ModTrail= OrderSelect(MyTicket,SELECT_BY_TICKET);

if (ModTrail>0)

Trail_stop= OrderModify(MyTicket,OrderOpenPrice(),NextStop,0,0,Red);

if(Trail_stop == false)

Print("Error modifying Trailing stop ", OrderTicket(), " Err#:", GetLastError());

}

}

int isCrossedS = Crossed(K_S,D_S);

if (Mas_Tip[0] >= 1 )

{

if (isCrossedS == 2 )

{

NextStop= Low[1]-(10*UsePoint);

StopLoss = OrderStopLoss();

if (NextStop < StopLoss)

{

ModTrail= OrderSelect(MyTicket,SELECT_BY_TICKET);

if (ModTrail>0)

Trail_stop= OrderModify(MyTicket,OrderOpenPrice(),NextStop,0,0,Red);

if(Trail_stop == false)

Print("Error modifying Trailing stop ", OrderTicket(), " Err#:", GetLastError());

}

}

}

else

if (Mas_Tip[1] >= 1)

{

if (isCrossedS == 1 )

{

NextStop = High[1]+(10*UsePoint);

StopLoss = OrderStopLoss();

if (NextStop > StopLoss)

{

ModTrail= OrderSelect(MyTicket,SELECT_BY_TICKET);

if (ModTrail>0)

Trail_stop= OrderModify(MyTicket,OrderOpenPrice(),NextStop,0,0,Red);

if(Trail_stop == false)

Print("Error modifying Trailing stop ", OrderTicket(), " Err#:", GetLastError());

}

}

}

 
ThemBonez:
Hi,

Im new to MQL4 Programming and am working on my first EA.

Overall my EA is working, however once my order is placed I am having trouble with OrderDelete() and OrderModify().

Below is a section of my code that includes my OrderSend, OrderDelete and OrderModify sections.

Can someone please check it out and let me know what Im doing wrong.

I've bolded the areas that are returning errors

Thank You

if (GlobalVariableGet(Trade)==1)

{

if (Lot()==false) //not enough money for min

return(0);

if (Mas_Tip[0]<1 && Mas_Tip[1]<1 && Mas_Tip[4]<1 && Mas_Tip[5]<1)

{

if ((K_S > D_S && ((K_S<75 && D_S<75) || (K_S<25 && D_S D_L && (K_L<25 || D_L<25)))

{

MN=TimeCurrent();

SwingLow = iLowest(NULL,PeriodLong,MODE_LOW,RSI_Length_L,1);

StopLoss = Low[SwingLow]-(10*UsePoint);

SL=StopLoss; // StopLoss

TP=0; // TakeProfit

Price=High[1]+(2*UsePoint);

MyTicket=OrderSend(Symb,OP_BUYSTOP,Lots_New,Price,UseSlippage,SL,TP,"",MN,0,Green);

Accounting();

}

if ((K_S 25 && D_S>25) || (K_S>75 && D_S>75))) && (K_L 75 || D_L>75)))

{

MN=TimeCurrent();

SwingHigh = iHighest(NULL,PeriodLong,MODE_HIGH,RSI_Length_L,1);

StopLoss = High[SwingHigh] +(10*UsePoint);

SL= StopLoss; // StopLoss

TP=0 ; // TakeProfit

Price=Low[1]-2*UsePoint;

MyTicket=OrderSend(Symb,OP_SELLSTOP,Lots_New,Price,UseSlippage,SL,TP,"",MN,0,Green);

Accounting();

}

}

}

//+------------------------------------------------------------------+

//Cancel Pending Orders

//+------------------------------------------------------------------+

int IsCrossedS = Crossed (K_S,D_S);

Accounting();

if (Mas_Tip[0]==0 && Mas_Tip[4]==1 && (MarketInfo(Symb,MODE_ASK)<=StopLoss || IsCrossedS ==2))

{

DelPend=OrderSelect(MyTicket,SELECT_BY_TICKET);

if (DelPend>0) Deleted = OrderDelete(MyTicket);

if(Deleted == false)

Print("Error deleting buy stop order ", OrderTicket(), " Err#:", GetLastError());

else MyTicket=0;

}

if (Mas_Tip[1]==0 && Mas_Tip[5]==1 && (MarketInfo(Symb,MODE_BID)>=StopLoss || IsCrossedS == 1))

{

DelPend=OrderSelect(MyTicket,SELECT_BY_TICKET);

if (DelPend>0) Deleted = OrderDelete(MyTicket);

if(Deleted == false)

Print("Error deleting buy stop order ", OrderTicket(), " Err#:", GetLastError());

else MyTicket=0;

}

//+------------------------------------------------------------------+

//Modify Pending Orders

//+------------------------------------------------------------------+

Accounting();

if (Mas_Tip[0]==0 && Mas_Tip[4]==1 &&

Bid > (High[1]+(10*UsePoint)))

{

Price=High[1]+(10*UsePoint);

ModPend=OrderSelect(MyTicket,SELECT_BY_TICKET);

if (ModPend>0) Open_Modify=OrderModify(MyTicket,Price,SL,0,0,0);

if(Open_Modify == false)

Print("Error modifying long entry ", OrderTicket(), " Err#:", GetLastError());

}

if (Mas_Tip[1]==0 && Mas_Tip[5]==1 &&

Ask < (Low[1]-(10*UsePoint)))

{

Price=Low[1]-(10*UsePoint);

ModPend=OrderSelect(MyTicket,SELECT_BY_TICKET);

if (ModPend>0) Open_Modify=OrderModify(MyTicket,Price,SL,0,0,0);

if(Open_Modify == false)

Print("Error modifying short entry ", OrderTicket(), " Err#:", GetLastError());

}

//+------------------------------------------------------------------+

//Trailing Stops

//+------------------------------------------------------------------+

Accounting();

int isCrossedL = Crossed (K_L,D_L);

if (Mas_Tip[0] == 1 && isCrossedL == 1 )

{

StopLoss = OrderStopLoss();

SwingLow = iLowest(NULL,PeriodLong,MODE_LOW,RSI_Length_L,1);

NextStop = Low[SwingLow]-(10*UsePoint);

if (NextStop > StopLoss)

{

ModTrail= OrderSelect(MyTicket,SELECT_BY_TICKET);

if (ModTrail>0)

Trail_stop= OrderModify(MyTicket,OrderOpenPrice(),NextStop,0,0,Red);

if(Trail_stop == false)

Print("Error modifying Trailing stop ", OrderTicket(), " Err#:", GetLastError());

}

}

else

if (Mas_Tip[1] == 1 &&isCrossedL == 2 )

{

SwingHigh = iHighest(NULL,PeriodLong,MODE_LOW,RSI_Length_L,1);

NextStop = High[SwingHigh]+(10*UsePoint);

StopLoss = OrderStopLoss();

if (NextStop < StopLoss)

{

ModTrail= OrderSelect(MyTicket,SELECT_BY_TICKET);

if (ModTrail>0)

Trail_stop= OrderModify(MyTicket,OrderOpenPrice(),NextStop,0,0,Red);

if(Trail_stop == false)

Print("Error modifying Trailing stop ", OrderTicket(), " Err#:", GetLastError());

}

}

int isCrossedS = Crossed(K_S,D_S);

if (Mas_Tip[0] >= 1 )

{

if (isCrossedS == 2 )

{

NextStop= Low[1]-(10*UsePoint);

StopLoss = OrderStopLoss();

if (NextStop < StopLoss)

{

ModTrail= OrderSelect(MyTicket,SELECT_BY_TICKET);

if (ModTrail>0)

Trail_stop= OrderModify(MyTicket,OrderOpenPrice(),NextStop,0,0,Red);

if(Trail_stop == false)

Print("Error modifying Trailing stop ", OrderTicket(), " Err#:", GetLastError());

}

}

}

else

if (Mas_Tip[1] >= 1)

{

if (isCrossedS == 1 )

{

NextStop = High[1]+(10*UsePoint);

StopLoss = OrderStopLoss();

if (NextStop > StopLoss)

{

ModTrail= OrderSelect(MyTicket,SELECT_BY_TICKET);

if (ModTrail>0)

Trail_stop= OrderModify(MyTicket,OrderOpenPrice(),NextStop,0,0,Red);

if(Trail_stop == false)

Print("Error modifying Trailing stop ", OrderTicket(), " Err#:", GetLastError());

}

}

}

Did you check what is the error code when you try to use OrderDelete() and OrderModify()?

If not, make some comment with the error code and then it will be clearer

I can not test the partial code, but if I have to guess, then you are probably having a problem with ticket numbers

 

4051 Invalid function parameter value.

and

[TD]Invalid ticket.
ERR_INVALID_TICKET[/TD] 4108
 
ThemBonez:
4051 Invalid function parameter value.

and

[TD]Invalid ticket.
ERR_INVALID_TICKET[/TD] 4108

Then I guessed right

You don't have a valid ticket number in MyTicket variable when you try to do those operations on pending orders. Check the routine that selects the correct ticket number that should be processed

 

Not sure what routine you mean and not sure what I need to change.

 
ThemBonez:
Not sure what routine you mean and not sure what I need to change.

How are you selecting the order ticket number that needs to be changed or deleted? There you have an error

 

I included the code in the first message, but:

...

MyTicket=OrderSend(Symb,OP_BUYSTOP,Lots_New,Price,UseSlippage,SL,TP,"",MN,0,Green);

...

ModTrail= OrderSelect(MyTicket,SELECT_BY_TICKET); if (ModTrail>0) Trail_stop= OrderModify(MyTicket,OrderOpenPrice(),NextStop,0,0,Red);

 
ThemBonez:
I included the code in the first message, but:

...

MyTicket=OrderSend(Symb,OP_BUYSTOP,Lots_New,Price,UseSlippage,SL,TP,"",MN,0,Green);

...

ModTrail= OrderSelect(MyTicket,SELECT_BY_TICKET); if (ModTrail>0) Trail_stop= OrderModify(MyTicket,OrderOpenPrice(),NextStop,0,0,Red);

ThemBonez

As I told already : I can not test or check a partial code.

Check :

1. How is your MyTicket variable declared (is it a local variable, static local variable or it is declared outside the body of any function or procedure)

2. It is receiving value of the ticket when a pending order is opened. In the mean time (from the time was opened to the time it has to be modified or deleted), that variable does not contain a correct ticket value any more - check why

 

Thank you

That makes things a little clearer.

A question:

When a pending order is opened and becomes a market order, does the ticket number change?

If so, can you share a routine with me to determine that ticket number?

Thank You

 
ThemBonez:
Thank you

That makes things a little clearer.

A question:

When a pending order is opened and becomes a market order, does the ticket number change?

If so, can you share a routine with me to determine that ticket number?

Thank You

ThemBonez

When a pending order is triggered and becomes a market order, it is opened with a completely different ticket number. I am afraid that there is no 100% reliable mode to find out the ticket number of the new market order

 

so then, how does one modify a stoploss on such an order?