technical indicator values not matching chart

 

I am trying to import the values of a few technical indicators into a SQL Server database, but the values that my script returns do not match exactly to the values I see on my charts.  Even though the open/close/high/low values do match perfectly.  Of course I have double checked that I am using the same parameters for each indicator which are the default parameters for each indicator.  Any help would be greatly appreciated 

double macd[],signal[],bbLow[],bbHigh[],dema5[],dema11[],dema21[],dema55[],dema200[],high[],low[],open[],close[];
int m_nLastBars,nBars;
bool m_bNewBar;
datetime time[];
//| initialization function--------------------------------------------------------------------
int OnInit()
{return(0);}
//| deinitialization function------------------------------------------------------------------
void OnDeinit(const int reason)
{}
//| tick function------------------------------------------------------------------------------
void OnTick()
{nBars=Bars("GBPUSD",PERIOD_D1);
if (m_nLastBars!=nBars)
   {m_nLastBars=nBars;
   m_bNewBar=true;
   int hdema5,hdema11,hdema21,hdema55,hdema200,hBB,hMacd;
   hdema5=iDEMA("GBPUSD",PERIOD_D1,5,0,PRICE_CLOSE);
   CopyBuffer(hdema5,0,0,2,dema5);
   hdema11=iDEMA("GBPUSD",PERIOD_D1,11,0,PRICE_CLOSE);
   CopyBuffer(hdema11,0,0,2,dema11);
   hdema21=iDEMA("GBPUSD",PERIOD_D1,21,0,PRICE_CLOSE);
   CopyBuffer(hdema21,0,0,2,dema21);
   hdema55=iDEMA("GBPUSD",PERIOD_D1,55,0,PRICE_CLOSE);
   CopyBuffer(hdema55,0,0,2,dema55);
   hdema200=iDEMA("GBPUSD",PERIOD_D1,200,0,PRICE_CLOSE);
   CopyBuffer(hdema200,0,0,2,dema200);
   hBB=iBands("GBPUSD",PERIOD_D1,20,0,2,PRICE_CLOSE);
   CopyBuffer(hBB,1,0,2,bbHigh);
   CopyBuffer(hBB,2,0,2,bbLow);
   hMacd=iMACD("GBPUSD",PERIOD_D1,12,26,9,PRICE_CLOSE);
   CopyBuffer(hMacd,0,0,2,macd);
   CopyBuffer(hMacd,1,0,2,signal);
   ArraySetAsSeries(low,true);
   CopyLow("GBPUSD",PERIOD_D1,0,2,low);
   ArraySetAsSeries(high,true);
   CopyHigh("GBPUSD",PERIOD_D1,0,2,high);
   ArraySetAsSeries(open,true);
   CopyOpen("GBPUSD",PERIOD_D1,0,2,open);
   ArraySetAsSeries(close,true);
   CopyClose("GBPUSD",PERIOD_D1,0,2,close);
   ArraySetAsSeries(time,true);
   CopyTime("GBPUSD",PERIOD_D1,0,2,time);
   int hfile;
   hfile=FileOpen("D1.csv",FILE_READ|FILE_WRITE|FILE_CSV|FILE_COMMON,'|');
   if (hfile!=INVALID_HANDLE)
      {FileSeek(hfile,0,SEEK_END);
      FileWrite(hfile,time[1],dema5[1],dema11[1],dema21[1],dema55[1],dema200[1],bbLow[1],bbHigh[1],macd[1]*100000,signal[1]*100000,open[1],close[1],high[1],low[1]);  //MACD & signal are multiplied by 100k just to avoid the output being in exponential format
      FileClose(hfile);}}
else
   {m_bNewBar=false;}}

 

jshumaker, would you please edit your code using SRC button

 

 
phi.nuts:

jshumaker, would you please edit your code using SRC button

double macd[],signal[],bbLow[],bbHigh[],dema5[],dema11[],dema21[],dema55[],dema200[],high[],low[],open[],close[];
int m_nLastBars,nBars;
bool m_bNewBar;
datetime time[];
//| initialization function--------------------------------------------------------------------
int OnInit()
{return(0);}
//| deinitialization function------------------------------------------------------------------
void OnDeinit(const int reason)
{}
//| tick function------------------------------------------------------------------------------
void OnTick()
{nBars=Bars("GBPUSD",PERIOD_D1);
if (m_nLastBars!=nBars)
   {m_nLastBars=nBars;
   m_bNewBar=true;
   int hdema5,hdema11,hdema21,hdema55,hdema200,hBB,hMacd;
   hdema5=iDEMA("GBPUSD",PERIOD_D1,5,0,PRICE_CLOSE);
   CopyBuffer(hdema5,0,0,2,dema5);
   hdema11=iDEMA("GBPUSD",PERIOD_D1,11,0,PRICE_CLOSE);
   CopyBuffer(hdema11,0,0,2,dema11);
   hdema21=iDEMA("GBPUSD",PERIOD_D1,21,0,PRICE_CLOSE);
   CopyBuffer(hdema21,0,0,2,dema21);
   hdema55=iDEMA("GBPUSD",PERIOD_D1,55,0,PRICE_CLOSE);
   CopyBuffer(hdema55,0,0,2,dema55);
   hdema200=iDEMA("GBPUSD",PERIOD_D1,200,0,PRICE_CLOSE);
   CopyBuffer(hdema200,0,0,2,dema200);
   hBB=iBands("GBPUSD",PERIOD_D1,20,0,2,PRICE_CLOSE);
   CopyBuffer(hBB,1,0,2,bbHigh);
   CopyBuffer(hBB,2,0,2,bbLow);
   hMacd=iMACD("GBPUSD",PERIOD_D1,12,26,9,PRICE_CLOSE);
   CopyBuffer(hMacd,0,0,2,macd);
   CopyBuffer(hMacd,1,0,2,signal);
   ArraySetAsSeries(low,true);
   CopyLow("GBPUSD",PERIOD_D1,0,2,low);
   ArraySetAsSeries(high,true);
   CopyHigh("GBPUSD",PERIOD_D1,0,2,high);
   ArraySetAsSeries(open,true);
   CopyOpen("GBPUSD",PERIOD_D1,0,2,open);
   ArraySetAsSeries(close,true);
   CopyClose("GBPUSD",PERIOD_D1,0,2,close);
   ArraySetAsSeries(time,true);
   CopyTime("GBPUSD",PERIOD_D1,0,2,time);
   int hfile;
   hfile=FileOpen("D1.csv",FILE_READ|FILE_WRITE|FILE_CSV|FILE_COMMON,'|');
   if (hfile!=INVALID_HANDLE)
      {FileSeek(hfile,0,SEEK_END);
      FileWrite(hfile,time[1],dema5[1],dema11[1],dema21[1],dema55[1],dema200[1],bbLow[1],bbHigh[1],macd[1]*100000,signal[1]*100000,open[1],close[1],high[1],low[1]);
      FileClose(hfile);}}
else
   {m_bNewBar=false;}}

 

You're making several mistake.

1. Always initiate the indicator handle in OnInit not OnTick, that way indicator handle will initiated for one time only, in your code, the handle is initiated on every tick. 

2. Always use ArraySetAsSeries  one time only in OnInit before using the array - not after using the array, and in your code, you should use ArraySetAsSeries for every buffer array of indicator you use

3. Check the return value of Copy... function, just in case there's failure, you won't copy wrong data

So here's the code, notice that in this code, I coded everything in OnTick not OnInit like I suggested before

int handle;
double my_ma[];
bool set_series_my_ma;

int OnInit()
  {
  

  return(0);
  }

void OnTick()
  {
  if (handle == 0 || handle == INVALID_HANDLE) handle = iMa ... //<<--- indicator function goes here, in this example we use iMA
  if (handle == INVALID_HANDLE) return;

  if (set_series_my_ma == false) set_series_my_ma = ArraySetAsSeries(my_ma, true);
  if (set_series_my_ma == false) return;

  if (CopyBuffer(handle, 0, 0, 100, my_ma) == 100)
     {
     Print ("my ma [0] ",my_ma[0]);
     }

  return;
  }

 

 

 
Looks like you are copying the values on a new bar because you only want values for closed bars, but you are then copying from position 0 which is the current bar just opened and the value of the indicator will jump about until the bar closes. I suggest you start at position 1 for the copy to ensure that you only get static indicator values for closed bars. otherwise you will be comparing apples and oranges...
 
I moved the code to the OnInit section but I only get 1 row written.  That being the first candle of the time period.  So I then tried moving that to the OnDeinit and that also gave me only 1 row but the last candle of the time period.  I would think that OnInit would not work because all of the data has not been loaded, correct?  At least trying OnDeinit would be after all data is loaded.  Or maybe I am misunderstanding how the base functions work
 
jshumaker:
I moved the code to the OnInit section but I only get 1 row written.  That being the first candle of the time period.  So I then tried moving that to the OnDeinit and that also gave me only 1 row but the last candle of the time period.  I would think that OnInit would not work because all of the data has not been loaded, correct?  At least trying OnDeinit would be after all data is loaded.  Or maybe I am misunderstanding how the base functions work

How many row you'd like to have ?

Your code below only write one row

int hfile;
   hfile=FileOpen("D1.csv",FILE_READ|FILE_WRITE|FILE_CSV|FILE_COMMON,'|');
   if (hfile!=INVALID_HANDLE)
      {FileSeek(hfile,0,SEEK_END);
      FileWrite(hfile,time[1],dema5[1],dema11[1],dema21[1],dema55[1],dema200[1],bbLow[1],bbHigh[1],macd[1]*100000,signal[1]*100000,open

                 [1],close[1],high[1],low[1]);
      FileClose(hfile);}}

 

 
phi.nuts:

How many row you'd like to have ?

Your code below only write one row

 

I changed that to a FOR loop.  I will copy the code I used when I get home from work.
 
Am I correct that moving that the OnInit section is not the proper function to place my code if I want to capture all of the historical data with the values of the technical indicators?
 
jshumaker:
Am I correct that moving that the OnInit section is not the proper function to place my code if I want to capture all of the historical data with the values of the technical indicators?
Is OK to put the code in anywhere as long as we get the correct initialization for the indicator handle. OnInit and OnDeinit are only executed one time, so if only one time, then using Script is better than EA. OnTick is executed in every tick, your file will be written in on every tick.
 
phi.nuts:
Is OK to put the code in anywhere as long as we get the correct initialization for the indicator handle. OnInit and OnDeinit are only executed one time, so if only one time, then using Script is better than EA. OnTick is executed in every tick, your file will be written in on every tick.
That helped!  I found a script called sHistoryExport, https://www.mql5.com/en/code/1252, and modified that.  I got all time frames (H1, H4 & D1) to export from 1999-1-1 to present, but when I tried to do the same for M15 my charts stop at 2009-1-1.  I know for MT4 I had to go to History Download Center to download historical data, but I do not see anything similar in MT5
sHistoryExport - handy script to export the historical data in the МetaТrader 4 format
sHistoryExport - handy script to export the historical data in the МetaТrader 4 format
  • votes: 14
  • 2012.12.19
  • Andrey Khatimlianskii
  • www.mql5.com
Export of all necessary instruments and time frames in one click with load and checking history