You are trying to use features that are meant to be used exclusively from an indicator.
IndicatorCounted() has no meaning at all when called from an EA. It always returns -1 when called within EA code. Try using a constant instead (like in your second loop) or Bars (if you want the whole history).
Also, arrays used within EA should be sized, initialized and the whole work that normally metatrader does when it comes to indicators and buffers within indicators. EA does not have an equivalent of
SetIndexBuffer() that is used in indicators. You are having uninitialized arrays in your Ea and only metatrader prevented your EA from crashing the platform (it always returns 0 in those cases, since the element value you are trying to read does not exist, and in fact, those arrays do not exist (they are just declared, not allocated, initialized,...))
regards
mladen
Quote:
Originally Posted by Xaun
I have a simple Custom Indicator with no externs called #TestIndicator that fills 8 buffers with the number 1.0 but when I bring back the buffers in my EA the buffer results are always 0 . Can you help me with this.
#TestIndicator
Code:
#property indicator_chart_window
#property indicator_buffers 8
double buf4_up[];
double buf4_down[];
double buf3_up[];
double buf3_down[];
double buf2_up[];
double buf2_down[];
double buf1_up[];
double buf1_down[];
int UniqueNum = 0070;
string shortname = "";
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init()
{
shortname = "#TFX";
IndicatorBuffers(8);
IndicatorShortName(shortname);
//---- indicators
SetIndexBuffer(0,buf4_up);
SetIndexBuffer(1,buf4_down);
SetIndexBuffer(2,buf3_up);
SetIndexBuffer(3,buf3_down);
SetIndexBuffer(4,buf2_up);
SetIndexBuffer(5,buf2_down);
SetIndexBuffer(6,buf1_up);
SetIndexBuffer(7,buf1_down);
//----
return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
//----
//----
return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function |
//+------------------------------------------------------------------+
int start()
{
int limit;
int counted_bars = IndicatorCounted();
//---- the last calculated bar will be recalculated
if(counted_bars > 0)
counted_bars--;
limit = Bars - counted_bars - 1;
//---- the main cycle
for(int i = limit; i >= 0; i--)
{
//----
buf4_up[i]=1.0;
buf4_down[i]=1.0;
buf3_up[i]=1.0;
buf3_down[i]=1.0;
buf2_up[i]=1.0;
buf2_down[i]=1.0;
buf1_up[i]=1.0;
buf1_down[i]=1.0;
}
//----
//----
//----
return(0);
}
//+------------------
EA Code
Code:
double test4_up[];
double test4_down[];
double test3_up[];
double test3_down[];
double test2_up[];
double test2_down[];
double test1_up[];
double test1_down[];
int UniqueNum = 009;
//+------------------------------------------------------------------+
//| expert initialization function |
//+------------------------------------------------------------------+
int init()
{
//----
Print("Inside init");
//----
return(0);
}
//+------------------------------------------------------------------+
//| expert deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
//----
Print("Inside deinit");
//----
return(0);
}
//+------------------------------------------------------------------+
//| expert start function |
//+------------------------------------------------------------------+
int start()
{
//----
Print("Inside start");
int counted_bars=IndicatorCounted();
int y5m=0, y1h=0, y30m=0, y15m=0, yy=0;
int i=0;
int limit=Bars-counted_bars;
for(i=0;i<limit;i++)
{
test4_up[i] = iCustom(NULL, 0, "#TestIndicator",0,i);
test4_down[i] = iCustom(NULL, 0, "#TestIndicator",1,i);
}
for(i=0;i<30;i++)
{
Print("Test 4 UP ", test4_up[i]," Bar ",i );
Print("Test 4 DOWN ", test4_down[i]," Bar ",i );
//Print("This is a test");
}
//----
return(0);
}
//+---------
Results in Tester...
|