LibreOCL/Lesson01/Part02/Prog01_02A/Prog01_02A.mq5

367 lines
30 KiB
MQL5
Raw Permalink Normal View History

2025-05-30 15:03:43 +02:00
<EFBFBD><EFBFBD>//============================================================================================= MQL5 ===
// LibreFEI v1.005 (MQL5)
// ;02=K9 <>4C;L >1@01>B:8 A>1KB89
// Librecoin(c)2014-2017
//============================================================================================= MQL5 ===
// REVIEWS
//------------------------------------------------------------------------------------------------------
// Fourier Extrapolator Adaptive - M:AB@0?>;OB>@ DC@L5 B2>@G5A:0O ?5@5@01>B:0.
//============================================================================================= MQL5 ===
// IDEAS
//------------------------------------------------------------------------------------------------------
// 12.12.2017 45O: @0AG5B=CN G0ABL 2K?>;=8BL A 8A?>;L7>20=85< OpenCL
//============================================================================================= MQL5 ===
// PROPERTY
//------------------------------------------------------------------------------------------------------
#define ver "1.005"
#property copyright "Librecoin(c)2014-2017, gpwr(c)2008-2010"
#property link "https://www.mql5.com/ru/users/kirillovyv"
#property strict
#property description "**************************************************"
#property description " Indicator LibreFEI v"+ver+" Lite (MQL5)"
#property description " Extrapolator by trigonometric (multitone) model"
#property description " From Russia with Love! :-)"
#property description " https://www.mql5.com/ru/users/kirillovyv"
#property description " Based from: gpwr(c)2008-2010, neoclassic(c)2009"
#property description "**************************************************"
#property version ver
const string vers = "LibreFEI v"+ver; //Version
//----- indicator property
#property indicator_chart_window
#property indicator_buffers 4
#property indicator_plots 2
//============================================================================================= MQL5 ===
// INTERFACE
//------------------------------------------------------------------------------------------------------
input int Npast = 650; //Past bars, to which trigonometric series is fitted
input int Nfut = 20; //Predicted future bars
input int Nharm = 9; //Min Narmonics in model
input double FreqTOL = 0.0000001; //Tolerance of frequency calculations
input int StartBar = 15; //! :0:>3> 10@0 =0G8=05<
input int MSTimerSet = 500; //#AB0=>2:0 B09<5@0 xxx <8;;8A5:C=4
input int nTickDelay = 5; //=B5@20; ?@>?CA:0 B8:>2 4;O B5AB8@>20=8O
input ulong IterLimit = 60; //Iteration limit 4;O Freq
input bool CommentOn = false; //K2>4 :><<5=B0@852 =0 M:@0=
input bool PrintOn = false; //5G0BL :><<5=B0@852 2 6C@=0;
//============================================================================================= MQL5 ===
// Global variable definition
//------------------------------------------------------------------------------------------------------
bool TimeInterrupt;
int nTickSumm;
//----- indicator buffers
double pv[]; //0 B>1@0605<K9 1CD5@ 8AB>@88 PAST
double fv[]; //1 B>1@0605<K9 1CD5@ ?@>3=>70 FUTURE
double pc[]; //2 0AG5B=K9 1CD5@ 8AB>@88 PAST
double fc[]; //3 0AG5B=K91CD5@ ?@>3=>70 FUTURE
//----- CD5@ @0AG5B=>3> ?0@0<5B@0
double xc[]; //CD5@ @0AG5B=>3> ?0@0<5B@0
//
//datetime xTime = D'2017.09.13 21:57:59';//@5<O =0G0;0 2K2>40 ?@>B>:>;>2
//datetime xTimeStop = D'2017.09.13 23:55:55';//@5<O >:>=G0=8O 2K2>40 ?@>B>:>;>2
//
//============================================================================================= MQL5 ===
// OnInit() - Custom indicator initialization function
//------------------------------------------------------------------------------------------------------
int OnInit()
{
//----- initialize global variables
TimeInterrupt=true;
nTickSumm=0;
//----- indicator parametrs
IndicatorSetInteger(INDICATOR_DIGITS,_Digits);
IndicatorSetString(INDICATOR_SHORTNAME,"LibreFEI");
//----- indicator buffers mapping
//@>H;>5 - 1CD5@ >B>1@065=8O PAST
ArraySetAsSeries(pv,true);
ArrayInitialize(pv,EMPTY_VALUE);
SetIndexBuffer(0,pv,INDICATOR_DATA);
PlotIndexSetString(0,PLOT_LABEL,"Past");
PlotIndexSetInteger(0,PLOT_DRAW_TYPE,DRAW_LINE);
PlotIndexSetInteger(0,PLOT_LINE_STYLE,STYLE_SOLID);
PlotIndexSetInteger(0,PLOT_LINE_WIDTH,2);
PlotIndexSetInteger(0,PLOT_LINE_COLOR,clrBlue);
//C4CI55 - 1CD5@ >B>1@065=8O FUTURE
ArraySetAsSeries(fv,true);
ArrayInitialize(fv,EMPTY_VALUE);
SetIndexBuffer(1,fv,INDICATOR_DATA);
PlotIndexSetString(1,PLOT_LABEL,"Future");
PlotIndexSetInteger(1,PLOT_DRAW_TYPE,DRAW_LINE);
PlotIndexSetInteger(1,PLOT_LINE_STYLE,STYLE_SOLID);
PlotIndexSetInteger(1,PLOT_LINE_WIDTH,2);
PlotIndexSetInteger(1,PLOT_LINE_COLOR,clrRed);
PlotIndexSetInteger(1,PLOT_SHIFT,Nfut); //future data vector i=0..Nfut; Nfut corresponds to bar=StartBar
//@>H;>5 - 1CD5@ @0AG5B0 PAST
ArraySetAsSeries(pc,true);
ArrayInitialize(pc,EMPTY_VALUE);
SetIndexBuffer(2,pc,INDICATOR_CALCULATIONS);
//C4CI55 - 1CD5@ @0AG5B0 FUTURE
ArraySetAsSeries(fc,true);//false
ArrayInitialize(fc,EMPTY_VALUE);
SetIndexBuffer(3,fc,INDICATOR_CALCULATIONS);
//CD5@ @0AG5B=>3> ?0@0<5B@0
ArrayResize(xc,Npast+1);
//----- 0AB@>9:8 B09<5@0 8 3;>10;L=KE AG5BG8:>2
{if(!MQLInfoInteger(MQL_TESTER))//5 B5AB8@>20=85
{
int err=-1;
int count=50;
{while((err!=0)&&(count>0))
{
ResetLastError();
EventSetMillisecondTimer(MSTimerSet); //#AB0=>2:0 B09<5@0 XXX <8;;8A5:C=4
err=GetLastError();
{if(err!=0)
{
Sleep(50);
}}//if(err!=0)
count--;
}}//while((err!=0)&&(count>0))
}}//if(!MQLInfoInteger(MQL_TESTER))
//-----
return(INIT_SUCCEEDED);
}//OnInit()
//
//============================================================================================= MQL5 ===
// OnDeinit() - Custom indicator iteration function |
//------------------------------------------------------------------------------------------------------
void OnDeinit(const int reason){
Comment("");
return;
}//OnDeinit()
//
//============================================================================================= MQL5 ===
// OnTimer()
//------------------------------------------------------------------------------------------------------
void OnTimer(){
TimeInterrupt=true;
nTickSumm=0;
}//OnTimer()
//
//============================================================================================= MQL5 ===
// OnCalculate() - Custom indicator iteration function
//------------------------------------------------------------------------------------------------------
int OnCalculate(const int rates_total,
const int prev_calculated,
const datetime& time[],
const double& open[],
const double& high[],
const double& low[],
const double& close[],
const long& tick_volume[],
const long& volume[],
const int& spread[])
{
//----- Check for insufficient data
{if(rates_total<Npast+StartBar+1)
{
{if(PrintOn)
{
Print("Error: not enough bars in history!"
+" Total="+IntegerToString(rates_total)
+" needed="+IntegerToString(Npast+StartBar+1));
}}//if(PrintOn)
return(0);
}}//if(rates_total<Npast)
//----- @>25@:0 =5>1E>48<>AB8 ?5@5AG5B0 ?> B8:0<
{if(MQLInfoInteger(MQL_TESTER))
{
nTickSumm++;
{if(MathMod(nTickSumm,nTickDelay)<1.0)
{
nTickSumm=0;
TimeInterrupt=true;
}}//if(MathMod(nTickSumm,7)<1.0)
}}//if(MQLInfoInteger(MQL_TESTER))
//----- @>25@:0 =5>1E>48<>AB8 ?5@5AG5B0 ?> B09<5@C
{if(rates_total<=prev_calculated)//@07<5@ 2E>4=KE B09<A5@89 <5=LH5 8;8 @025=, G5< @0=55 >1@01>B0=> (>1KG=> @07<5@ 2E>4=KE =0 ?@54H5AB2CNI5< B8:5)
{//7<5=5=89 =5B - ?@>25@O5< B09<5@
{if(!TimeInterrupt)//5A;8 =5B ?@5@K20=8O >B msec B09<5@0
{
return(rates_total); //!20;8205<, 81> =5B 87<5=5=89 8 ?@5@K20=8O B09<5@0
}else{//5ABL ?@5@K20=85 B09<5@0 - A1@0AK205< 8 2K?>;=O5< @0AG5B
TimeInterrupt=false;
}}//if(!TimeInterrupt)
}}//if(rates_total<=prev_calculated)
//----- ?@545;5=85 A5@89=>AB8 <0AA82>2 40==KE
ArraySetAsSeries(open,true);
ArraySetAsSeries(close,true);
ArraySetAsSeries(high,true);
ArraySetAsSeries(low,true);
ArraySetAsSeries(spread,true);
//----- 0?>;=5=85 8AE>4=>3> <0AA820 4;O @0AG5B0
{for(int i=0;i<Npast+1;i++)
{
xc[i]=close[i+StartBar];
}}//for(int i=0;Npast+1;i++)
Calc_Fourier(xc,pc,fc,Npast,Nharm);
//----- 5@5:;04:0 <0AA82>2 4;O >B>1@065=8O
//0?>;=5=85 8AE>4=>3> <0AA820 4;O @0AG5B0 ?@>3=>70
{for(int i=0;i<Npast;i++)
{
xc[i]=close[i];
}}//for(int i=0;i<Npast;i++)
//----- 0AG5B 8 2K2>4 8=48:0B>@=KE 1CD5@>2 8 A?@02>G=>9 8=D>@<0F88
{if(CommentOn)
{
Comment( "LibreFEI"+" "+
string(TimeCurrent())+" "+
"ST="+IntegerToString(StartBar)+" "+
"NF="+IntegerToString(Nfut)+" "+
"NP="+IntegerToString(Npast)+" "+
"NH="+IntegerToString(Nharm)+" "
);
}}//if(CommentOn)
{if(PrintOn)
{
Print( string(TimeCurrent())+" "+
"NP="+IntegerToString(Npast)+" "+
"NH="+IntegerToString(Nharm)+" "
);
}}//if(PrintOn)
PlotIndexSetInteger(0,PLOT_DRAW_TYPE,DRAW_NONE); //pv
PlotIndexSetInteger(1,PLOT_DRAW_TYPE,DRAW_NONE); //fv
PlotIndexSetInteger(2,PLOT_DRAW_TYPE,DRAW_NONE);
PlotIndexSetInteger(3,PLOT_DRAW_TYPE,DRAW_NONE);
ArrayInitialize(pv,EMPTY_VALUE);
ArrayInitialize(fv,EMPTY_VALUE);
ArrayCopy(pv,pc,0,0,Npast+StartBar+1);
{for(int i=0;i<=Nfut;i++)
{
fv[Nfut-i]=fc[i];
}}//for(int i=0;i<=Nfut;i++)
PlotIndexSetInteger(0,PLOT_DRAW_TYPE,DRAW_LINE); //pv
PlotIndexSetInteger(1,PLOT_DRAW_TYPE,DRAW_LINE); //fv
PlotIndexSetInteger(2,PLOT_DRAW_TYPE,DRAW_LINE);
PlotIndexSetInteger(3,PLOT_DRAW_TYPE,DRAW_LINE);
ChartRedraw(ChartID());
//-----
return(rates_total);
}//OnCalculate()
//
//============================================================================================= MQL5 ===
// Calc_Fourier() - Calc_Fourier
//------------------------------------------------------------------------------------------------------
void Calc_Fourier(double &x[],double &p[],double &f[],int iNpast,int iNharm){
//-----
//----- initialize indicator buffers to EMPTY_VALUE
ArrayInitialize(p,EMPTY_VALUE);
ArrayInitialize(f,EMPTY_VALUE);
//----- Find average of past values
double av=0.0;
//0?>;=5=85 8AE>4=>3> <0AA820 4;O @0AG5B0 8 =0:>?;5=85 AC<<K 4;O 2KG8A;5=8O A@54=53>
{for(int i=0;i<iNpast;i++)
{
av=av+x[i];
}}//for(int i=0;i<Npast;i++)
// 0AG5B A@54=53>
av=av/iNpast;
//----- initialize model outputs
{for(int i=0;i<iNpast;i++)
{
p[i]=av; //@>H;>5 70?>;=O5< A@54=8< 7=0G5=85< >B [0] =0 <0:A8<0;L=CN 3;C18=C
}}//for(int i=0;i<iNpast;i++)
{for(int i=0;i<=Nfut;i++)
{
f[i]=av; //C4CI55 70?>;=O5< A@54=8< 7=0G5=85< >B [0] =0 3;C18=C Nfut
}}//for(int i=0;i<iNpast;i++)
//----- fit trigonometric model and calculate predictions
double w,m,c,s;
{for(int harm=1;harm<=iNharm;harm++)//Nharmm - @07 ?@>3>=O5<AO 2 F8:;5
{
Freq(x,p,iNpast,w,m,c,s);
double mcc=0.0;
double ss=0.0;
{for(int i=0;i<iNpast;i++)
{
mcc=m+c*MathCos(w*i);
ss=s*MathSin(w*i);
p[i]+=mcc+ss;
{if(i<=Nfut)
{
f[i]+=mcc-ss;
}}//if(i<=Nfut)
}}//for(int i=0;i<iNpast;i++)
}}//for(int harm=1;harm<=Nharmm;harm++)
return;
}//Calc_Fourier()
//
//============================================================================================= MQL5 ===
// Freq() - Quinn and Fernandes algorithm for finding frequency
//------------------------------------------------------------------------------------------------------
void Freq(double& x[],double& ipc[],int n,double& w,double& m,double& c,double& s)
{
double z[];
ArrayResize(z,n);
double alpha=0.0;
double beta=2.0;
z[0]=x[0]-ipc[0];
ulong IterCount=0;
{while((MathAbs(alpha-beta)>FreqTOL)&&(IterCount<IterLimit))
{
alpha=beta;
z[1]=x[1]-ipc[1]+alpha*z[0];
double num=z[0]*z[1];
double den=z[0]*z[0];
{for(int i=2;i<n;i++)
{
z[i]=x[i]-ipc[i]+alpha*z[i-1]-z[i-2];
num+=z[i-1]*(z[i]+z[i-2]);
den+=z[i-1]*z[i-1];
}}//for(int i=2;i<n;i++)
beta=num/den;
IterCount++;
}}//while(MathAbs(alpha-beta)>FreqTOL)
w=MathArccos(beta/2.0);
TrigFit(x,ipc,n,w,m,c,s);
}//Freq()
//
//============================================================================================= MQL5 ===
// TrigFit() - Least-squares fitting of trigonometric series
//------------------------------------------------------------------------------------------------------
void TrigFit(double& x[],double& ipc[],int n,double w,double& m,double& c,double& s)
{
double Sc =0.0;
double Ss =0.0;
double Scc=0.0;
double Sss=0.0;
double Scs=0.0;
double Sx =0.0;
double Sxc=0.0;
double Sxs=0.0;
{for(int i=0;i<n;i++)
{
double cos_w_i=MathCos(w*i);
double sin_w_i=MathSin(w*i);
double dx=x[i]-ipc[i];
Sc +=cos_w_i;
Ss +=sin_w_i;
Scc+=cos_w_i*cos_w_i;
Sss+=sin_w_i*sin_w_i;
Scs+=cos_w_i*sin_w_i;
Sx +=dx;
Sxc+=dx*cos_w_i;
Sxs+=dx*sin_w_i;
}}//for(int i=0;i<n;i++)
Sc /=n;
Ss /=n;
Scc/=n;
Sss/=n;
Scs/=n;
Sx /=n;
Sxc/=n;
Sxs/=n;
{if(w==0.0)
{
m=Sx;
c=0.0;
s=0.0;
}else{
// calculating a, b, and m
double den=MathPow(Scs-Sc*Ss,2)-(Scc-Sc*Sc)*(Sss-Ss*Ss);
c=((Sxs-Sx*Ss)*(Scs-Sc*Ss)-(Sxc-Sx*Sc)*(Sss-Ss*Ss))/den;
s=((Sxc-Sx*Sc)*(Scs-Sc*Ss)-(Sxs-Sx*Ss)*(Scc-Sc*Sc))/den;
m=Sx-c*Sc-s*Ss;
}}//if(w==0.0)
return;
}//TrigFit()
//