close order works comparing to moving average, but not when it's moving average with offset - please help

 

Hi, thanks in advance for the help!

I've spent hours trying to get this to work but after searching everywhere I still don't understand why it's not working. Any help would be appreciated.


The following is defined at the top of the program for users to enter their offset from the exit moving average:

extern double TrailingExitMAOffsetPct = 0.01;


The only difference between Code1 and Code2 is these lines:

Code1

         if(Close[1]<ma && OrderProfit() > 0)
         if(Close[1]>ma && OrderProfit() > 0)

Code2

         if(Close[1]<ma_offset_buy && OrderProfit() > 0)
         if(Close[1]>ma_offset_sell && OrderProfit() > 0)


For whatever reason, it's like the comparison operator cannot compare the two values and breaks on Code2?  Any ideas?  Thank you once again!


---------------------------------------------------------------------------------------------------------------------------

Full code below:

Code1 (works in tester showing positions are exited when close price crosses moving average (ma)):

void CheckForClose()
  {
   int error = 0;
//--- go trading only for first tiks of new bar
   if(Volume[0]>1) return;
//--- get indicator values
   double ma=iMA(NULL,0,ExitMovingPeriod,0,MODE_SMA,PRICE_CLOSE,0);
   double ma_offset=ma*TrailingExitMAOffsetPct;
   double ma_offset_buy=NormalizeDouble(ma-ma_offset,5);
   double ma_offset_sell=NormalizeDouble(ma+ma_offset,5);
  
//---
   for(int i=0;i<OrdersTotal();i++)
     {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break;
      if(OrderMagicNumber()!=MagicNumber || OrderSymbol()!=Symbol()) continue;
      //--- check order type 
      if(OrderType()==OP_BUY)
        {
         if(Close[1]<ma && OrderProfit() > 0)
           {
           Print("ma_offset_buy",ma_offset_buy);
            if(!OrderClose(OrderTicket(),OrderLots(),Bid,3,White))
               {
               error = GetLastError();
               Print("OrderClose error ",error, " = ", ErrorDescription(error));
               }
           }
         break;
        }
      if(OrderType()==OP_SELL)
        {
         if(Close[1]>ma && OrderProfit() > 0)
           {
           Print("ma_offset_sell",ma_offset_sell); 
            if(!OrderClose(OrderTicket(),OrderLots(),Ask,3,White))
               {
               error = GetLastError();
               Print("OrderClose error ",error, " = ", ErrorDescription(error));
               }
           }
         break;
        }
     }
//---
  }


Code 2 (does NOT work in tester, positions do not get closed (it's supposed to exit when close price crosses moving average + offset)):

void CheckForClose()
  {
   int error = 0;
//--- go trading only for first tiks of new bar
   if(Volume[0]>1) return;
//--- get indicator values
   double ma=iMA(NULL,0,ExitMovingPeriod,0,MODE_SMA,PRICE_CLOSE,0);
   double ma_offset=ma*TrailingExitMAOffsetPct;
   double ma_offset_buy=NormalizeDouble(ma-ma_offset,5);
   double ma_offset_sell=NormalizeDouble(ma+ma_offset,5);
  
//---
   for(int i=0;i<OrdersTotal();i++)
     {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break;
      if(OrderMagicNumber()!=MagicNumber || OrderSymbol()!=Symbol()) continue;
      //--- check order type 
      if(OrderType()==OP_BUY)
        {
         if(Close[1]<ma_offset_buy && OrderProfit() > 0)
           {
           Print("ma_offset_buy",ma_offset_buy);
            if(!OrderClose(OrderTicket(),OrderLots(),Bid,3,White))
               {
               error = GetLastError();
               Print("OrderClose error ",error, " = ", ErrorDescription(error));
               }
           }
         break;
        }
      if(OrderType()==OP_SELL)
        {
         if(Close[1]>ma_offset_sell && OrderProfit() > 0)
           {
           Print("ma_offset_sell",ma_offset_sell); 
            if(!OrderClose(OrderTicket(),OrderLots(),Ask,3,White))
               {
               error = GetLastError();
               Print("OrderClose error ",error, " = ", ErrorDescription(error));
               }
           }
         break;
        }
     }
//---
  }
 
whats1thingnow:

Hi, thanks in advance for the help!

I've spent hours trying to get this to work but after searching everywhere I still don't understand why it's not working. Any help would be appreciated.


The following is defined at the top of the program for users to enter their offset from the exit moving average:


The only difference between Code1 and Code2 is these lines:

Code1

Code2


For whatever reason, it's like the comparison operator cannot compare the two values and breaks on Code2?  Any ideas?  Thank you once again!


---------------------------------------------------------------------------------------------------------------------------

Full code below:

Code1 (works in tester showing positions are exited when close price crosses moving average (ma)):


Code 2 (does NOT work in tester, positions do not get closed (it's supposed to exit when close price crosses moving average + offset)):

 double ma_offset_buy=NormalizeDouble(ma-ma_offset,5);
 double ma_offset_sell=NormalizeDouble(ma+ma_offset,5);
  
Try with ' Digits' instead of 5
I dont know What is your broker's Digits and
Which Symbol you use
 
Ahmet Metin Yilmaz:
Try with ' Digits' instead of 5
I dont know What is your broker's Digits and
Which Symbol you use

thank you for your quick response


i tried

   double ma_offset_buy=NormalizeDouble(ma-ma_offset,Digits);
   double ma_offset_sell=NormalizeDouble(ma+ma_offset,Digits);


still no luck


this EA is for multiple symbols including indices, forex, metals


the broker is CMC Markets (cfd)


is it because Close[1] cannot be compared to a float value or real value or something similar?  

 
  1. whats1thingnow:
       double ma_offset=ma*TrailingExitMAOffsetPct;

    Always post all relevant code. If TrailingExistMAOffsetPct is actually a percent, price × 25 is not a price difference (price × 0.25 would be.)
         How To Ask Questions The Smart Way. 2004
              Be precise and informative about your problem

  2. if(Volume[0]>1) return;

    For a new bar test, Bars is unreliable (a refresh/reconnect can change number of bars on chart,) volume is unreliable (miss ticks,) Price is unreliable (duplicate prices and The == operand. - MQL4 programming forum.) Always use time.
              New candle - MQL4 programming forum #3 2014.04.04

    I disagree with making a new bar function, because it can only be called once per tick. A variable can be tested multiple times.
              Running EA once at the start of each bar - MQL4 programming forum 2011.05.06

 
whats1thingnow:

is it because Close[1] cannot be compared to a float value or real value or something similar?  

You can change with this 
iClose(Symbol(), 0,1)
 

William Roeder:

  1. Always post all relevant code. If TrailingExistMAOffsetPct is actually a percent, price × 25 is not a price difference (price × 0.25 would be.)
         How To Ask Questions The Smart Way. 2004
              Be precise and informative about your problem

  2. For a new bar test, Bars is unreliable (a refresh/reconnect can change number of bars on chart,) volume is unreliable (miss ticks,) Price is unreliable (duplicate prices and The == operand. - MQL4 programming forum.) Always use time.
              New candle - MQL4 programming forum #3 2014.04.04

    I disagree with making a new bar function, because it can only be called once per tick. A variable can be tested multiple times.
              Running EA once at the start of each bar - MQL4 programming forum 2011.05.06

thanks for the help

1. TrailingExitMAOffsetPct is default set to 1% as per posted at the very top:

extern double TrailingExitMAOffsetPct = 0.01;


i have the print there to make sure the calculation is correct... it is doing the 1% offset

 Print("ma_offset_buy",ma_offset_buy);


the problem remains that when i use the variables ma_offset_buy and ma_offset_sell, positions are not closed:

         if(Close[1]<ma_offset_buy && OrderProfit() > 0)
         if(Close[1]>ma_offset_sell && OrderProfit() > 0)



2. slightly confused regarding this point - i understand you are saying don't use volume b/c it's unreliable

if(Volume[0]>1) return;

instead use time as per https://www.mql5.com/en/forum/150885#comment_3766801


can you elaborate on


  1. I disagree with making a new bar function, because it can only be called once per tick. A variable can be tested multiple times.
              Running EA once at the start of each bar - MQL4 programming forum 2011.05.06


--------------



Ahmet Metin Yilmaz
:

You can change with this 
iClose(Symbol(), 0,1)

yes, i have tried both

Close[1]<ma_offset_buy
iClose(Symbol(), 0,1)<ma_offset_buy

positions do not get closed for neither

Reason: