.

Re: Coding Help

#1031
Hey! ... aha moment .... Thanks master mrTool !... my indi need one more simple line code of yours : ChartRedraw(0);
;)

Code: Select all

#property indicator_chart_window
#define C 67

extern bool indicator_ON = true;

int OnInit()
  {
   return(INIT_SUCCEEDED);
  }
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime& time[],
                const double& open[],
                const double& high[],
                const double& low[],
                const double& close[],
                const long& tick_volume[],
                const long& volume[],
                const int& spread[])
  {
   return(rates_total);
  }
 ///
void OnChartEvent(const int id,const long& lparam,const double& dparam,const string& sparam)
  {
   if(id == CHARTEVENT_KEYDOWN && lparam == C) 
   {press_C();}
   
  }
///////
void press_C()
{
//
  if (indicator_ON)
  {
   updateCHARTTYPE();
   ChartRedraw(0);
  }                              
}
///////
void updateCHARTTYPE ()
{
long     Chart_ID       = ChartID();
//
double CHARTTYPE = ChartGetInteger(Chart_ID,CHART_MODE);
if          (CHARTTYPE !=1) ChartSetInteger(Chart_ID,CHART_MODE,1); 
else if     (CHARTTYPE !=0) ChartSetInteger(Chart_ID,CHART_MODE,0);
}


Re: Coding Help

#1032
Hello Guys

I am inciting my studies in MQL4 language, so my level of knowledge is that of a novice.
I have used the EA Builder to create object-oriented way my first indicators and so I will make several combinations of technical indicators and then analyze the generated codes, which will give me a little notion of things.

My question is:
When I import a custom indicator for EA Builder I do not have the option to declare a condition applied to opening or closing the price.

Example:
In this image you can see that the option to declare a value the condition of the price applied at the time of its closing, is available when we choose the indicator RSI (Relative Strength Index)
Image


But after importing the MomPinboll indicator from "Karakurt " I do not have the option to declare a value the condition and apply it to opening or closing the price.
Image


What do I need to do to make this option available which is crucial to create my indicator?

If any kind-hearted guru can help me I would be immensely grateful.
Good luck to everyone
MomPinboll.mq4
(1.89 KiB) Downloaded 10 times

Re: Coding Help

#1033
nyol wrote:
Mon Aug 05, 2019 6:28 am
Hello Guys

I am inciting my studies in MQL4 language, so my level of knowledge is that of a novice.
I have used the EA Builder to create object-oriented way my first indicators and so I will make several combinations of technical indicators and then analyze the generated codes, which will give me a little notion of things.

My question is:
When I import a custom indicator for EA Builder I do not have the option to declare a condition applied to opening or closing the price.

Example:
In this image you can see that the option to declare a value the condition of the price applied at the time of its closing, is available when we choose the indicator RSI (Relative Strength Index)
Image


But after importing the MomPinboll indicator from "Karakurt " I do not have the option to declare a value the condition and apply it to opening or closing the price.
Image


What do I need to do to make this option available which is crucial to create my indicator?

If any kind-hearted guru can help me I would be immensely grateful.
Good luck to everyone
Hi, iRSIOnArray has no price options.
BufferRSI = iRSIOnArray( BufferROC, 0, ExtPeriodRSI, i )
you can only change from:
Close - Close [i + ExtPeriodROC];
with:
Open - Open [i + ExtPeriodROC];

but MrTools can answer you better.
MomPinboll_Mod.mq4
(4.36 KiB) Downloaded 27 times
The perfect indicatoris our head ...

Too many indicators confuse the head

Re: Coding Help

#1034
You guys have been a tremendous help. I'm so glad I found this community. Below is the code for a volitilty quality indicator I have. I wish to be able to turn off the Solid line divergences and the histogram as I only use the dotted line divergences on the chart. The code won't let me turn off the solid lines by using "none" as the color without turning off the dotted divergence lines. The histogram I can just minimize the window so that really isn't an issue. I'm just trying to keep my chart uncluttered as best I can.

Code: Select all

//+------------------------------------------------------------------+
//|                                           Volatility quality.mq4 |
//|                                                                  |
//|                                                                  |
//| Volatility quality index originaly developed by                  |
//| Thomas Stridsman (August 2002 Active Trader Magazine)            |
//+------------------------------------------------------------------+
#property copyright "www.forex-station.com"
#property link      "www.forex-station.com"


#property indicator_separate_window
#property indicator_buffers    5
#property indicator_color1     PaleVioletRed
#property indicator_color2     PaleVioletRed
#property indicator_color3     DeepSkyBlue
#property indicator_color4     DeepSkyBlue
#property indicator_color5     Silver
#property indicator_width1     2
#property indicator_width3     2
#property indicator_width5     2
#property indicator_levelcolor DarkGray

//
//
//
//
//

extern string TimeFrame                 = "Current time frame";
extern int    PriceSmoothing            = 5;
extern int    PriceSmoothingMethod      = MODE_LWMA;
extern double Filter                    = 0.05;
extern bool   divergenceVisible         = true;
extern bool   divergenceOnValuesVisible = true;
extern bool   divergenceOnChartVisible  = true;
extern color  divergenceBullishColor    = Green;
extern color  divergenceBearishColor    = OrangeRed;
extern string divergenceUniqueID        = "vq diverge1";
extern bool   HistogramOnSlope          = false;
extern bool   Interpolate               = true;
extern bool   alertsOn                  = false;
extern bool   alertsOnSlope             = false;
extern bool   alertsOnCurrent           = true;
extern bool   alertsMessage             = true;
extern bool   alertsSound               = false;
extern bool   alertsEmail               = false;

//
//
//
//
//

double Upa[];
double Upb[];
double Dna[];
double Dnb[];
double sumVqi[];
double Vqi[];
double trend[];
double slope[];

int    timeFrame;
string indicatorFileName;
bool   returnBars;
bool   calculateValue;
string shortName;

//+------------------------------------------------------------------
//|                                                                  
//+------------------------------------------------------------------
// 
//
//
//
//

int init()
  {
   IndicatorBuffers(8);
      SetIndexBuffer(0,Dna);   SetIndexStyle(0,DRAW_HISTOGRAM);
      SetIndexBuffer(1,Dnb);   SetIndexStyle(1,DRAW_HISTOGRAM);
      SetIndexBuffer(2,Upa);   SetIndexStyle(2,DRAW_HISTOGRAM);
      SetIndexBuffer(3,Upb);   SetIndexStyle(3,DRAW_HISTOGRAM);
      SetIndexBuffer(4,sumVqi); 
      SetIndexBuffer(5,Vqi);   
      SetIndexBuffer(6,trend); 
      SetIndexBuffer(7,slope);
      SetLevelValue(0,0); 
      
      PriceSmoothing = MathMax(PriceSmoothing,1);
        
        //
        //
        //
        //
        //
      
        indicatorFileName = WindowExpertName();
        returnBars        = (TimeFrame=="returnBars");     if (returnBars)     return(0);
        calculateValue    = (TimeFrame=="calculateValue");
        if (calculateValue)
        {
           int s = StringFind(divergenceUniqueID,":",0);
                 shortName = divergenceUniqueID;
                 divergenceUniqueID = StringSubstr(divergenceUniqueID,0,s);
                 return(0);
        }            
        timeFrame = stringToTimeFrame(TimeFrame);
      
        //
        //
        //
        //
        //
      
   shortName = divergenceUniqueID+":  "+timeFrameToString(timeFrame)+"  Volatility quality zero line ";
   IndicatorShortName(shortName);
return(0);
}

//
//
//
//
//

int deinit() 
{

   int lookForLength = StringLen(divergenceUniqueID);
   for (int i=ObjectsTotal()-1; i>=0; i--) 
   {
      string objectName = ObjectName(i);
      if (StringSubstr(objectName,0,lookForLength) == divergenceUniqueID) ObjectDelete(objectName);
   }
   
return(0);
}

//------------------------------------------------------------------
//
//------------------------------------------------------------------
//
//
//
//
//

int start()
{
   int counted_bars=IndicatorCounted();
   int i,limit;

   if(counted_bars<0) return(-1);
   if(counted_bars>0) counted_bars--;
         limit = MathMin(Bars-counted_bars,Bars-1);
         if (returnBars) { Dna[0] = limit+1; return(0); }
         double pipMultiplier = MathPow(10,_Digits%2);
         
   //
   //
   //
   //
   //
   
   if (calculateValue || timeFrame == Period())
   {         
     for(i=limit; i>=0; i--)
     {
      if (i==(Bars-1))
      {
         Vqi[i]    = 0;
         sumVqi[i] = 0;
         continue;
      }
      
      //
      //
      //
      //
      //
      
         double cHigh  = iMA(NULL,0,PriceSmoothing,0,PriceSmoothingMethod,PRICE_HIGH ,i);
         double cLow   = iMA(NULL,0,PriceSmoothing,0,PriceSmoothingMethod,PRICE_LOW  ,i);
         double cOpen  = iMA(NULL,0,PriceSmoothing,0,PriceSmoothingMethod,PRICE_OPEN ,i);
         double cClose = iMA(NULL,0,PriceSmoothing,0,PriceSmoothingMethod,PRICE_CLOSE,i);
         double pClose = iMA(NULL,0,PriceSmoothing,0,PriceSmoothingMethod,PRICE_CLOSE,i+1);

         double trueRange = MathMax(cHigh,pClose)-MathMin(cLow,pClose);
         double     range = cHigh-cLow;
      
            if (range != 0 && trueRange!=0)
               double vqi = ((cClose-pClose)/trueRange + (cClose-cOpen)/range)*0.5;
            else      vqi = Vqi[i+1];

      //
      //
      //
      //
      //
         
         Vqi[i]    = MathAbs(vqi) * (cClose - pClose+cClose - cOpen) * 0.5;
         sumVqi[i] = Vqi[i];
         Dna[i]    = EMPTY_VALUE;
         Dnb[i]    = EMPTY_VALUE;
         Upa[i]    = EMPTY_VALUE;
         Upb[i]    = EMPTY_VALUE;
            if (Filter > 0) if (fabs(sumVqi[i]-sumVqi[i+1]) < Filter*Point*pipMultiplier) sumVqi[i] = sumVqi[i+1];
      
      //
      //
      //
      //
      //
      
      trend[i] = trend[i+1];
      slope[i] = slope[i+1];
          
          if (sumVqi[i] > 0)           trend[i] =  1;
          if (sumVqi[i] < 0)           trend[i] = -1;
          if (sumVqi[i] > sumVqi[i+1]) slope[i] =  1;
          if (sumVqi[i] < sumVqi[i+1]) slope[i] = -1;
               
          if (divergenceVisible)
          {
             CatchBullishDivergence(sumVqi,i);
             CatchBearishDivergence(sumVqi,i);
          }
                                     
          if (HistogramOnSlope)
          {
             if (trend[i]== 1 && slope[i] == 1) Upa[i] = sumVqi[i];
             if (trend[i]== 1 && slope[i] ==-1) Upb[i] = sumVqi[i];
             if (trend[i]==-1 && slope[i] ==-1) Dna[i] = sumVqi[i];
             if (trend[i]==-1 && slope[i] == 1) Dnb[i] = sumVqi[i];
          }
          else
          {                  
             if (trend[i]== 1) Upa[i] = sumVqi[i];
             if (trend[i]==-1) Dna[i] = sumVqi[i];
          }
               
      }
      manageAlerts();
      return(0);
      }   
      
     //
     //
     //
     //
     //
     
     limit = MathMax(limit,MathMin(Bars-1,iCustom(NULL,timeFrame,indicatorFileName,"returnBars",0,0)*timeFrame/Period()));
   
     for(i=limit; i>=0; i--)
     {
      int y = iBarShift(NULL,timeFrame,Time[i]);
         sumVqi[i] = iCustom(NULL,timeFrame,indicatorFileName,"calculateValue",PriceSmoothing,PriceSmoothingMethod,Filter,divergenceVisible,divergenceOnValuesVisible,divergenceOnChartVisible,divergenceBullishColor,divergenceBearishColor,shortName,Interpolate,4,y);
         trend[i]  = iCustom(NULL,timeFrame,indicatorFileName,"calculateValue",PriceSmoothing,PriceSmoothingMethod,Filter,divergenceVisible,divergenceOnValuesVisible,divergenceOnChartVisible,divergenceBullishColor,divergenceBearishColor,shortName,Interpolate,6,y);
         slope[i]  = iCustom(NULL,timeFrame,indicatorFileName,"calculateValue",PriceSmoothing,PriceSmoothingMethod,Filter,divergenceVisible,divergenceOnValuesVisible,divergenceOnChartVisible,divergenceBullishColor,divergenceBearishColor,shortName,Interpolate,7,y);
         Dna[i]    = EMPTY_VALUE;
         Dnb[i]    = EMPTY_VALUE;
         Upa[i]    = EMPTY_VALUE;
         Upb[i]    = EMPTY_VALUE;

         //
         //
         //
         //
         //
      
         if (!Interpolate || y==iBarShift(NULL,timeFrame,Time[i-1])) continue;

         //
         //
         //
         //
         //

         datetime time = iTime(NULL,timeFrame,y);
            for(int n = 1; i+n < Bars && Time[i+n] >= time; n++) continue;
            for(int k = 1; k < n; k++)
               sumVqi[i+k]  = sumVqi[i] + (sumVqi[i+n] - sumVqi[i]) * k/n;
   }
   for (i=limit;i>=0;i--)
   {
      if (HistogramOnSlope)
          {
             if (trend[i]== 1 && slope[i] == 1) Upa[i] = sumVqi[i];
             if (trend[i]== 1 && slope[i] ==-1) Upb[i] = sumVqi[i];
             if (trend[i]==-1 && slope[i] ==-1) Dna[i] = sumVqi[i];
             if (trend[i]==-1 && slope[i] == 1) Dnb[i] = sumVqi[i];
          }
          else
          {                  
             if (trend[i]== 1) Upa[i] = sumVqi[i];
             if (trend[i]==-1) Dna[i] = sumVqi[i];
          }
                               
    }
    manageAlerts();
return(0);
}

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
//
//
//
//
//

void manageAlerts()
{
   if (!calculateValue && alertsOn)
   {
      if (alertsOnCurrent)
           int whichBar = 0;
      else     whichBar = 1; whichBar = iBarShift(NULL,0,iTime(NULL,timeFrame,whichBar));
      
      //
      //
      //
      //
      //
      
      if (alertsOnSlope)
      {
         if (slope[whichBar] != slope[whichBar+1])
         {
            if (slope[whichBar] == 1) doAlert(whichBar,"slope changed to up");
            if (slope[whichBar] ==-1) doAlert(whichBar,"slope changed to down");
         }         
      }
      else
      {
         if (trend[whichBar] != trend[whichBar+1])
         {
            if (trend[whichBar] == 1) doAlert(whichBar,"crossed zero line up");
            if (trend[whichBar] ==-1) doAlert(whichBar,"crossed zero line down");
         }         
      }         
   }
}   

//
//
//
//
//

void doAlert(int forBar, string doWhat)
{
   static string   previousAlert="nothing";
   static datetime previousTime;
   string message;
   
      if (previousAlert != doWhat || previousTime != Time[forBar]) {
          previousAlert  = doWhat;
          previousTime   = Time[forBar];

          //
          //
          //
          //
          //

          message =  StringConcatenate(Symbol()," ",timeFrameToString(timeFrame)," at ",TimeToStr(TimeLocal(),TIME_SECONDS)," VQ oscillator ",doWhat);
             if (alertsMessage) Alert(message);
             if (alertsEmail)   SendMail(StringConcatenate(Symbol(),"VQ"),message);
             if (alertsSound)   PlaySound("alert2.wav");
      }
}

//+------------------------------------------------------------------
//|                                                                  
//+------------------------------------------------------------------
//
//
//
//
//

void CatchBullishDivergence(double& values[], int i)
{
   i++;
            ObjectDelete(divergenceUniqueID+"l"+DoubleToStr(Time[i],0));
            ObjectDelete(divergenceUniqueID+"l"+"os" + DoubleToStr(Time[i],0));            
   if (!IsIndicatorLow(values,i)) return;  

   //
   //
   //
   //
   //

   int currentLow = i;
   int lastLow    = GetIndicatorLastLow(values,i+1);
      if (values[currentLow] > values[lastLow] && Low[currentLow] < Low[lastLow])
      {
         if(divergenceOnChartVisible)  DrawPriceTrendLine("l",Time[currentLow],Time[lastLow],Low[currentLow],Low[lastLow],divergenceBullishColor,STYLE_SOLID);
         if(divergenceOnValuesVisible) DrawIndicatorTrendLine("l",Time[currentLow],Time[lastLow],values[currentLow],values[lastLow],divergenceBullishColor,STYLE_SOLID);
      }
      if (values[currentLow] < values[lastLow] && Low[currentLow] > Low[lastLow])
      {
         if(divergenceOnChartVisible)  DrawPriceTrendLine("l",Time[currentLow],Time[lastLow],Low[currentLow],Low[lastLow], divergenceBullishColor, STYLE_DOT);
         if(divergenceOnValuesVisible) DrawIndicatorTrendLine("l",Time[currentLow],Time[lastLow],values[currentLow],values[lastLow], divergenceBullishColor, STYLE_DOT);
      }
}

//
//
//
//
//

void CatchBearishDivergence(double& values[], int i)
{
   i++; 
            ObjectDelete(divergenceUniqueID+"h"+DoubleToStr(Time[i],0));
            ObjectDelete(divergenceUniqueID+"h"+"os" + DoubleToStr(Time[i],0));            
   if (IsIndicatorPeak(values,i) == false) return;

   //
   //
   //
   //
   //
      
   int currentPeak = i;
   int lastPeak = GetIndicatorLastPeak(values,i+1);
      if (values[currentPeak] < values[lastPeak] && High[currentPeak]>High[lastPeak])
      {
         if (divergenceOnChartVisible)  DrawPriceTrendLine("h",Time[currentPeak],Time[lastPeak],High[currentPeak],High[lastPeak],divergenceBearishColor,STYLE_SOLID);
         if (divergenceOnValuesVisible) DrawIndicatorTrendLine("h",Time[currentPeak],Time[lastPeak],values[currentPeak],values[lastPeak],divergenceBearishColor,STYLE_SOLID);
      }
      if(values[currentPeak] > values[lastPeak] && High[currentPeak] < High[lastPeak])
      {
         if (divergenceOnChartVisible)  DrawPriceTrendLine("h",Time[currentPeak],Time[lastPeak],High[currentPeak],High[lastPeak], divergenceBearishColor, STYLE_DOT);
         if (divergenceOnValuesVisible) DrawIndicatorTrendLine("h",Time[currentPeak],Time[lastPeak],values[currentPeak],values[lastPeak], divergenceBearishColor, STYLE_DOT);
      }
}

//
//
//
//
//

bool IsIndicatorPeak(double& values[], int i) { return(values[i] >= values[i+1] && values[i] > values[i+2] && values[i] > values[i-1]); }
bool IsIndicatorLow( double& values[], int i) { return(values[i] <= values[i+1] && values[i] < values[i+2] && values[i] < values[i-1]); }

int GetIndicatorLastPeak(double& values[], int shift)
{
   for(int i = shift+5; i<Bars; i++)
         if (values[i] >= values[i+1] && values[i] > values[i+2] && values[i] >= values[i-1] && values[i] > values[i-2]) return(i);
   return(-1);
}
int GetIndicatorLastLow(double& values[], int shift)
{
   for(int i = shift+5; i<Bars; i++)
         if (values[i] <= values[i+1] && values[i] < values[i+2] && values[i] <= values[i-1] && values[i] < values[i-2]) return(i);
   return(-1);
}

//+------------------------------------------------------------------
//|                                                                  
//+------------------------------------------------------------------
//
//
//
//
//

void DrawPriceTrendLine(string first,datetime t1, datetime t2, double p1, double p2, color lineColor, double style)
{
   string   label = divergenceUniqueID+first+"os"+DoubleToStr(t1,0);
   if (Interpolate) t2 += Period()*60-1;
    
   ObjectDelete(label);
      ObjectCreate(label, OBJ_TREND, 0, t1+Period()*60-1, p1, t2, p2, 0, 0);
         ObjectSet(label, OBJPROP_RAY, false);
         ObjectSet(label, OBJPROP_COLOR, lineColor);
         ObjectSet(label, OBJPROP_STYLE, style);
}

//
//
//
//
//

void DrawIndicatorTrendLine(string first,datetime t1, datetime t2, double p1, double p2, color lineColor, double style)
{
   int indicatorWindow = WindowFind(shortName);
   if (indicatorWindow < 0) return;
   if (Interpolate) t2 += Period()*60-1;
   
   string label = divergenceUniqueID+first+DoubleToStr(t1,0);
   ObjectDelete(label);
      ObjectCreate(label, OBJ_TREND, indicatorWindow, t1+Period()*60-1, p1, t2, p2, 0, 0);
         ObjectSet(label, OBJPROP_RAY, false);
         ObjectSet(label, OBJPROP_COLOR, lineColor);
         ObjectSet(label, OBJPROP_STYLE, style);
}

//+-------------------------------------------------------------------
//|                                                                  
//+-------------------------------------------------------------------
//
//

string sTfTable[] = { "M1","M5","M15","M30","H1","H4","D1","W1","MN" };
int    iTfTable[] = { 1,5,15,30,60,240,1440,10080,43200 };

//
//
//
//
//

int stringToTimeFrame(string tfs) {
   tfs = stringUpperCase(tfs);
   for (int i=ArraySize(iTfTable)-1; i>=0; i--)
         if (tfs==sTfTable[i] || tfs==""+iTfTable[i]) return(MathMax(iTfTable[i],Period()));
                                                      return(Period());
}

//
//
//
//
//

string timeFrameToString(int tf) {
   for (int i=ArraySize(iTfTable)-1; i>=0; i--) 
         if (tf==iTfTable[i]) return(sTfTable[i]);
                              return("");
}

//
//
//
//
//

string stringUpperCase(string str) {
   string   s = str;

   for (int length=StringLen(str)-1; length>=0; length--) {
      int tchar = StringGetChar(s, length);
         if((tchar > 96 && tchar < 123) || (tchar > 223 && tchar < 256))
                     s = StringSetChar(s, length, tchar - 32);
         else if(tchar > -33 && tchar < 0)
                     s = StringSetChar(s, length, tchar + 224);
   }
   return(s);
}      

Re: Coding Help

#1035

Code: Select all

   {
         if (divergenceOnChartVisible)  DrawPriceTrendLine("h",Time[currentPeak],Time[lastPeak],High[currentPeak],High[lastPeak],divergenceBearishColor,STYLE_SOLID);
         if (divergenceOnValuesVisible) DrawIndicatorTrendLine("h",Time[currentPeak],Time[lastPeak],values[currentPeak],values[lastPeak],divergenceBearishColor,STYLE_SOLID);
      }
If I had just placed a double slash in front of this part, would that turn off the solid lines? I can tinker with code but this is such a nice indicator I didn't want to ruin it.

Edit: I renamed it to preserve the original and tried the above idea. It took out the bearish divergence completely. not just the solid line. The green lines both dotted and solid stayed.

Edit: NVMD. I got it. took out the whole function and it worked. Just needed to take out one line above in addition to the function below.


Re: Coding Help

#1036
^eagle^ wrote:
Sun Aug 11, 2019 10:30 pm
You guys have been a tremendous help. I'm so glad I found this community. Below is the code for a volitilty quality indicator I have. I wish to be able to turn off the Solid line divergences and the histogram as I only use the dotted line divergences on the chart. The code won't let me turn off the solid lines by using "none" as the color without turning off the dotted divergence lines. The histogram I can just minimize the window so that really isn't an issue. I'm just trying to keep my chart uncluttered as best I can.

Code: Select all

//+------------------------------------------------------------------+
//|                                           Volatility quality.mq4 |
//|                                                                  |
//|                                                                  |
//| Volatility quality index originaly developed by                  |
//| Thomas Stridsman (August 2002 Active Trader Magazine)            |
//+------------------------------------------------------------------+
#property copyright "www.forex-station.com"
#property link      "www.forex-station.com"


#property indicator_separate_window
#property indicator_buffers    5
#property indicator_color1     PaleVioletRed
#property indicator_color2     PaleVioletRed
#property indicator_color3     DeepSkyBlue
#property indicator_color4     DeepSkyBlue
#property indicator_color5     Silver
#property indicator_width1     2
#property indicator_width3     2
#property indicator_width5     2
#property indicator_levelcolor DarkGray

//
//
//
//
//

extern string TimeFrame                 = "Current time frame";
extern int    PriceSmoothing            = 5;
extern int    PriceSmoothingMethod      = MODE_LWMA;
extern double Filter                    = 0.05;
extern bool   divergenceVisible         = true;
extern bool   divergenceOnValuesVisible = true;
extern bool   divergenceOnChartVisible  = true;
extern color  divergenceBullishColor    = Green;
extern color  divergenceBearishColor    = OrangeRed;
extern string divergenceUniqueID        = "vq diverge1";
extern bool   HistogramOnSlope          = false;
extern bool   Interpolate               = true;
extern bool   alertsOn                  = false;
extern bool   alertsOnSlope             = false;
extern bool   alertsOnCurrent           = true;
extern bool   alertsMessage             = true;
extern bool   alertsSound               = false;
extern bool   alertsEmail               = false;

//
//
//
//
//

double Upa[];
double Upb[];
double Dna[];
double Dnb[];
double sumVqi[];
double Vqi[];
double trend[];
double slope[];

int    timeFrame;
string indicatorFileName;
bool   returnBars;
bool   calculateValue;
string shortName;

//+------------------------------------------------------------------
//|                                                                  
//+------------------------------------------------------------------
// 
//
//
//
//

int init()
  {
   IndicatorBuffers(8);
      SetIndexBuffer(0,Dna);   SetIndexStyle(0,DRAW_HISTOGRAM);
      SetIndexBuffer(1,Dnb);   SetIndexStyle(1,DRAW_HISTOGRAM);
      SetIndexBuffer(2,Upa);   SetIndexStyle(2,DRAW_HISTOGRAM);
      SetIndexBuffer(3,Upb);   SetIndexStyle(3,DRAW_HISTOGRAM);
      SetIndexBuffer(4,sumVqi); 
      SetIndexBuffer(5,Vqi);   
      SetIndexBuffer(6,trend); 
      SetIndexBuffer(7,slope);
      SetLevelValue(0,0); 
      
      PriceSmoothing = MathMax(PriceSmoothing,1);
        
        //
        //
        //
        //
        //
      
        indicatorFileName = WindowExpertName();
        returnBars        = (TimeFrame=="returnBars");     if (returnBars)     return(0);
        calculateValue    = (TimeFrame=="calculateValue");
        if (calculateValue)
        {
           int s = StringFind(divergenceUniqueID,":",0);
                 shortName = divergenceUniqueID;
                 divergenceUniqueID = StringSubstr(divergenceUniqueID,0,s);
                 return(0);
        }            
        timeFrame = stringToTimeFrame(TimeFrame);
      
        //
        //
        //
        //
        //
      
   shortName = divergenceUniqueID+":  "+timeFrameToString(timeFrame)+"  Volatility quality zero line ";
   IndicatorShortName(shortName);
return(0);
}

//
//
//
//
//

int deinit() 
{

   int lookForLength = StringLen(divergenceUniqueID);
   for (int i=ObjectsTotal()-1; i>=0; i--) 
   {
      string objectName = ObjectName(i);
      if (StringSubstr(objectName,0,lookForLength) == divergenceUniqueID) ObjectDelete(objectName);
   }
   
return(0);
}

//------------------------------------------------------------------
//
//------------------------------------------------------------------
//
//
//
//
//

int start()
{
   int counted_bars=IndicatorCounted();
   int i,limit;

   if(counted_bars<0) return(-1);
   if(counted_bars>0) counted_bars--;
         limit = MathMin(Bars-counted_bars,Bars-1);
         if (returnBars) { Dna[0] = limit+1; return(0); }
         double pipMultiplier = MathPow(10,_Digits%2);
         
   //
   //
   //
   //
   //
   
   if (calculateValue || timeFrame == Period())
   {         
     for(i=limit; i>=0; i--)
     {
      if (i==(Bars-1))
      {
         Vqi[i]    = 0;
         sumVqi[i] = 0;
         continue;
      }
      
      //
      //
      //
      //
      //
      
         double cHigh  = iMA(NULL,0,PriceSmoothing,0,PriceSmoothingMethod,PRICE_HIGH ,i);
         double cLow   = iMA(NULL,0,PriceSmoothing,0,PriceSmoothingMethod,PRICE_LOW  ,i);
         double cOpen  = iMA(NULL,0,PriceSmoothing,0,PriceSmoothingMethod,PRICE_OPEN ,i);
         double cClose = iMA(NULL,0,PriceSmoothing,0,PriceSmoothingMethod,PRICE_CLOSE,i);
         double pClose = iMA(NULL,0,PriceSmoothing,0,PriceSmoothingMethod,PRICE_CLOSE,i+1);

         double trueRange = MathMax(cHigh,pClose)-MathMin(cLow,pClose);
         double     range = cHigh-cLow;
      
            if (range != 0 && trueRange!=0)
               double vqi = ((cClose-pClose)/trueRange + (cClose-cOpen)/range)*0.5;
            else      vqi = Vqi[i+1];

      //
      //
      //
      //
      //
         
         Vqi[i]    = MathAbs(vqi) * (cClose - pClose+cClose - cOpen) * 0.5;
         sumVqi[i] = Vqi[i];
         Dna[i]    = EMPTY_VALUE;
         Dnb[i]    = EMPTY_VALUE;
         Upa[i]    = EMPTY_VALUE;
         Upb[i]    = EMPTY_VALUE;
            if (Filter > 0) if (fabs(sumVqi[i]-sumVqi[i+1]) < Filter*Point*pipMultiplier) sumVqi[i] = sumVqi[i+1];
      
      //
      //
      //
      //
      //
      
      trend[i] = trend[i+1];
      slope[i] = slope[i+1];
          
          if (sumVqi[i] > 0)           trend[i] =  1;
          if (sumVqi[i] < 0)           trend[i] = -1;
          if (sumVqi[i] > sumVqi[i+1]) slope[i] =  1;
          if (sumVqi[i] < sumVqi[i+1]) slope[i] = -1;
               
          if (divergenceVisible)
          {
             CatchBullishDivergence(sumVqi,i);
             CatchBearishDivergence(sumVqi,i);
          }
                                     
          if (HistogramOnSlope)
          {
             if (trend[i]== 1 && slope[i] == 1) Upa[i] = sumVqi[i];
             if (trend[i]== 1 && slope[i] ==-1) Upb[i] = sumVqi[i];
             if (trend[i]==-1 && slope[i] ==-1) Dna[i] = sumVqi[i];
             if (trend[i]==-1 && slope[i] == 1) Dnb[i] = sumVqi[i];
          }
          else
          {                  
             if (trend[i]== 1) Upa[i] = sumVqi[i];
             if (trend[i]==-1) Dna[i] = sumVqi[i];
          }
               
      }
      manageAlerts();
      return(0);
      }   
      
     //
     //
     //
     //
     //
     
     limit = MathMax(limit,MathMin(Bars-1,iCustom(NULL,timeFrame,indicatorFileName,"returnBars",0,0)*timeFrame/Period()));
   
     for(i=limit; i>=0; i--)
     {
      int y = iBarShift(NULL,timeFrame,Time[i]);
         sumVqi[i] = iCustom(NULL,timeFrame,indicatorFileName,"calculateValue",PriceSmoothing,PriceSmoothingMethod,Filter,divergenceVisible,divergenceOnValuesVisible,divergenceOnChartVisible,divergenceBullishColor,divergenceBearishColor,shortName,Interpolate,4,y);
         trend[i]  = iCustom(NULL,timeFrame,indicatorFileName,"calculateValue",PriceSmoothing,PriceSmoothingMethod,Filter,divergenceVisible,divergenceOnValuesVisible,divergenceOnChartVisible,divergenceBullishColor,divergenceBearishColor,shortName,Interpolate,6,y);
         slope[i]  = iCustom(NULL,timeFrame,indicatorFileName,"calculateValue",PriceSmoothing,PriceSmoothingMethod,Filter,divergenceVisible,divergenceOnValuesVisible,divergenceOnChartVisible,divergenceBullishColor,divergenceBearishColor,shortName,Interpolate,7,y);
         Dna[i]    = EMPTY_VALUE;
         Dnb[i]    = EMPTY_VALUE;
         Upa[i]    = EMPTY_VALUE;
         Upb[i]    = EMPTY_VALUE;

         //
         //
         //
         //
         //
      
         if (!Interpolate || y==iBarShift(NULL,timeFrame,Time[i-1])) continue;

         //
         //
         //
         //
         //

         datetime time = iTime(NULL,timeFrame,y);
            for(int n = 1; i+n < Bars && Time[i+n] >= time; n++) continue;
            for(int k = 1; k < n; k++)
               sumVqi[i+k]  = sumVqi[i] + (sumVqi[i+n] - sumVqi[i]) * k/n;
   }
   for (i=limit;i>=0;i--)
   {
      if (HistogramOnSlope)
          {
             if (trend[i]== 1 && slope[i] == 1) Upa[i] = sumVqi[i];
             if (trend[i]== 1 && slope[i] ==-1) Upb[i] = sumVqi[i];
             if (trend[i]==-1 && slope[i] ==-1) Dna[i] = sumVqi[i];
             if (trend[i]==-1 && slope[i] == 1) Dnb[i] = sumVqi[i];
          }
          else
          {                  
             if (trend[i]== 1) Upa[i] = sumVqi[i];
             if (trend[i]==-1) Dna[i] = sumVqi[i];
          }
                               
    }
    manageAlerts();
return(0);
}

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
//
//
//
//
//

void manageAlerts()
{
   if (!calculateValue && alertsOn)
   {
      if (alertsOnCurrent)
           int whichBar = 0;
      else     whichBar = 1; whichBar = iBarShift(NULL,0,iTime(NULL,timeFrame,whichBar));
      
      //
      //
      //
      //
      //
      
      if (alertsOnSlope)
      {
         if (slope[whichBar] != slope[whichBar+1])
         {
            if (slope[whichBar] == 1) doAlert(whichBar,"slope changed to up");
            if (slope[whichBar] ==-1) doAlert(whichBar,"slope changed to down");
         }         
      }
      else
      {
         if (trend[whichBar] != trend[whichBar+1])
         {
            if (trend[whichBar] == 1) doAlert(whichBar,"crossed zero line up");
            if (trend[whichBar] ==-1) doAlert(whichBar,"crossed zero line down");
         }         
      }         
   }
}   

//
//
//
//
//

void doAlert(int forBar, string doWhat)
{
   static string   previousAlert="nothing";
   static datetime previousTime;
   string message;
   
      if (previousAlert != doWhat || previousTime != Time[forBar]) {
          previousAlert  = doWhat;
          previousTime   = Time[forBar];

          //
          //
          //
          //
          //

          message =  StringConcatenate(Symbol()," ",timeFrameToString(timeFrame)," at ",TimeToStr(TimeLocal(),TIME_SECONDS)," VQ oscillator ",doWhat);
             if (alertsMessage) Alert(message);
             if (alertsEmail)   SendMail(StringConcatenate(Symbol(),"VQ"),message);
             if (alertsSound)   PlaySound("alert2.wav");
      }
}

//+------------------------------------------------------------------
//|                                                                  
//+------------------------------------------------------------------
//
//
//
//
//

void CatchBullishDivergence(double& values[], int i)
{
   i++;
            ObjectDelete(divergenceUniqueID+"l"+DoubleToStr(Time[i],0));
            ObjectDelete(divergenceUniqueID+"l"+"os" + DoubleToStr(Time[i],0));            
   if (!IsIndicatorLow(values,i)) return;  

   //
   //
   //
   //
   //

   int currentLow = i;
   int lastLow    = GetIndicatorLastLow(values,i+1);
      if (values[currentLow] > values[lastLow] && Low[currentLow] < Low[lastLow])
      {
         if(divergenceOnChartVisible)  DrawPriceTrendLine("l",Time[currentLow],Time[lastLow],Low[currentLow],Low[lastLow],divergenceBullishColor,STYLE_SOLID);
         if(divergenceOnValuesVisible) DrawIndicatorTrendLine("l",Time[currentLow],Time[lastLow],values[currentLow],values[lastLow],divergenceBullishColor,STYLE_SOLID);
      }
      if (values[currentLow] < values[lastLow] && Low[currentLow] > Low[lastLow])
      {
         if(divergenceOnChartVisible)  DrawPriceTrendLine("l",Time[currentLow],Time[lastLow],Low[currentLow],Low[lastLow], divergenceBullishColor, STYLE_DOT);
         if(divergenceOnValuesVisible) DrawIndicatorTrendLine("l",Time[currentLow],Time[lastLow],values[currentLow],values[lastLow], divergenceBullishColor, STYLE_DOT);
      }
}

//
//
//
//
//

void CatchBearishDivergence(double& values[], int i)
{
   i++; 
            ObjectDelete(divergenceUniqueID+"h"+DoubleToStr(Time[i],0));
            ObjectDelete(divergenceUniqueID+"h"+"os" + DoubleToStr(Time[i],0));            
   if (IsIndicatorPeak(values,i) == false) return;

   //
   //
   //
   //
   //
      
   int currentPeak = i;
   int lastPeak = GetIndicatorLastPeak(values,i+1);
      if (values[currentPeak] < values[lastPeak] && High[currentPeak]>High[lastPeak])
      {
         if (divergenceOnChartVisible)  DrawPriceTrendLine("h",Time[currentPeak],Time[lastPeak],High[currentPeak],High[lastPeak],divergenceBearishColor,STYLE_SOLID);
         if (divergenceOnValuesVisible) DrawIndicatorTrendLine("h",Time[currentPeak],Time[lastPeak],values[currentPeak],values[lastPeak],divergenceBearishColor,STYLE_SOLID);
      }
      if(values[currentPeak] > values[lastPeak] && High[currentPeak] < High[lastPeak])
      {
         if (divergenceOnChartVisible)  DrawPriceTrendLine("h",Time[currentPeak],Time[lastPeak],High[currentPeak],High[lastPeak], divergenceBearishColor, STYLE_DOT);
         if (divergenceOnValuesVisible) DrawIndicatorTrendLine("h",Time[currentPeak],Time[lastPeak],values[currentPeak],values[lastPeak], divergenceBearishColor, STYLE_DOT);
      }
}

//
//
//
//
//

bool IsIndicatorPeak(double& values[], int i) { return(values[i] >= values[i+1] && values[i] > values[i+2] && values[i] > values[i-1]); }
bool IsIndicatorLow( double& values[], int i) { return(values[i] <= values[i+1] && values[i] < values[i+2] && values[i] < values[i-1]); }

int GetIndicatorLastPeak(double& values[], int shift)
{
   for(int i = shift+5; i<Bars; i++)
         if (values[i] >= values[i+1] && values[i] > values[i+2] && values[i] >= values[i-1] && values[i] > values[i-2]) return(i);
   return(-1);
}
int GetIndicatorLastLow(double& values[], int shift)
{
   for(int i = shift+5; i<Bars; i++)
         if (values[i] <= values[i+1] && values[i] < values[i+2] && values[i] <= values[i-1] && values[i] < values[i-2]) return(i);
   return(-1);
}

//+------------------------------------------------------------------
//|                                                                  
//+------------------------------------------------------------------
//
//
//
//
//

void DrawPriceTrendLine(string first,datetime t1, datetime t2, double p1, double p2, color lineColor, double style)
{
   string   label = divergenceUniqueID+first+"os"+DoubleToStr(t1,0);
   if (Interpolate) t2 += Period()*60-1;
    
   ObjectDelete(label);
      ObjectCreate(label, OBJ_TREND, 0, t1+Period()*60-1, p1, t2, p2, 0, 0);
         ObjectSet(label, OBJPROP_RAY, false);
         ObjectSet(label, OBJPROP_COLOR, lineColor);
         ObjectSet(label, OBJPROP_STYLE, style);
}

//
//
//
//
//

void DrawIndicatorTrendLine(string first,datetime t1, datetime t2, double p1, double p2, color lineColor, double style)
{
   int indicatorWindow = WindowFind(shortName);
   if (indicatorWindow < 0) return;
   if (Interpolate) t2 += Period()*60-1;
   
   string label = divergenceUniqueID+first+DoubleToStr(t1,0);
   ObjectDelete(label);
      ObjectCreate(label, OBJ_TREND, indicatorWindow, t1+Period()*60-1, p1, t2, p2, 0, 0);
         ObjectSet(label, OBJPROP_RAY, false);
         ObjectSet(label, OBJPROP_COLOR, lineColor);
         ObjectSet(label, OBJPROP_STYLE, style);
}

//+-------------------------------------------------------------------
//|                                                                  
//+-------------------------------------------------------------------
//
//

string sTfTable[] = { "M1","M5","M15","M30","H1","H4","D1","W1","MN" };
int    iTfTable[] = { 1,5,15,30,60,240,1440,10080,43200 };

//
//
//
//
//

int stringToTimeFrame(string tfs) {
   tfs = stringUpperCase(tfs);
   for (int i=ArraySize(iTfTable)-1; i>=0; i--)
         if (tfs==sTfTable[i] || tfs==""+iTfTable[i]) return(MathMax(iTfTable[i],Period()));
                                                      return(Period());
}

//
//
//
//
//

string timeFrameToString(int tf) {
   for (int i=ArraySize(iTfTable)-1; i>=0; i--) 
         if (tf==iTfTable[i]) return(sTfTable[i]);
                              return("");
}

//
//
//
//
//

string stringUpperCase(string str) {
   string   s = str;

   for (int length=StringLen(str)-1; length>=0; length--) {
      int tchar = StringGetChar(s, length);
         if((tchar > 96 && tchar < 123) || (tchar > 223 && tchar < 256))
                     s = StringSetChar(s, length, tchar - 32);
         else if(tchar > -33 && tchar < 0)
                     s = StringSetChar(s, length, tchar + 224);
   }
   return(s);
}      
Maybe something like this?
vq.mq4
(17.37 KiB) Downloaded 23 times

Re: Coding Help

#1037
mrtools wrote:
Mon Aug 12, 2019 2:53 am
^eagle^ wrote:
Sun Aug 11, 2019 10:30 pm
You guys have been a tremendous help. I'm so glad I found this community. Below is the code for a volitilty quality indicator I have. I wish to be able to turn off the Solid line divergences and the histogram as I only use the dotted line divergences on the chart. The code won't let me turn off the solid lines by using "none" as the color without turning off the dotted divergence lines. The histogram I can just minimize the window so that really isn't an issue. I'm just trying to keep my chart uncluttered as best I can.

Code: Select all

//+------------------------------------------------------------------+
//|                                           Volatility quality.mq4 |
//|                                                                  |
//|                                                                  |
//| Volatility quality index originaly developed by                  |
//| Thomas Stridsman (August 2002 Active Trader Magazine)            |
//+------------------------------------------------------------------+
#property copyright "www.forex-station.com"
#property link      "www.forex-station.com"


#property indicator_separate_window
#property indicator_buffers    5
#property indicator_color1     PaleVioletRed
#property indicator_color2     PaleVioletRed
#property indicator_color3     DeepSkyBlue
#property indicator_color4     DeepSkyBlue
#property indicator_color5     Silver
#property indicator_width1     2
#property indicator_width3     2
#property indicator_width5     2
#property indicator_levelcolor DarkGray

//
//
//
//
//

extern string TimeFrame                 = "Current time frame";
extern int    PriceSmoothing            = 5;
extern int    PriceSmoothingMethod      = MODE_LWMA;
extern double Filter                    = 0.05;
extern bool   divergenceVisible         = true;
extern bool   divergenceOnValuesVisible = true;
extern bool   divergenceOnChartVisible  = true;
extern color  divergenceBullishColor    = Green;
extern color  divergenceBearishColor    = OrangeRed;
extern string divergenceUniqueID        = "vq diverge1";
extern bool   HistogramOnSlope          = false;
extern bool   Interpolate               = true;
extern bool   alertsOn                  = false;
extern bool   alertsOnSlope             = false;
extern bool   alertsOnCurrent           = true;
extern bool   alertsMessage             = true;
extern bool   alertsSound               = false;
extern bool   alertsEmail               = false;

//
//
//
//
//

double Upa[];
double Upb[];
double Dna[];
double Dnb[];
double sumVqi[];
double Vqi[];
double trend[];
double slope[];

int    timeFrame;
string indicatorFileName;
bool   returnBars;
bool   calculateValue;
string shortName;

//+------------------------------------------------------------------
//|                                                                  
//+------------------------------------------------------------------
// 
//
//
//
//

int init()
  {
   IndicatorBuffers(8);
      SetIndexBuffer(0,Dna);   SetIndexStyle(0,DRAW_HISTOGRAM);
      SetIndexBuffer(1,Dnb);   SetIndexStyle(1,DRAW_HISTOGRAM);
      SetIndexBuffer(2,Upa);   SetIndexStyle(2,DRAW_HISTOGRAM);
      SetIndexBuffer(3,Upb);   SetIndexStyle(3,DRAW_HISTOGRAM);
      SetIndexBuffer(4,sumVqi); 
      SetIndexBuffer(5,Vqi);   
      SetIndexBuffer(6,trend); 
      SetIndexBuffer(7,slope);
      SetLevelValue(0,0); 
      
      PriceSmoothing = MathMax(PriceSmoothing,1);
        
        //
        //
        //
        //
        //
      
        indicatorFileName = WindowExpertName();
        returnBars        = (TimeFrame=="returnBars");     if (returnBars)     return(0);
        calculateValue    = (TimeFrame=="calculateValue");
        if (calculateValue)
        {
           int s = StringFind(divergenceUniqueID,":",0);
                 shortName = divergenceUniqueID;
                 divergenceUniqueID = StringSubstr(divergenceUniqueID,0,s);
                 return(0);
        }            
        timeFrame = stringToTimeFrame(TimeFrame);
      
        //
        //
        //
        //
        //
      
   shortName = divergenceUniqueID+":  "+timeFrameToString(timeFrame)+"  Volatility quality zero line ";
   IndicatorShortName(shortName);
return(0);
}

//
//
//
//
//

int deinit() 
{

   int lookForLength = StringLen(divergenceUniqueID);
   for (int i=ObjectsTotal()-1; i>=0; i--) 
   {
      string objectName = ObjectName(i);
      if (StringSubstr(objectName,0,lookForLength) == divergenceUniqueID) ObjectDelete(objectName);
   }
   
return(0);
}

//------------------------------------------------------------------
//
//------------------------------------------------------------------
//
//
//
//
//

int start()
{
   int counted_bars=IndicatorCounted();
   int i,limit;

   if(counted_bars<0) return(-1);
   if(counted_bars>0) counted_bars--;
         limit = MathMin(Bars-counted_bars,Bars-1);
         if (returnBars) { Dna[0] = limit+1; return(0); }
         double pipMultiplier = MathPow(10,_Digits%2);
         
   //
   //
   //
   //
   //
   
   if (calculateValue || timeFrame == Period())
   {         
     for(i=limit; i>=0; i--)
     {
      if (i==(Bars-1))
      {
         Vqi[i]    = 0;
         sumVqi[i] = 0;
         continue;
      }
      
      //
      //
      //
      //
      //
      
         double cHigh  = iMA(NULL,0,PriceSmoothing,0,PriceSmoothingMethod,PRICE_HIGH ,i);
         double cLow   = iMA(NULL,0,PriceSmoothing,0,PriceSmoothingMethod,PRICE_LOW  ,i);
         double cOpen  = iMA(NULL,0,PriceSmoothing,0,PriceSmoothingMethod,PRICE_OPEN ,i);
         double cClose = iMA(NULL,0,PriceSmoothing,0,PriceSmoothingMethod,PRICE_CLOSE,i);
         double pClose = iMA(NULL,0,PriceSmoothing,0,PriceSmoothingMethod,PRICE_CLOSE,i+1);

         double trueRange = MathMax(cHigh,pClose)-MathMin(cLow,pClose);
         double     range = cHigh-cLow;
      
            if (range != 0 && trueRange!=0)
               double vqi = ((cClose-pClose)/trueRange + (cClose-cOpen)/range)*0.5;
            else      vqi = Vqi[i+1];

      //
      //
      //
      //
      //
         
         Vqi[i]    = MathAbs(vqi) * (cClose - pClose+cClose - cOpen) * 0.5;
         sumVqi[i] = Vqi[i];
         Dna[i]    = EMPTY_VALUE;
         Dnb[i]    = EMPTY_VALUE;
         Upa[i]    = EMPTY_VALUE;
         Upb[i]    = EMPTY_VALUE;
            if (Filter > 0) if (fabs(sumVqi[i]-sumVqi[i+1]) < Filter*Point*pipMultiplier) sumVqi[i] = sumVqi[i+1];
      
      //
      //
      //
      //
      //
      
      trend[i] = trend[i+1];
      slope[i] = slope[i+1];
          
          if (sumVqi[i] > 0)           trend[i] =  1;
          if (sumVqi[i] < 0)           trend[i] = -1;
          if (sumVqi[i] > sumVqi[i+1]) slope[i] =  1;
          if (sumVqi[i] < sumVqi[i+1]) slope[i] = -1;
               
          if (divergenceVisible)
          {
             CatchBullishDivergence(sumVqi,i);
             CatchBearishDivergence(sumVqi,i);
          }
                                     
          if (HistogramOnSlope)
          {
             if (trend[i]== 1 && slope[i] == 1) Upa[i] = sumVqi[i];
             if (trend[i]== 1 && slope[i] ==-1) Upb[i] = sumVqi[i];
             if (trend[i]==-1 && slope[i] ==-1) Dna[i] = sumVqi[i];
             if (trend[i]==-1 && slope[i] == 1) Dnb[i] = sumVqi[i];
          }
          else
          {                  
             if (trend[i]== 1) Upa[i] = sumVqi[i];
             if (trend[i]==-1) Dna[i] = sumVqi[i];
          }
               
      }
      manageAlerts();
      return(0);
      }   
      
     //
     //
     //
     //
     //
     
     limit = MathMax(limit,MathMin(Bars-1,iCustom(NULL,timeFrame,indicatorFileName,"returnBars",0,0)*timeFrame/Period()));
   
     for(i=limit; i>=0; i--)
     {
      int y = iBarShift(NULL,timeFrame,Time[i]);
         sumVqi[i] = iCustom(NULL,timeFrame,indicatorFileName,"calculateValue",PriceSmoothing,PriceSmoothingMethod,Filter,divergenceVisible,divergenceOnValuesVisible,divergenceOnChartVisible,divergenceBullishColor,divergenceBearishColor,shortName,Interpolate,4,y);
         trend[i]  = iCustom(NULL,timeFrame,indicatorFileName,"calculateValue",PriceSmoothing,PriceSmoothingMethod,Filter,divergenceVisible,divergenceOnValuesVisible,divergenceOnChartVisible,divergenceBullishColor,divergenceBearishColor,shortName,Interpolate,6,y);
         slope[i]  = iCustom(NULL,timeFrame,indicatorFileName,"calculateValue",PriceSmoothing,PriceSmoothingMethod,Filter,divergenceVisible,divergenceOnValuesVisible,divergenceOnChartVisible,divergenceBullishColor,divergenceBearishColor,shortName,Interpolate,7,y);
         Dna[i]    = EMPTY_VALUE;
         Dnb[i]    = EMPTY_VALUE;
         Upa[i]    = EMPTY_VALUE;
         Upb[i]    = EMPTY_VALUE;

         //
         //
         //
         //
         //
      
         if (!Interpolate || y==iBarShift(NULL,timeFrame,Time[i-1])) continue;

         //
         //
         //
         //
         //

         datetime time = iTime(NULL,timeFrame,y);
            for(int n = 1; i+n < Bars && Time[i+n] >= time; n++) continue;
            for(int k = 1; k < n; k++)
               sumVqi[i+k]  = sumVqi[i] + (sumVqi[i+n] - sumVqi[i]) * k/n;
   }
   for (i=limit;i>=0;i--)
   {
      if (HistogramOnSlope)
          {
             if (trend[i]== 1 && slope[i] == 1) Upa[i] = sumVqi[i];
             if (trend[i]== 1 && slope[i] ==-1) Upb[i] = sumVqi[i];
             if (trend[i]==-1 && slope[i] ==-1) Dna[i] = sumVqi[i];
             if (trend[i]==-1 && slope[i] == 1) Dnb[i] = sumVqi[i];
          }
          else
          {                  
             if (trend[i]== 1) Upa[i] = sumVqi[i];
             if (trend[i]==-1) Dna[i] = sumVqi[i];
          }
                               
    }
    manageAlerts();
return(0);
}

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
//
//
//
//
//

void manageAlerts()
{
   if (!calculateValue && alertsOn)
   {
      if (alertsOnCurrent)
           int whichBar = 0;
      else     whichBar = 1; whichBar = iBarShift(NULL,0,iTime(NULL,timeFrame,whichBar));
      
      //
      //
      //
      //
      //
      
      if (alertsOnSlope)
      {
         if (slope[whichBar] != slope[whichBar+1])
         {
            if (slope[whichBar] == 1) doAlert(whichBar,"slope changed to up");
            if (slope[whichBar] ==-1) doAlert(whichBar,"slope changed to down");
         }         
      }
      else
      {
         if (trend[whichBar] != trend[whichBar+1])
         {
            if (trend[whichBar] == 1) doAlert(whichBar,"crossed zero line up");
            if (trend[whichBar] ==-1) doAlert(whichBar,"crossed zero line down");
         }         
      }         
   }
}   

//
//
//
//
//

void doAlert(int forBar, string doWhat)
{
   static string   previousAlert="nothing";
   static datetime previousTime;
   string message;
   
      if (previousAlert != doWhat || previousTime != Time[forBar]) {
          previousAlert  = doWhat;
          previousTime   = Time[forBar];

          //
          //
          //
          //
          //

          message =  StringConcatenate(Symbol()," ",timeFrameToString(timeFrame)," at ",TimeToStr(TimeLocal(),TIME_SECONDS)," VQ oscillator ",doWhat);
             if (alertsMessage) Alert(message);
             if (alertsEmail)   SendMail(StringConcatenate(Symbol(),"VQ"),message);
             if (alertsSound)   PlaySound("alert2.wav");
      }
}

//+------------------------------------------------------------------
//|                                                                  
//+------------------------------------------------------------------
//
//
//
//
//

void CatchBullishDivergence(double& values[], int i)
{
   i++;
            ObjectDelete(divergenceUniqueID+"l"+DoubleToStr(Time[i],0));
            ObjectDelete(divergenceUniqueID+"l"+"os" + DoubleToStr(Time[i],0));            
   if (!IsIndicatorLow(values,i)) return;  

   //
   //
   //
   //
   //

   int currentLow = i;
   int lastLow    = GetIndicatorLastLow(values,i+1);
      if (values[currentLow] > values[lastLow] && Low[currentLow] < Low[lastLow])
      {
         if(divergenceOnChartVisible)  DrawPriceTrendLine("l",Time[currentLow],Time[lastLow],Low[currentLow],Low[lastLow],divergenceBullishColor,STYLE_SOLID);
         if(divergenceOnValuesVisible) DrawIndicatorTrendLine("l",Time[currentLow],Time[lastLow],values[currentLow],values[lastLow],divergenceBullishColor,STYLE_SOLID);
      }
      if (values[currentLow] < values[lastLow] && Low[currentLow] > Low[lastLow])
      {
         if(divergenceOnChartVisible)  DrawPriceTrendLine("l",Time[currentLow],Time[lastLow],Low[currentLow],Low[lastLow], divergenceBullishColor, STYLE_DOT);
         if(divergenceOnValuesVisible) DrawIndicatorTrendLine("l",Time[currentLow],Time[lastLow],values[currentLow],values[lastLow], divergenceBullishColor, STYLE_DOT);
      }
}

//
//
//
//
//

void CatchBearishDivergence(double& values[], int i)
{
   i++; 
            ObjectDelete(divergenceUniqueID+"h"+DoubleToStr(Time[i],0));
            ObjectDelete(divergenceUniqueID+"h"+"os" + DoubleToStr(Time[i],0));            
   if (IsIndicatorPeak(values,i) == false) return;

   //
   //
   //
   //
   //
      
   int currentPeak = i;
   int lastPeak = GetIndicatorLastPeak(values,i+1);
      if (values[currentPeak] < values[lastPeak] && High[currentPeak]>High[lastPeak])
      {
         if (divergenceOnChartVisible)  DrawPriceTrendLine("h",Time[currentPeak],Time[lastPeak],High[currentPeak],High[lastPeak],divergenceBearishColor,STYLE_SOLID);
         if (divergenceOnValuesVisible) DrawIndicatorTrendLine("h",Time[currentPeak],Time[lastPeak],values[currentPeak],values[lastPeak],divergenceBearishColor,STYLE_SOLID);
      }
      if(values[currentPeak] > values[lastPeak] && High[currentPeak] < High[lastPeak])
      {
         if (divergenceOnChartVisible)  DrawPriceTrendLine("h",Time[currentPeak],Time[lastPeak],High[currentPeak],High[lastPeak], divergenceBearishColor, STYLE_DOT);
         if (divergenceOnValuesVisible) DrawIndicatorTrendLine("h",Time[currentPeak],Time[lastPeak],values[currentPeak],values[lastPeak], divergenceBearishColor, STYLE_DOT);
      }
}

//
//
//
//
//

bool IsIndicatorPeak(double& values[], int i) { return(values[i] >= values[i+1] && values[i] > values[i+2] && values[i] > values[i-1]); }
bool IsIndicatorLow( double& values[], int i) { return(values[i] <= values[i+1] && values[i] < values[i+2] && values[i] < values[i-1]); }

int GetIndicatorLastPeak(double& values[], int shift)
{
   for(int i = shift+5; i<Bars; i++)
         if (values[i] >= values[i+1] && values[i] > values[i+2] && values[i] >= values[i-1] && values[i] > values[i-2]) return(i);
   return(-1);
}
int GetIndicatorLastLow(double& values[], int shift)
{
   for(int i = shift+5; i<Bars; i++)
         if (values[i] <= values[i+1] && values[i] < values[i+2] && values[i] <= values[i-1] && values[i] < values[i-2]) return(i);
   return(-1);
}

//+------------------------------------------------------------------
//|                                                                  
//+------------------------------------------------------------------
//
//
//
//
//

void DrawPriceTrendLine(string first,datetime t1, datetime t2, double p1, double p2, color lineColor, double style)
{
   string   label = divergenceUniqueID+first+"os"+DoubleToStr(t1,0);
   if (Interpolate) t2 += Period()*60-1;
    
   ObjectDelete(label);
      ObjectCreate(label, OBJ_TREND, 0, t1+Period()*60-1, p1, t2, p2, 0, 0);
         ObjectSet(label, OBJPROP_RAY, false);
         ObjectSet(label, OBJPROP_COLOR, lineColor);
         ObjectSet(label, OBJPROP_STYLE, style);
}

//
//
//
//
//

void DrawIndicatorTrendLine(string first,datetime t1, datetime t2, double p1, double p2, color lineColor, double style)
{
   int indicatorWindow = WindowFind(shortName);
   if (indicatorWindow < 0) return;
   if (Interpolate) t2 += Period()*60-1;
   
   string label = divergenceUniqueID+first+DoubleToStr(t1,0);
   ObjectDelete(label);
      ObjectCreate(label, OBJ_TREND, indicatorWindow, t1+Period()*60-1, p1, t2, p2, 0, 0);
         ObjectSet(label, OBJPROP_RAY, false);
         ObjectSet(label, OBJPROP_COLOR, lineColor);
         ObjectSet(label, OBJPROP_STYLE, style);
}

//+-------------------------------------------------------------------
//|                                                                  
//+-------------------------------------------------------------------
//
//

string sTfTable[] = { "M1","M5","M15","M30","H1","H4","D1","W1","MN" };
int    iTfTable[] = { 1,5,15,30,60,240,1440,10080,43200 };

//
//
//
//
//

int stringToTimeFrame(string tfs) {
   tfs = stringUpperCase(tfs);
   for (int i=ArraySize(iTfTable)-1; i>=0; i--)
         if (tfs==sTfTable[i] || tfs==""+iTfTable[i]) return(MathMax(iTfTable[i],Period()));
                                                      return(Period());
}

//
//
//
//
//

string timeFrameToString(int tf) {
   for (int i=ArraySize(iTfTable)-1; i>=0; i--) 
         if (tf==iTfTable[i]) return(sTfTable[i]);
                              return("");
}

//
//
//
//
//

string stringUpperCase(string str) {
   string   s = str;

   for (int length=StringLen(str)-1; length>=0; length--) {
      int tchar = StringGetChar(s, length);
         if((tchar > 96 && tchar < 123) || (tchar > 223 && tchar < 256))
                     s = StringSetChar(s, length, tchar - 32);
         else if(tchar > -33 && tchar < 0)
                     s = StringSetChar(s, length, tchar + 224);
   }
   return(s);
}      
Maybe something like this?
I just figured it out. But yours is probably better! LOL.

Re: Coding Help

#1038
Hello MrTools

Would you please take a look at the attached ‘ArrZZx2Alerts Final Draft TT’ indicator’s code (kindly uploaded by Deez) and please confirm what the blue and orange moving averages actually are, and what their default settings are? Unfortunately I cannot read code.

I’ve tried to work it out by overlaying with different types of moving averages, but have not been successful in finding a match. The lines do recalculate/repaint slightly.

With thanks as always

Holmes27
ArrZZx2 Alerts Final Draft TT.mq4
(32.19 KiB) Downloaded 20 times
Indicator query screensave.png

Re: Coding Help

#1039
Holmes27 wrote:
Wed Aug 14, 2019 4:52 am
Hello MrTools

Would you please take a look at the attached ‘ArrZZx2Alerts Final Draft TT’ indicator’s code (kindly uploaded by Deez) and please confirm what the blue and orange moving averages actually are, and what their default settings are? Unfortunately I cannot read code.

I’ve tried to work it out by overlaying with different types of moving averages, but have not been successful in finding a match. The lines do recalculate/repaint slightly.

With thanks as always

Holmes27
Not completely sure but think it's the old snake (tma centered) moving averages.

Re: Coding Help

#1040
mrtools wrote:
Wed Aug 14, 2019 9:12 am
Holmes27 wrote:
Wed Aug 14, 2019 4:52 am
Hello MrTools

Would you please take a look at the attached ‘ArrZZx2Alerts Final Draft TT’ indicator’s code (kindly uploaded by Deez) and please confirm what the blue and orange moving averages actually are, and what their default settings are? Unfortunately I cannot read code.

I’ve tried to work it out by overlaying with different types of moving averages, but have not been successful in finding a match. The lines do recalculate/repaint slightly.

With thanks as always

Holmes27
Not completely sure but think it's the old snake (tma centered) moving averages.
Thanks for your help with that identification Mr Tools, it's very much appreciated,

Holmes27



Who is online

Users browsing this forum: CommonCrawl [Bot], triny56 and 1 guest