trade when indicator from separate timeframe agree - page 2

 
WHRoeder:
This makes the EA incompatible with any other including itself on other pairs
I took out the for loop and added this one.

I seem to be getting multiple trades in various directions which seem to close at sl and/or close multiple close results

//+------------------------------------------------------------------+
//|                                                    Dirty_Rat.mq4 |
//|                                        Agent86's Dirty Rat Trade |
//|                                     http://www.iclbiz.com/joomla |
//+------------------------------------------------------------------+
#property copyright "Agent86 N/A"
#property link      "www.iclbiz.com/joomla"

//---- input parameters
extern double    TakeProfit=300.0;
extern double    Lots=0.1;
extern double    StopLoss=20.0;
extern int       MagicNumber=12345; 

//++++ These are adjusted for 5 digit brokers.

int     pips2points;    // slippage  3 pips    3=points    30=points
double  pips2dbl;       // Stoploss 15 pips    0.0015      0.00150
int     Digits.pips;    // DoubleToStr(dbl/pips2dbl, Digits.pips)

    // OrderSend(... Slippage.Pips * pips2points, Bid - StopLossPips * pips2dbl
//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {
   if (Digits == 5 || Digits == 3)
   {    // Adjust for five (5) digit brokers.
      pips2dbl    = Point*10; pips2points = 10;   Digits.pips = 1;
   } 
   else 
    {    
      pips2dbl    = Point;    pips2points =  1;   Digits.pips = 0; 
    }
    // OrderSend(... Slippage.Pips * pips2points, Bid - StopLossPips * pips2dbl
//---- 

//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
  
  
//---- 
   
//----
   return(0);
  }


int Crossed (double line1 , double line2)
   {
      static int last_direction = 0;
      static int current_direction = 0;
      
      if(line1>line2)current_direction = 1; //up   //might need to add here for second macd agreement
      if(line1<line2)current_direction = 2; //down



      if(current_direction != last_direction) //changed 
      {
            last_direction = current_direction;
            return (last_direction);
      }
      else
      {
            return (0);
      }
   }
   
int Crossed_2 (double line1 , double line2) //i'll use this later for macd agreement on 2 timeframes
   {
      static int last_direction_2 = 0;
      static int current_direction_2 = 0;
      
      if(line1>line2)current_direction_2 = 1; //up
      if(line1<line2)current_direction_2 = 2; //down



      if(current_direction_2 != last_direction_2) //changed 
      {
            last_direction_2 = current_direction_2;
            return (last_direction_2);
      }
      else
      {
            return (0);
      }
   }
   
   
    
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
  {
//---- 

 
   int cnt, ticket, total, mrmagic, pos;
   double faster, slower, faster_2, slower_2;
   
   
   if(Bars<100)
     {
      Print("bars less than 100");
      return(0);  
     }
   if(TakeProfit<10)
     {
      Print("TakeProfit less than 10");
      return(0);  // check TakeProfit
     }
     
     
   faster = iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_MAIN,1); //MODE_MAIN
   slower = iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_SIGNAL,1); //MODE_SIGNAL
   faster_2 = iMACD(NULL,PERIOD_H4,12,26,9,PRICE_CLOSE,MODE_MAIN,1); //MODE_MAIN
   slower_2 = iMACD(NULL,PERIOD_H4,12,26,9,PRICE_CLOSE,MODE_SIGNAL,1); //MODE_SIGNAL
   total = 0;
   
   int isCrossed  = Crossed (faster,slower);
   int isCrossed_2 = Crossed_2 (faster_2,slower_2); // i'll use this later for macd agreement on separate timeframes
   
for (pos = OrdersTotal()-1; pos >= 0; pos--)
    if(OrderSelect(pos, SELECT_BY_POS)
      && OrderMagicNumber() == mrmagic
      && OrderSymbol() == Symbol()){
         total++;
         return(0);
         }
        
         
    if(total == 0)
     {
       if(isCrossed == 1){ // I want to add && isCrossed_2 == 1) but 0 trades occur with the note below
            ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,3*pips2points,Bid-StopLoss*pips2dbl,Bid+TakeProfit*pips2dbl,"Agent86",12345,0,Green);
            if(ticket>0){
               if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) Print("BUY order opened : ",OrderOpenPrice());
              }
            else Print("Error opening BUY order : ",GetLastError()); 
            return(0);
         }
       if(isCrossed == 2){ // I want to add && isCrossed_2 == 2) but 0 trades occur with the note above
            ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,3*pips2points,Ask+StopLoss*pips2dbl,Ask-TakeProfit*pips2dbl,"Agent86",12345,0,Red);
            if(ticket>0){
               if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) Print("SELL order opened : ",OrderOpenPrice());
              }
            else Print("Error opening SELL order : ",GetLastError()); 
            return(0);
          }  

/*
for(cnt=0;cnt<total;cnt++){
      OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
      if(OrderType()<=OP_SELL && OrderSymbol()==Symbol()){
         if(OrderType()==OP_BUY){   // long position is opened
            // should it be closed?
           if(isCrossed == 2){
               OrderClose(OrderTicket(),OrderLots(),Bid,3,Violet); // close position
               return(0); // exit
               }                 
            // should it be closed?
            if(isCrossed == 1){
               OrderClose(OrderTicket(),OrderLots(),Ask,3,Violet); // close position
               return(0); // exit
               }
              }          
            } 
          }
*/               
        }
   return(0);
  }
  
//+------------------------------------------------------------------+
Regardless of using both of the for loops or one or the other this produces many open/close trades which all close with s/l
At the end of the results there are numerous closed trades in positive gains which say: close at stop

I know I have to close these trades somehow and do not appear to understand the concept even though I am using the example from the lessons which I thought I understood what to do and how it works.

Oh well, Back to the Drawing Board again.

Thanks for all the answers I will eventually understand this, I think I need some more lessons and reading.


P.S
I did code the wasLastBuy codes etc. into this code as well, which I have taken out temporarily because it caused the EA to only place 1 trade then never again traded.
 
Agent86:
Ok, so it's sort of an EA ID, and would be a different number for each order and associated with a particular EA

What about the same EA on different time frames, would this be handled by the magic number also ? Or a totally different process ?
I advocate order select loops like
    for(pos = OrdersTotal()-1; pos >= 0 ; pos--) if (
        OrderSelect(pos, SELECT_BY_POS)                 // Only my orders w/
    &&  OrderMagicNumber()  == magic.number             // my magic number
    &&  OrderSymbol()       == Symbol()                 // and my pair.
    ){

Just in case the EA is put on other charts and the user doesn't change the magic number.

For the case where the EA is put on the same pair but different timeframe, either the user MUST change the magic number, or you must use a range of magic numbers

extern int      Magic.Number.Base               = 20110202;
int     magic.number;
string  TF.text
int     init(){
    /*++++ Adjust for the current chart timeframe */{               static
    int     TFperiod[]  = { PERIOD_M1,  PERIOD_M5,  PERIOD_M15, PERIOD_M30,
                            PERIOD_H1,  PERIOD_H4,  PERIOD_D1,  PERIOD_W1,
                            PERIOD_MN1  };                      static
    string  TFtext[]    = { "M1",       "M5",       "M15",      "M30",
                            "H1",       "H4",       "D1",       "W1",
                            "MN1"       };
        for(int iTF=0; TFperiod[iTF] < Period(); iTF++){}
        TF.text                 = TFtext[iTF];
        magic.number    = Magic.Number.Base + iTF;
    /*---- Adjust for the current chart timeframe */}
}