trade when indicator from separate timeframe agree

 
Noob, here

I'm having a heck of a time trying to make an EA trade an indicator on 1 timeframe - while the indicator from another timeframe is in the same direction
Seems that no matter what I do it either trades in one direction only, or NO trades
Or maybe at best 2 trades and that all, LOL

I need to understand the general concept.

Basic indicator agreement
For example trade 5min EMA crosses, but only if they are in the same direction as the 1hr EMA crosses. Or in the same direction as the Daily Crosses etc. and so on.

Please advise

Thanks

 
Agent86:
I need to understand the general concept.
For example trade 5min EMA crosses, but only if they are in the same direction as the 1hr EMA crosses. Or in the same direction as the Daily Crosses etc. and so on.
double  emaM5curr = iMA(NULL, PERIOD_M5, EMAlength, 0, MODE_EMA, PRICE_CLOSE,1),
        emaM5prev = iMA(NULL, PERIOD_M5, EMAlength, 0, MODE_EMA, PRICE_CLOSE,2),
        emaH1curr = iMA(NULL, PERIOD_H1, EMAlength, 0, MODE_EMA, PRICE_CLOSE,2),
        emaH1prev = iMA(NULL, PERIOD_H1, EMAlength, 0, MODE_EMA, PRICE_CLOSE,1);
        
static bool wasLastBuy = false;
            wasLastSell = false;
if (emaM5curr > emaM5prev
 && emaH1curr > emaH1prev
 && !wasLastBuy){
    int ticket = OrderSend( OP_BUY );
    if (ticket < 0) Alert("OS(b) failed: ",GetLastError());
    else{           wasLastBuy = true;  wasLastSell = false;
    }
}
if (emaM5curr < emaM5prev
 && emaH1curr < emaH1prev
 && !wasLastSell){ ...
x
 
I'm struggling with this a little
But I think I understand

I assume that I simply have to define these variables with something like wasLastBuy = ( OrderSelect(SELECT_BY_POS, MODE_TRADES);

Perhaps a bit more code then this, but something to define wasLastBuy and wasLastSell should do it for me.

I'll post some finished code for others to follow for example incase they want to know this subject too.

Please advise thanks.


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

//---- input parameters
extern double    TakeProfit=300.0;
extern double    Lots=0.1;
extern double    StopLoss=50.0;
//++++ 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);
  }
   
    
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
  {
//---- 

   int ticket;  
     
   double   faster = iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_MAIN,0), //MODE_MAIN
            slower = iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_SIGNAL,0), //MODE_SIGNAL
            faster_2 = iMACD(NULL,PERIOD_H4,12,26,9,PRICE_CLOSE,MODE_MAIN,0), //MODE_MAIN
            slower_2 = iMACD(NULL,PERIOD_H4,12,26,9,PRICE_CLOSE,MODE_SIGNAL,0); //MODE_SIGNAL
   
   
   
   static bool wasLastBuy = true,wasLastSell = true;

// regarding wasLastBuy = false; 
// !wasLastBuy ?
// I am confusing myself and do not completely understand this static bool statement
// this EA only places 2 trades then quits
// I also need to put in the OrderClose statments
          
   if(faster > slower
    && faster_2 > slower_2
    && !wasLastBuy){
       ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,3*pips2points,Ask-StopLoss*pips2dbl,Ask+TakeProfit*pips2dbl,"Agent86",12345,0,Green);
       if (ticket < 0)Alert("OS(b) failed: ",GetLastError());
       else{
       wasLastBuy = true;
       wasLastSell = false;
       return(0);
      }
   }
   if(faster < slower
    && faster_2 < slower_2
    && !wasLastSell){
       ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,3*pips2points,Bid-StopLoss*pips2dbl,Bid+TakeProfit*pips2dbl,"Agent86",12345,0,Green);
       if (ticket < 0)Alert("OS(b) failed: ",GetLastError());
       else{
       wasLastBuy = false;
       wasLastSell = true;
       return(0);  
      }
   }
 return(0);
 
 }  

/* I got something wrong here, I'll work this over a bit later
if(ticket >= 1){
   OrderSelect(SELECT_BY_POS, MODE_TRADES);
   if(OrderType()==OP_BUY && OrderSymbol()==Symbol()){
   //should it be closed
      if(faster < slower){            
         OrderClose(OrderTicket(),OrderLots(),Bid,3*pips2points,Violet); // close position
         return(0); // exit
         }                 
   if(OrderType()==OP_SELL && OrderSymbol()==Symbol()){
   //should it be closed
      if(faster > slower){
      OrderClose(OrderTicket(),OrderLots(),Ask,3*pips2points,Violet); // close position
         return(0); // exit
        }
       }
     }           
  }
   return(0);
  }
//+------------------------------------------------------------------+

*/


I know something is not write but I seem to be missing something I cannot figure out.

I have another EA based on lesson 13 My_First_EA and did edit it with some similar code previously, however the EA either does not place trades or ends up only placing 2 trades in the begining then never again.

Here is the other code:


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

//---- input parameters
extern double    TakeProfit=300.0;
extern double    Lots=0.1;
extern double    StopLoss=12.0;
//++++ 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
   {
      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;
   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,0); //MODE_MAIN
   slower = iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_SIGNAL,0); //MODE_SIGNAL
   faster_2 = iMACD(NULL,PERIOD_H4,12,26,9,PRICE_CLOSE,MODE_MAIN,0); //MODE_MAIN
   slower_2 = iMACD(NULL,PERIOD_H4,12,26,9,PRICE_CLOSE,MODE_SIGNAL,0); //MODE_SIGNAL
   
   int isCrossed  = Crossed (faster,slower);
   int isCrossed_2 = Crossed_2 (faster_2,slower_2); // i'll use this later
   
   total  = OrdersTotal(); 
   if(total < 1)
     {
       if(isCrossed == 1 && isCrossed_2 == 1)
         {
            ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,3*pips2points,Ask-StopLoss*pips2dbl,Ask+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 && isCrossed_2 == 2)
         {
            ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,3*pips2points,Ask+StopLoss*pips2dbl,Bid-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);
         }
         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 || isCrossed_2 == 2)
                {
                 OrderClose(OrderTicket(),OrderLots(),Bid,3*pips2points,Violet); // close position
                 return(0); // exit
                }        
           
            // should it be closed?
            if(isCrossed == 1 || isCrossed_2 == 1)
              {
               OrderClose(OrderTicket(),OrderLots(),Ask,3*pips2points,Violet); // close position
               return(0); // exit
              }
           }
        }
     }
   return(0);
  }
//+------------------------------------------------------------------+


The second code only places 3 trades

The first code I'm still working on but getting myself confused.

Please advise

 
total  = OrdersTotal(); 
if(total < 1)
This makes the EA incompatible with any other including itself on other pairs
total = 0;
    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.
    ){
       total++;
    }
if (total == 0)...
 

I see
Thanks


I guess It's ok for now, while I work out my other issues; and while learning.I'll only use it on the 1 currency while I learn other code and then I'll work that out too.

I don't understand how to use the magicnumber yet, so I'll have to read up on some of that too.

Thanks this will help a lot.

 

The Magic number is very simple to implement and use . . . you just add it as a variable when you do an OrderSend and you use it as a check when you are selecting orders to process . . .

The purpose is that you can identify Orders that were placed by a particular EA . . . for example, you have EA1 running on GBPUSD H1 and EA2 on GBPUSD H4, you don't want EA1 closing/deleting/modifying EA2's orders or visa versa.

 
This thread continues here
 
RaptorUK:

The Magic number is very simple to implement and use . . . you just add it as a variable when you do an OrderSend and you use it as a check when you are selecting orders to process . . .

The purpose is that you can identify Orders that were placed by a particular EA . . . for example, you have EA1 running on GBPUSD H1 and EA2 on GBPUSD H4, you don't want EA1 closing/deleting/modifying EA2's orders or visa versa.

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 ?

 
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 ?


Not . . . "a different number for each order" it would usually be the same number for all Orders being placed by the same instance of an EA.

You could use the magic number to differentiate TFs also . . it's up to you how you implement it.

 
RaptorUK:

Not . . . "a different number for each order" it would usually be the same number for all Orders being placed by the same instance of an EA.

You could use the magic number to differentiate TFs also . . it's up to you how you implement it.

Ohhhh! right same number OK. I was thinking more like a database ID like mysql or something of which I also know only little about.

Noted for future reference

Thanks