#property copyright"Mql_id:-YesMrSamuel----:Akinbowale-Samuel" #property link"https://www.mql5.com/en/users/yesmrsamuel/seller" #property description "Momentum" #property strict #property indicator_separate_window #property indicator_buffers 1 #property indicator_color1 DodgerBlue //--- input parameter input int InpMomPeriod=14; // Momentum Period //--- buffers double ExtMomBuffer[]; //+------------------------------------------------------------------+ //| Custom indicator initialization function | //+------------------------------------------------------------------+ int OnInit(void) { string short_name; //--- indicator line SetIndexStyle(0,DRAW_LINE); SetIndexBuffer(0,ExtMomBuffer); //--- name for DataWindow and indicator subwindow label short_name="Mom("+IntegerToString(InpMomPeriod)+")"; IndicatorShortName(short_name); SetIndexLabel(0,short_name); //--- check for input parameter if(InpMomPeriod<=0) { Print("Wrong input parameter Momentum Period=",InpMomPeriod); return(INIT_FAILED); } //--- SetIndexDrawBegin(0,InpMomPeriod); //--- initialization done return(INIT_SUCCEEDED); } //+------------------------------------------------------------------+ //| Momentum | //+------------------------------------------------------------------+ 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[]) { int i,limit; //--- check for bars count and input parameter if(rates_total<=InpMomPeriod || InpMomPeriod<=0) return(0); //--- counting from 0 to rates_total ArraySetAsSeries(ExtMomBuffer,false); ArraySetAsSeries(close,false); //--- initial zero if(prev_calculated<=0) { for(i=0; i<InpMomPeriod; i++) ExtMomBuffer[i]=0.0; limit=InpMomPeriod; } else limit=prev_calculated-1; //--- the main loop of calculations for(i=limit; i<rates_total; i++) ExtMomBuffer[i]=close[i]*100/close[i-InpMomPeriod]; //--- done return(rates_total); } //+------------------------------------------------------------------+
This is just the momentum graph I want the moving average of the momentum.
Then why don't you take this and run iMAOnArray on its buffer?
What would be the size of the array? Taking into consideration the period.
What would the size of the array? Taking into consideration the period.
Check the documentation of iMAOnArray for a sample code. Should be easy to code. Set up a 2nd indicator buffer to hold the MA result values.
I did use a second buffer but its not working correctly.
Buffer2[i] = iMomentum(NULL, PERIOD_CURRENT, 14, PRICE_CLOSE, i);
Buffer1[i]= iMAOnArray(Buffer2,0,14,0,MODE_SMA,0);
Show the whole code.
#include <stdlib.mqh>
#include <stderror.mqh>
//--- indicator settings
#property indicator_separate_window
#property indicator_buffers 2
#property indicator_type1 DRAW_LINE
#property indicator_style1 STYLE_SOLID
#property indicator_width1 1
#property indicator_color1 0xFFAA00
#property indicator_label1 ""
//--- indicator buffers
double Buffer1[];
double Buffer2[];
double myPoint; //initialized in OnInit
void myAlert(string type, string message)
{
if(type == "print")
Print(message);
else if(type == "error")
{
Print(type+" | MT4I1 @ "+Symbol()+","+Period()+" | "+message);
}
else if(type == "order")
{
}
else if(type == "modify")
{
}
}
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
IndicatorBuffers(2);
SetIndexBuffer(0, Buffer1);
SetIndexEmptyValue(0, 0);
SetIndexBuffer(1, Buffer2);
SetIndexEmptyValue(0, 0);
//initialize myPoint
myPoint = Point();
if(Digits() == 5 || Digits() == 3)
{
myPoint *= 10;
}
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[])
{
int limit = rates_total - prev_calculated;
//--- counting from 0 to rates_total
ArraySetAsSeries(Buffer1, true);
//--- initial zero
if(prev_calculated < 1)
{
ArrayInitialize(Buffer1, 0);
}
else
limit++;
//--- main loop
for(int i = limit-1; i >= 0; i--)
{
if (i >= MathMin(5000-1, rates_total-1-50)) continue; //omit some old rates to prevent "Array out of range" or slow calculation
//Indicator Buffer 1
if(true //no conditions!
)
// {
// Buffer1[i] = Buffer1[i-1]+(((iMomentum(NULL, PERIOD_CURRENT, 14, PRICE_CLOSE, (i))+iMomentum(NULL, PERIOD_CURRENT, 14, PRICE_CLOSE, (i-1))+iMomentum(NULL, PERIOD_CURRENT, 14, PRICE_CLOSE, (i+2)))+iMomentum(NULL, PERIOD_CURRENT, 14, PRICE_CLOSE, (i+3))+iMomentum(NULL, PERIOD_CURRENT, 14, PRICE_CLOSE, (i+4))+iMomentum(NULL, PERIOD_CURRENT, 14, PRICE_CLOSE, (i+5))+iMomentum(NULL, PERIOD_CURRENT, 14, PRICE_CLOSE, (i+6))+iMomentum(NULL, PERIOD_CURRENT, 14, PRICE_CLOSE, (i+7))+iMomentum(NULL, PERIOD_CURRENT, 14, PRICE_CLOSE, (i+8))+iMomentum(NULL, PERIOD_CURRENT, 14, PRICE_CLOSE, (i+9))+iMomentum(NULL, PERIOD_CURRENT, 14, PRICE_CLOSE, (i+10))+iMomentum(NULL, PERIOD_CURRENT, 14, PRICE_CLOSE, (i+11))+iMomentum(NULL, PERIOD_CURRENT, 14, PRICE_CLOSE, (i+12))+iMomentum(NULL, PERIOD_CURRENT, 14, PRICE_CLOSE, (i+13)))/14.0);
// Buffer1[i] = Buffer1[i-1]+iMomentum(NULL, PERIOD_CURRENT, 14, PRICE_CLOSE, (i))+(iMomentum(NULL, PERIOD_CURRENT, 14, PRICE_CLOSE, (i))-iMomentum(NULL, PERIOD_CURRENT, 14, PRICE_CLOSE, (i-14)))/14;
// ExtLineBuffer[i]=ExtLineBuffer[i-1]+(price[i]-price[i-InpMAPeriod])/InpMAPeriod;
Buffer2[i] = iMomentum(NULL, PERIOD_CURRENT, 14, PRICE_CLOSE, i);
//iMA(NULL,0,13,8,MODE_SMMA,PRICE_CLOSE,i);
// double macurrent=iMAOnArray(ExtBuffer,0,5,0,MODE_LWMA,0);
Buffer1[i]= iMAOnArray(Buffer2,0,14,0,MODE_SMA,0);
// =X;
/* }
else
{
Buffer1[i] = 0;
}*/
}
return(rates_total);
}
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
iMA(NULL, PERIOD_CURRENT, 14, 0, MODE_SMA, (iMomentum(NULL, PERIOD_CURRENT, 14, PRICE_CLOSE, 0)), 0);
This is what i have but its not working.