Using Cusom Indicator with EA (Passing inputs and getting outputs)

 
I know, there has been a very informative thread of Shimodax and sub on this topic, but I guess, I'm at a much lower level of expertise than these folks! :)

Here is my problem. I have a daily pivot point calculating and plotting indicator (downloaded from the web). I want to use the Support/resistance values in another EA to decide my stop/loss as well profit targets. I followed the instructions for iCustom and the EA is running, but the values (support/resistance points imported from the custom indicator) do not seem right. I'm doing something wrong, but am not able to pinpoint it. I'm enclosing the code for both (the pivot point indicator as well as the EA) here.

Pivot Point Calculator Code
//+------------------------------------------------------------------+
//|                                              TimeZone Pivots.mq4 |
//|                                 Copyright © 2005, Denis Manigart |
//|                                     mailto:emaildomino@gmail.com |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2005, Denis Manigart"
#property link      "mailto:emaildomino@gmail.com"

#property indicator_chart_window
#property indicator_buffers 8

#property indicator_color1 DarkOrange
#property indicator_color2 Khaki
#property indicator_color3 Khaki
#property indicator_color4 Khaki
#property indicator_color5 Khaki
#property indicator_color6 Khaki
#property indicator_color7 Khaki

//#property indicator_color8 DarkViolet
//#property indicator_color9 Blue
//#property indicator_color10 Red
//#property indicator_color4 Gray

//---- input parameters
extern int CalledFromExpert=0; //To enable this indicator to be called from another EA
extern int TimeZoneOfData=2; // by default if time zone of data is at GMT +2
                             // This gives you the Pivot calculations for 0 GMT

//---- buffers
double PivotArray[];
double R1Array[];
double S1Array[];
double R2Array[];
double S2Array[];
double R3Array[];
double S3Array[];

double TodayOpenBuffer[];
double YesterdayCloseBuffer[];
double YesterdayHighBuffer[];
double YesterdayLowBuffer[];


//---- global variables
int indexbegin = 0;
double todayopen = 0;
double yesterdayclose = 0;

double barhigh = 0;
double dayhigh = 0;
double yesterdayhigh = 0;

double barlow = 0;
double daylow = 0;
double yesterdaylow = 0;

double Pivot;
double R1;
double S1;
double R2;
double S2;
double R3;
double S3;

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
{
//---- indicators
	
	// Pivot
SetIndexStyle(0,DRAW_LINE);
SetIndexBuffer(0,PivotArray);
IndicatorShortName("TDPivot");
SetIndexLabel(0,"Pivot");
SetIndexEmptyValue(0, 0.0);
// Resistance 1
SetIndexStyle(1,DRAW_LINE);
SetIndexBuffer(1,R1Array);
SetIndexLabel(1,"R1");
SetIndexEmptyValue(1, 0.0);
// Support 1
SetIndexStyle(2,DRAW_LINE);
SetIndexBuffer(2,S1Array);
SetIndexLabel(2,"S1");
SetIndexEmptyValue(2, 0.0);
// Resistance 2
SetIndexStyle(3,DRAW_LINE);
SetIndexBuffer(3,R2Array);
SetIndexLabel(3,"R2");
SetIndexEmptyValue(3, 0.0);
// Support 2
SetIndexStyle(4,DRAW_LINE);
SetIndexBuffer(4,S2Array);
SetIndexLabel(4,"S2");
SetIndexEmptyValue(4, 0.0);
// Resistance 3
SetIndexStyle(5,DRAW_LINE);
SetIndexBuffer(5,R3Array);
SetIndexLabel(5,"R3");
SetIndexEmptyValue(5, 0.0);
// Support 3
SetIndexStyle(6,DRAW_LINE);
SetIndexBuffer(6,S3Array);
SetIndexLabel(6,"S3");
SetIndexEmptyValue(6, 0.0);

// You may also display the previous day close - high - low

// Daily Close	
//	SetIndexStyle(5, DRAW_LINE);
//	SetIndexBuffer(5, YesterdayCloseBuffer);
//	SetIndexLabel(5, "Daily Close");
//	SetIndexEmptyValue(5, 0.0);

// Daily High	
//	SetIndexStyle(6, DRAW_LINE);
//	SetIndexBuffer(6, YesterdayHighBuffer);
//	SetIndexLabel(6, "YesterDay High");
//	SetIndexEmptyValue(6, 0.0);	
	
// Daily Low	
//	SetIndexStyle(7, DRAW_LINE);
//	SetIndexBuffer(7, YesterdayLowBuffer);
//	SetIndexLabel(7, "YesterDay Low");
//	SetIndexEmptyValue(7, 0.0);
	

//----
	indexbegin = Bars - 20;
	if (indexbegin < 0)
		indexbegin = 0;
return(0);
}

//+------------------------------------------------------------------+
//| Custor indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//---- 
   
//----
   return(0);
 }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
{
   int i;
  
	int counted_bars = IndicatorCounted();
	
	//---- check for possible errors
	if (counted_bars < 0) counted_bars = 0;
	//---- last counted bar will be recounted
	if (counted_bars > 0) counted_bars--;
	if (counted_bars > indexbegin) counted_bars = indexbegin;


		for (i = indexbegin-counted_bars; i >= 0; i--)
		{ 
		
		   if ( i == indexbegin-counted_bars) 
		       {
		        dayhigh = High[i];
		        daylow = Low[i];
		       }
		       
		       barlow = Low[i];	
				 barhigh = High[i];
				 
			if ( barhigh >= dayhigh) 
			    dayhigh = barhigh;	 		
			
			if ( barlow <= daylow) 
			    daylow = barlow;
		
//Cycle through all the bars and fill the indicator bars with the Pivot point values		   
		
			if ((TimeMinute(Time[i]) == 00) && (TimeHour(Time[i]) - TimeZoneOfData == 00))
				{todayopen = Open[i];
				 yesterdayclose = Close[i+1];
				 yesterdaylow = daylow;
				 daylow = Low [i]; // input new day value
				
				 yesterdayhigh = dayhigh;
				 dayhigh = High [i]; // input new day value
				
				Pivot = (yesterdayhigh + yesterdaylow + yesterdayclose)/3;
            R1 = (2*Pivot)-yesterdaylow;
            S1 = (2*Pivot)-yesterdayhigh;
            R2 = Pivot-S1+R1;
            S2 = Pivot-R1+S1;
				R3 = R1+(yesterdayhigh-yesterdaylow);
				S3 = S1-(yesterdayhigh-yesterdaylow);
				 }
				
				//These can be used for any calculations 
				TodayOpenBuffer[i] = todayopen;
				YesterdayCloseBuffer[i] = yesterdayclose;
				YesterdayHighBuffer[i] = yesterdayhigh;
				YesterdayLowBuffer[i] = yesterdaylow;
            PivotArray[i]=Pivot;
            R1Array[i]=R1;
            S1Array[i]=S1;
            R2Array[i]=R2;
            S2Array[i]=S2;
            R3Array[i]=R3;
            S3Array[i]=S3;
	}
	return(0);
}
//+------------------------------------------------------------------+


EA Code

//+------------------------------------------------------------------+
//|                                            Long MA Crossover.mq4 |
//|                                                    Forex Maratha |
//|                                        http://www.metaquotes.net |
//+------------------------------------------------------------------+
#property copyright "Forex Maratha"
#property link      "http://www.metaquotes.net"

//---- input parameters
extern int       TrailingStop=50;
extern int       RiskProfile=1;

//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {
//---- 
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
//---- 
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+

int start()
  {
   double MATun1, MATun2, MATun3;
   double R1, R2, R3, S1, S2, S3, Pivot;
   double TakeProfit=34, Lots=0.5;
   
   int cnt, ticket, total;
// initial data checks
// it is important to make sure that the expert works with a normal
// chart and the user did not make any mistakes setting external 
// variables (Lots, StopLoss, TakeProfit, 
// TrailingStop) in our case, we check TakeProfit
// on a chart of less than 100 bars
   if(Bars<100)
     {
      Print("bars less than 100");
      return(0);  
     }
   if(TakeProfit<10)
     {
      Print("TakeProfit less than 10");
      return(0);  // check TakeProfit
     }
// to simplify the coding and speed up access
// data are put into internal variables
   MATun1=iMA(NULL,0,144,0,MODE_EMA, PRICE_CLOSE,0);
   MATun2=iMA(NULL,0,169,0,MODE_EMA, PRICE_CLOSE,0);
   MATun3=iMA(NULL,0,12,0,MODE_EMA, PRICE_CLOSE,0);
   Pivot=iCustom(NULL,0,"TZP",1,2,0,0);
   R1=iCustom(NULL,0,"TZP",1,2,1,0);
   S1=iCustom(NULL,0,"TZP",1,2,2,0);
   R2=iCustom(NULL,0,"TZP",1,2,3,0);
   S2=iCustom(NULL,0,"TZP",1,2,4,0);
   R3=iCustom(NULL,0,"TZP",1,2,5,0);
   S3=iCustom(NULL,0,"TZP",1,2,6,0);
   Print("Support Levels ", S1, S2, S3);
   Print("Resistance Levels ", R1, R2, R3);
   total=OrdersTotal();
   if(total<1) 
     {
      // no opened orders identified
      if(AccountFreeMargin()<(1000*Lots))
        {
         Print("We have no money. Free Margin = ", AccountFreeMargin());
         return(0);  
        }
      // check for long position (BUY) possibility
      if ((MATun1>=MATun3)&&(MATun3>=MATun2)&&(Bid>=MATun1))
        {
         ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,3,S1,Ask+TakeProfit*Point,"macd sample",16384,0,Green);
         
         if(ticket>0)
           {
            if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) Print("BUY order opened : ",OrderOpenPrice());
           }
         else Print("Error opening BUY order : ",GetLastError()); 
         return(0); 
        }
      // check for short position (SELL) possibility
      if ((MATun1<=MATun3)&&(MATun3<=MATun2)&&(Ask<=MATun1))
        {
         ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,3,R1,Bid-TakeProfit*Point,"macd sample",16384,0,Red);
         Print("Resistance Levels ", R1, R2, R3);
         if(ticket>0)
           {
            if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) Print("SELL order opened : ",OrderOpenPrice());
           }
         else Print("Error opening SELL order : ",GetLastError()); 
         return(0); 
        }
      return(0);
     }
   // it is important to enter the market correctly, 
   // but it is more important to exit it correctly...   
   for(cnt=0;cnt<total;cnt++)
     {
      OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
      if(OrderType()<=OP_SELL &&   // check for opened position 
         OrderSymbol()==Symbol())  // check for symbol
        {
         if(OrderType()==OP_BUY)   // long position is opened
           {
            // should it be closed?
            if(Ask>=R1 || Ask >= OrderOpenPrice()+TakeProfit)
                {
                 OrderClose(OrderTicket(),OrderLots(),Bid,3,Violet); // close position
                 return(0); // exit
                }
            // check for trailing stop
            if(TrailingStop>0)  
              {                 
               if(Bid-OrderOpenPrice()>Point*TrailingStop)
                 {
                  if(OrderStopLoss()<Bid-Point*TrailingStop)
                    {
                     OrderModify(OrderTicket(),OrderOpenPrice(),Bid-Point*TrailingStop,OrderTakeProfit(),0,Green);
                     return(0);
                    }
                 }
              }
           }
         else // go to short position
           {
            // should it be closed?
            if(Bid<=S1 || Bid <= OrderOpenPrice()-TakeProfit)
              {
               OrderClose(OrderTicket(),OrderLots(),Ask,3,Violet); // close position
               return(0); // exit
              }
            // check for trailing stop
            if(TrailingStop>0)  
              {                 
               if((OrderOpenPrice()-Ask)>(Point*TrailingStop))
                 {
                  if((OrderStopLoss()>(Ask+Point*TrailingStop)) || (OrderStopLoss()==0))
                    {
                     OrderModify(OrderTicket(),OrderOpenPrice(),Ask+Point*TrailingStop,OrderTakeProfit(),0,Red);
                     return(0);
                    }
                 }
              }
           }
        }
     }
   return(0);
  }
// the end.



My guess is that I'm unnecessarily calculating pivot points for all days visible on the chart instead of just the previous day and current day. Also, how can EA communicate with the indicator to ensure that the pivot points calculated are relevant for current bar under calculation in EA?

Any help would be appreciated.

 
So, while I wait for the feedback, here is the interim solution...instead of custome indicator, I created a subroutine in the EA itself to provide the vital inputs...here is the subroutine...I know that it is little lame to have three loops, but was too lazy to put everything in one loop (really lazy!) :)
void PivotCalcs()
{
int handle,i,j,LoopCount;
double YH=0, YL=9999, YC;
// Calculate LoopCount such that previous 49 hours are covered
// to ensure that the pivot and sup/res calcs are carried out on
// prior day data.
LoopCount=2*(24*60)/Period()+1;
// Find high from the previous day
for (i=LoopCount;i>=1;i--)
   {if ((TimeMinute(Time[i]) == 00) && (TimeHour(Time[i]) - TimeZoneOfData == 00))
      {for (j=i;j>=(i-23);j--)
         {if (High[j]>YH) YH=High[j];
         }
      }
   }
// Find low from the previos day
for (i=LoopCount;i>=1;i--)
   {if ((TimeMinute(Time[i]) == 00) && (TimeHour(Time[i]) - TimeZoneOfData == 00))
      {for (j=i;j>=(i-23);j--)
         {if ((Low[j]<YL)&&(j>0)) 
            {YL=Low[j];
            }
         }
      }
   }
// Find close from the previous day   
for (i=LoopCount;i>=1;i--)
   {if ((TimeMinute(Time[i]) == 00) && (TimeHour(Time[i]) - TimeZoneOfData == 00))
      {for (j=i-1;j>=1;j--)
         {if ((TimeMinute(Time[j]) == 00) && (TimeHour(Time[j]) - TimeZoneOfData == 00))
            {YC=Close[j];
            }
         }
      }
   }
// Carry out Pivot and Support/Resistance calculations
Pivot = (YH+YL+YC)/3;
R1=2*Pivot-YL;
S1=2*Pivot-YH;
R2=Pivot-S1+R1;
S2=Pivot-R1+S1;
R3=R1+YH-YL;
S3=S1-YH+YL;
/*Write to the file
handle=FileOpen("Pivots", FILE_CSV|FILE_WRITE, ';');
  if(handle>0)
    {
     // write table columns headers
     FileWrite(handle, "Time;High;Low;Close;Pivot;R1;R2;R3;S1;S2;S3");
     // write data
     FileWrite(handle, Time[1], High[1], Low[1], Close[1],Pivot, R1, R2, R3, S1, S2, S3) ;
     FileClose(handle);
    }
Print("High=",YH," Low=",YL," Close=",YC," Pivot=",Pivot," R1=",R1," R2=",R2," R3=",R3," S1=",S1," S2=",S2," S3=",S3);*/
return(0);
}



I still can't find a way to append the file in MQL (I've the code to write once above - inside comments)....any clues?

Reason: