Questions from Beginners MQL4 MT4 MetaTrader 4 - page 30

 
spoiltboy:

Greetings. I've asked around, but haven't found an answer yet. Could you please tell me why with the following entry

if (x!=maxpr1){x=maxpr1; OrderDelete(ticketD);}

if (z!=minpr1){z=minpr1; OrderDelete(ticketUP);}

The system constantly generates errors

: invalid ticket for OrderDeletefunction

: OrderDelete error 4051

According to the reference book it is

Invalid value of the function parameter

Although the parameters seem to be the same.

Although if you remove the condition and leave only

OrderDelete(ticketD);

OrderDelete(ticketUP);

it deletes successfully (but I need it by condition). What I do wrong? Here is the whole thing:

Because it must be selected through OrderSelect, yourticketD value is lost, and most likely there instead of ticket, some yukhnya, try to print, and compare. And in general, this style of writing is nonsense
 
Vitaly Muzichenko:
Because it has to be selected via OrderSelect, yourticketD value is lost, and most likely there is some bullshit instead of ticket, try to print it out and compare.

Is it because the delete function is triggered by the condition that the value is lost? Tried writing it like this:

if (x!=maxpr1){x=maxpr1; OrderDelete(ticketD);}

OrderDelete(ticketUP);

on the coming condition for "ticketD" writes the same errors, and the order"ticketUP" is successfully deleted on every tick.

 
Vitaly Muzichenko:
And in general, this style of writing is nonsense.
I've just started, I'd welcome comments.
 
spoiltboy:
I'm just getting started, I'd welcome comments.
paste the code with "SRC" so you can look at it
 
Renat Akhtyamov:
insert code via "SRC" so you can look at
extern int pointsl=100, pointtp=100, MagicB=1111, MagicS=2222, bars=10;  extern double lotB=0.1, lotS=0.1;
double slB, tpB, slS, tpS;  double x=0, z=0;


void OnTick()  
{
double maxpr1=-9999; double minpr1=9999;

for(int shift1=0; shift1<bars; shift1++)
{double i=iHigh(Symbol(), PERIOD_CURRENT, shift1);
if (i>maxpr1){maxpr1=i;}}

for(int shiftA1=0; shiftA1<bars; shiftA1++)
{double y=iLow(Symbol(), PERIOD_CURRENT, shiftA1);
if (y<minpr1) {minpr1=y;}}

if (BuyLimitCount()==0 && BuyCount()==0){
slB=NormalizeDouble(minpr1-pointsl*Point,5);
tpB=NormalizeDouble(minpr1+pointtp*Point,5);
int ticketUP=OrderSend(Symbol(), OP_BUYLIMIT, lotB, minpr1, 3, slB, tpB, "", MagicB, 0, Red);
if (ticketUP==-1) Print("ERROR OP_BUY"); else Print("OP_BUY OK");}

if (SellLimitCount()==0 && SellCount() ==0){
slS=NormalizeDouble(maxpr1+pointsl*Point,5);
tpS=NormalizeDouble(maxpr1-pointtp*Point,5);
int ticketD=OrderSend(Symbol(), OP_SELLLIMIT, lotS, maxpr1, 3, slS, tpS, "", MagicS, 0, Blue);
if (ticketD==-1) Print("ERROR OP_SELL"); else Print("OP_SELL OK");}

if (x!=maxpr1){x=maxpr1; OrderDelete(ticketD);}
OrderDelete(ticketUP);


double maxpr=-9999; double minpr=9999;

for(int shift=0; shift<bars; shift++)
{double e=iHigh(Symbol(), PERIOD_CURRENT, shift);
if (e>maxpr){maxpr=e;}}

for(int shiftA=0; shiftA<bars; shiftA++)
{double r=iLow(Symbol(), PERIOD_CURRENT, shiftA);
if (r<minpr) {minpr=r;}}

string a;
if(bars==1)a="bar: ";
else a= IntegerToString(bars,1) + " bar's: ";
Comment("Last ", a, "max ", DoubleToStr(maxpr, 5), ", min ", DoubleToStr(minpr, 5),".");
}

int BuyLimitCount(){
int count=0;
for(int i=OrdersTotal()-1; i>=0; i--){
if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES)==true){
if(OrderMagicNumber()==MagicB){
if(OrderType()==OP_BUYLIMIT)
count++;}}}return(count);}

int BuyCount(){
int count=0;
for(int i=OrdersTotal()-1; i>=0; i--){
if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES)==true){
if(OrderMagicNumber()==MagicB){
if(OrderType()==OP_BUY)
count++;}}}return(count);}

int SellLimitCount(){
int count=0;
for(int i=OrdersTotal()-1; i>=0; i--){
if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES)==true){
if(OrderMagicNumber()==MagicS){
if(OrderType()==OP_SELLLIMIT)
count++;}}}return(count);}

int SellCount(){
int count=0;
for(int i=OrdersTotal()-1; i>=0; i--){
if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES)==true){
if(OrderMagicNumber()==MagicS){
if(OrderType()==OP_SELL)
count++;}}}return(count);}
 
spoiltboy:

Is it because the delete function is triggered by the condition that the value is lost? Tried writing it like this:

if (x!=maxpr1){x=maxpr1; OrderDelete(ticketD);}

OrderDelete(ticketUP);

When the condition for "ticketD" comes, it writes the same errors, and the order"ticketUP" is successfully deleted on every tick.

We should check for the existence of the order, and if there is one with such a ticket, we delete it; if there isn't, we do nothing. What happens is that the order is already deleted, and you keep deleting it again
 
Vitaly Muzichenko:
We should check for the existence of the order and if there is one with such a tick, we will delete it, if not, we will not do anything. What is happening is that the order has already been deleted and you keep deleting it again

In this entry, the order withticketUP is first opened and then deleted on every tick, while the order tikcetD, if the corresponding condition occurs, generates a deletion error. It is according to the journal.

Or maybe I have confused something?

 
spoiltboy:

but is the bottom line of the two by any chance redundant?

if (x!=maxpr1){x=maxpr1; OrderDelete(ticketD);}
OrderDelete(ticketUP);//лишняя?

If not superfluous, where is the deletion condition?

This is a variant of code that tries to delete a pending order on every tick, in my opinion.

And the number of the ticket is unclear.

That's what will return the error.

 
spoiltboy:

In this entry, theticketUP orderis first opened and then deleted on every tick, while the tikcetD order, if the corresponding condition is met, generates a deletion error. It is according to the journal.

Or maybe I am confused?

well, correctly.

what's the question then?

 
Renat Akhtyamov:

Well, that's right.

What is the question then?

This is for different orders. The function of removing one with a condition, removing the other without a condition.

The question is why in my case delete function when without condition works successfully and if I put it in condition it gives error when condition occurs.

Reason: