What is wrong with my EA ?

 

Hello All,

I have a general template of an EA that I use to test systems. At the moment I am testing a indicator "better volume indicator" and I am trying to use it in my EA.

From the Indicator MQL file. I get the buffers:

      SetIndexBuffer(0,red);
      SetIndexStyle(0,DRAW_HISTOGRAM,0,width2);
      SetIndexLabel(0,"Climax High ");
     
      SetIndexBuffer(4,white);
      SetIndexStyle(4,DRAW_HISTOGRAM,0,width2);
      SetIndexLabel(4,"Climax Low ");


In my EA: I use:

double up  = iCustom(Symbol(), Period(), "vol", 0, 1);
double down = iCustom(Symbol(), Period(), "vol", 4, 1);

Now when there is climax high/low volume, indicator shows it as a positive number (indicator pic attached). Buy/sell conditions in my EA is also given below:

if(up > 0 && BuyTicket == 0)
   {
   OrderSelect(SellTicket,SELECT_BY_TICKET);
   // Close order
   if(OrderCloseTime() == 0 && SellTicket > 0)
   {
      double CloseLots = OrderLots();
      double ClosePrice = Ask;
      bool Closed = OrderClose(SellTicket,CloseLots,ClosePrice,UseSlippage,Red);
   }
   double OpenPrice = Ask;
   // Calculate stop loss and take profit
   if(TakeProfit > 0) double BuyTakeProfit = OpenPrice + (TakeProfit * UsePoint);
   if(StopLoss > 0) double BuyStopLoss = OpenPrice - (StopLoss * UsePoint);
   // Open buy order
      BuyTicket = OrderSend(Symbol(),OP_BUY,LotSize,OpenPrice,UseSlippage,
      BuyStopLoss,BuyTakeProfit,"Buy Order",MagicNumber,0,Green);
      SellTicket = 0;
   // Breakeven Logic    

   }

-- SELL

   if(down > 0 && SellTicket == 0)
   {
      OrderSelect(BuyTicket,SELECT_BY_TICKET);
   if(OrderCloseTime() == 0 && BuyTicket > 0)
   {
      CloseLots = OrderLots();
      ClosePrice = Bid;
      Closed = OrderClose(BuyTicket,CloseLots,ClosePrice,UseSlippage,Red);
   }
   OpenPrice = Bid;
   if(StopLoss > 0) double SellStopLoss = OpenPrice + (StopLoss * UsePoint);
   if(TakeProfit > 0) double SellTakeProfit = OpenPrice - (TakeProfit * UsePoint);
      SellTicket = OrderSend(Symbol(),OP_SELL,LotSize,OpenPrice,UseSlippage,
      SellStopLoss,SellTakeProfit,"Sell Order",MagicNumber,0,Red);
      BuyTicket = 0;
   }  


Problem is :

It is opening trades at each tick but none of the other indicators I test with same EA show this behavior. I am unable to find out the root cause of the problem with this indicator. Any help would be highly appreciated.

Thanks

Files:
indicator.PNG  8 kb
 
double up  = iCustom(Symbol(), Period(), "vol", 0, 1);
double down = iCustom(Symbol(), Period(), "vol", 4, 1);

You're checking the previous/closed bar, so whenever it's bigger than zero it will trigger.

 
pipPod:

You're checking the previous/closed bar, so whenever it's bigger than zero it will trigger.

Thank you for your reply pippod,

But I am using the same logic with other indicators also and it only opens one trade.

After opening the first order, In the buy/sell condition when it checks if there are no buy orders open (BuyTicket == 0), it should not open any other trades. Right ?

what should I add to this to limit it to only one order ?

Thanks

 

Not familiar with the indicator but according to the trade logic it's probably getting a buy and sell signal at the same time.

I suggest to print up and down at both buy and sell.

    if(up > 0 && BuyTicket == 0)
   {
   printf("up = %.1f down = %.1f",up,down);
   OrderSelect(SellTicket,SELECT_BY_TICKET);
 
pipPod:

Not familiar with the indicator but according to the trade logic it's probably getting a buy and sell signal at the same time.

I suggest to print up and down at both buy and sell.

hi pipPod,

Thank you for your reply. I have printed the values and they are same for buy and sell :( Please see attached image(print.PNG)

As in the first image that I uploaded it should show a positive no for climax high or climax low values in Mt4 data window and in theory, I should be able to use this values from icustom function.  I have tried, everything I knew :(

Any help will be highly appreciated.

Here is the indicator code:

#property copyright ""
#property link      ""

// vol.mq4
// modified to correct start loop

#property indicator_separate_window
#property indicator_buffers 9
#property indicator_color1 Green          
#property indicator_color2 LightGray
#property indicator_color3 LightGray
#property indicator_color4 LightGray
#property indicator_color5 Crimson      
#property indicator_color6 Magenta      
#property indicator_color7 Black    
#property indicator_width1 2
#property indicator_width2 2
#property indicator_width3 2
#property indicator_width4 2
#property indicator_width5 2
#property indicator_width6 2


extern int     NumberOfBars = 5000 ; // 0 ; 500;
extern string  Note = "0 means Display all bars";
extern int     MAPeriod = 100 ;
extern int     LookBack = 10;
extern int     width1 = 2 ;
extern int     width2 = 2 ;

double red[],blue[],yellow[],green[],white[],magenta[],v4[];

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- indicators
      SetIndexBuffer(0,red);
      SetIndexStyle(0,DRAW_HISTOGRAM,0,width2);
      SetIndexLabel(0,"Climax High ");
     
      SetIndexBuffer(1,blue);
      SetIndexStyle(1,DRAW_HISTOGRAM,0,width2);
      SetIndexLabel(1,"Neutral");
     
      SetIndexBuffer(2,yellow);
      SetIndexStyle(2,DRAW_HISTOGRAM,0,width2);
      SetIndexLabel(2,"Low ");
     
      SetIndexBuffer(3,green);
      SetIndexStyle(3,DRAW_HISTOGRAM,0,width2);
      SetIndexLabel(3,"HighChurn ");

      SetIndexBuffer(4,white);
      SetIndexStyle(4,DRAW_HISTOGRAM,0,width2);
      SetIndexLabel(4,"Climax Low ");

      SetIndexBuffer(5,magenta);
      SetIndexStyle(5,DRAW_HISTOGRAM,0,width2);
      SetIndexLabel(5,"ClimaxChurn ");

      SetIndexBuffer(6,v4);
      SetIndexStyle(6,DRAW_LINE,0,2);
      SetIndexLabel(6,"Average("+MAPeriod+")");

      IndicatorShortName("vol" );
     
 

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

//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {

   double VolLowest,Range,Value2,Value3,HiValue2,HiValue3,LoValue3,tempv2,tempv3,tempv;
   int limit;
   int counted_bars=IndicatorCounted();
//---- last counted bar will be recounted
   if(counted_bars>0) counted_bars--;

   if ( NumberOfBars == 0 )
      limit = Bars-counted_bars;
   if ( NumberOfBars > 0 && NumberOfBars < Bars )  //
      limit = NumberOfBars - counted_bars;

   for(int i=0; i<limit; i++)  
      {
         red[i] = 0; blue[i] = Volume[i]; yellow[i] = 0; green[i] = 0; white[i] = 0; magenta[i] = 0;
         Value2=0;Value3=0;HiValue2=0;HiValue3=0;LoValue3=99999999;tempv2=0;tempv3=0;tempv=0;

         VolLowest = Volume[iLowest(NULL,0,MODE_VOLUME,20,i)];
         if (Volume[i] == VolLowest)
            {
               yellow[i] = NormalizeDouble(Volume[i],0);
               blue[i]=0;
            }

         Range = (High[i]-Low[i]);
         Value2 = Volume[i]*Range;

         if (  Range != 0 )
            Value3 = Volume[i]/Range;

         for ( int n=i;n<i+MAPeriod;n++ )
            {
               tempv= Volume[n] + tempv;
            }
          v4[i] = NormalizeDouble(tempv/MAPeriod,0);

          for ( n=i;n<i+LookBack;n++)
            {
               tempv2 = Volume[n]*((High[n]-Low[n]));
               if ( tempv2 >= HiValue2 )
                  HiValue2 = tempv2;

               if ( Volume[n]*((High[n]-Low[n])) != 0 )
                  {          
                     tempv3 = Volume[n] / ((High[n]-Low[n]));
                     if ( tempv3 > HiValue3 )
                        HiValue3 = tempv3;
                     if ( tempv3 < LoValue3 )
                        LoValue3 = tempv3;
                  }
            }

          if ( Value2 == HiValue2  && Close[i] > (High[i] + Low[i]) / 2 )
            {
               red[i] = NormalizeDouble(Volume[i],0);
               blue[i]=0;
               yellow[i]=0;
            }  

          if ( Value3 == HiValue3 )
            {
               green[i] = NormalizeDouble(Volume[i],0);               
               blue[i] =0;
               yellow[i]=0;
               red[i]=0;
            }
          if ( Value2 == HiValue2 && Value3 == HiValue3 )
            {
               magenta[i] = NormalizeDouble(Volume[i],0);
               blue[i]=0;
               red[i]=0;
               green[i]=0;
               yellow[i]=0;
            }
         if ( Value2 == HiValue2  && Close[i] <= (High[i] + Low[i]) / 2 )
            {
               white[i] = NormalizeDouble(Volume[i],0);
               magenta[i]=0;
               blue[i]=0;
               red[i]=0;
               green[i]=0;
               yellow[i]=0;
            }

        
      }
//----

//----
   return(0);
  }

//+------------------------------------------------------------------+
Files:
print.PNG  109 kb
 

Hello All,

ok, I have printed values returned by icustom and they are empty values (2147483647). Why icustom is unable to read the value from the indicator ?

can anyone please help me with this ? I have tried almost everything I could find in the documentation and online.

Thanks

 
s9919:

Hello All,

ok, I have printed values returned by icustom and they are empty values (2147483647). Why icustom is unable to read the value from the indicator ?

can anyone please help me with this ? I have tried almost everything I could find in the documentation and online.

Thanks

No, you have not tried everything. You are not calling the indicator correctly.

Look in the MetaTrader tutorials to see how to call an indicator properly .

Sorry for this "tough love" answer, but you need to learn to do it properly.

PS if you want to try the lazy approach, just look inside the code of any EA that uses external indicators to see how it is done.

 
David Raine:

No, you have not tried everything. You are not calling the indicator correctly.

Look in the MetaTrader tutorials to see how to call an indicator properly .

Sorry for this "tough love" answer, but you need to learn to do it properly.

PS if you want to try the lazy approach, just look inside the code of any EA that uses external indicators to see how it is done.

Hello David,

I have added external variables and add following lines also. Still my problem persists. May be if you could be a bit more specific about the direction that should I take that would be more helpful.

Thanks anyways!

iCustom(Symbol(),Period(),"vol",NumberOfBars,Note,MAPeriod,LookBack,width1,width2,0,0);
double up = iCustom(Symbol(), Period(), "vol",NumberOfBars,Note,MAPeriod,LookBack,width1,width2, 0, 1);
double down = iCustom(Symbol(), Period(), "vol",NumberOfBars,Note,MAPeriod,LookBack,width1,width2, 4, 1);
 
s9919:

Hello David,

I have added external variables and add following lines also. Still my problem persists. May be if you could be a bit more specific about the direction that should I take that would be more helpful.

Thanks anyways!

I see it had 9 buffers are you sure you are reading the correct index?

Also why are you reading the previous/closed bar?

double  iCustom(
   string       symbol,           // symbol
   int          timeframe,        // timeframe
   string       name,             // path/name of the custom indicator compiled program
   ...                            // custom indicator input parameters (if necessary)
   int          mode,             // line index
   int          shift             // shift
   );
 
Marco vd Heijden:

I see it had 9 buffers are you sure you are reading the correct index?

Also why are you reading the previous/closed bar?

double  iCustom(
   string       symbol,           // symbol
   int          timeframe,        // timeframe
   string       name,             // path/name of the custom indicator compiled program
   ...                            // custom indicator input parameters (if necessary)
   int          mode,             // line index
   int          shift             // shift
   );

hi Marco,

I changed it to 7 buffer. As I am taking indexes from the indicator's code. IndexBuffer 0 is for Climax High and IndexBuffer 4 is for Climax Low so I am using these in iCustom. It is constantly changing the color of the volume histogram for the current bar. After bar close, color is permanent and volume value is placed into either ClimaxHigh,ClimaxLow or remaining three labels. That is why I am checking the value of the previous candle and then making a buy or sell decision.

      SetIndexBuffer(0,red);
      SetIndexStyle(0,DRAW_HISTOGRAM,0,width2);
      SetIndexLabel(0,"Climax High ");
     
      SetIndexBuffer(1,blue);
      SetIndexStyle(1,DRAW_HISTOGRAM,0,width2);
      SetIndexLabel(1,"Neutral");
     
      SetIndexBuffer(2,yellow);
      SetIndexStyle(2,DRAW_HISTOGRAM,0,width2);
      SetIndexLabel(2,"Low ");
     
      SetIndexBuffer(3,green);
      SetIndexStyle(3,DRAW_HISTOGRAM,0,width2);
      SetIndexLabel(3,"HighChurn ");

      SetIndexBuffer(4,white);
      SetIndexStyle(4,DRAW_HISTOGRAM,0,width2);
      SetIndexLabel(4,"Climax Low ");

      SetIndexBuffer(5,magenta);
      SetIndexStyle(5,DRAW_HISTOGRAM,0,width2);
      SetIndexLabel(5,"ClimaxChurn ");

      SetIndexBuffer(6,v4);
      SetIndexStyle(6,DRAW_LINE,0,2);
      SetIndexLabel(6,"Average("+MAPeriod+")");
 
s9919:

hi Marco,

I changed it to 7 buffer. As I am taking indexes from the indicator's code. IndexBuffer 0 is for Climax High and IndexBuffer 4 is for Climax Low so I am using these in iCustom. It is constantly changing the color of the volume histogram for the current bar. After bar close, color is permanent and volume value is placed into either ClimaxHigh,ClimaxLow or remaining three labels. That is why I am checking the value of the previous candle and then making a buy or sell decision.

Volume can become very large and you declared it a double where it needs to be long type.
Reason: