i want to consecutive order

 
//+------------------------------------------------------------------+
//|                                                    MyFirstEA.mq4 |
//|                                  
//|                                      
//+------------------------------------------------------------------+
#property copyright "Copyright © 2008, ForexArea.com"
#property link      "http://www.forexarea.com"

extern double  dLots          = 1;
extern int     iTakeProfit    = 100;
extern int     iTrailingStop  = 25
extern int     iStopLoss      = 50
extern int     iMagicNumber   = 12444;

//+------------------------------------------------------------------+
//|   This is purely for study purposes for my students and          |
//|   should NOT be traded live.                                     |
//|                                         http://www.forexarea.com |
//+------------------------------------------------------------------+

//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
  {
//----
   int iTotalOrders, iCrossed;
   
   // see if we have a new cross
   iCrossed  = CheckForCross();
   
   // get total number of orders 
   iTotalOrders = OrdersTotal();
   
   // do functions 
   if(iTotalOrders == 0) PlaceOrder(iCrossed);
   if(iTotalOrders != 0) TrailingStop(iTotalOrders);
   
//----
   return(0);
  }
//+------------------------------------------------------------------+

void PlaceOrder(int iSignal){

   int iTicket;
   
   if(iSignal == 1)
   {
      iTicket=OrderSend(Symbol(),OP_BUY,dLots,Ask,3,Bid-iStopLoss*Point,Ask+iTakeProfit*Point,"My First EA",iMagicNumber,0,Green);
      if(iTicket>0){
         if(OrderSelect(iTicket,SELECT_BY_TICKET,MODE_TRADES)) Print("BUY order opened : ",OrderOpenPrice());
      }else{
         Print("Error opening BUY order : ",GetLastError());
      }
   }
   
   if(iSignal == 2)
   {
      iTicket=OrderSend(Symbol(),OP_SELL,dLots,Bid,3,Ask+iStopLoss*Point,Bid-iTakeProfit*Point,"My First EA",iMagicNumber,0,Red);
      if(iTicket>0){
         if(OrderSelect(iTicket,SELECT_BY_TICKET,MODE_TRADES)) Print("SELL order opened : ",OrderOpenPrice());
      }else{
         Print("Error opening SELL order : ",GetLastError());
      }
   }
   
   return(0);
   
}

void TrailingStop(int iTotal){

   int iCount;
   
   if(iTrailingStop < 1)return(-1); // error
   
   for(iCount=0;iCount<iTotal;iCount++){

      OrderSelect(iCount, SELECT_BY_POS, MODE_TRADES);
      if(OrderSymbol()==Symbol() && OrderMagicNumber() == iMagicNumber)
         switch(OrderType()){
            case OP_BUY:
               if(Bid-OrderOpenPrice()>Point*iTrailingStop){
                  if(OrderStopLoss()<Bid-Point*iTrailingStop){
                     OrderModify(OrderTicket(),OrderOpenPrice(),Bid-Point*iTrailingStop,OrderTakeProfit(),0,Green);
                  }
               }
               break;
            case OP_SELL:
               if((OrderOpenPrice()-Ask)>(Point*iTrailingStop)){
                  if((OrderStopLoss()>(Ask+Point*iTrailingStop)) || (OrderStopLoss()==0)){
                     OrderModify(OrderTicket(),OrderOpenPrice(),Ask+Point*iTrailingStop,OrderTakeProfit(),0,Red);
                  }
               }
               break;
         }
      }
   
      return(0);
}

int CheckForCross(){
   static int siLastDirection = 0;
   static int siCurrentDirection = 0;
   double ma7, ma14, ma21, ma49,ma70,ma100;
      
   // get the current prices for each moving average
   ma7 = iMA(NULL,0,3,MODE_EMA,PRICE_CLOSE,0);
   ma14 = iMA(NULL,0,6,MODE_EMA,PRICE_CLOSE,0);
   ma21 = iMA(NULL,0,18,0,MODE_EMA,PRICE_CLOSE,0);
   ma49 = iMA(NULL,0,21,0,MODE_EMA,PRICE_CLOSE,0);
   ma70 = iMA(NULL,0,70,0,MODE_EMA,PRICE_CLOSE,0);
   ma100 = iMA(NULL,0,100,0,MODE_EMA,PRICE_CLOSE,0);

   
   // check if lines have crossed
   if( ma7 > ma14 && ma7 > ma21  && ma7 > ma49 && ma7 > ma70 ) siCurrentDirection = 1; //up
   if(ma7 < ma14 && ma7 < ma21  && ma7 < ma49  && ma7 < ma70 ) siCurrentDirection = 2; //down

   if(siCurrentDirection != siLastDirection){
      // they have so return the new signal
      siLastDirection = siCurrentDirection;
      return (siLastDirection);
   }
   
   // no cross
   return(0);
} 
i want to consecutive order but my ea just have new order if old order finish. Help me . please tell me how to find mistake. I want have order consecutive when have new signal
 

Add input: extern int iMaxOrders = 2;

Replace line if(iTotalOrders == 0) PlaceOrder(iCrossed);

with if(iTotalOrders < iMaxOrders) PlaceOrder(iCrossed); 

 
sxTed:

Add input: extern int iMaxOrders = 2;

Replace line if(iTotalOrders == 0) PlaceOrder(iCrossed);

with if(iTotalOrders < iMaxOrders) PlaceOrder(iCrossed); 

it not work.it still has to finish one order and for one new command, but it does not allow the command is not finished but still have the right to place new orders
 
phipho:
it not work.it still has to finish one order and for one new command, but it does not allow the command is not finished but still have the right to place new orders


Did you make a program yourself phipho  are you able to understand what happens here and do you think  you can explain someone else how your program is running ??

 
i'm sorry it work. :). how can i put stoploss at region where ma7 cut ma70 ?
 
phipho:
i'm sorry it work. :). how can i put stoploss at region where ma7 cut ma70 ?

Have you tried to compile your program...

extern int     iTrailingStop  = 25
extern int     iStopLoss      = 50

 

According to me you will find errors compiling it.... 

Also I like to have your answer on this ??  Do you know what ma7 is  ????

    according to me it is not a moving average with period 7 but what do you think it is.....

   ma7 = iMA(NULL,0,3,MODE_EMA,PRICE_CLOSE,0);
   ma14 = iMA(NULL,0,6,MODE_EMA,PRICE_CLOSE,0);
   ma21 = iMA(NULL,0,18,0,MODE_EMA,PRICE_CLOSE,0);
   ma49 = iMA(NULL,0,21,0,MODE_EMA,PRICE_CLOSE,0);
   ma70 = iMA(NULL,0,70,0,MODE_EMA,PRICE_CLOSE,0);
   ma100 = iMA(NULL,0,100,0,MODE_EMA,PRICE_CLOSE,0);

ma7  -  What is this calculation calculate ??  

Do you miss a    "0"   and has it to be written like  

ma7 = iMA(NULL,0,3,0,MODE_EMA,PRICE_CLOSE,0);

 or did you wrote ma21,ma49,ma70 and ma100  function wrong ??

 
Or Factor the code and Simplify
Original
   ma7 = iMA(NULL,0,3,MODE_EMA,PRICE_CLOSE,0);
   ma14 = iMA(NULL,0,6,MODE_EMA,PRICE_CLOSE,0);
   ma21 = iMA(NULL,0,18,0,MODE_EMA,PRICE_CLOSE,0);
   ma49 = iMA(NULL,0,21,0,MODE_EMA,PRICE_CLOSE,0);
   ma70 = iMA(NULL,0,70,0,MODE_EMA,PRICE_CLOSE,0);
   ma100 = iMA(NULL,0,100,0,MODE_EMA,PRICE_CLOSE,0);
Simplified
double MA(int len, int iBar=0){ 
   return( iMA(NULL,0, len, 0,MODE_EMA,PRICE_CLOSE,iBar) ); 
}
:
   ma7 = MA(3); // 3 sic
   ma14 = MA(6);
   ma21 = MA(18);
   ma49 = MA(21);
   ma70 = MA(70);
   ma100 = MA(100);
 

sorry I got it wrong, but tou want to put the stoploss when that ma7 cut ma70, how,

      
   // get the current prices for each moving average
   ma7 = iMA(NULL,0,7,0,MODE_EMA,PRICE_CLOSE,0);
   ma14 = iMA(NULL,0,14,0,MODE_EMA,PRICE_CLOSE,0);
   ma21 = iMA(NULL,0,21,0,MODE_EMA,PRICE_CLOSE,0);
   ma49 = iMA(NULL,0,49,0,MODE_EMA,PRICE_CLOSE,0);
   ma70 = iMA(NULL,0,70,0,MODE_EMA,PRICE_CLOSE,0);
   ma100 = iMA(NULL,0,100,0,MODE_EMA,PRICE_CLOSE,0);


 
phipho:   want to put the stoploss when that ma7 cut ma70, how,

We can only look when last cross did happen we can't see in the future 

to look when ma7 was smaller then ma70  you have to make something like 

int i;
double ma7,ma70;
for(i=0;ma7>ma70;i++)
  {
   ma7 = iMA(NULL,0,7,0,MODE_EMA,PRICE_CLOSE,i);
   ma70 = iMA(NULL,0,70,0,MODE_EMA,PRICE_CLOSE,i);
   if(ma7<ma70)Print("ma7<ma70  i=   ",i);
  }

 but according to me 

You have to start learning how to program or you have to pay someone to make your programs

Make clear what you want and show your attemps how you tried.... 

 
phipho: want to put the stoploss when that ma7 cut ma70, how,
deVries: We can only look when last cross did happen we can't see in the future
  1. It is just easier to just check and close the order when they do cross.
  2. But you can compute the price where the two EMAs will cross for the current candle and update that for each candle:
// slow.alpha              = 2.0 / (SlowEMA   + 1);

// Compute what price will the fast EMA cross the slow.
// fastCurr = fastPrev + fastA * (Bid - fastPrev)
// slowCurr = slowPrev + slowA * (Bid - slowPrev)
// fastPrev+fastA(cross-fastPrev) = slowPrev+slowA(cross-slowPrev)
// fastPrev(1-fastA)+fastA(cross) = slowPrev(1-slowA)+slowA(cross)
// cross(fastA-slowA) = slowPrev(1-slowA)-fastPrev(1-fastA)
double cross = (slowPrev*(1.-slow.alpha)-fastPrev*(1.-fast.alpha)
               ) / (fast.alpha - slow.alpha);
Reason: