Help with Functions In mql4 Programing Laang

 

hi i made this expert with function and it is not work

but without fuction it work very well

so i want to know the wrong in this function

//+------------------------------------------------------------------+
//|                                                    MYFIRSTEA.mq4 |
//|                                            Copyright 2016,NGINC. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2016,NGINC."
#property link      "https://www.mql5.com"
#property version   "1.00"

//--- input parameters
input double   TakeProfit=250.0;
input double   Lots=0.1;
input double   Traillingstop=35.0;
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
   
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
   
  }
  
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
  int cnt,ticket,total;
  double shortema,longema;
  if(Bars<100)
  {
  Print("Bars less than 100");
  return;
  }
  if(TakeProfit<10)
  {
  Print ("TakeProfit less than 10");
  return;
  }
  shortema = iMA(NULL,0,8,0,MODE_EMA,PRICE_CLOSE,0);
  longema = iMA(NULL,0,13,0,MODE_EMA,PRICE_CLOSE,0);
  int iscrossed= crossed (shortema,longema);
  total=OrdersTotal();
  if(total<1)
  {
  if(iscrossed==1)
  {
  ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,3,0,Ask+TakeProfit*Point,"MY EA",1,0,Green);
   if(ticket>0)
   {
    if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES))
    {
    Print("Buy Order Opened : ", OrderOpenPrice());
    }
   }
   else 
   {
   Print("Erroe Openning order Buy :",GetLastError());
   return;
   }
  }
  if (iscrossed==2)
  {
  ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,3,0,Bid-TakeProfit*Point,"MY EA",1,0,Red);
  if(ticket>0)
   {
    if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES))
    Print("Sell Order Opened",OrderOpenPrice());
   }
   else
   Print("Error Openning Order Sell: " , GetLastError());
   return;
  }
  return;
  }
//---
   for(cnt=0;cnt<total;cnt++)
   {
   if(!OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES))
   continue;
    if(OrderType()<=OP_SELL && OrderSymbol()==Symbol())
    {
     if (OrderType()== OP_BUY)
     {
      if(iscrossed==2)
      {
       if(!OrderClose(OrderTicket(),OrderLots(),Bid,3,Violet))
       Print("Order Close Errot " , GetLastError());
       return;
      }
      if (Traillingstop>0)
      {
       if((Bid-OrderOpenPrice())>(Point*Traillingstop))
        {
          if((OrderStopLoss())<(Bid-(Point*Traillingstop)))
          {
           if (!OrderModify(OrderTicket(),OrderOpenPrice(),Bid-Point*Traillingstop,OrderTakeProfit(),0,Green))
           Print("Order Modify Error " , GetLastError());
           return;
          }
        }
      }
     }
     else
     if(iscrossed==1)
     {
      if(!OrderClose(OrderTicket(),OrderLots(),Ask,3,Violet))
      Print("Order Close Error ",GetLastError());
      return;
     }
     if(Traillingstop>0)
      {
       if((OrderOpenPrice()-Ask)>(Point*Traillingstop))
        {
         if ((OrderStopLoss())>(Ask+(Point*Traillingstop)) || (OrderStopLoss()==0))
          {
          if(!OrderModify(OrderTicket(),OrderOpenPrice(),Ask+Point*Traillingstop,OrderTakeProfit(),0,Red))
          Print("Order Modify Error " ,GetLastError());
          return;
          }
         
        }
      }
    }
   }
   return;
  }
//+------------------------------------------------------------------+
int crossed (double line1 , double line2)
{
  static int currentDirection = 0 ;
  static int lastDirection = 0 ;
  
  if (line1>line2)currentDirection=1;// up
  if (line1<line2)currentDirection=2;// down
  if (currentDirection != lastDirection)
  {
   currentDirection = lastDirection;
   return(lastDirection);
  }
   else
   {
   return(0);
   }
  
}
 

I think current direction always is 0

you return always 0

 
Omar AlKassar:

I think current direction always is 0

you return always 0

yes I think so but why i dose not change
 
mazen nafee:
yes I think so but why i dose not change
what you mean ?
 

why you need last direction 

int crossed (double line1 , double line2)
{
  static int currentDirection = 0 ;
  
  
  if (line1>line2)currentDirection=1;// up
  if (line1<line2)currentDirection=2;// down

   return(currentDirection );
  
}
 

If you change ...

currentDirection = lastDirection;

 To ...

lastDirection = currentDirection;

 it 99% will work ...

I think this is your mistake because it is logically the crossed( ) function is used to detect when the cross is happened while you didn't updated the direction status correctly when the new direction is changed.

So, I believe if you replaced it with the following, it will work ...

int Crossed (double line1 , double line2){
   static int last_direction = 0;
   static int current_direction = 0;
      
   if(line1>line2)current_direction = 1; // UP
   if(line1<line2)current_direction = 2; // DOWN

   if(current_direction != last_direction){
      last_direction = current_direction;
      return (last_direction);
   }
   else{
      return (0);
   }
}
 

Yes that what I'm talking about

also he didn't need Last direction

simply he can return current direction

 
Osama Shaban:

If you change ...

 To ...

 it 99% will work ...

I think this is your mistake because it is logically the crossed( ) function is used to detect when the cross is happened while you didn't updated the direction status correctly when the new direction is changed.

So, I believe if you replaced it with the following, it will work ...

yes it works 

thank you very mutch

 
Omar AlKassar:

Yes that what I'm talking about

also he didn't need Last direction

simply he can return current direction

yes you are right 

but i want to know how to make it work as Mr.Osama did.

and thanks for helping me 

 

what i understand is the error was

 currentDirection = lastDirection;

it mean i take the result of direction which is (1or 2) and made it equal to last direction which is always(0)

and when i use

 lastDirection = currentDirection ;

i made lastDirection equal to (1or 2) as currentDirection 

so Mr Omar was right when said it always return 0

and Mr Osama declared why it return 0 

 

Exacly Mazen.

Omar was right. Me said how to correct the mistake.

Happy that it works now. 

Reason: