2
Views
0
Downloads
0
Favorites
UltraXMA
//+------------------------------------------------------------------+
//| UltraXMA.mq5 |
//| Copyright © 2011, Nikolay Kositsin |
//| Khabarovsk, farria@mail.redcom.ru |
//+------------------------------------------------------------------+
//| Place the SmoothAlgorithms.mqh file |
//| to the terminal_data_folder\MQL5\Include |
//+------------------------------------------------------------------+
//---- author of the indicator
#property copyright "Copyright © 2011, Nikolay Kositsin"
//---- link to the website of the author
#property link "farria@mail.redcom.ru"
//---- indicator version
#property version "1.00"
//---- drawing indicator in a separate window
#property indicator_separate_window
//---- 2 buffers are used for calculation and drawing the indicator
#property indicator_buffers 2
//---- one plot is used
#property indicator_plots 1
//+-----------------------------------+
//| Declaration of constants |
//+-----------------------------------+
#define RESET 0 // the constant for getting the command for the indicator recalculation back to the terminal
//+-----------------------------------+
//| Filling drawing parameters |
//+-----------------------------------+
//---- drawing indicator as a filling between two lines
#property indicator_type1 DRAW_FILLING
//---- BlueViolet and DarkOrange colors are used as the indicator filling colors
#property indicator_color1 BlueViolet,DarkOrange
//---- displaying the indicator label
#property indicator_label1 "Ultra XMA"
//+-----------------------------------+
//| CXMA class description |
//+-----------------------------------+
#include <SmoothAlgorithms.mqh>
//+-----------------------------------+
//---- declaration of the CXMA class variables from the SmoothAlgorithms.mqh file
CXMA XMA[];
//+-----------------------------------+
//| Declaration of enumerations |
//+-----------------------------------+
enum Applied_price_ // Type of constant
{
PRICE_CLOSE_ = 1, // Close
PRICE_OPEN_, // Open
PRICE_HIGH_, // High
PRICE_LOW_, // Low
PRICE_MEDIAN_, // Median Price (HL/2)
PRICE_TYPICAL_, // Typical Price (HLC/3)
PRICE_WEIGHTED_, // Weighted Close (HLCC/4)
PRICE_SIMPLE, // Simple Price (OC/2)
PRICE_QUARTER_, // Quarted Price (HLOC/4)
PRICE_TRENDFOLLOW0_, // TrendFollow_1 Price
PRICE_TRENDFOLLOW1_ // TrendFollow_2 Price
};
/*enum Smooth_Method - enumeration is declared in the SmoothAlgorithms.mqh file
{
MODE_SMA_, // SMA
MODE_EMA_, // EMA
MODE_SMMA_, // SMMA
MODE_LWMA_, // LWMA
MODE_JJMA, // JJMA
MODE_JurX, // JurX
MODE_ParMA, // ParMA
MODE_T3, // T3
MODE_VIDYA, // VIDYA
MODE_AMA, // AMA
}; */
//+-----------------------------------+
//| Declaration of enumeration |
//+-----------------------------------+
enum WIDTH
{
Width_1=1, // 1
Width_2, // 2
Width_3, // 3
Width_4, // 4
Width_5 // 5
};
//+-----------------------------------+
//| Declaration of enumeration |
//+-----------------------------------+
enum STYLE
{
SOLID_, // Solid line
DASH_, // Dashed line
DOT_, // Dotted line
DASHDOT_, // Dot-dash line
DASHDOTDOT_ // Dot-dash line with double dots
};
//+----------------------------------------------+
//| Indicator input parameters |
//+----------------------------------------------+
input ENUM_APPLIED_PRICE Applied_price=PRICE_CLOSE; // Applied price
//----
input Smooth_Method W_Method=MODE_JJMA; // Smoothing method
input int StartLength=3; // Initial smoothing period
input int WPhase=100; // Smoothing parameter
//----
input uint Step=5; // Period change step
input uint StepsTotal=10; // Number of period changes
//----
input Smooth_Method SmoothMethod=MODE_JJMA; // Smoothing method
input int SmoothLength=3; // Smoothing depth
input int SmoothPhase=100; // Smoothing parameter
input Applied_price_ IPC=PRICE_CLOSE; // Applied price
//----
input uint UpLevel=80; // Overbought level
input uint DnLevel=20; // Oversold level
input color UpLevelsColor=Red; // Overbought level color
input color DnLevelsColor=Red; // Oversold level color
input STYLE Levelstyle=DASH_; // Levels style
input WIDTH LevelsWidth=Width_1; // Levels width
//+----------------------------------------------+
//---- declaration of dynamic arrays that
//---- will be used as indicator buffers
double BullsBuffer[];
double BearsBuffer[];
//---- declaration of the variables array for storing signal lines periods
int period[];
//---- declaration of variables arrays for the XMA indicator smoothed values storage
double xxma0[],xxma1[];
//---- declaration of the integer variables for the start of data calculation
uint StTot1,StTot2;
int min_rates_total,min_rates_xma;
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
void OnInit()
{
//---- memory distribution for variables' arrays
int size=int(StepsTotal+3);
if(ArrayResize(XMA,size)<size) Print("Failed to distribute the memory for XMA[] array");
size-=2;
if(ArrayResize(xxma0,size)<size) Print("Failed to distribute the memory for xxma0[] array");
if(ArrayResize(xxma1,size)<size) Print("Failed to distribute the memory for xxma1[] array");
if(ArrayResize(period,size)<size) Print("Failed to distribute the memory for period[] array");
ZeroMemory(xxma0);
ZeroMemory(xxma1);
ZeroMemory(period);
//---- initialization of variables
for(int sm=int(StepsTotal); sm>=0 && !IsStopped(); sm--) period[sm]=int(StartLength+sm*Step);
//---- setting up alerts for unacceptable values of external variables
XMA[0].XMALengthCheck("StartLength", StartLength);
XMA[0].XMAPhaseCheck("WPhase", WPhase, W_Method);
XMA[0].XMALengthCheck("SmoothLength", SmoothLength);
XMA[0].XMAPhaseCheck("SmoothPhase", SmoothPhase, SmoothMethod);
//---- initialization of variables of the start of data calculation
min_rates_xma=XMA[0].GetStartBars(W_Method,StartLength+Step*StepsTotal,WPhase)+1;
min_rates_total=min_rates_xma+XMA[0].GetStartBars(SmoothMethod,SmoothLength,SmoothPhase);
StTot1=StepsTotal+1;
StTot2=StepsTotal+2;
//---- set BullsBuffer[] dynamic array as an indicator buffer
SetIndexBuffer(0,BullsBuffer,INDICATOR_DATA);
//---- performing shift of the beginning of counting of drawing the indicator 1 by min_rates_total
PlotIndexSetInteger(0,PLOT_DRAW_BEGIN,min_rates_total);
//---- indexing the elements in the buffer as timeseries
ArraySetAsSeries(BullsBuffer,true);
//---- set BearsBuffer[] dynamic array as an indicator buffer
SetIndexBuffer(1,BearsBuffer,INDICATOR_DATA);
//---- performing shift of the beginning of counting of drawing the indicator 2 by min_rates_total
PlotIndexSetInteger(1,PLOT_DRAW_BEGIN,min_rates_total);
//---- indexing the elements in the buffer as timeseries
ArraySetAsSeries(BearsBuffer,true);
//---- initializations of a variable for the indicator short name
string shortname="Ultra XMA";
//---- 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,2);
//---- lines drawing parameters
IndicatorSetInteger(INDICATOR_LEVELS,2);
IndicatorSetDouble(INDICATOR_LEVELVALUE,0,StepsTotal*UpLevel/100);
IndicatorSetInteger(INDICATOR_LEVELCOLOR,0,UpLevelsColor);
IndicatorSetInteger(INDICATOR_LEVELSTYLE,0,Levelstyle);
IndicatorSetInteger(INDICATOR_LEVELWIDTH,0,LevelsWidth);
IndicatorSetDouble(INDICATOR_LEVELVALUE,1,StepsTotal*DnLevel/100);
IndicatorSetInteger(INDICATOR_LEVELCOLOR,1,DnLevelsColor);
IndicatorSetInteger(INDICATOR_LEVELSTYLE,1,Levelstyle);
IndicatorSetInteger(INDICATOR_LEVELWIDTH,1,LevelsWidth);
//----
}
//+------------------------------------------------------------------+
//| 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(rates_total<min_rates_total) return(RESET);
//---- declarations of local variables
int limit,bar,maxbar1,maxbar2;
double upsch,dnsch,price_;
//---- calculation of maxbar initial index for the XMASeries() function
maxbar1=rates_total-1;
maxbar2=rates_total-1-min_rates_xma;
//---- calculation of the 'limit' starting index for the bars recalculation loop
if(prev_calculated>rates_total || prev_calculated<=0)// checking for the first start of the indicator calculation
limit=maxbar1; // starting index for calculation of all bars
else limit=rates_total-prev_calculated; // starting index for calculation of new bars
//---- indexing elements in arrays as time series
ArraySetAsSeries(open,true);
ArraySetAsSeries(low,true);
ArraySetAsSeries(high,true);
ArraySetAsSeries(close,true);
//---- main indicator calculation loop
for(bar=limit; bar>=0 && !IsStopped(); bar--)
{
//---- call of the PriceSeries function to get the input price 'price_'
price_=PriceSeries(IPC,bar,open,low,high,close);
for(int sm=int(StepsTotal); sm>=0 && !IsStopped(); sm--)
xxma0[sm]=XMA[sm].XMASeries(maxbar1,prev_calculated,rates_total,W_Method,WPhase,period[sm],price_,bar,true);
if(bar>maxbar2)
{
if(bar) ArrayCopy(xxma1,xxma0,0,0,WHOLE_ARRAY);
continue;
}
upsch=0;
dnsch=0;
for(int sm=int(StepsTotal); sm>=0 && !IsStopped(); sm--)
if(xxma0[sm]>xxma1[sm]) upsch++;
else dnsch++;
BullsBuffer[bar]=XMA[StTot1].XMASeries(maxbar2,prev_calculated,rates_total,SmoothMethod,SmoothPhase,SmoothLength,upsch,bar,true);
BearsBuffer[bar]=XMA[StTot2].XMASeries(maxbar2,prev_calculated,rates_total,SmoothMethod,SmoothPhase,SmoothLength,dnsch,bar,true);
if(bar) ArrayCopy(xxma1,xxma0,0,0,WHOLE_ARRAY);
}
//----
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
---