Problem reading data from customer indicator to EA

 

Hi:

I am using an indicator I have traded manually for 18 months with good success. I am attempting to automate the process. However, after reading about 25 posts relating to the subject of passing data from a custom indicator, I am having no success.

I am calling the indicator as follows:

double smi = iCustom(Symbol(),0,"Stochastic Momentum Index", 10,8,5,5,0,BarShift);
double signal = iCustom(Symbol(),0,"Stochastic Momentum Index",10,8,5,5,1,BarShift);

The attached indicator, stochastic momentum index, should pass a value between 100 and +100. The first two buffers contain the indicator and the signal. The remaining six are for intermediate values. However, all eight buffers display in the journal as an EMPTY_VALUE or 2147483647.

I have tried setting EMPTY_VALUE to 0, but this doesn't help because I need an actual value of the index to enter and exit my trades. I understand from one post that some indicators do this and generate a value only on a new tick. Therefore, I added code to start on the next bar, with no effect.

The indicator code:

//+------------------------------------------------------------------+
//|                                                          SMI.mq4 |
//|                      Copyright © 2005, MetaQuotes Software Corp. |
//|                                        http://www.metaquotes.net |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2005, MetaQuotes Software Corp."
#property link      "http://www.metaquotes.net"

#property indicator_separate_window
#property indicator_buffers 2
#property indicator_color1 Navy
#property indicator_color2 Red
#property indicator_level1 0
//---- input parameters
extern int       Period_Q=10;
extern int       Period_R=8;
extern int       Period_S=5;
extern int       Signal=5;
//---- buffers
double SMI_Buffer[];
double Signal_Buffer[];
double SM_Buffer[];
double EMA_SM[];
double EMA2_SM[];
double EMA_HQ[];
double EMA2_HQ[];
double HQ_Buffer[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- indicators
    
   IndicatorBuffers(8);
   SetIndexStyle(0,DRAW_LINE);
   SetIndexBuffer(0,SMI_Buffer);
   SetIndexEmptyValue(0,0);
   SetIndexStyle(1,DRAW_LINE);
   SetIndexBuffer(1,Signal_Buffer);
   SetIndexEmptyValue(1,0);
   SetIndexLabel(0,"SMI");
   SetIndexLabel(1,"Signal SMI");
   SetIndexBuffer(2,SM_Buffer);
   SetIndexBuffer(3,EMA_SM);
   SetIndexBuffer(4,EMA2_SM);
   SetIndexBuffer(5,EMA_HQ);
   SetIndexBuffer(6,EMA2_HQ);
   SetIndexBuffer(7,HQ_Buffer);

   IndicatorShortName("SMI("+Period_Q+","+Period_R+","+Period_S+","+Signal+")");
//----
   return(0);
   }
int deinit()
  {
//---- TODO: add your code here
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
   int    counted_bars=IndicatorCounted();
   int limit;
   int i;
//   double Median_Q[];
   if(counted_bars<0) return(-1);
   if(counted_bars>0) counted_bars--;
   limit=Bars-Period_Q-counted_bars;
   if(counted_bars>0) counted_bars--;

   for (i=limit;i>=0;i--)
      {
      
      HQ_Buffer[i]=High[Highest(NULL,0,MODE_HIGH,Period_Q,i)]-Low[Lowest(NULL,0,MODE_LOW,Period_Q,i)];
      SM_Buffer[i]=Close[i]-(High[Highest(NULL,0,MODE_HIGH,Period_Q,i)]+Low[Lowest(NULL,0,MODE_LOW,Period_Q,i)])/2;//Median_Q[i];
      }

   for (i=limit-Period_R;i>=0;i--)
      {
      EMA_SM[i]=iMAOnArray(SM_Buffer,0,Period_R,0,MODE_EMA,i);
      EMA_HQ[i]=iMAOnArray(HQ_Buffer,0,Period_R,0,MODE_EMA,i);
      }
   for (i=limit-Period_R-Period_S;i>=0;i--)
      {
      EMA2_SM[i]=iMAOnArray(EMA_SM,0,Period_S,0,MODE_EMA,i);
      EMA2_HQ[i]=iMAOnArray(EMA_HQ,0,Period_S,0,MODE_EMA,i);
      }
   for (i=limit-Period_R-Period_S-Signal;i>=0;i--)
      {
      SMI_Buffer[i]=100*EMA2_SM[i]/0.5/EMA2_HQ[i];
      }
   for (i=limit-Period_R-Period_S;i>=0;i--)
      {
      Signal_Buffer[i]=iMAOnArray(SMI_Buffer,0,Signal,0,MODE_EMA,i);
      }


   return(0);
  }
//+------------------------------------------------------------------+#property copyright "Metaquotes Corp"


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

I would appreciate any comments or advice on the problem.

Thanks,

W. B. Davis

 
wbdavis:

Hi:

I am using an indicator I have traded manually for 18 months with good success. I am attempting to automate the process. However, after reading about 25 posts relating to the subject of passing data from a custom indicator, I am having no success.

I am calling the indicator as follows:

The attached indicator, stochastic momentum index, should pass a value between 100 and +100. The first two buffers contain the indicator and the signal. The remaining six are for intermediate values. However, all eight buffers display in the journal as an EMPTY_VALUE or 2147483647.

I have tried setting EMPTY_VALUE to 0, but this doesn't help because I need an actual value of the index to enter and exit my trades. I understand from one post that some indicators do this and generate a value only on a new tick. Therefore, I added code to start on the next bar, with no effect.

The indicator code:

I would appreciate any comments or advice on the problem.

Thanks,

W. B. Davis

between 100 and +100 ??

It is not clear from your post what the problem is that you are having?

Are you trying to code an EA to place trades based on the indicator or have you made adjustments to the code of an existing indicator?

If you have used the indicator for 18 months and it works well, then there is probably nothing wrong with the indicator. If the lines are being drawn, then the buffers cannot be empty.

I would have thought that the problem is in the EA, yet you have posted the code of the indicator?

 
limit=Bars-Period_Q-counted_bars;

After the first run of the program, wouldn't this make limit a negative number?

 
GumRai:

After the first run of the program, wouldn't this make limit a negative number?


Sorry, the indicator gives values from 100 to -100--my typo. Your comment on the operation of the indicator points me to the EA as well. If your time permits, could you review it? I have probably made a simple error, but I can't seem to locate it! I have attached the EA as a file, since the comment was too long for the system to accept.


	          
Files:
testfea.mq4  10 kb
 
wbdavis:


Sorry, the indicator gives values from 100 to -100--my typo. Your comment on the operation of the indicator points me to the EA as well. If your time permits, could you review it? I have probably made a simple error, but I can't seem to locate it! I have attached the EA as a file, since the comment was too long for the system to accept.


I can't see how you have managed to use the indicator for 18 months as it will need to be constantly refreshed and then recent previous values would repaint with the refresh.

I have made some alterations in the indicator code which will allow it to work in the EA.

The problem is that the EA will just keep opening and closing trades, so that will need to be looked at

//This added to inputs
extern int       StartBar=100;

//This

   if(counted_bars<0) return(-1);
   if(counted_bars>0) counted_bars--;
   limit=Bars-Period_Q-counted_bars;
   if(counted_bars>0) counted_bars--;

//changed to
   if(counted_bars<0) return(-1);
   if(counted_bars>0) counted_bars--;
   limit=Bars-counted_bars;
   if(limit>StartBar)
      limit=limit-StartBar;
//so that it doesn't try to calculate MAs on data that does not exist

//Highest changed to iHighest
     HQ_Buffer[i]=High[iHighest(NULL,0,MODE_HIGH,Period_Q,i)]-Low[iLowest(NULL,0,MODE_LOW,Period_Q,i)];
     SM_Buffer[i]=Close[i]-(High[iHighest(NULL,0,MODE_HIGH,Period_Q,i)]+Low[iLowest(NULL,0,MODE_LOW,Period_Q,i)])/2;//Median_Q[i];
     
//Removed code that made i negative
   for (i=limit-Period_R;i>=0;i--)
      {
      EMA_SM[i]=iMAOnArray(SM_Buffer,0,Period_R,0,MODE_EMA,i);
      EMA_HQ[i]=iMAOnArray(HQ_Buffer,0,Period_R,0,MODE_EMA,i);
      }
   for (i=limit-Period_R-Period_S;i>=0;i--)
      {
      EMA2_SM[i]=iMAOnArray(EMA_SM,0,Period_S,0,MODE_EMA,i);
      EMA2_HQ[i]=iMAOnArray(EMA_HQ,0,Period_S,0,MODE_EMA,i);
      }
   for (i=limit-Period_R-Period_S-Signal;i>=0;i--)
      {
      SMI_Buffer[i]=100*EMA2_SM[i]/0.5/EMA2_HQ[i];
      }
   for (i=limit-Period_R-Period_S;i>=0;i--)
      {
      Signal_Buffer[i]=iMAOnArray(SMI_Buffer,0,Signal,0,MODE_EMA,i);
      }

//So now
   for (i=limit;i>=0;i--)
      {
      EMA_SM[i]=iMAOnArray(SM_Buffer,0,Period_R,0,MODE_EMA,i);
      EMA_HQ[i]=iMAOnArray(HQ_Buffer,0,Period_R,0,MODE_EMA,i);
      }
   for (i=limit;i>=0;i--)
      {
      EMA2_SM[i]=iMAOnArray(EMA_SM,0,Period_S,0,MODE_EMA,i);
      EMA2_HQ[i]=iMAOnArray(EMA_HQ,0,Period_S,0,MODE_EMA,i);
      }
   for (i=limit;i>=0;i--)
      {
      SMI_Buffer[i]=100*EMA2_SM[i]/0.5/EMA2_HQ[i];
      }
   for (i=limit;i>=0;i--)
      {
      Signal_Buffer[i]=iMAOnArray(SMI_Buffer,0,Signal,0,MODE_EMA,i);
      }
 
wbdavis:

Hi:

I am using an indicator I have traded manually for 18 months with good success. I am attempting to automate the process. However, after reading about 25 posts relating to the subject of passing data from a custom indicator, I am having no success.

I am calling the indicator as follows:

The attached indicator, stochastic momentum index, should pass a value between 100 and +100. The first two buffers contain the indicator and the signal. The remaining six are for intermediate values. However, all eight buffers display in the journal as an EMPTY_VALUE or 2147483647.

I have tried setting EMPTY_VALUE to 0, but this doesn't help because I need an actual value of the index to enter and exit my trades. I understand from one post that some indicators do this and generate a value only on a new tick. Therefore, I added code to start on the next bar, with no effect.

The indicator code:

I would appreciate any comments or advice on the problem.

Thanks,

W. B. Davis


I Using this customindicator "re-write by transport_david " very good for write EA:

//+------------------------------------------------------------------+
//|                                                  SMI_Correct.mq4 |
//|                                      re-write by transport_david |
//|                                                                  |
//+------------------------------------------------------------------+
#property copyright ""
#property link      ""
//----
#property indicator_separate_window
#property indicator_buffers 2
#property indicator_color1 Red
#property indicator_color2 White
//----
#property indicator_maximum 100
#property indicator_minimum -100
//----
#property indicator_level1 50
#property indicator_level2 0
#property indicator_level3 -50
//---- input parameters
// MetaStock uses H/L (13) , 1st EMA(25) , 2nd EMA(2) , no signal line
// fmlabs does not recommend any settings
extern int Period_Q= 13; // HH LL
extern int Period_R= 25; // 1st EMA
extern int Period_S=  2; // 2nd EMA
extern int Signal=5; // Signal EMA
extern int ShowBars=1000;
//---- buffers
double SMI_Buffer[];
double Signal_Buffer[];
double SM_Buffer[];
double EMA_SM[];
double EMA2_SM[];
double EMA_HQ[];
double EMA2_HQ[];
double HQ_Buffer[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- indicators
   IndicatorBuffers(8);
   SetIndexEmptyValue(0,0.0);
   SetIndexStyle(0,DRAW_LINE);
   SetIndexBuffer(0,Signal_Buffer);
   SetIndexLabel(0,"Signal SMI");
   //
   SetIndexEmptyValue(1,0.0);
   SetIndexStyle(1,DRAW_LINE);
   SetIndexBuffer(1,SMI_Buffer);
   SetIndexLabel(1,"SMI");
   //
   SetIndexEmptyValue(2,0.0);
   SetIndexBuffer(2,SM_Buffer);
   SetIndexStyle(2,DRAW_NONE);
   //
   SetIndexEmptyValue(3,0.0);
   SetIndexBuffer(3,EMA_SM);
   SetIndexStyle(3,DRAW_NONE);
   //
   SetIndexEmptyValue(4,0.0);
   SetIndexBuffer(4,EMA2_SM);
   SetIndexStyle(4,DRAW_NONE);
   //
   SetIndexEmptyValue(5,0.0);
   SetIndexBuffer(5,EMA_HQ);
   SetIndexStyle(5,DRAW_NONE);
   //
   SetIndexEmptyValue(6,0.0);
   SetIndexBuffer(6,EMA2_HQ);
   SetIndexStyle(6,DRAW_NONE);
   //
   SetIndexEmptyValue(7,0.0);
   SetIndexBuffer(7,HQ_Buffer);
   SetIndexStyle(7,DRAW_NONE);
   IndicatorShortName("SMI_Correct("+Period_Q+","+Period_R+","+Period_S+","+Signal+")");
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custor indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//---- TODO: add your code here
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
   int limit;
   int i;
//----
   limit=ShowBars;
   if (limit>=Bars - 1) limit=Bars - 1;
   for(i=limit;i>=0;i--)
     {                                                                                                                         // found at http://www.fmlabs.com/reference/default.htm?url=SMI.htm
      //                      highesthigh - lowestlow
      SM_Buffer[i]=Close[i]-((High[iHighest(Symbol(),0,MODE_HIGH,Period_Q,i)]+Low[iLowest(Symbol(),0,MODE_LOW,Period_Q,i)])/2); // SM_Buffer = Close - -------------------------
      //                                 2
      HQ_Buffer[i]=High[iHighest(Symbol(),0,MODE_HIGH,Period_Q,i)]-Low[iLowest(Symbol(),0,MODE_LOW,Period_Q,i)];                // HQ_Buffer = highesthigh - lowestlow
     }
   for(i=limit-Period_R;i>=0;i--)
     {
      EMA_SM[i]=iMAOnArray(SM_Buffer,0,Period_R,0,MODE_EMA,i);                                                                  // EMA_SM = EMA(SM_Buffer)
      EMA_HQ[i]=iMAOnArray(HQ_Buffer,0,Period_R,0,MODE_EMA,i);                                                                  // EMA_HQ = EMA(HQ_Buffer)
     }
   for(i=limit-Period_R-Period_S;i>=0;i--)
     {
      EMA2_SM[i]=iMAOnArray(EMA_SM,0,Period_S,0,MODE_EMA,i);                                                                    // EMA2_SM = EMA(EMA(SM_Buffer))
      EMA2_HQ[i]=iMAOnArray(EMA_HQ,0,Period_S,0,MODE_EMA,i);                                                                    // EMA2_HQ = EMA(EMA(HQ_Buffer))
     }
   for(i=limit-Period_R-Period_S-Signal;i>=0;i--)
     {                                                                                                                         //                  EMA2_SM
      SMI_Buffer[i]=100*(EMA2_SM[i]/(EMA2_HQ[i]/2));                                                                            // SMI = 100 x ( ------------- )
     }                                                                                                                         //                EMA2_HQ / 2
   for(i=limit-Period_R-Period_S;i>=0;i--)
     {
      Signal_Buffer[i]=iMAOnArray(SMI_Buffer,0,Signal,0,MODE_EMA,i);                                                            // Signal_Buffer = EMA(SMI_Buffer)
     }
//---- TODO: add your code here
//----
   return(0);
  }
//+------------------------------------------------------------------+

My back test EA:

 
BacktestThis
Files:
 

You have to re-think your EA,

The condition to open a buy, for example, is simply that smi > signal

The condition to close the buy is that the smi < 25.0

smi can be > signal and < 25 at the same time, so when this happens a buy is opened 1 tick, closed the next and this cycle repeated every other tick.

 
GumRai:

You have to re-think your EA,

The condition to open a buy, for example, is simply that smi > signal

The condition to close the buy is that the smi < 25.0

smi can be > signal and < 25 at the same time, so when this happens a buy is opened 1 tick, closed the next and this cycle repeated every other tick.


Thanks GumRai for taking the time to help me. I have been using the SMI very, very subjectively over the past 18 months. I only average about three good swing trades per week. So, I really will have to think how to put my approach into a proper EA, but you have given me a very good start.

Thanks again,

WBD

 
Adwin.co:
This


Thank you for the corrected SMI. I will run this along with the corrections given by GumRai and see what happens. Obviously, I have a lot of testing and rethinking to do. Thanks for your help.

WBD

 
wbdavis:


Thanks GumRai for taking the time to help me. I have been using the SMI very, very subjectively over the past 18 months. I only average about three good swing trades per week. So, I really will have to think how to put my approach into a proper EA, but you have given me a very good start.

Thanks again,

WBD


I was quite interested in this, so have made some modifications to your EA.

Now, it will only trade on the cross, not just one line being above/below the other. It should only make 1 trade at a time and if a signal is given in the opposite direction to an open trade before TP or SL is hit, the trade will be closed.

Take a look and see if it is useful

Files:
testfea2.mq4  9 kb
Reason: