Momentum on Moving Average of price

 

I have written the following piece of code to show momentum of MA of price as an indicator on the chart. But nothing shows up. whats wrong with the code?

//+------------------------------------------------------------------+
//| Acc.mq4 |
//| |
//| |
//+------------------------------------------------------------------+
#property copyright ""
#property link ""

#property indicator_separate_window
#property indicator_buffers 1
#property indicator_color1 Red
//---- input parameters
extern int TimePeriod=20;
extern int MAPeriod=20;

//---- buffers
double ExtMapBuffer1[];
double ExtMapBuffer2[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init()
{
string short_name;
//---- indicators
SetIndexStyle(0,DRAW_LINE);
//SetIndexStyle(1,DRAW_LINE);
SetIndexBuffer(0,ExtMapBuffer1);
//SetIndexBuffer(0,ExtMapBuffer2);
//----
//---- name for DataWindow and indicator subwindow label
//ArraySetAsSeries(ExtMapBuffer2,true);
short_name="Acc("+TimePeriod+")";
IndicatorShortName(short_name);
SetIndexLabel(0,short_name);
//----
//SetIndexDrawBegin(0,TimePeriod);
//----
return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
//----

//----
return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function |
//+------------------------------------------------------------------+
int start()
{
int counted_bars=IndicatorCounted();
int i, j;
//----
if(Bars<=TimePeriod) return(0);
//---- initial zero
if(counted_bars<1)
for(i=1;i<=TimePeriod;i++) ExtMapBuffer1[Bars-i]=0.0;
//----
i=Bars-TimePeriod-1;
if(counted_bars>=TimePeriod) i=Bars-counted_bars-1;
//Print(Bars + " " + counted_bars);
//----
for(j=i; j>=0; j--)
ExtMapBuffer2[j]=iMA(NULL,0,MAPeriod,0,MODE_EMA,PRICE_TYPICAL,j);
//----
for(j=i; j>=0; j--) {
//ExtMapBuffer1[j]=iMAOnArray(ExtMapBuffer2, Bars, TimePeriod,0,MODE_SMA,j);
//Print(ExtMapBuffer2[j]);
ExtMapBuffer1[j]=iMomentumOnArray(ExtMapBuffer2, Bars, TimePeriod,j);
}
return(0);
}
//+------------------------------------------------------------------+

 
crazyfx:

I have written the following piece of code to show momentum of MA of price as an indicator on the chart. But nothing shows up. whats wrong with the code?

//+------------------------------------------------------------------+
//| Acc.mq4 |
//| |
//| |
//+------------------------------------------------------------------+
#property copyright ""
#property link ""

#property indicator_separate_window
#property indicator_buffers 1
#property indicator_color1 Red
//---- input parameters
extern int TimePeriod=20;
extern int MAPeriod=20;

//---- buffers
double ExtMapBuffer1[];
double ExtMapBuffer2[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init()
{
string short_name;
//---- indicators
SetIndexStyle(0,DRAW_LINE);
//SetIndexStyle(1,DRAW_LINE);
SetIndexBuffer(0,ExtMapBuffer1);
//SetIndexBuffer(0,ExtMapBuffer2);
//----
//---- name for DataWindow and indicator subwindow label
//ArraySetAsSeries(ExtMapBuffer2,true);
short_name="Acc("+TimePeriod+")";
IndicatorShortName(short_name);
SetIndexLabel(0,short_name);
//----
//SetIndexDrawBegin(0,TimePeriod);
//----
return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
//----

//----
return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function |
//+------------------------------------------------------------------+
int start()
{
int counted_bars=IndicatorCounted();
int i, j;
//----
if(Bars<=TimePeriod) return(0);
//---- initial zero
if(counted_bars<1)
for(i=1;i<=TimePeriod;i++) ExtMapBuffer1[Bars-i]=0.0;
//----
i=Bars-TimePeriod-1;
if(counted_bars>=TimePeriod) i=Bars-counted_bars-1;
//Print(Bars + " " + counted_bars);
//----
for(j=i; j>=0; j--)
ExtMapBuffer2[j]=iMA(NULL,0,MAPeriod,0,MODE_EMA,PRICE_TYPICAL,j);
//----
for(j=i; j>=0; j--) {
//ExtMapBuffer1[j]=iMAOnArray(ExtMapBuffer2, Bars, TimePeriod,0,MODE_SMA,j);
//Print(ExtMapBuffer2[j]);
ExtMapBuffer1[j]=iMomentumOnArray(ExtMapBuffer2, Bars, TimePeriod,j);
}
return(0);
}
//+------------------------------------------------------------------+



not sure if this i what you're after, but try changing this line:

#property indicator_separate_window

to this:

#property indicator_chart_window
 
outofdebt:


not sure if this i what you're after, but try changing this line:

to this:


Thanks for the tip but that did not help. Anyway, since its a momentum that I am trying to print, it has to be in new window.
 

I wonder if it is because you have this line

//SetIndexBuffer(0,ExtMapBuffer2);
commented out?
 
outofdebt:

I wonder if it is because you have this line

commented out?

That is because I am printing the other buffer:

SetIndexBuffer(0,ExtMapBuffer1);

ExtMapBuffer2 gets printed fine but I really want to show the momentum rather than the MA

 
crazyfx:

That is because I am printing the other buffer:

SetIndexBuffer(0,ExtMapBuffer1);

ExtMapBuffer2 gets printed fine but I really want to show the momentum rather than the MA


OK, I switched a few things, and I got it to show up in the separate window. I'll post it here with changes I made. You'll have to copy/paste it. Try it and see.

//+------------------------------------------------------------------+
//| Acc.mq4 |
//| |
//| |
//+------------------------------------------------------------------+
#property copyright ""
#property link ""

#property indicator_separate_window
#property indicator_buffers 1
#property indicator_color1 Red
//---- input parameters
extern int TimePeriod=20;
extern int MAPeriod=20;

//---- buffers
double ExtMapBuffer1[];
double ExtMapBuffer2[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init()
{
string short_name;
//---- indicators
SetIndexStyle(0,DRAW_LINE);
//SetIndexStyle(1,DRAW_LINE);
SetIndexBuffer(0,ExtMapBuffer1);
//SetIndexBuffer(0,ExtMapBuffer2);
//----
//---- name for DataWindow and indicator subwindow label
//ArraySetAsSeries(ExtMapBuffer2,true);
short_name="Acc("+TimePeriod+")";
IndicatorShortName(short_name);
SetIndexLabel(0,short_name);
//----
//SetIndexDrawBegin(0,TimePeriod);
//----
return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
//----

//----
return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function |
//+------------------------------------------------------------------+
int start()
{
int i,counted_bars=IndicatorCounted();
int j;
//----
if(Bars<=TimePeriod) return(0);
//---- initial zero
if(counted_bars<1)

for(i=1;i<=TimePeriod;i++) ExtMapBuffer1[Bars-i]=0.0;
//----
i=Bars-TimePeriod-1;
if(counted_bars>=TimePeriod) i=Bars-counted_bars-1;
//Print(Bars + " " + counted_bars);
//---- 
for(j=i; j>=0; j--)
ExtMapBuffer1[j]=iMA(NULL,0,MAPeriod,0,MODE_EMA,PRICE_TYPICAL,j);
//---- 
for(j=i; j>=0; j--) {
//ExtMapBuffer1[j]=iMAOnArray(ExtMapBuffer2, Bars, TimePeriod,0,MODE_SMA,j);
//Print(ExtMapBuffer2[j]);
ExtMapBuffer2[j]=iMomentumOnArray(ExtMapBuffer1, Bars, TimePeriod,j);
}
return(0);
}
//+------------------------------------------------------------------+
 
outofdebt:


OK, I switched a few things, and I got it to show up in the separate window. I'll post it here with changes I made. You'll have to copy/paste it. Try it and see.


But you are still showing MA rather than Momentum. As I mentioned in my previous mail, I am able to see the MA buffer on the screen but not the Momentum buffer.
 

OK, sorry for not understanding you. I tried uncommenting the following line: SetIndexBuffer(0,ExtMapBuffer2);

in the code I pasted above, and I used time period 10, MA period 30 for the inputs and this is what it looks like on my screen:

(I think the momentum is being calculated because a 30 period MA would be smooth and would not have a sharp peak like in the pic)

IDK though...LOL

 
Thanks, could you please repost the code that you used now?
 
crazyfx:
Thanks, could you please repost the code that you used now?


same as what i posted above, but uncommented this line:

SetIndexBuffer(0,ExtMapBuffer2);

 

Thanks a lot! I am able to see the graph now.

However, its pretty strange though. Why should we have to have both of them in the indexbuffer to the same index number whereas I am interested only in ExtMapBuffer2?

SetIndexBuffer(0,ExtMapBuffer1);
SetIndexBuffer(0,ExtMapBuffer2);

Reason: