help me trailing stop of buy sell stop - page 2

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

extern double  dLots          = 1;
extern int BuyStop=20;
extern int SellStop=20;
extern int     iTakeProfit    = 100;
extern int     iTrailingStop  = 35;
extern int     iStopLoss      = 30;
extern int     iMagicNumber   = 12444;
 extern int iMaxOrders = 4;

//+------------------------------------------------------------------+
//|   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 < iMaxOrders) PlaceOrder(iCrossed); 
   if(iTotalOrders != iMaxOrders ) TrailingStop(iTotalOrders);
   
//----
   return(0);
  }
//+------------------------------------------------------------------+

void PlaceOrder(int iSignal){

   int iTicket;
   double op1=Ask+BuyStop*Point;
   double op2=Bid-SellStop*Point;
   
   
   
   if(iSignal == 1)
   {
      iTicket=OrderSend(Symbol(),OP_BUYSTOP,dLots,  op1,3,op1-iStopLoss*Point,op1+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_SELLSTOP,dLots,op2,3,op2+iStopLoss*Point,op2-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 ma2,ma200;
   
   
      
   // get the current prices for each moving average
   ma50= iMA(NULL,0,50,0,MODE_SMA,PRICE_CLOSE,0);
   ma70=iMA(NULL,0,70,0,MODE_SMA,PRICE_CLOSE,0);
   

   
   // check if lines have crossed
  if(  ma50>ma70 ) siCurrentDirection = 1; //up
   if( ma50<ma70 ) siCurrentDirection = 2; //down

   if(siCurrentDirection != siLastDirection){
      // they have so return the new signal
      siLastDirection = siCurrentDirection;
      return (siLastDirection);
   }
   
   // no cross
   return(0);
} 
THis it all. 
 
stoploss take profig buy and sell stop it work all but trainling stop it not
 

Do a test with

extern int BuyStop=200;
extern int SellStop=200;
extern int     iTakeProfit    = 1000;
extern int     iTrailingStop  = 350;
extern int     iStopLoss      = 300;

 read jornal of strategytester also

report result 

 

Also compile your EA before strategytest  

miss defination for ma50 and ma70 

Reason: