Help with my first MQL4 steps... SMAs and OBJ_VLINE

 

Hi all,

I'd like to code a simple indocator with draws a vertical line at the position where 2 SMAs intersect, but I have no clue how to... :(


Maybe somebody can help me out!

These are my current lines:

//| Custom indicator deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
//----

//----
return(0);
}
int Crossed (double line1, double line2)
{
static int last_direction = 0;
static int current_direction = 0;

if(line1>line2)current_direction = 1; //up
if(line1<line2)current_direction = 2; //down


if (current_direction != last_direction) //changed
{
last_direction = current_direction;
return (last_direction);
}
else
{
return (0);
}
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function |
//+------------------------------------------------------------------+
int start()
{
//----
int counted_bars=IndicatorCounted();
double shortSMA, longSMA;

//---- check for possible errors
if(counted_bars<0) return(-1);
//---- last counted bar will be recounted
if (counted_bars>0) counted_bars--;

int pos=Bars-counted_bars;


shortSMA = iMA(NULL,0,8,0,0,0,0);
longSMA = iMA(NULL,0,21,0,0,0,0);

int isCrossed = Crossed(shortSMA,longSMA);

if (isCrossed == 1) //blue vertical line;
if (isCrossed == 2) //red vertical line;



Thanks in advance for your effords!


Greetings
 

Please use this to post code . . . it makes it easier to read.

 
ATCDevil:
I'd like to code a simple indocator with draws a vertical line at the position where 2 SMAs intersect, but I have no clue how to... :(
  1. Use SRC
  2. No Slaves here, learn to code or pay someone. We're not going to code it FOR you. We are willing to HELP you.
  3. shortSMA = iMA(NULL,0,8,0,0,0,0);
    longSMA = iMA(NULL,0,21,0,0,0,0);
    Always use enumerations not numbers so the code becomes documented. Don't hard code numbers.
    extern int Length.Short =  8;
    extern int Length.Long  = 21;
    
    shortSMA    = iMA(NULL,0, Length.Short,0, MODE_SMA, PRICE_CLOSE, shift);
    longSMA     = iMA(NULL,0, Length.Long, 0, MODE_SMA, PRICE_CLOSE, shift);
    

  4. Code it as a normal indicator. That means NO STATICs
    for (int shift = MathMin(Bars - 1 - IndicatorCounted(), Bars-2); shift >= 0; shift--){
        shortSMA    = iMA(NULL,0, Length.Short,0, MODE_SMA, PRICE_CLOSE, shift);
        longSMA     = iMA(NULL,0, Length.Long, 0, MODE_SMA, PRICE_CLOSE, shift);
    
        shortSMApre = iMA(NULL,0, Length.Short,0, MODE_SMA, PRICE_CLOSE, shift+1);
        longSMApre  = iMA(NULL,0, Length.Long, 0, MODE_SMA, PRICE_CLOSE, shift+1);
        
        bool cross = (shortSMA-longSMA) * (shortSMAprev-longSMAprev) < 0;
        if (cross){
            if (shoftSMA > longSMA) Color clr = Blue; else clr = Red;
            string name = "SMAx" + TimeToStr(Time[shift], TIME_MINUTES);
            VLine(name, Time[shift], clr);
        }
    }
    =============
    void VLine(string name, datetime T0, color clr){        #define WINDOW_MAIN 0
        if (!Show.Objects)  return;
        /**/ if (ObjectMove( name, 0, T0, 0 )){}
        else if(!ObjectCreate( name, OBJ_VLINE, WINDOW_MAIN, T0, 0 ))
            Alert("ObjectCreate(",name,",VLINE) failed: ", GetLastError() );
        if (!ObjectSet(name, OBJPROP_COLOR, clr )) // Allow color change
            Alert("ObjectSet(", name, ",Color) [1] failed: ", GetLastError() );
        if (!ObjectSetText(name, TimeToStr(T0, TIME_MINUTES), 10))
            Alert("ObjectSetText(",name,") [1] failed: ", GetLastError());
    }
    x
Reason: