Need to change the indicator to multipair.

 

Hi All,

help is needed. anyone can change this code to multipair. i have tried it. something i am missing. at line 89 it is giving the error.

DSS = delta/(HighRange - LowRange)*100.0;


code is added.  my change it here below.



#property indicator_separate_window
#property indicator_minimum 0
#property indicator_maximum 100
#property indicator_buffers 3
#property indicator_color1 clrWhite
#property indicator_width1 1
#property indicator_color2 DeepSkyBlue
#property indicator_width2 2
#property indicator_color3 Gold
#property indicator_width3 2
#property indicator_level1 10
#property indicator_level2 90


extern int       EMA_period        = 8;
extern int       Stochastic_period = 13;
extern string    Pair = "GBPUSD";


double DssBuffer[];
double MitBuffer[];
double DssBuffer_UP[];
double DssBuffer_DW[];


double smooth_coefficient;
datetime alertonce = 0;

int init()
{
   IndicatorBuffers(4);
   SetIndexBuffer(0,DssBuffer);
   SetIndexStyle(2,DRAW_LINE);
   SetIndexBuffer(1,DssBuffer_UP);
   SetIndexStyle(1,DRAW_ARROW);
   SetIndexArrow(1,228);
   SetIndexBuffer(2,DssBuffer_DW);
   SetIndexStyle(2,DRAW_ARROW);
   SetIndexArrow(2,230);

   SetIndexBuffer(3,MitBuffer);
   
   SetIndexEmptyValue(0, 0.0);
   SetIndexLabel(0, "DSS");
   SetIndexEmptyValue(1, 0.0);
   SetIndexLabel(1, "DSS Up");
   SetIndexEmptyValue(2, 0.0);
   SetIndexLabel(2, "DSS Down");

   IndicatorShortName ("RSS MultiP("+Pair+"--"+EMA_period+","+Stochastic_period+")");

   smooth_coefficient = 2.0 / (1.0 + EMA_period);
   return(0);
}


int start()
{
   int i, limit, counted_bars=IndicatorCounted(), Bar_OK;
   
   if (counted_bars == 0)   limit = iBars(Pair,1) - Stochastic_period;
   if (counted_bars > 0)   limit = iBars(Pair,1) - counted_bars;
   
   
   //if (counted_bars<0) return(-1);
   //if(counted_bars>0) counted_bars--;
   //limit = MathMin(Bars-counted_bars,Bars-1);
   
   
   
   double HighRange, LowRange;
   double delta, MIT;
   for (i = limit; i >= 0; i--)
   {
      HighRange = iHigh(Pair,0,iHighest(Pair,0,2,Stochastic_period,i));
      
      
      LowRange = iLow(Pair,0,iLowest(Pair,0,1,Stochastic_period,i));
      
      
      delta = iClose(Pair,0,i) - LowRange;
      
      
      
      MIT = delta/(HighRange - LowRange)*100.0;
      
      MitBuffer[i] = smooth_coefficient * (MIT - MitBuffer[i+1]) + MitBuffer[i+1];
      
   }

   double DSS;
   for (i = limit; i >= 0; i--)
   {
      
      HighRange = MitBuffer[ArrayMaximum(MitBuffer, Stochastic_period, i)];
      LowRange = MitBuffer[ArrayMinimum(MitBuffer, Stochastic_period, i)];
      //printf("size"+ ArraySize(MitBuffer)+ "---loop "+i+"---MitbufferIndex: "+ArrayMinimum(MitBuffer, 21, i));
      
      delta = MitBuffer[i] - LowRange;
      DSS = delta/(HighRange - LowRange)*100.0;
      DssBuffer[i] = smooth_coefficient * (DSS - DssBuffer[i+1]) + DssBuffer[i+1];
   }

   for (i = limit; i >= 0; i--)
   {
      //DssBuffer_UP[i]=EMPTY_VALUE;
      //DssBuffer_DW[i]=EMPTY_VALUE;
      Bar_OK=0;
      
      if(DssBuffer[i]>DssBuffer[i+1])
      {
         DssBuffer_UP[i]=DssBuffer[i];
         Bar_OK=1;
         DssBuffer_DW[i] = 0.0;
      }

      if(DssBuffer[i]<DssBuffer[i+1])
      {
         DssBuffer_DW[i]=DssBuffer[i];
         Bar_OK=1;
         DssBuffer_UP[i] = 0.0;
      }

      if(Bar_OK==0)
      {
         DssBuffer_UP[i]=DssBuffer_UP[i+1];
         DssBuffer_DW[i]=DssBuffer_DW[i+1];
      }   
   }

 
   
   return(0);
}
Documentation on MQL5: Language Basics / Preprocessor / Program Properties (#property)
Documentation on MQL5: Language Basics / Preprocessor / Program Properties (#property)
  • www.mql5.com
Program Properties (#property) - Preprocessor - Language Basics - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 
  1. Please edit your (original) post and use the CODE button (or Alt+S)! (For large amounts of code, attach it.)
              General rules and best pratices of the Forum. - General - MQL5 programming forum (2019)
              Messages Editor

  2. momarzai: something i am missing. at line 89 it is giving the error.

    You don't indicate what line is 89, nor what the error is. Do you really expect an answer? There are no mind readers here and our crystal balls are cracked. Always post all relevant code (using Code button) or attach the source file.
         How To Ask Questions The Smart Way. (2004)
              Be precise and informative about your problem

  3. int init()
    {
       ⋮
    }
    
    
    int start()
    {
       ⋮

    You should stop using the old event handlers (init, start, deinit) and IndicatorCounted() and start using new event handlers (OnInit, OnTick/OnCalculate, OnDeinit).
              Event Handling Functions - MQL4 Reference
              How to do your lookbacks correctly - MQL4 programming forum #9-14 & #19 (2016)

 
William Roeder #:
  1. Please edit your (original) post and use the CODE button (or Alt+S)! (For large amounts of code, attach it.)
              General rules and best pratices of the Forum. - General - MQL5 programming forum (2019)
              Messages Editor

  2. You don't indicate what line is 89, nor what the error is. Do you really expect an answer? There are no mind readers here and our crystal balls are cracked. Always post all relevant code (using Code button) or attach the source file.
         How To Ask Questions The Smart Way. (2004)
              Be precise and informative about your problem

  3. You should stop using the old event handlers (init, start, deinit) and IndicatorCounted() and start using new event handlers (OnInit, OnTick/OnCalculate, OnDeinit).
              Event Handling Functions - MQL4 Reference
              How to do your lookbacks correctly - MQL4 programming forum #9-14 & #19 (2016)

Thanks for feedback, offcourse thier are no mind readers. but just run it in mt4 you would see it too.  in line 89 the error is divided by zero.

i didnt changed the code on event lvl. wanted first it work the way i want. then working i will change it.

these two line below returns 0. which makes in line 89 delta/zero. what exactly happend, that it returns 0. i cant figure it out. but in some magic way. sometine it works. without error.

HighRange = MitBuffer[ArrayMaximum(MitBuffer, Stochastic_period, i)];
 LowRange = MitBuffer[ArrayMinimum(MitBuffer, Stochastic_period, i)];


i hoop that i cleared it up.


english is not my native language. sry

 
momarzai #: Thanks for feedback, offcourse thier are no mind readers. but just run it in mt4 you would see it too.  in line 89 the error is divided by zero. i didnt changed the code on event lvl. wanted first it work the way i want. then working i will change it. these two line below returns 0. which makes in line 89 delta/zero. what exactly happend, that it returns 0. i cant figure it out. but in some magic way. sometine it works. without error.

HighRange = MitBuffer[ArrayMaximum(MitBuffer, Stochastic_period, i)];
LowRange = MitBuffer[ArrayMinimum(MitBuffer, Stochastic_period, i)];

i hoop that i cleared it up. english is not my native language. sry

  1. We are under no obligation to run or debug your code for you. That is up to you to do. So if you require help, it is up to you to identify the offending line explicitly (such as highlighting it, for example) and reporting what error is reported by showing the log entry.
  2. When using ArrayMaximum or ArrayMinimum, you should always verify that the index they return is valid, before using it as an array index. You are not doing that. You cannot assume that they always return a valid index.
    As per the documentation — "Return Value: The function returns an index of a found element taking into account the array serial. In case of failure it returns -1."
  3. If HighRange and LowRange are both zero, or if there difference is zero, then obviously you will get a "divide by zero" error. So, you have to verify those conditions in your code and handle the outcome.
    To identify the reasons for this, either place more "Print" statements in your code to log the various values for you monitor the outcome, or use the built-in debugger and place some breakpoints so you can monitor the variables and find the reason for the issue.
EDIT: And please use the "</>" icon or Alt-S when adding code. Don't just copy/paste your code as plain text. So, please edit your original post and do this for your code so that it will be easier to read.
 
Fernando Carreiro #:
  1. We are under no obligation to run or debug your code for you. That is up to you to do. So if you require help, it is up to you to identify the offending line explicitly (such as highlighting it, for example) and reporting what error is reported by showing the log entry.
  2. When using ArrayMaximum or ArrayMinimum, you should always verify that the index they return is valid, before using it as an array index. You are not doing that. You cannot assume that they always return a valid index.
    As per the documentation — "Return Value: The function returns an index of a found element taking into account the array serial. In case of failure it returns -1."
  3. If HighRange and LowRange are both zero, or if there difference is zero, then obviously you will get a "divide by zero" error. So, you have to verify those conditions in your code and handle the outcome.
    To identify the reasons for this, either place more "Print" statements in your code to log the various values for you monitor the outcome, or use the built-in debugger and place some breakpoints so you can monitor the variables and find the reason for the issue.
EDIT: And please use the "</>" icon or Alt-S when adding code. Don't just copy/paste your code as plain text. So, please edit your original post and do this for your code so that it will be easier to read.

Thanks Fernando,

offcourse nobody is under any obligation. just thought out of cerioucity . point is noted for future.


"When using ArrayMaximum or ArrayMinimum"  i tesetd 

MitBuffer

it is always contain data. what do you mean with checking array index. how can i check it if it is valid index?


thanks


ArrayMaximum - Array Functions - MQL4 Reference
ArrayMaximum - Array Functions - MQL4 Reference
  • docs.mql4.com
ArrayMaximum - Array Functions - MQL4 Reference
 
momarzai #: "When using ArrayMaximum or ArrayMinimum"  i tesetd. it is always contain data. what do you mean with checking array index. how can i check it if it is valid index?
// the following code was not compiled or tested, but simply typed out

int indexMax = ArrayMaximum( MitBuffer, Stochastic_period, i );
if( indexMax >= 0 ) {
   Print( "Valid index for maximum found: ", indexMax );
   HighRange = MitBuffer[ indexMax ];
} else {
   Print( "Invalid index for maximum!" );
   HighRange = some_default_value;
};
 
Fernando Carreiro #: i am greatfull for the help. Thanks.  i have solved it like you said. hoop that the else clause doesnt fail. hoop the logic is right. if index is -1 then use the previouse value.
     
      indexH = ArrayMaximum(MitBuffer, Stochastic_period, i);
      indexL = ArrayMinimum(MitBuffer, Stochastic_period, i);
      
      if( indexH >= 0 && indexL >=0) {
         
          HighRange = MitBuffer[indexH];
          LowRange = MitBuffer[indexL];
      } else {
         //Print( "Invalid index for maximum!" );
        HighRange = MitBuffer[ArrayMaximum(MitBuffer, Stochastic_period, i+1)];
        LowRange = MitBuffer[ArrayMinimum(MitBuffer, Stochastic_period, i+1)];
      };