Trade Closing Between Different Time Frames

 

How can I prevent my ea from closing a trade when I switch from one time chart to another?  For example,  suppose I have a trade opened on the 1 hour chart.  The ea closes when one moving average crosses over another moving average.  Assume in the 1 hour chart the crossover has not occured.  However, if I switch my chart to the 4 hour chart, a crossover over of the same moving average with respect to the 4 hour chart has occured, and the ea closes the trade.  This is a very irritating weakness in my ea because sometimes I want to glimpse the higher time frames without the ea automatically closing the trade.  How can I prevent this?  I have included the code.

//+------------------------------------------------------------------+
//|                                                  close trade.mq4 |
//|                                                                  |
//|                                                                  |
//+------------------------------------------------------------------+
 

#property copyright "PRM"
#property link      "PRM"
 
 
extern int enable_long_close = 0;
extern int enable_short_close = 0;
extern int close_fast_ma = 3;
extern int close_slow_ma = 8;
 
 
double close_fast_ma_value = 0.0;
double close_slow_ma_value = 0.0; 
 
int init()
  {
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   return(0);
  }
 
int start()
  {
     

close_fast_ma_value = iMA(NULL,PERIOD_H1, close_fast_ma, 0, MODE_EMA, PRICE_CLOSE, 0);
close_slow_ma_value = iMA(NULL,PERIOD_H1, close_slow_ma, 0, MODE_EMA, PRICE_CLOSE, 0);  

   
   if (close_fast_ma_value < close_slow_ma_value && enable_long_close == 1)  
      {
        while(OrdersTotal()>0)
        {
          if(OrderSelect(0,SELECT_BY_POS))
             {
               if(OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),3,Red) == true)
               {
               enable_long_close = 0;
               }
               
             }
          }
   
         }

   if (close_fast_ma_value > close_slow_ma_value && enable_short_close == 1)  
      {
        while(OrdersTotal()>0)
        {
          if(OrderSelect(0,SELECT_BY_POS))
             {
               if (OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),3,Red) == true)
               {
               enable_short_close = 0;
                }
              }
          }
   
         } 
    
    return(0);
  }
Files:
 
forestmyopia:

 

How can I prevent my ea from closing a trade when I switch from one time chart to another?  For example,  suppose I have a trade opened on the 1 hour chart.  The ea closes when one moving average crosses over another moving average.  Assume in the 1 hour chart the crossover has not occured.  However, if I switch my chart to the 4 hour chart, a crossover over of the same moving average with respect to the 4 hour chart has occured, and the ea closes the trade.  This is a very irritating weakness in my ea because sometimes I want to glimpse the higher time frames without the ea automatically closing the trade.  How can I prevent this?  I have included the code.

 

I don't see how this is possible as you have coded it to use MAs from PERIOD_H1 time frame

close_fast_ma_value = iMA(NULL,PERIOD_H1, close_fast_ma, 0, MODE_EMA, PRICE_CLOSE, 0);
close_slow_ma_value = iMA(NULL,PERIOD_H1, close_slow_ma, 0, MODE_EMA, PRICE_CLOSE, 0);  

 

 .

 
i wonder how it can be possible...  i am far to be a pro, but i agree, why your ea see a cross on h4 if you ask the h1 ma have you tried to print the value somewhere? let us know
 
i suppose by checking the ChartPeriod and validate  if actual timeframe on the chart the ea is on is h1 before to send the close order would prevent this, by if you stay to long on the 4h time frame and the cross on h1 ma really occurs... doesn't help                                           really interesting issue
 
Gachette2:
i suppose by checking the ChartPeriod and validate  if actual timeframe on the chart the ea is on is h1 before to send the close order would prevent this, by if you stay to long on the 4h time frame and the cross on h1 ma really occurs... doesn't help                                           really interesting issue
//+------------------------------------------------------------------+
//|                                                  close trade.mq4 |
//|                                                                  |
//|                                                                  |
//+------------------------------------------------------------------+
 

#property copyright "PRM"
#property link      "PRM"
 
 
extern int enable_long_close = 0;
extern int enable_short_close = 0;
extern int chart_period = 240; //ADDED THIS VARIABLE
extern int close_fast_ma = 3;
extern int close_slow_ma = 8;
 
int time_period_minutes = 0; //ADDED THIS VARIABLE
 
double close_fast_ma_value = 0.0;
double close_slow_ma_value = 0.0; 
 
int init()
  {
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   return(0);
  }
 
int start()
  {
     

time_period_minutes = Period(); //PERFORMED THIS PREDEFINED VARIABLE FUNCTION

close_fast_ma_value = iMA(NULL,PERIOD_H1, close_fast_ma, 0, MODE_EMA, PRICE_CLOSE, 0);
close_slow_ma_value = iMA(NULL,PERIOD_H1, close_slow_ma, 0, MODE_EMA, PRICE_CLOSE, 0);  

   
   if (close_fast_ma_value < close_slow_ma_value && enable_long_close == 1 && time_period_minutes == chart_period)// CHECKED CHART PERIOD HERE  
      {
        while(OrdersTotal()>0)
        {
          if(OrderSelect(0,SELECT_BY_POS))
             {
               if(OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),3,Red) == true)
               {
               enable_long_close = 0;
               }
               
             }
          }
   
         }

   if (close_fast_ma_value > close_slow_ma_value && enable_short_close == 1 && time_period_minutes == chart_period)// CHECKED CHART PERIOD HERE   
      {
        while(OrdersTotal()>0)
        {
          if(OrderSelect(0,SELECT_BY_POS))
             {
               if (OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),3,Red) == true)
               {
               enable_short_close = 0;
                }
              }
          }
   
         } 
    
    return(0);
  }
Thanks Gachette2.  Your last suggestion fixed the problem as shown in the above code.
 

I'm confused by your logic.

Now with the input

chart_period = 240;

 orders will ONLY be closed if the current chart is H4

You may as well have

int start()
  {
   if(Period()!=chart_period)
      return(0);  
//Rest of code

 as there is no point in doing the other calculations unless the chart is H4 or whatever the input is set to.

 
You are correct about there is no point in doing the other calculations.  There are more than one way to implement the code to accomplish, but your way is the quickest and easiest.  Less is more.  The point of all of this is I generally trade on the 4 hour chart.  I open a trade on the 4 hour chart and I want the trade to close only on the 4 hr chart based on the moving averages that are on the 4 hr chart.  However, sometimes, because I am lazy, I want to peek at the higher time frames from the same chart or maybe a lower timer frame just to see how the trade is going with respect to other time frame moving averages.  When I do this, I want to make sure the ea does not close the trade when I look at a different time frame.  The abover code takes care of this.
 
forestmyopia:

 

How can I prevent my ea from closing a trade when I switch from one time chart to another?  For example,  suppose I have a trade opened on the 1 hour chart.  The ea closes when one moving average crosses over another moving average.  Assume in the 1 hour chart the crossover has not occured.  However, if I switch my chart to the 4 hour chart, a crossover over of the same moving average with respect to the 4 hour chart has occured, and the ea closes the trade.  This is a very irritating weakness in my ea because sometimes I want to glimpse the higher time frames without the ea automatically closing the trade.  How can I prevent this?  I have included the code.

 

 

forestmyopia:
You are correct about there is no point in doing the other calculations.  There are more than one way to implement the code to accomplish, but your way is the quickest and easiest.  Less is more.  The point of all of this is I generally trade on the 4 hour chart.  I open a trade on the 4 hour chart and I want the trade to close only on the 4 hr chart based on the moving averages that are on the 4 hr chart.  However, sometimes, because I am lazy, I want to peek at the higher time frames from the same chart or maybe a lower timer frame just to see how the trade is going with respect to other time frame moving averages.  When I do this, I want to make sure the ea does not close the trade when I look at a different time frame.  The abover code takes care of this.

 You are confusing matters

close_fast_ma_value = iMA(NULL,PERIOD_H1, close_fast_ma, 0, MODE_EMA, PRICE_CLOSE, 0);
close_slow_ma_value = iMA(NULL,PERIOD_H1, close_slow_ma, 0, MODE_EMA, PRICE_CLOSE, 0);  

   

 You have hard-coded the MAs to calculate based on the H1 chart

 
GumRai:

 

 You are confusing matters

 You have hard-coded the MAs to calculate based on the H1 chart

 

 

 
You are correct.  Thanks for pointing it out.  It should read PERIOD_H4.
 

No, it should read

close_fast_ma_value = iMA(NULL,chart_period , close_fast_ma, 0, MODE_EMA, PRICE_CLOSE, 0);
close_slow_ma_value = iMA(NULL,chart_period , close_slow_ma, 0, MODE_EMA, PRICE_CLOSE, 0);  

 Then it will work on the period that you input

remove the

time_period_minutes = Period(); //PERFORMED THIS PREDEFINED VARIABLE FUNCTION

 and

 if (close_fast_ma_value < close_slow_ma_value && enable_long_close == 1 && time_period_minutes == chart_period)// CHECKED CHA

 highlighted

 if (close_fast_ma_value > close_slow_ma_value && enable_short_close == 1 && time_period_minutes == chart_period)// CHECKED CHA

 Then it should work as you intended no matter what time-frame you have open