Draw VLine at iMA cross for entire chart.

 

Thanks for any help on this. I just want to draw an VLine (vertical line) at each ma cross on my charts. How can I access back-data for an iMA? (I can code it going forward, that's simple, but going backwards?)


Thank you very much for any help!

 
//+------------------------------------------------------------------+
//| ma-vline.mq4 |
//| fx1 |
//| fx1.net |
//+------------------------------------------------------------------+
#property copyright "fx1"
#property link "fx1.net"

#property indicator_chart_window
#property indicator_buffers 3
#property indicator_color1 Blue
#property indicator_color2 Red
double ExtBlueBuffer[];
double ExtRedBuffer[];

//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init()
{
SetIndexBuffer(0,ExtBlueBuffer);
SetIndexBuffer(1,ExtRedBuffer);
SetIndexStyle(0,DRAW_LINE);
SetIndexStyle(1,DRAW_LINE);
return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
//----
//----
return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function |
//+------------------------------------------------------------------+
int start()
{
int limit;
int counted_bars=IndicatorCounted();
if(counted_bars<0) return(-1);
if(counted_bars>0) counted_bars--;
limit=Bars-counted_bars;
double ma1;
double ma2;
for(int i=0; i<limit; i++)
{
ma1=iMA(NULL,0,7,0,MODE_SMMA,PRICE_MEDIAN,i);
ma2=iMA(NULL,0,14,0,MODE_SMMA,PRICE_MEDIAN,i);
ExtBlueBuffer[i]=ma1;
ExtRedBuffer[i]=ma2;
if (ExtBlueBuffer[i]<ExtRedBuffer[i] && ExtBlueBuffer[i+1]>ExtRedBuffer[i+1])
{
string name="vline"+DoubleToStr(i,0);
ObjectCreate(name,OBJ_VLINE,0,iTime(Symbol(),0,i),0);
}
}
return(0);
}

 
Great! Thank you very much.
 
Although, it doesn't seem to draw any vlines... odd. Maybe because I am in strategy tester.
 



its working on my terminal

 

You may use this little puppy... it draws a Vline at given time with given color.

I use it as a very handy debugging & visualization tool.

//-----------------------------------------------------------
void DrawVline (color Color, datetime time)
   {//Use: DrawVline(Yellow, Time[i]);
   static int cntr;
   static double PrevVlineTime;
   if (time == PrevVlineTime) return(0);//Still on same bar
      ObjectCreate( "UpLine"+cntr, OBJ_VLINE, 0, time, 0);
      ObjectSet("UpLine"+cntr, OBJPROP_COLOR, Color);
      ObjectSet("UpLine"+cntr, OBJPROP_BACK,true);
      cntr++;
      PrevVlineTime = time;//Store bar time of last drawn line
   return(0);
   }     
//-----------------------------------------------------------  
 

Hi Guys,


Found this golden Oldie and tried the code, it works 100% BUT it will only draw a V-line when the Blue crosses down...what about when the blue crosses up? Tried to play around with the code but cant figure it out...will anybody please help? 

 
  1. Please edit your (original) post and use the CODE button (Alt-S)! (For large amounts of code, attach it.)
              General rules and best pratices of the Forum. - General - MQL5 programming forum
              Messages Editor

  2. string name="vline"+DoubleToStr(i,0); 
    Object names must be unique. When a new bar forms, you will be (potentially) reusing names. Time is unique.

  3. ma1=iMA(NULL,0,7,0,MODE_SMMA,PRICE_MEDIAN,i); 
    Never any reason to use the SMMA(L) it's equivalent to the EMA(2L-1).
              The Smoothed Moving Average or SMMA - How to Avoid It - NinjaTrader Programming | futures.io
 
Taskin Osman:



its working on my terminal

hello do you have this indicator please ?

 best regards

 
  1. [Deleted]: I just want to draw an VLine (vertical line) at each ma cross on my charts.

    Can't be done on MT4. A cross is between two candles and Vline is on a candle.

  2. barrondudow: do you have this indicator please ?
    Did you bother to read the entire thread? The code was given in #1
 
William Roeder:
  1. Never any reason to use the SMMA(L) it's equivalent to the EMA(2L-1).            The Smoothed Moving Average or SMMA - How to Avoid It - NinjaTrader Programming

Thanks, it is true!