//+------------------------------------------------------------------+
//| Frama600.mq4 |
//| Copyright 2019, James Scott |
//| https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2019, James Scott"
#property link "https://www.mql5.com"
#property version "1.00"
#property strict
#property indicator_chart_window
// --- indicator settings
#property indicator_buffers 1
#property indicator_plots 1
#property indicator_type1 DRAW_LINE
#property indicator_color1 DarkBlue
#property indicator_width1 1
#property indicator_label1 "FrAMA"
// *******#property indicator_applied_price PRICE_CLOSE
//--- input parameters
input int InpPeriodFrAMA=14;
input int InpShift=0;
//--- indicator buffers
double FrAmaBuffer[];
double HighVal[];
double LowVal[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
//--- indicator buffers mapping
SetIndexBuffer(0,FrAmaBuffer,INDICATOR_DATA);
SetIndexBuffer(1,HighVal,INDICATOR_CALCULATIONS);
SetIndexBuffer(2,LowVal,INDICATOR_CALCULATIONS);
//--- sets first bar from what index will be drawn
PlotIndexSetInteger(0,PLOT_DRAW_BEGIN,2*InpPeriodFrAMA-1);
//--- sets indicator shift
PlotIndexSetInteger(0,PLOT_SHIFT,InpShift);
//--- name for indicator label
IndicatorSetString(INDICATOR_SHORTNAME,"FrAMA("+string(InpPeriodFrAMA)+")");
//--- name for index label
PlotIndexSetString(0,PLOT_LABEL,"FrAMA("+string(InpPeriodFrAMA)+")");
//--- initialization done
//---
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| 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 data
if(rates_total<2*InpPeriodFrAMA)
return(0);
//--- preparing calculation
int limit,copied;
double Hi1,Hi2,Hi3,Lo1,Lo2,Lo3;
double N1,N2,N3,D;
double ALFA;
//--- load High
if(IsStopped()) return(0); //Checking for stop flag
copied=CopyHigh(_Symbol,_Period,0,rates_total,HighVal);
if(copied!=rates_total)
{
Print("Can't load High prices.");
return(0);
}
//--- load Low
if(IsStopped()) return(0); //Checking for stop flag
copied=CopyLow(_Symbol,_Period,0,rates_total,LowVal);
if(copied!=rates_total)
{
Print("Can't load Low prices.");
return(0);
}
//--- start calculations
if(prev_calculated==0)
{
limit=2*InpPeriodFrAMA-1;
//--- fill in indicator array
for(int i=0;i<=limit;i++)
FrAmaBuffer[i]=close[i];
}
else limit=prev_calculated-1;
//--- main cycle
for(int i=limit;i<rates_total && !IsStopped();i++)
{
Hi1=iHighest600(i,InpPeriodFrAMA);
Lo1=iLowest600(i,InpPeriodFrAMA);
Hi2=iHighest600(i-InpPeriodFrAMA,InpPeriodFrAMA);
Lo2=iLowest600(i-InpPeriodFrAMA,InpPeriodFrAMA);
Hi3=iHighest600(i,2*InpPeriodFrAMA);
Lo3=iLowest600(i,2*InpPeriodFrAMA);
N1=(Hi1-Lo1)/InpPeriodFrAMA;
N2=(Hi2-Lo2)/InpPeriodFrAMA;
N3=(Hi3-Lo3)/(2*InpPeriodFrAMA);
D=(log(N1+N2)-log(N3))/log(2.0);
ALFA=exp(-4.6*(D-1.0));
FrAmaBuffer[i]=ALFA*Close[i]+(1-ALFA)*FrAmaBuffer[i-1];
}
//--- return value of prev_calculated for next call
return(rates_total);
}
//+------------------------------------------------------------------+
//| Timer function |
//+------------------------------------------------------------------+
void OnTimer()
{
//---
}
//+------------------------------------------------------------------+
//| ChartEvent function |
//+------------------------------------------------------------------+
void OnChartEvent(const int id,
const long &lparam,
const double &dparam,
const string &sparam)
{
//---
}
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
//| Find the highest value in data range |
//+------------------------------------------------------------------+
double iHighest600(int StartPos,
int Depth)
{
double res;
//--- check for parameters StartPos and Depth
if(StartPos<0 || StartPos-Depth+1<0 || Depth<0)
{
Print("Invalid parameter in function",__FUNCTION__,": StartPos =",StartPos,", Depth = ",Depth);
return(0.0);
}
//---
res=HighVal[StartPos];
for(int i=StartPos-Depth+1;i<StartPos;i++)
if(HighVal[i]>res)
res=HighVal[i];
return(res);
}
//+------------------------------------------------------------------+
//| Find the lowest value in data range |
//+------------------------------------------------------------------+
double iLowest600(int StartPos,
int Depth)
{
double res;
//--- check for parameters StartPos and Depth
if(StartPos<0 || StartPos-Depth+1<0 || Depth<0)
{
Print("Invalid parameter in function",__FUNCTION__,": StartPos =",StartPos,", Depth = ",Depth);
return(0.0);
}
//---
res=LowVal[StartPos];
for(int i=StartPos-Depth+1;i<StartPos;i++)
if(LowVal[i]<res)
res=LowVal[i];
return(res);
}
//+------------------------------------------------------------------+