Can't read data with iCustom?

To add comments, please log in or register
J
21
J  

Hello, what I am trying to do is very similar to: http://www.forexfactory.com/showthread.php?t=217362

7bit explains how to do it here: http://www.forexfactory.com/showpost.php?p=3424034&postcount=19

The method he posted is what I've been trying to do, but for some reason it doesn't work. The arrow buffers (bullishDivergence and bearishDivergence) ALWAYS return the default value if I attempt to read them with iCustom with an EA. The custom indicator has arrows if applied to a chart, writes the expected values to a log, etc. but every test I attempt through an EA with iCustom returns the default value that it has if there is no arrow.

Ray
2653
Ray  

Show your code.

J
21
J  

Ridiculously Simple Expert:

int start() {
//----

double MacdCurr, MacdPrev;

MacdCurr=iCustom(NULL,PERIOD_M15,"MACD_Divergence_V1.1","*** MACD/OSMA Settings ***",6,60,12,"*** Indicator Settings ***",0.0005,0.0005,0,true,true,true,2,0);
MacdPrev=iCustom(NULL,PERIOD_M15,"MACD_Divergence_V1.1","*** MACD/OSMA Settings ***",6,60,12,"*** Indicator Settings ***",0.0005,0.0005,0,true,true,true,3,0);

int nCrossover = 0;
//if(MacdSignalCurr == 2147483647 && MacdSignalPrev == 2147483647) return(0);
if(MacdCurr != EMPTY_VALUE) nCrossover = 1;
if(MacdPrev != EMPTY_VALUE) nCrossover = -1;

if(nCrossover == 1) Alert("UP" + MacdCurr);
if(nCrossover == -1) Alert("DOWN" + MacdPrev);

//----
return(0);
}


Indicator:

//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init()
{
//---- indicators
SetIndexStyle(0, DRAW_ARROW);
SetIndexStyle(1, DRAW_ARROW);
SetIndexStyle(2, DRAW_LINE);
SetIndexStyle(3, DRAW_LINE);
//----
SetIndexBuffer(0, bullishDivergence);
SetIndexBuffer(1, bearishDivergence);
SetIndexBuffer(2, macd);
SetIndexBuffer(3, signal);
//----
SetIndexArrow(0, 233);
SetIndexArrow(1, 234);
//----
return(0);
}

//+------------------------------------------------------------------+
//| Custom indicator iteration function |
//+------------------------------------------------------------------+
int start()
{
int countedBars = IndicatorCounted();
if(countedBars < 0)
countedBars = 0;
CalculateIndicator(countedBars);
//----
return(0);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void CalculateIndicator(int countedBars)
{
for(int i = Bars - countedBars; i >= 0; i--)
{
CalculateMACD(i);
CatchBullishDivergence(i + 2);
CatchBearishDivergence(i + 2);
}
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void CatchBullishDivergence(int shift)
{
if(IsIndicatorTrough(shift) == false)
return;
int currentTrough = shift;
int lastTrough = GetIndicatorLastTrough(shift);
//----
if(macd[currentTrough] > macd[lastTrough] &&
Low[currentTrough] < Low[lastTrough])
{
bullishDivergence[currentTrough] = macd[currentTrough] -
arrowsDisplacement;
//----
if(drawPriceTrendLines == true)
DrawPriceTrendLine(Time[currentTrough], Time[lastTrough],
Low[currentTrough],
Low[lastTrough], Green, STYLE_SOLID);
//----
if(drawIndicatorTrendLines == true)
DrawIndicatorTrendLine(Time[currentTrough],
Time[lastTrough],
macd[currentTrough],
macd[lastTrough],
Green, STYLE_SOLID);
//----
if(displayAlert == true)
DisplayAlert("Classical bullish divergence on: ",
currentTrough);
}
//----
if(macd[currentTrough] < macd[lastTrough] &&
Low[currentTrough] > Low[lastTrough])
{
bullishDivergence[currentTrough] = macd[currentTrough] -
arrowsDisplacement;
//----
if(drawPriceTrendLines == true)
DrawPriceTrendLine(Time[currentTrough], Time[lastTrough],
Low[currentTrough],
Low[lastTrough], Green, STYLE_DOT);
//----
if(drawIndicatorTrendLines == true)
DrawIndicatorTrendLine(Time[currentTrough],
Time[lastTrough],
macd[currentTrough],
macd[lastTrough],
Green, STYLE_DOT);
//----
if(displayAlert == true)
DisplayAlert("Reverse bullish divergence on: ",
currentTrough);
}
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void CatchBearishDivergence(int shift)
{
if(IsIndicatorPeak(shift) == false)
return;
int currentPeak = shift;
int lastPeak = GetIndicatorLastPeak(shift);
//----
if(macd[currentPeak] < macd[lastPeak] &&
High[currentPeak] > High[lastPeak])
{
bearishDivergence[currentPeak] = macd[currentPeak] +
arrowsDisplacement;

if(drawPriceTrendLines == true)
DrawPriceTrendLine(Time[currentPeak], Time[lastPeak],
High[currentPeak],
High[lastPeak], Red, STYLE_SOLID);

if(drawIndicatorTrendLines == true)
DrawIndicatorTrendLine(Time[currentPeak], Time[lastPeak],
macd[currentPeak],
macd[lastPeak], Red, STYLE_SOLID);

if(displayAlert == true)
DisplayAlert("Classical bearish divergence on: ",
currentPeak);
}
if(macd[currentPeak] > macd[lastPeak] &&
High[currentPeak] < High[lastPeak])
{
bearishDivergence[currentPeak] = macd[currentPeak] +
arrowsDisplacement;
//----
if(drawPriceTrendLines == true)
DrawPriceTrendLine(Time[currentPeak], Time[lastPeak],
High[currentPeak],
High[lastPeak], Red, STYLE_DOT);
//----
if(drawIndicatorTrendLines == true)
DrawIndicatorTrendLine(Time[currentPeak], Time[lastPeak],
macd[currentPeak],
macd[lastPeak], Red, STYLE_DOT);
//----
if(displayAlert == true)
DisplayAlert("Reverse bearish divergence on: ",
currentPeak);
}
}
//+------------------------------------------------------------------+
Ray
2653
Ray  

Does the indicator put an arrow on bar 0?

J
21
J  
for(int i = Bars - countedBars; i >= 0; i--)
{
CalculateMACD(i);
CatchBullishDivergence(i + 2);
CatchBearishDivergence(i + 2);
}


Do you think this could be the problem then? It should place arrows at the current bar, but does go back 2 places in the buffer history.

Ray
2653
Ray  

Well, your EA is looking for arrows at bar 0.

I can't run your indicator code right now, and it is not so easy to read, so...

Write the Alert to look back, find the latest signal, tell how many bars back it was, and remember not to alert on that one again.

J
21
J  
Thanks. I'll try that.
bernd
989
bernd  
GameMusic3:

Ridiculously Simple Expert:

MacdCurr=iCustom(NULL,PERIOD_M15,"MACD_Divergence_V1.1","*** MACD/OSMA Settings ***",6,60,12,"*** Indicator Settings ***",0.0005,0.0005,0,true,true,true,2,0);
MacdPrev=iCustom(NULL,PERIOD_M15,"MACD_Divergence_V1.1","*** MACD/OSMA Settings ***",6,60,12,"*** Indicator Settings ***",0.0005,0.0005,0,true,true,true,3,0);

Shouldn't MACD current and previous be (..., 2,0) and (..., 2,1) ?

(...,2,x) is the MACD buffer, (...,2,0) is current and (...,2,1) would be previous.

In your code you are reading buffer 2, current value and buffer 3, current value instead. Even if it is intended to be this way (I didn't study all of the code) then your variable naming is a bit confusing.

whroeder1
17945
whroeder1  

  1. MacdCurr=iCustom(NULL,PERIOD_M15,"MACD_Divergence_V1.1","*** MACD/OSMA Settings ***",6,60,12,
    "*** Indicator Settings ***",0.0005,0.0005,0,true,true,true,2,0);
    MacdPrev=iCustom(NULL,PERIOD_M15,"MACD_Divergence_V1.1","*** MACD/OSMA Settings ***",6,60,12,
    "*** Indicator Settings ***",0.0005,0.0005,0,true,true,true,3,0);
    
    ...
    
    SetIndexBuffer(0, bullishDivergence);
    SetIndexBuffer(1, bearishDivergence);
    You're looking at buffers 2 and 3 but the indicator code shows 0 and 1.
To add comments, please log in or register