HELP asked for heikin ashi with sound

 

I use the basic heikin ashi functuion.
I try to make a buy sound when the candles 1 and 2 are of a different color.
This seems to work fine now and then, but some times I just get sounds without the proper condition.
What am I not doing right?
Thanks for helping....
This is the code (just added the lines with the playSound command)
//+------------------------------------------------------------------+
//| Heiken Ashi.mq4 |
//| Copyright c 2004, MetaQuotes Software Corp. |
//| https://www.metaquotes.net/ |
//+------------------------------------------------------------------+
//| For Heiken Ashi we recommend next chart settings ( press F8 or |
//| select on menu 'Charts'->'Properties...'): |
//| - On 'Color' Tab select 'Black' for 'Line Graph' |
//| - On 'Common' Tab disable 'Chart on Foreground' checkbox and |
//| select 'Line Chart' radiobutton |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2004, MetaQuotes Software Corp."
#property link "https://www.metaquotes.net/"

#property indicator_chart_window
#property indicator_buffers 4
#property indicator_color1 Red
#property indicator_color2 White
#property indicator_color3 Red
#property indicator_color4 White
#property indicator_width1 1
#property indicator_width2 1
#property indicator_width3 3
#property indicator_width4 3

//----
extern color color1 = Red;
extern color color2 = White;
extern color color3 = Red;
extern color color4 = White;
//---- buffers
double ExtMapBuffer1[];
double ExtMapBuffer2[];
double ExtMapBuffer3[];
double ExtMapBuffer4[];
//----
int ExtCountedBars=0;
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//|------------------------------------------------------------------|
int init()
{
//---- indicators
SetIndexStyle(0,DRAW_HISTOGRAM, 0, 1, color1);
SetIndexBuffer(0, ExtMapBuffer1);
SetIndexStyle(1,DRAW_HISTOGRAM, 0, 1, color2);
SetIndexBuffer(1, ExtMapBuffer2);
SetIndexStyle(2,DRAW_HISTOGRAM, 0, 3, color3);
SetIndexBuffer(2, ExtMapBuffer3);
SetIndexStyle(3,DRAW_HISTOGRAM, 0, 3, color4);
SetIndexBuffer(3, ExtMapBuffer4);

//----
SetIndexDrawBegin(0,10);
SetIndexDrawBegin(1,10);
SetIndexDrawBegin(2,10);
SetIndexDrawBegin(3,10);

//---- initialization done */
return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
//---- TODO: add your code here

//----
return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function |
//+------------------------------------------------------------------+
int start()
{
double haOpen, haHigh, haLow, haClose;
if(Bars<=10) return(0);
ExtCountedBars=IndicatorCounted();
//---- check for possible errors
if (ExtCountedBars<0) return(-1);
//---- last counted bar will be recounted
if (ExtCountedBars>0) ExtCountedBars--;
int pos=Bars-ExtCountedBars-1;
while(pos>=0)
{
haOpen=(ExtMapBuffer3[pos+1]+ExtMapBuffer4[pos+1])/2;
haClose=(Open[pos]+High[pos]+Low[pos]+Close[pos])/4;
haHigh=MathMax(High[pos], haOpen);
haLow=MathMin(Low[pos], haOpen);
if (haClose>=haOpen)
{
ExtMapBuffer1[pos]=haLow;
ExtMapBuffer2[pos]=haHigh;
}
else
{
ExtMapBuffer1[pos]=haHigh;
ExtMapBuffer2[pos]=haLow;
}
ExtMapBuffer3[pos]=haOpen;
ExtMapBuffer4[pos]=haClose;
pos--;
}
if(ExtMapBuffer4[2] < ExtMapBuffer3[2] && ExtMapBuffer4[1] >= ExtMapBuffer3[1]) PlaySound("buy.wav");
if(ExtMapBuffer4[2] >= ExtMapBuffer3[2] && ExtMapBuffer4[1] < ExtMapBuffer3[1]) PlaySound("sell.wav");
}
//----
return(0);

//+------------------------------------------------------------------+

 
It looks like it will try to play a sound on every tick during the current bar when the specified condition of the prior bars is true.
 
phy wrote >>
It looks like it will try to play a sound on every tick during the current bar when the specified condition of the prior bars is true.

Thanks for your reply.

That is correct, but that is OK, so I get a number of warnings.

BUT, it also plays irregular, any of the 2 sounds when the specified condition is NOT true.

Like just now it plays the Buy sound, with bar 0 is white and the previous 2 bars are also white.

I also note, when it plays the correct buy sound after the correct buy condition, on the next bar it will play the sell sound while it keeps having the same colored bar and visa versa.

On the other hand some times it works correctlya number of times. Very strange???

Any other suggestion?

Thanks,

Sylvano

 
sylvano45 wrote >>

Thanks for your reply.

That is correct, but that is OK, so I get a number of warnings.

BUT, it also plays irregular, any of the 2 sounds when the specified condition is NOT true.

Like just now it plays the Buy sound, with bar 0 is white and the previous 2 bars are also white.

I also note, when it plays the correct buy sound after the correct buy condition, on the next bar it will play the sell sound while it keeps having the same colored bar and visa versa.

On the other hand some times it works correctlya number of times. Very strange???

Any other suggestion?

Thanks,

Sylvano

Just to let you know that I changed the indicator so that it is started only once with a new bar.

But I still have the same problem...

//+------------------------------------------------------------------+
//| Heiken Ashi.mq4 |
//| Copyright c 2004, MetaQuotes Software Corp. |
//| https://www.metaquotes.net/ |
//+------------------------------------------------------------------+
//| For Heiken Ashi we recommend next chart settings ( press F8 or |
//| select on menu 'Charts'->'Properties...'): |
//| - On 'Color' Tab select 'Black' for 'Line Graph' |
//| - On 'Common' Tab disable 'Chart on Foreground' checkbox and |
//| select 'Line Chart' radiobutton |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2004, MetaQuotes Software Corp."
#property link "https://www.metaquotes.net/"

#property indicator_chart_window
#property indicator_buffers 4
#property indicator_color1 Red
#property indicator_color2 White
#property indicator_color3 Red
#property indicator_color4 White
#property indicator_width1 1
#property indicator_width2 1
#property indicator_width3 3
#property indicator_width4 3

//----
extern color color1 = Red;
extern color color2 = White;
extern color color3 = Red;
extern color color4 = White;
extern int count_Alert=3;

//---- buffers
double ExtMapBuffer1[];
double ExtMapBuffer2[];
double ExtMapBuffer3[];
double ExtMapBuffer4[];
//----
int ExtCountedBars=0;
double ftime;
int count_a=0;
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//|------------------------------------------------------------------|
int init()
{
//---- indicators
SetIndexStyle(0,DRAW_HISTOGRAM, 0, 1, color1);
SetIndexBuffer(0, ExtMapBuffer1);
SetIndexStyle(1,DRAW_HISTOGRAM, 0, 1, color2);
SetIndexBuffer(1, ExtMapBuffer2);
SetIndexStyle(2,DRAW_HISTOGRAM, 0, 3, color3);
SetIndexBuffer(2, ExtMapBuffer3);
SetIndexStyle(3,DRAW_HISTOGRAM, 0, 3, color4);
SetIndexBuffer(3, ExtMapBuffer4);

//----
SetIndexDrawBegin(0,10);
SetIndexDrawBegin(1,10);
SetIndexDrawBegin(2,10);
SetIndexDrawBegin(3,10);

//---- initialization done */
return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
//---- TODO: add your code here

//----
return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function |
//+------------------------------------------------------------------+
int start()
{
double haOpen, haHigh, haLow, haClose;
if(Bars<=10) return(0);
ExtCountedBars=IndicatorCounted();
if(ftime!=Time[0]) count_a=0;
//---- check for possible errors
if (ExtCountedBars<0) return(-1);
//---- last counted bar will be recounted
if (ExtCountedBars>0) ExtCountedBars--;
int pos=Bars-ExtCountedBars-1;
while(pos>=0)
{
haOpen=(ExtMapBuffer3[pos+1]+ExtMapBuffer4[pos+1])/2;
haClose=(Open[pos]+High[pos]+Low[pos]+Close[pos])/4;
haHigh=MathMax(High[pos], haOpen);
haLow=MathMin(Low[pos], haOpen);
if (haClose>=haOpen)
{
ExtMapBuffer1[pos]=haLow;
ExtMapBuffer2[pos]=haHigh;
}
else
{
ExtMapBuffer1[pos]=haHigh;
ExtMapBuffer2[pos]=haLow;
}
ExtMapBuffer3[pos]=haOpen;
ExtMapBuffer4[pos]=haClose;
pos--;
}
if(ExtMapBuffer4[2] < ExtMapBuffer3[2] && ExtMapBuffer4[1] >= ExtMapBuffer3[1]) buy_alert(1);
if(ExtMapBuffer4[2] >= ExtMapBuffer3[2] && ExtMapBuffer4[1] < ExtMapBuffer3[1]) sell_alert(1);
}
//----
return(0);

//+------------------------------------------------------------------+

void buy_alert (int w)
{
ftime=Time[0];
if(count_a<count_Alert)
{count_a+=1;
//if(w==1) Alert(Symbol()," ",Period(),"Down trendline break");
PlaySound("buy.wav");
}
}
return;

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

void sell_alert (int w)
{
ftime=Time[0];
if(count_a<count_Alert)
{count_a+=1;
//if(w==1) Alert(Symbol()," ",Period(),"Up trendline break");
PlaySound("sell.wav");
}
}
return;

 
phy wrote >>
It looks like it will try to play a sound on every tick during the current bar when the specified condition of the prior bars is true.

Sorry,

I gave myself a good .....

I had a second chart open that I had forgotten about....

Problem solved.

Thanks

 

good

Reason: