2
Views
0
Downloads
0
Favorites
Sinewave2
//+------------------------------------------------------------------+
//| Sinewave2.mq5 |
//| |
//| Sinewave2 |
//| |
//| Algorithm taken from book |
//| "Cybernetics Analysis for Stock and Futures" |
//| by John F. Ehlers |
//| |
//| contact@mqlsoft.com |
//| http://www.mqlsoft.com/ |
//+------------------------------------------------------------------+
//---- author of the indicator
#property copyright "Coded by Witold Wozniak"
//---- author of the indicator
#property link "www.mqlsoft.com"
//---- indicator version
#property version "1.00"
//---- drawing the indicator in a separate window
#property indicator_separate_window
//---- two buffers are used for calculation and drawing the indicator
#property indicator_buffers 2
//---- two plots are used
#property indicator_plots 2
//+----------------------------------------------+
//| Sinewave indicator drawing parameters |
//+----------------------------------------------+
//---- drawing the indicator 1 as a line
#property indicator_type1 DRAW_LINE
//---- red color is used for the indicator line
#property indicator_color1 Red
//---- the indicator 1 line is a continuous curve
#property indicator_style1 STYLE_SOLID
//---- indicator 1 line width is equal to 1
#property indicator_width1 1
//---- displaying the indicator line label
#property indicator_label1 "Sinewave"
//+----------------------------------------------+
//| LeadSinewave indicator drawing parameters |
//+----------------------------------------------+
//---- drawing the indicator 2 as a line
#property indicator_type2 DRAW_LINE
//---- blue color is used for the LeadSinewave line
#property indicator_color2 Blue
//---- the indicator 2 line is a continuous curve
#property indicator_style2 STYLE_SOLID
//---- indicator 2 line width is equal to 1
#property indicator_width2 1
//---- displaying the indicator line label
#property indicator_label2 "LeadSinewave"
//+----------------------------------------------+
//| Horizontal levels display parameters |
//+----------------------------------------------+
#property indicator_level1 0.0
#property indicator_levelcolor Gray
#property indicator_levelstyle STYLE_DASHDOTDOT
//+----------------------------------------------+
//| Indicator window parameters |
//+----------------------------------------------+
//#property indicator_minimum -1
//#property indicator_maximum 1
//+----------------------------------------------+
//| Declaration of constants |
//+----------------------------------------------+
#define RESET 0 // the constant for getting the command for the indicator recalculation back to the terminal
#define MAXPERIOD 100 // the constant for the maximum period limitation
//+----------------------------------------------+
//| Indicator input parameters |
//+----------------------------------------------+
input double Alpha=0.07; // Indicator ratio
input int Shift=0; // Horizontal shift of the indicator in bars
//+----------------------------------------------+
//---- declaration of dynamic arrays that
//---- will be used as indicator buffers
double SineBuffer[];
double LeadSineBuffer[];
//---- declaration of integer variables for the indicators handles
int CP_Handle;
//---- declaration of the integer variables for the start of data calculation
int min_rates_total;
//---- declaration of dynamic arrays that
//---- will be used as ring buffers
int Count[];
double Smooth[],Price[],Cycle[];
//---- declaration of global variables
double K0,K1,K2,K3;
double rad2Deg,deg2Rad;
//+------------------------------------------------------------------+
//| Recalculation of position of the newest element in the array |
//+------------------------------------------------------------------+
void Recount_ArrayZeroPos(int &CoArr[],// Return the current value of the price series by the link
int Size)
{
//----
int numb,Max1,Max2;
static int count=1;
Max2=Size;
Max1=Max2-1;
count--;
if(count<0) count=Max1;
for(int iii=0; iii<Max2; iii++)
{
numb=iii+count;
if(numb>Max1) numb-=Max2;
CoArr[iii]=numb;
}
//----
}
//+------------------------------------------------------------------+
//| Getting the difference of the price time series values |
//+------------------------------------------------------------------+
double Get_Price(const double &High[],const double &Low[],int bar)
{
//----
return((High[bar]+Low[bar])/2.0);
}
//+------------------------------------------------------------------+
//| Calculation of the financial asset average price |
//+------------------------------------------------------------------+
double Get_SmoothValue(const double &PriceArray[],int &CountArray[],int bar)
{
//----
return((PriceArray[CountArray[0]]+2*PriceArray[CountArray[1]]
+2*PriceArray[CountArray[2]]+PriceArray[CountArray[3]])/6);
//----
}
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
void OnInit()
{
//---- initialization of variables of the start of data calculation
min_rates_total=7;
//---- getting handle of the CyclePeriod indicator
CP_Handle=iCustom(NULL,0,"CyclePeriod",Alpha);
if(CP_Handle==INVALID_HANDLE) Print(" Failed to get handle of the CyclePeriod indicator");
//---- initialization of variables
K0=MathPow((1.0 - 0.5*Alpha),2);
K1=2.0;
K2=K1 *(1.0 - Alpha);
K3=MathPow((1.0 - Alpha),2);
rad2Deg = 45.0 / MathArctan(1.0);
deg2Rad = 1.0 / rad2Deg;
//---- memory distribution for variables' arrays
ArrayResize(Count,MAXPERIOD);
ArrayResize(Price,MAXPERIOD);
ArrayResize(Smooth,MAXPERIOD);
ArrayResize(Cycle,MAXPERIOD);
ArrayInitialize(Count,0);
ArrayInitialize(Price,0);
ArrayInitialize(Smooth,0);
ArrayInitialize(Cycle,0);
//---- set SineBuffer[] dynamic array as an indicator buffer
SetIndexBuffer(0,SineBuffer,INDICATOR_DATA);
//---- shifting the indicator 1 horizontally by Shift
PlotIndexSetInteger(0,PLOT_SHIFT,Shift);
//---- performing shift of the beginning of counting of drawing the indicator 1 by min_rates_total
PlotIndexSetInteger(0,PLOT_DRAW_BEGIN,min_rates_total);
//---- set LeadSineBuffer[] dynamic array as an indicator buffer
SetIndexBuffer(1,LeadSineBuffer,INDICATOR_DATA);
//---- shifting the indicator 2 horizontally by Shift
PlotIndexSetInteger(1,PLOT_SHIFT,Shift);
//---- performing shift of the beginning of counting of drawing the indicator 2 by min_rates_total+1
PlotIndexSetInteger(1,PLOT_DRAW_BEGIN,min_rates_total+1);
//---- initializations of a variable for the indicator short name
string shortname;
StringConcatenate(shortname,"Sinewave2(",DoubleToString(Alpha,4),", ",Shift,")");
//--- creation of the name to be displayed in a separate sub-window and in a tooltip
IndicatorSetString(INDICATOR_SHORTNAME,shortname);
//--- determination of accuracy of displaying the indicator values
IndicatorSetInteger(INDICATOR_DIGITS,_Digits);
//----
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total, // number of bars in history at the current tick
const int prev_calculated,// number of bars calculated at previous call
const datetime &time[],
const double &open[],
const double& high[], // price array of maximums of price for the indicator calculation
const double& low[], // price array of minimums of price for the indicator calculation
const double &close[],
const long &tick_volume[],
const long &volume[],
const int &spread[])
{
//---- checking the number of bars to be enough for the calculation
if(BarsCalculated(CP_Handle)<rates_total || rates_total<min_rates_total) return(RESET);
//---- declarations of local variables
int first,bar,DcPeriod;
int bar0,bar1,bar2,bar3;
double RealPart,ImagPart,DCPhase,Arg,period[1];
//---- calculation of the 'first' starting index for the bars recalculation loop
if(prev_calculated>rates_total || prev_calculated<=0) // checking for the first start of the indicator calculation
first=3; // starting index for calculation of all bars
else first=prev_calculated-1; // starting index for calculation of new bars
//---- main indicator calculation loop
for(bar=first; bar<rates_total && !IsStopped(); bar++)
{
bar0=Count[0];
bar1=Count[1];
bar2=Count[2];
bar3=Count[3];
Price[bar0]=Get_Price(high,low,bar);
Smooth[bar0]=Get_SmoothValue(Price,Count,0);
if(bar>min_rates_total) Cycle[bar0]=K0*(Smooth[bar0]-K1*Smooth[bar1]+Smooth[bar2])+K2*Cycle[bar1]-K3*Cycle[bar2];
else Cycle[bar0]=(Price[bar0]-2.0*Price[bar1]+Price[bar2])/4.0;
//--- copy newly appeared data in the array
if(CopyBuffer(CP_Handle,0,rates_total-1-bar,1,period)<=0) return(RESET);
DcPeriod=int(MathFloor(period[0]));
DcPeriod=MathMin(DcPeriod,bar); // cutting the smoothing down to the real number of bars
RealPart=0.0;
ImagPart=0.0;
for(int iii=0; iii<DcPeriod; iii++)
{
Arg=deg2Rad*360.0*iii/DcPeriod;
RealPart+=MathSin(Arg)*Cycle[Count[iii]];;
ImagPart+=MathCos(Arg)*Cycle[Count[iii]];
}
DCPhase=0.0;
if(MathAbs(ImagPart)>0.001) DCPhase=rad2Deg*MathArctan(RealPart/ImagPart);
else
{
if(RealPart >= 0.0) DCPhase = 90.0;
else DCPhase = -90.0;
}
DCPhase+=90.0;
if(ImagPart<0) DCPhase+=180.0;
if(DCPhase>315.0) DCPhase-=360.0;
SineBuffer[bar]=MathSin(DCPhase*deg2Rad);
LeadSineBuffer[bar]=MathSin((DCPhase+45.0)*deg2Rad);
if(DCPhase == 180 && LeadSineBuffer[bar-1] > 0) LeadSineBuffer[bar] = MathSin(45 * deg2Rad);
if(DCPhase == 0 && LeadSineBuffer[bar-1] < 0) LeadSineBuffer[bar] = MathSin(225 * deg2Rad);
if(bar<rates_total-1) Recount_ArrayZeroPos(Count,MAXPERIOD);
}
//----
return(rates_total);
}
//+------------------------------------------------------------------+
Comments
Markdown Formatting Guide
# H1
## H2
### H3
**bold text**
*italicized text*
[title](https://www.example.com)

`code`
```
code block
```
> blockquote
- Item 1
- Item 2
1. First item
2. Second item
---