Why it makes trades even if they aren't allowed?

 

Can anybody help me with this problem? As you can see on picture, where is highlighted the main expression and the values from graph on left, the second trade won't be allowed by this expression... but it happens and i can't figure WHY? But it isn't all - it even don't match the expression for SELL so...

(+DI is blue, -DI is red and ADX is silver)

PLEASE HELP - THANKS

MAARTY

//+------------------------------------------------------------------+
//|                                                          ADX.mq4 |
//+------------------------------------------------------------------+

extern double Lot = 0.1;
extern double TrailingStop = 10;
extern int    Risk = 10;
extern bool   bought = false, sold = false;

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

int start()
  {
   double MarginRequired, Lots;
   double PDI0, MDI0;
   double PDI1, MDI1;
   double ADX0, ADX1;
   int    ticket;

   MarginRequired = MarketInfo(Symbol(),MODE_MARGINREQUIRED);

   PDI0   = iADX(NULL,0,14,PRICE_CLOSE,MODE_PLUSDI,0);
   MDI0   = iADX(NULL,0,14,PRICE_CLOSE,MODE_MINUSDI,0);
   PDI1   = iADX(NULL,0,14,PRICE_CLOSE,MODE_PLUSDI,1);
   MDI1   = iADX(NULL,0,14,PRICE_CLOSE,MODE_MINUSDI,1);
   ADX0   = iADX(NULL,0,14,PRICE_CLOSE,MODE_MAIN,0);   
   ADX1   = iADX(NULL,0,14,PRICE_CLOSE,MODE_MAIN,1);
   
   if(bought == false && sold == false)
     {
      Lots = 0.1;
      if(AccountFreeMargin()<(1000*Lots))
        {
         Print("We have no money. Free Margin = ", AccountFreeMargin());
         return(0);  
        }
      while(((MarginRequired*Lots) <= (AccountFreeMargin()/Risk)) && Lots<50)
        {
         Lots = Lots + 0.1;
        } 
      
// check for BUY/SELL possibility
   
      if(ADX0 > ADX1)
        {
         if(PDI0 > MDI0 && PDI0 > PDI1 && MDI0 < MDI1)
           {
            ticket = OrderSend(Symbol(),OP_BUY,Lots,Ask,3,0,0,"maarty kupuje",666,0,Blue);
            if(ticket > 0)
              {
               if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) Print("Nakoupeno za: ",OrderOpenPrice());
               bought = true;
              }
            else Print("Error opening BUY order : ",GetLastError()); 
            return(0); 
           }
         if(PDI0 < MDI0 && PDI0 < PDI1 && MDI0 > MDI1)
           {
            ticket = OrderSend(Symbol(),OP_SELL,Lots,Bid,3,0,0,"maarty prodava",666,0,Red);
            if(ticket > 0)
              {
               if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) Print("Prodano za: ",OrderOpenPrice());
               sold = true;
              }
            else Print("Error opening SELL order : ",GetLastError()); 
            return(0); 
           }
        }   
     }
   if(bought)
     {
      while(ADX0 > ADX1)
        {
         if(TrailingStop>0)  
           {               
            OrderModify(OrderTicket(),OrderOpenPrice(),Low[0]-Point*TrailingStop,OrderTakeProfit(),0,Green);
            return(0);
           }
        }
      OrderClose(OrderTicket(),OrderLots(),Bid,3,Violet);
      bought = false;
     }
   if(sold)
     {
      while(ADX0 > ADX1)
        {
         if(TrailingStop>0)  
           {               
            OrderModify(OrderTicket(),OrderOpenPrice(),High[0]+Point*TrailingStop,OrderTakeProfit(),0,Green);
            return(0);
           }
        }
      OrderClose(OrderTicket(),OrderLots(),Ask,3,Violet);
      sold = false;
     }
   return(0);
  }
 

M

Use the SRC button in the posting page here and paste in the whole code - cant read it all - or well enough!

-BB-

 
BarrowBoy:

M

Use the SRC button in the posting page here and paste in the whole code - cant read it all - or well enough!

-BB-

here it is - thanks...

Files:
adx.mq4  4 kb
 

M

> second trade won't be allowed by this expression

You think?

In fact it will & it can because you are not defining the logic as closely enough

As you are evaluating every tick - start() runs every tick - the expression

(ADX0 > ADX1)

and

(PDI0 < MDI0 && PDI0 < PDI1 && MDI0 > MDI1)

could both have been true at some point during the bars formation

Also the values of

bought 

and

sold 

are being reset every tick to the extern values

To preserve their value for across successive runs of start(), change them from externs to static values

Good Luck

-BB-

 

Thanks for explanation. I didn't knew, that it runs every tick :( Now I know why all my tries crashes...

Reason: