Questions and Answers to Programmers on MQL 4 - page 6

 
Is the code complicated, or is there something simpler?
 

1. Store order tickets in an array or 2 variables.

2. Select them one by one with OrderSelect (the array is more convenient here) .

3. Check the type for the current moment.

4. If type OP_BUY || OP_SELL -> OrderDelete for second ticket.

 

Please help me with a simple example, I understand many things, but the order select is a bit of a jam!

 

Here is the code where the error is ????? Doesn't delete second pendent when one pendent triggers ......


if (MA1<MA2)
{
if(n==0)
{
//==================================================================================================================//

tiket=OrderSend(Symbol(),OP_BUY,Lots,NormalizeDouble(Ask,Digits),Slip,slb,tpb,NULL,MagicNumber,0,CLR_NONE);
if(tiket<0) Alert("Error # - ",GetLastError()," On instrument ", Symbol()); //write error//
//================================================================================================================//
t2 = OrderSend(Symbol(), OP_BUYSTOP, Lots*Overlot,OtkrBS, Slip,0,tpb, 0, MagicNumber,expiration);
//================================================================================================================//
t1 = OrderSend(Symbol(), OP_SELLSTOP, Lots*Overlot+1,OtkrSS,Slip,0,slb, 0, MagicNumber,expiration);
//================================================================================================================//

//==================================================================================================================//
}
//==================================================================================================================//
else
//=================================================================================================================//
if (MA1>MA2)
{
tiket=OrderSend(Symbol(),OP_SELL,Lots,NormalizeDouble(Bid,Digits),Slip,sls,tps,NULL,MagicNumber,0,CLR_NONE);
if(tiket<0) Alert("Error # - ",GetLastError()," On instrument ", Symbol()); //write error//
//================================================================================================================//
t2 = OrderSend(Symbol(), OP_BUYSTOP, Lots*Overlot+1,OtkrSS1,Slip,0,sls, 0, MagicNumber,expiration);
//================================================================================================================//
t1 = OrderSend(Symbol(), OP_SELLSTOP, Lots*Overlot, OtkrBS1,Slip,0,tps, 0, MagicNumber,expiration);
//================================================================================================================//
}
//==================================================================================================================//
// if SELLSTOP has triggered, delete BUYSTOP
if(OrderSelect(t1, SELECT_BY_TICKET))
{
if(OrderType()==OP_SELL)
{
OrderDelete(t2);
}
}
//----
// if BUYSTOP has triggered, delete SELLSTOP
if(OrderSelect(t2, SELECT_BY_TICKET))
{
if(OrderType()==OP_BUY)
{
OrderDelete(t1);
}
}

 

You're confused about the conditions. I think you have {} brackets in the wrong place:

if ( n == 0 )
{
        if (MA1<MA2) 
        {
                //==================================================================================================================//

                tiket=OrderSend(Symbol(),OP_BUY,Lots,NormalizeDouble(Ask,Digits),Slip,slb,tpb,NULL,MagicNumber,0,CLR_NONE); 
                if(tiket<0) Alert("Ошибка № - ",GetLastError()," На инструменте ", Symbol()); //пишет ошибку// 
                //================================================================================================================//
                t2 = OrderSend(Symbol(), OP_BUYSTOP, Lots*Overlot,OtkrBS, Slip,0,tpb, 0, MagicNumber,expiration);
                //================================================================================================================//
                t1 = OrderSend(Symbol(), OP_SELLSTOP, Lots*Overlot+1,OtkrSS,Slip,0,slb, 0, MagicNumber,expiration);
                //================================================================================================================//

                //==================================================================================================================//
        }
        //==================================================================================================================// 
        else 
        //=================================================================================================================// 
        if (MA1>MA2) 
        {
                tiket=OrderSend(Symbol(),OP_SELL,Lots,NormalizeDouble(Bid,Digits),Slip,sls,tps,NULL,MagicNumber,0,CLR_NONE);
                if(tiket<0) Alert("Ошибка № - ",GetLastError()," На инструменте ", Symbol()); //пишет ошибку//
                //================================================================================================================//
                t2 = OrderSend(Symbol(), OP_BUYSTOP, Lots*Overlot+1,OtkrSS1,Slip,0,sls, 0, MagicNumber,expiration);
                //================================================================================================================//
                t1 = OrderSend(Symbol(), OP_SELLSTOP, Lots*Overlot, OtkrBS1,Slip,0,tps, 0, MagicNumber,expiration);
        //================================================================================================================//
        }
}
//==================================================================================================================//
// если сработал SELLSTOP удаляем BUYSTOP
if(OrderSelect(t1, SELECT_BY_TICKET))
{
        if(OrderType()==OP_SELL)
        {
                OrderDelete(t2);
        }
}
//----
// если сработал BUYSTOP удаляем SELLSTOP
if(OrderSelect(t2, SELECT_BY_TICKET))
{
        if(OrderType()==OP_BUY)
        {
                OrderDelete(t1);
        }
}

Or something like that.

 
didn't work unfortunately........
 

What's amazing is that I have a similar EA. I checked everything with it, it's all the same in principle. It works, and this one doesn't...

The difference is that the other EA has only one pose for buy entry and two pendants.

 

tickets in global variables? (outside of start and other functions)

If not, they are reset every tick.

Insert more Print()-s at each step of the Expert Advisor, then you will understand where it crashes and what it does not do.

 

HOORAY AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Winner !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!1

!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

 
hasayama:

tickets in global variables? (outside of start and other functions)

If not, they are reset every tick.

Insert more Print()-s at each step of the Expert Advisor, then you will understand where it crashes and what it does not do.

I'm madly grateful to you, I've missed the point that I've declared tickets as double in the beginning and the order cannot be registered as 1.0 and 2.0. That's why the function for deletion was waiting for floating point