Examples: Transferring an Indicator Code into an Expert Advisor Code. General Structural Schemes of an Expert Advisor and Indicator Functions
Hi Nikolay,
Thank you for your article. Excellent work!!
I used RAVI but I still get the old problem of a difference in values when I use the EA to call the CI and the values of the CI on the graph itself.
For Example, the value of RAVI CI attached to the graph is -0.3127 on 2006.02.10 12:00.
The value of Get_RaviSeriesTest is -0.30348183 for the same date and the same timeframe.
This the output on Tick basesed backtesting:
07:52:25 2006.02.10 12:00 Get_RAVISeriesTest EURUSD,H4: -0.30348183 -0.30348183 0.00000000
07:52:25 2006.02.10 12:00 Get_RAVISeriesTest EURUSD,H4: -0.30348183 -0.30348183 0.00000000
07:52:25 2006.02.10 12:00 Get_RAVISeriesTest EURUSD,H4: -0.30348183 -0.30348183 0.00000000
07:52:25 2006.02.10 12:01 Get_RAVISeriesTest EURUSD,H4: -0.30348183 -0.30348183 0.00000000
07:52:25 2006.02.10 12:01 Get_RAVISeriesTest EURUSD,H4: -0.30348183 -0.30348183 0.00000000
07:52:25 2006.02.10 12:02 Get_RAVISeriesTest EURUSD,H4: -0.30348183 -0.30348183 0.00000000
07:52:25 2006.02.10 12:02 Get_RAVISeriesTest EURUSD,H4: -0.30348183 -0.30348183 0.00000000
07:52:25 2006.02.10 12:02 Get_RAVISeriesTest EURUSD,H4: -0.30348183 -0.30348183 0.00000000
Can you help please? I am unable to use any CI in my EA because of this problem.
Please find attached RAVI and Get_RAVISeriesTest.
//+------------------------------------------------------------------+ //| RAVI.mq4 | //| Copyright © 2005, MetaQuotes Software Corp. | //| https://www.metaquotes.net/ | //+------------------------------------------------------------------+ #property copyright "Copyright © 2005, MetaQuotes Software Corp." #property link "https://www.metaquotes.net/" //---- îòðèñîâêà èíäèêàòîðà â îòäåëüíîì îêíå #property indicator_separate_window //---- êîëè÷åñòâî èíäèêàòîðíûõ áóôôåðîâ #property indicator_buffers 1 //---- öâåò èíäèêàòîðà #property indicator_color1 Red //---- ÂÕÎÄÍÛÅ ÏÀÐÀÌÅÒÐÛ ÈÍÄÈÊÀÒÎÐÀ extern int Period1=25; extern int Period2=66; extern int MA_Metod=2; extern int PRICE=1; //---- èíäèêàòîðíûå áóôôåðû double ExtBuffer[]; //+------------------------------------------------------------------+ //| RAVI initialization function | //+------------------------------------------------------------------+ int init() { //---- ñòèëü îòðèñîâêè èíäèêàòîðà SetIndexStyle(0,DRAW_LINE); //---- èíäèêàòîðíûå áóôåðû SetIndexBuffer(0,ExtBuffer); //---- íàçâàíèå èíäèêàòîðà è ëåéáû äëÿ ñóáîêîí IndicatorShortName("RAVI (" + Period1+ ","+Period2+")"); SetIndexLabel(0,"RAVI"); //---- çàâåðøåíèå èíèöèàëèçàöèè return(0); } //+------------------------------------------------------------------+ //| RAVI iteration function | //+------------------------------------------------------------------+ int start() { int MinBars=MathMax(Period1, Period2); //---- ïðîâåðêà êîëè÷åñòâà áàðîâ íà äîñòàòî÷íîñòü äëÿ ðàñ÷¸òà if (Bars<MinBars) return(0); //----+ Ââåäåíèå ïåðåìåííûõ ñ ïëàâàþùåé òî÷êîé double MA1,MA2,result; //----+ Ââåäåíèå öåëûõ ïåðåìåííûõ è ïîëó÷åíèå óæå ïîñ÷èòàííûõ áàðîâ int MaxBar,bar,limit,counted_bars=IndicatorCounted(); //---- ïðîâåðêà íà âîçìîæíûå îøèáêè if (counted_bars<0) return(-1); //---- ïîñëåäíèé ïîäñ÷èòàííûé áàð äîëæåí áûòü ïåðåñ÷èòàí if (counted_bars>0) counted_bars--; //---- îïðåäåëåíèå íîìåðà ñàìîãî ñòàðîãî áàðà, //íà÷èíàÿ ñ êîòîðîãî áóäåò ïðîèçåä¸í ïåðåñ÷¸ò âñåõ áàðîâ MaxBar=Bars-1-MinBars; //---- îïðåäåëåíèå íîìåðà ñàìîãî ñòàðîãî áàðà, //íà÷èíàÿ ñ êîòîðîãî áóäåò ïðîèçåä¸í ïåðåñ÷¸ò íîâûõ áàðîâ limit=Bars-counted_bars-1; //---- èíèöèàëèçàöèÿ íóëÿ if (limit>MaxBar) { for (int ii=Bars-1; ii>=MaxBar; ii--)ExtBuffer[ii]=0.0; limit=MaxBar; } //---- îñíîâíîé öèêë for(bar=0; bar<=limit; bar++) { MA1=iMA(NULL,0,Period1,0,MA_Metod,PRICE,bar); MA2=iMA(NULL,0,Period2,0,MA_Metod,PRICE,bar); //---- result=((MA1-MA2)/MA2)*100; ExtBuffer[bar]=result; } //---- return(0); } //+------------------------------------------------------------------+
//+------------------------------------------------------------------+ //| Get_RAVISeriesTest.mq4 | //| Copyright © 2007, MetaQuotes Software Corp. | //| https://www.metaquotes.net/ | //+------------------------------------------------------------------+ #property copyright "Copyright © 2007, MetaQuotes Software Corp." #property link "https://www.metaquotes.net/" //---- ÂÕÎÄÍÛÅ ÏÀÐÀÌÅÒÐÛ ÝÊÑÏÅÐÒÀ extern bool NullBarRecount = true; //---- èíäèêàòîðíûå áóôôåðû //double RAVI_Buffer0[]; double RAVI_Buffer1[]; //double RAVI_Buffer2[]; //+------------------------------------------------------------------+ //| Get_RAVISeries() function | //+------------------------------------------------------------------+ #include <Get_RAVISeries.mqh> //+------------------------------------------------------------------+ //| Custom Expert initialization function | //+------------------------------------------------------------------+ int init() { //---- //---- çàâåðøåíèå èíèöèàëèçàöèè return(0); } //+------------------------------------------------------------------+ //| Custom Expert iteration function | //+------------------------------------------------------------------+ int start() { //---- double Ind_Velue,Resalt; //---- //if (!Get_RAVISeries(0,Symbol(),0,NullBarRecount,10,20,1,0,RAVI_Buffer0)) // return(0); if (!Get_RAVISeries(1,Symbol(),240,NullBarRecount,25,66,2,1,RAVI_Buffer1)) return(0); //if (!Get_RAVISeries(2,Symbol(),1440,NullBarRecount,30,70,3,3,RAVI_Buffer2)) // return(0); //---- ïîëó÷åíèå èíäèêàòîðíûõ çíà÷åíèé äëÿ òåñòà 0 //Ind_Velue=iCustom(NULL,0,"RAVI",10,20,1,0,0,2); //Resalt=RAVI_Buffer0[2]-Ind_Velue; //Print( " "+Ind_Velue+" "+RAVI_Buffer0[2]+" "+Resalt+"" ); //---- ïîëó÷åíèå èíäèêàòîðíûõ çíà÷åíèé äëÿ òåñòà 1 Ind_Velue=iCustom(NULL,240,"RAVI",25,66,2,1,0,2); Resalt=RAVI_Buffer1[2]-Ind_Velue; Print( " "+Ind_Velue+" "+RAVI_Buffer1[2]+" "+Resalt+"" ); //---- ïîëó÷åíèå èíäèêàòîðíûõ çíà÷åíèé äëÿ òåñòà 2 //Ind_Velue=iCustom(NULL,1440,"RAVI",30,70,3,3,0,2); //Resalt=RAVI_Buffer2[2]-Ind_Velue; //Print( " "+Ind_Velue+" "+RAVI_Buffer2[2]+" "+Resalt+"" ); //---- return(0); } //+------------------------------------------------------------------+
New article Transferring an Indicator Code into an Expert Advisor Code. General Structural Schemes of an Expert Advisor and Indicator Functions has been published:
Author: Nikolay Kositsin