Change Histogram Type

shabaz
1066

Hi Guys

Hope you are well, 

Im trying to make an indicator that uses histograms, at the moment it checks for a signal and draws a signal of one bar on histogram, how can i make it so its a continues histogram, so if a buy signal shows and the indicator draws a continuas blue histogram/buffer until a bearish signal comes then it starts making red histogram/buffer untill a buy signal and so on.

Many Thanks in advance



#include <stdlib.mqh>
#include <stderror.mqh>

//--- indicator settings
#property indicator_separate_window

#property indicator_minimum 0.0
#property indicator_maximum 1.0

#property indicator_buffers 2

#property indicator_type1 DRAW_HISTOGRAM
#property indicator_style1 STYLE_SOLID
#property indicator_width1 5
#property indicator_color1 0xFFAA00
#property indicator_label1 "Buy"

#property indicator_type2 DRAW_HISTOGRAM
#property indicator_style2 STYLE_SOLID
#property indicator_width2 5
#property indicator_color2 0x0000FF
#property indicator_label2 "Sell"


//--- indicator buffers
double Buffer1[];
double Buffer2[];


datetime time_alert; //used when sending alert
bool Send_Email = true;
bool Audible_Alerts = true;
bool Push_Notifications = true;
double myPoint; //initialized in OnInit

void myAlert(string type, string message)
  {
   int handle;
   if(type == "print")
      Print(message);
   else if(type == "error")
     {
      Print(type+" | fractal_32_his_se_Alert_fix @ "+Symbol()+","+IntegerToString(Period())+" | "+message);
     }
   else if(type == "order")
     {
     }
   else if(type == "modify")
     {
     }
   else if(type == "indicator")
     {
      Print(type+" | fractal_32_his_se_Alert_fix @ "+Symbol()+","+IntegerToString(Period())+" | "+message);
      if(Audible_Alerts) Alert(type+" | fractal_32_his_se_Alert_fix @ "+Symbol()+","+IntegerToString(Period())+" | "+message);
      if(Send_Email) SendMail("fractal_32_his_se_Alert_fix", type+" | fractal_32_his_se_Alert_fix @ "+Symbol()+","+IntegerToString(Period())+" | "+message);
      handle = FileOpen("fractal_32_his_se_Alert_fix.txt", FILE_TXT|FILE_READ|FILE_WRITE|FILE_SHARE_READ|FILE_SHARE_WRITE, ';');
      if(handle != INVALID_HANDLE)
        {
         FileSeek(handle, 0, SEEK_END);
         FileWrite(handle, type+" | fractal_32_his_se_Alert_fix @ "+Symbol()+","+IntegerToString(Period())+" | "+message);
         FileClose(handle);
        }
      if(Push_Notifications) SendNotification(type+" | fractal_32_his_se_Alert_fix @ "+Symbol()+","+IntegerToString(Period())+" | "+message);
     }
  }

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {   
   IndicatorBuffers(2);
   SetIndexBuffer(0, Buffer1);
   SetIndexEmptyValue(0, EMPTY_VALUE);
   SetIndexArrow(0, 241);
   SetIndexBuffer(1, Buffer2);
   SetIndexEmptyValue(1, EMPTY_VALUE);
   SetIndexArrow(1, 242);
   //initialize myPoint
   myPoint = Point();
   if(Digits() == 5 || Digits() == 3)
     {
      myPoint *= 10;
     }
   return(INIT_SUCCEEDED);
  }

//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
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[])
  {
   int limit = rates_total - prev_calculated;
   //--- counting from 0 to rates_total
   ArraySetAsSeries(Buffer1, true);
   ArraySetAsSeries(Buffer2, true);
   //--- initial zero
   if(prev_calculated < 1)
     {
      ArrayInitialize(Buffer1, EMPTY_VALUE);
      ArrayInitialize(Buffer2, EMPTY_VALUE);
     }
   else
      limit++;
   
   //--- main loop
   for(int i = limit-1; i >= 0; i--)
     {
      if (i >= MathMin(500000-1, rates_total-1-50)) continue; //omit some old rates to prevent "Array out of range" or slow calculation   
      
      //Indicator Buffer 1
      if(iCustom(NULL, PERIOD_CURRENT, "non re", 2, false, 0, i) != 0 && iCustom(NULL, PERIOD_CURRENT, "non re", 2, false, 0, i) != EMPTY_VALUE //non re is not equal to fixed value
     
      )
        {
         Buffer1[i] = Low[1+i]; //Set indicator value at Candlestick Low
         if(i == 1 && Time[1] != time_alert) myAlert("indicator", "Buy"); //Alert on next bar open
         time_alert = Time[1];
        }
      else
        {
         Buffer1[i] = EMPTY_VALUE;
        }
      //Indicator Buffer 2
      if(iCustom(NULL, PERIOD_CURRENT, "non re", 2, false, 1, i) != 0 && iCustom(NULL, PERIOD_CURRENT, "non re", 2, false, 1, i) != EMPTY_VALUE //non re is not equal to fixed value
     
       )
        {
         Buffer2[i] = High[1+i]; //Set indicator value at Candlestick High
         if(i == 1 && Time[1] != time_alert) myAlert("indicator", "Sell"); //Alert on next bar open
         time_alert = Time[1];
        }
      else
        {
         Buffer2[i] = EMPTY_VALUE;
        }
     }
   return(rates_total);
  }

Dominik Christian Egert
2606
BTW, you should call iCustom only once, in OnInit. (As far as this is MQL5)

You need to do this after you have calculated both buffers. You check if buffer2 is equal to empty value, if so, you copy the old value into the new place. - carry forward.

Instead of

else
        {
         Buffer1[i] = EMPTY_VALUE;
        }
Set this

else
        {
         Buffer1[i] = Buffer1[i + 1];
        }
shabaz
1066
shabaz  
Dominik Christian Egert #:
BTW, you should call iCustom only once, in OnInit. (As far as this is MQL5)

You need to do this after you have calculated both buffers. You check if buffer2 is equal to empty value, if so, you copy the old value into the new place. - carry forward.

Instead of

Set this

Hi Dominik,

Thank you for your reply, I have tried that and the buy buffer is carried forward unless a sell buffer shows but goes back to the buyer immediately after the sell buffer.

What I'm trying achieve is to carry a buy forward until a sell buffer shows and then start carrying forward the sell buffer until the buy buffer shows. like a trend histogram for example.


Thanks again for the help, im still learning the ropes and appreciate your help.

Dominik Christian Egert
2606
Well, I would say, you need to maintain at least 3 states then. But it's probably easier to go with 5.

First state: no signal
Second/third state: buy/sell signal
Fourth/fifth state: carry forward.

Create yourself a table or a map in excel, and write down the conditions and dependencies.

Then you can code it.


shabaz
1066
shabaz  
Dominik Christian Egert #:
Well, I would say, you need to maintain at least 3 states then. But it's probably easier to go with 5.

First state: no signal
Second/third state: buy/sell signal
Fourth/fifth state: carry forward.

Create yourself a table or a map in excel, and write down the conditions and dependencies.

Then you can code it.


Thank you Dominik, 

I will give it a shot