molanis.com/forum - Tools for MetaTrader : The place to ask about the best expert advisor builder, expert advisor downloads, and expert advisor programming

AFTER YOU REGISTER SEND US AN EMAIL TO ACTIVATE YOUR ACCOUNT - Before posting:-1- Please read the user guide -2- Try the examples -3- Search in the forum
It is currently Sat Apr 27, 2024 5:30 pm

All times are UTC - 5 hours




Post new topic Reply to topic  [ 11 posts ]  Go to page 1, 2  Next
Author Message
 Post subject: heiken ashi custom indicator
PostPosted: Tue Nov 29, 2011 8:28 am 
Offline

Joined: Tue Jun 14, 2011 5:45 pm
Posts: 17
Hi
Would anyone be able to help me to integrate the Heiken Ashi custom indicator into a strategy using the icustom TA block?
Im trying to build an EA that opens a buy position when the Heiken Ashi candle turns white, but am not sure which mode to use and how to write the conditions. Here is the code, any advice much appreciated:

//+------------------------------------------------------------------+
#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);
//---- indicator buffers mapping
SetIndexBuffer(0,ExtMapBuffer1);
SetIndexBuffer(1,ExtMapBuffer2);
SetIndexBuffer(2,ExtMapBuffer3);
SetIndexBuffer(3,ExtMapBuffer4);
//---- 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], MathMax(haOpen, haClose));
haLow=MathMin(Low[pos], MathMin(haOpen, haClose));
if (haOpen<haClose)
{
ExtMapBuffer1[pos]=haLow;
ExtMapBuffer2[pos]=haHigh;
}
else
{
ExtMapBuffer1[pos]=haHigh;
ExtMapBuffer2[pos]=haLow;
}
ExtMapBuffer3[pos]=haOpen;
ExtMapBuffer4[pos]=haClose;
pos--;
}
//----
return(0);
}
//+------------------------------------------------------------------+


Top
 Profile  
 
 Post subject: Re: heiken ashi custom indicator
PostPosted: Tue Nov 29, 2011 12:00 pm 
Offline

Joined: Tue Dec 22, 2009 12:22 am
Posts: 1761
This indicator is written to show visual signals. You cannot integrate it with an ea.
We had plans to integrate that indicator in the next release so I will fix the indicator - add code to create an extra signal, and then I will post the ex4 here and the startegy file .mol


Last edited by molanisfx on Tue Nov 29, 2011 2:30 pm, edited 1 time in total.

Top
 Profile  
 
 Post subject: Re: heiken ashi custom indicator
PostPosted: Tue Nov 29, 2011 12:01 pm 
Offline

Joined: Tue Dec 22, 2009 12:22 am
Posts: 1761
The import function in the strategy builder reads the indicator code and tells you the number of modes. It does not tell you which one to use. You need to understand the indicator signals and color to select the right signal.
Custom indicators available on the Internet are created by hundreds of coders, with different coding styles, so there is no set rule - the coder decides what he wants. Some coders want to see a visual signal so they write code that cannot be integrated with an expert advisor. Some coders are good and structured so you can rely on their code.

This indicator has 4 signals:
#property indicator_buffers 4
#property indicator_color1 Red
#property indicator_color2 White
#property indicator_color3 Red
#property indicator_color4 White



this part of the code #property indicator_buffers 4 tells you the number of signals

this code:
#property indicator_color1 Red
#property indicator_color2 White
#property indicator_color3 Red
#property indicator_color4 White
shows the colors fot the signals. Mind you, in the screen you only see a red and a white bar, internally the indicator uses 4 signals to show you those bars. This is a common practice in mt4, coders use several signals but users think there is only one signal because visually it looks like on signal (same applies to an indicator line that changes colors, it has two signals each one with its own color. When you put it togheter it looks like one signal)
For this indicator, one signal paints the bar, another signal paints the lines that simule high and low. Try changing the colors of the indicator and see how it behaves.

this part of the code tells you the modes:

SetIndexBuffer(0, ExtMapBuffer1);

So mode 0 is equivalent to ExtMapBuffer1 and will be painted using red color #property indicator_color1 Red
Most of the time, you can get the mode or signal number, using this formula color#-1, in this case color 1 - 1 = 0 so the mode for color red is zero (this works fine when you have different colors, in this case you have red two times)

There are some custom indicators that have a value of zero or empty used to hide the signals so visually users do not see them. To know the indicator values you can add some code to print out the values


Print("ExtMapBuffer1[0]=",ExtMapBuffer1[0]);
Print("ExtMapBuffer1[1]=",ExtMapBuffer1[1]);
Print("ExtMapBuffer2[0]=",ExtMapBuffer2[0]);
Print("ExtMapBuffer2[1]=",ExtMapBuffer2[1]);
Print("ExtMapBuffer3[0]=",ExtMapBuffer3[0]);
Print("ExtMapBuffer3[1]=",ExtMapBuffer3[1]);
Print("ExtMapBuffer4[0]=",ExtMapBuffer4[0]);
Print("ExtMapBuffer4[1]=",ExtMapBuffer4[1]);
[0] means current bar ; [1] means previous bar

After adding that code under the terminal window, in the tab expert you will see the signals values.

11:09:25 Heiken-Ashi-molanis EURUSD,M1: ExtMapBuffer1[0]=1.3325
11:09:25 Heiken-Ashi-molanis EURUSD,M1: ExtMapBuffer1[1]=1.3328
11:09:25 Heiken-Ashi-molanis EURUSD,M1: ExtMapBuffer2[0]=1.3322
11:09:25 Heiken-Ashi-molanis EURUSD,M1: ExtMapBuffer2[1]=1.3321
11:09:25 Heiken-Ashi-molanis EURUSD,M1: ExtMapBuffer3[0]=1.3325
11:09:25 Heiken-Ashi-molanis EURUSD,M1: ExtMapBuffer3[1]=1.3328
11:09:25 Heiken-Ashi-molanis EURUSD,M1: ExtMapBuffer4[0]=1.3322
11:09:25 Heiken-Ashi-molanis EURUSD,M1: ExtMapBuffer4[1]=1.3323

this indicator is really challenging to integrate, since you have all signals active. Visually you only see one color but the indicator plots the red and white bar at the same time, visually one overlaps and cover the other one so we only see one color.

Fix will come in the next post


Top
 Profile  
 
 Post subject: Re: heiken ashi custom indicator
PostPosted: Tue Nov 29, 2011 2:34 pm 
Offline

Joined: Tue Dec 22, 2009 12:22 am
Posts: 1761
To use this indicator we need to add a new signal. We will use 0 to indicate white and 1 to indicate red
0=white (BUY)
1=red (SELL)

We will add a mode or signal mode=4

So to get the signal you need to use mode 4, if the signals is zero the bar is white, if the signal is 1 the bar is red

The big issue with this indicator (besides that it was designed to give visual signals) is that it repaints the current bar. So the signal in the current bar could be white, then red, and then white, and then end red. Thus you may get conflicting / false signlas in the current bar (many indicators have this issue but people do not notice because they look at the chart after the fact or in bactesting - when you look at it live you see the issue)

If you trade bars, the trading condition is evaluated at the open. Let's imaging that at the open, the current value is red, so you sell. The next tick changes the current bar to white, the next tick to red, the final tick or close price changes the indicator to white. Then you look at the chart after the fact and you say the strategy builder is not working. I told it to buy when white and sell when red. The chart is white so it should have bought but it sold instead. This is the problem with repainting and trading the current bar. If you trade ticks you can have buy and sell within the same current bar.

At this point one can take three roads:
-Fix the indicator to avoid repainting
-Trade using the previous bar
-Trade using the current bar (knowing that it has issues)

Fixing the indicator may destroy the underlying logic. Trading the previous bar gives you delayed signals. Trading the current bar may give you false signals.
On top of that you need to define if you want to trade every tick or every bar. In short, you have many options to choose from. You need to take one and stick to it. No choice is perfect. (probably a good solution would be to mix all the options and confirm the signal with another indicator - or you could use the current and the previous bar for confirmation - again many possibilities)

These are the issues people may face in algorithmic trading. Looking at charts after the fact shows the perfect world - the real world is not as easy


Top
 Profile  
 
 Post subject: Re: heiken ashi custom indicator
PostPosted: Tue Nov 29, 2011 2:39 pm 
Offline

Joined: Tue Dec 22, 2009 12:22 am
Posts: 1761
I'll take the easy road. Will trade using the previous bar. You can try on your own which road feels better for you.

I attached the compiled indicator and the .mol strategy file and some backtesting.


Attachments:
File comment: Backtesting
HSMolanisBacktesting.PNG
HSMolanisBacktesting.PNG [ 23 KiB | Viewed 17334 times ]
File comment: Compiled - fixed indicator
Heiken-Ashi-molanis.ex4 [3.81 KiB]
Downloaded 1085 times
File comment: Strategy file - expert advisor
molanis-Heiken.mol [6.7 KiB]
Downloaded 1213 times
Top
 Profile  
 
 Post subject: Re: heiken ashi custom indicator
PostPosted: Tue Nov 29, 2011 5:12 pm 
Offline

Joined: Tue Jun 14, 2011 5:45 pm
Posts: 17
Thank-you Molanis, for such a detailed, informative and prompt response.


Top
 Profile  
 
 Post subject: Re: heiken ashi custom indicator
PostPosted: Wed Jun 06, 2012 9:53 am 
Offline

Joined: Sat May 26, 2012 5:57 am
Posts: 1
I try to add this expert in my EUR/USD chart but nothing happend. I also try to add the Heiken Ashi indicator but it's the same. Nothing happend. What i am doing wrong? :cry:

By the way, i use the MT4


Top
 Profile  
 
 Post subject: Re: heiken ashi custom indicator
PostPosted: Tue Jun 12, 2012 9:10 am 
Offline
Site Admin

Joined: Fri Oct 16, 2009 3:40 pm
Posts: 451
The .exe needs the custom indicator. Get the .mol and see how it was done, then get the indicator code from the initial post save it and compile it.


Top
 Profile  
 
 Post subject: Re: heiken ashi custom indicator
PostPosted: Tue Jun 12, 2012 3:21 pm 
Offline

Joined: Sun Jun 19, 2011 9:38 pm
Posts: 270
Attached is an mq4 for a 4 mode HA indi from some other EA. I may have posted this in some other thread already, which brings me to another topic:

There should be some kind of master list of indi's that work with SB and indi's that don't... or maybe just the indi's that do. :geek: Then, you (Molanis) wouldn't have to repeat yourselves so much and I wouldn't have to search so much.

While I'm at it, how about grouping similar topics together? You (Molanis) must grow tired of seeing new topics that read, "I have a problem." Not much help to me either. A code mod's section for new Pro users would be really nice (I'm shooting for the stars now).

Don't get me wrong. Support is stellar and the product is killer. Just longing for some forum organization.


Attachments:
Heiken Ashi2.mq4 [2.14 KiB]
Downloaded 1034 times

_________________
I'm not a programmer, but I play one on TV.
Top
 Profile  
 
 Post subject: Re: heiken ashi custom indicator
PostPosted: Fri Mar 20, 2015 3:28 pm 
Offline

Joined: Thu Jun 23, 2011 5:06 pm
Posts: 54
I have created an EA using the attached mol file. However I cant figure out why the alert indicator only shows the BUY signal on my chart. I have compare both BUY and SELL algorithms and the only difference is the buy has heiken ashi pointing to color zero and the sell to color 1. Have I misunderstood he above instructions? Note that I have imported the above HeikenAshi2 indictor.

Any help will be greatly appreciated.


Attachments:
799_H4_Z+3_test only_huMan.mol [19.14 KiB]
Downloaded 876 times
Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 11 posts ]  Go to page 1, 2  Next

All times are UTC - 5 hours


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron
Powered by Molanis © 2009