jshumaker, would you please edit your code using SRC button
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; }
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);}}
How many row you'd like to have ?
Your code below only write one row
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.
- votes: 14
- 2012.12.19
- Andrey Khatimlianskii
- www.mql5.com
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
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[];