need your help please

Back to topics list To post a new topic, please log in or register
avatar
7
waimaj 2015.05.03 04:05
 
hi everybody


please i need your help because i have no coding background and want to code simple indicator.

i  am trying to code an indicator that draws two emas(55,100)on the main chart and also draws horizontal line at the point when they equal each other (ema1=ema2).the horizontal line should start at that point till the end of the chart until a new point is generated.

the following code gives dozen of errors that is my miserable attempt.

#property copyright "Copyright 2015, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict
#property indicator_chart_window
#property indicator_buffers 2
#property indicator_color1 SpringGreen
#property indicator_color2 Red
#property indicator_color1 DarkTurquoise
#property indicator_color2 Red
extern int prd1= 55;
extern int prd2=100;
double mova1[];
double mova2[];
//+ ------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()  {

  SetIndexBuffer(0,mova1);
  SetIndexBuffer (1,mova2);
  SetIndexStyle(0,DRAW_LINE);
  SetIndexStyle(1,DRAW_LINE);
//--- indicator buffers mapping
   
//---
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start() {
  
  int i,limit;
   int countedbars= IndicatorCounted();
   if (countedbars<0) return -1;
   if(countedbars>0) countedbars--;
   limit=Bars-countedbars;
   
   for(i=0; i<limit; i++)
   
   mova1[i]= iMA(NULL,0,prd1,0,1,0,i);
   mova2[i]= iMA(NULL,0,prd2,0,1,0,i);
   
   double equalp = (mova1-mova2 );
   double drawp;
   if (equalp == 0) ( drawp = equalp);
   
   ObjectCreate("crss",OBJ_HLINE,0,0,drawp);
   
   
   
   
   
   
   
//---
   
//--- return value of prev_calculated for next call
   return(0);
  }
avatar
Moderator
3416
GumRai 2015.05.03 04:53 #
 

I don't see curly braces to determine what goes into the for loop and if test

It would be extremely rare for 2 MAs to be exactly the same value on a bar so check for a cross

ie

if((mova1[i]>mova2[i] && mova1[i+1]<mova2[i+1]) || (mova1[i]<mova2[i] && mova1[i+1]>mova2[i+1]))

If you just want a single horizontal line on your chart, create it in init and move it in your main code. If this is the case, you would need to count down in your loop so that the line is at the most recent cross, not the very first cross on the chart

avatar
12995
WHRoeder 2015.05.03 13:59 #
 
  1. Exactly. The == operand. - MQL4 forum
  2. Or perhaps
    bool  isUp = mova1[i]   > mova2[i];
    bool wasUp = mova1[i+1] > mova2[i+1];
    bool isCross = isUp != wasUp; if(isCross)
    
avatar
250
ffoorr 2015.05.04 16:03 #
 

Not easy to do that :


//+------------------------------------------------------------------+
//|                                                       Maimaj.mq4 |
//|                                                 GGG Macon France |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2015, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict
#property indicator_chart_window
#property indicator_buffers 4
#property indicator_color1 SpringGreen
#property indicator_color2 Red
#property indicator_color3 Lime
#property indicator_color4 Magenta
extern bool cross_on_ema_on_close = false;
extern int C= 55;
extern int L=100;
double CC[];
double LL[];
double hausse[];
double baisse[];

double cross_up, cross_down;
//+ ------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()  {
  SetIndexBuffer(0,CC);       SetIndexStyle(0,DRAW_LINE,0,2);
  SetIndexBuffer (1,LL);      SetIndexStyle(1,DRAW_LINE,0,2);
  SetIndexBuffer(2,hausse);   SetIndexStyle(2,DRAW_LINE,0,4);
  SetIndexBuffer (3,baisse);  SetIndexStyle(3,DRAW_LINE,0,4);
  SetIndexEmptyValue(2,0.0);
  SetIndexEmptyValue(3,0.0);
//---
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start() {
  
   int i,limit;
   int countedbars= IndicatorCounted();
   if (countedbars<0) return -1;
   if(countedbars>0) countedbars--;
   limit=Bars-countedbars;
   
  for(i=0; i<limit; i++)  CC[i]= iMA(NULL,0,C,0,1,0,i);
  for(i=0; i<limit; i++)  LL[i]= iMA(NULL,0,L,0,1,0,i);
  //==================================================== 

   i= Bars - 500;
   if( countedbars > 500 ) i = Bars - countedbars -1;
   while( i >= 0 )
    {
     if( CC[i+1]>=LL[i+1] && CC[i]<=LL[i] )
         { 
         if( cross_on_ema_on_close ) cross_down = Close[i];
         else                        cross_down = CC[i]; 
         hausse[i+1]=0.0;
         }
      
        baisse[i] = cross_down  ;

       if( CC[i+1]<=LL[i+1] && CC[i]>=LL[i] )
         {
         if(  cross_on_ema_on_close ) cross_up = Close[i];
         else                         cross_up = CC[i]; 
         baisse[i+1] = 0.0;
         }
      
         hausse[i] =cross_up;
    
    if( CC[i] > LL[i] ) baisse  [i]  = 0.0;
    if( CC[i] < LL[i] ) hausse[i]  = 0.0;
   //---  
   i--; 
   }
   //-------- 
   return(0);
  }
  //========================================================================
avatar
7
waimaj 2015.05.05 01:50 #
 
ffoorr:

Not easy to do that :


thank you very much ffoorr for your kind help. apparently that is exactly what i want, but let me check it carefully any further question i shall post. thanks once more for your generosity.
avatar
7
waimaj 2015.07.15 01:52 #
 

ffoorr: 

Not easy to do that :

 

Monsieur   ffoorr

 

One more and last favor, in the attached indicator please make the same lines as above when whatever two mas equal each other same like the above indicator. you can limit number of lines for each moving average via an external variable lets call it Linesnum .

 

thanks in advance  



Attached files:
  wrksp11.mq4  (18.16 KB)
Back to topics list  

To add comments, please log in or register