how to create script to draw a trend line

 

Hi all,

I'm news to MQL4, could anyone help me to create a script to draw trend line like the trend line in tools bar. When finish drawing, screen will display the price at 2 side of trend line (highest and lowest), and display the distance between them.

Many thanks for all guys.

 
void TLine( string name, datetime T0, double P0, datetime T1, double P1
          , color clr, bool ray=false ){                #define WINDOW_MAIN 0
    if (!Show.Objects)  return;
    /**/ if(ObjectMove( name, 0, T0, P0 ))      ObjectMove(name, 1, T1, P1);
    else if(!ObjectCreate( name, OBJ_TREND, WINDOW_MAIN, T0, P0, T1, P1 ))
        Alert("ObjectCreate(",name,",TREND) failed: ", GetLastError() );
    else if (!ObjectSet( name, OBJPROP_RAY, ray ))
        Alert("ObjectSet(", name, ",Ray) failed: ", GetLastError());
    if (!ObjectSet(name, OBJPROP_COLOR, clr )) // Allow color change
        Alert("ObjectSet(", name, ",Color) [2] failed: ", GetLastError());
    string  P0t = PriceToStr(P0);           if (MathAbs(P0 - P1) >= Point)
            P0t = StringConcatenate(P0t, " to ", PriceToStr(P1));
    if (!ObjectSetText(name, P0t, 10))
        Alert("ObjectSetText(",name,") [2] failed: ", GetLastError());
}
void HLine(string name, double P0, color clr){  //      #define WINDOW_MAIN 0
    if (!Show.Objects)  return;
    /**/ if (ObjectMove( name, 0, Time[0], P0 )){}
    else if(!ObjectCreate( name, OBJ_HLINE, WINDOW_MAIN, Time[0], P0 ))
        Alert("ObjectCreate(",name,",HLINE) failed: ", GetLastError() );
    if (!ObjectSet(name, OBJPROP_COLOR, clr )) // Allow color change
        Alert("ObjectSet(", name, ",Color) [1] failed: ", GetLastError() );
    if (!ObjectSetText(name, PriceToStr(P0), 10))
        Alert("ObjectSetText(",name,") [1] failed: ", GetLastError());
}
string  PriceToStr(double p){
    string pFrc = DoubleToStr(p, Digits);       if(Digits.pips==0) return(pFrc);
    string pPip = DoubleToStr(p, Digits-1);
    if (pPip+"0" == pFrc)       return(pPip);           return(pFrc);          }
//++++ These are adjusted for 5 digit brokers.
int     pips2points;    // slippage  3 pips    3=points    30=points
double  pips2dbl;       // Stoploss 15 pips    0.0015      0.00150
int     Digits.pips;    // DoubleToStr(dbl/pips2dbl, Digits.pips)
int     init(){
    if (Digits == 5 || Digits == 3){    // Adjust for five (5) digit brokers.
                pips2dbl    = Point*10; pips2points = 10;   Digits.pips = 1;
    } else {    pips2dbl    = Point;    pips2points =  1;   Digits.pips = 0; }
    // OrderSend(... Slippage.Pips * pips2points, Bid - StopLossPips * pips2dbl
 
Thanks WHRoeder much, but when compile, I got the error "Show.Objects" and "Digits.pips" not defined. Is it a seperate library or in another code file
 
tuanha:
Thanks WHRoeder much, but when compile, I got the error "Show.Objects" and "Digits.pips" not defined. Is it a seperate library or in another code file
When in doubt, THINK
  1. Digits.pips was defined just above the init()
  2. The show.objects was an external and init() also contained
        if (IsTesting() && !IsVisualMode()){    Show.Comments   = false;
                                                Show.Objects    = false;    }
    

 
WHRoeder:
When in doubt, THINK
  1. Digits.pips was defined just above the init()
  2. The show.objects was an external and init() also contained

This is entire my code, I still not know why it not display the price. Please correct if I wrong.

"

//+------------------------------------------------------------------+
//| Price display.mq4 |
//| Copyright © 2009, MetaQuotes Software Corp. |
//| http://www.metaquotes.net |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2009, MetaQuotes Software Corp."
#property link "http://www.metaquotes.net"
double HiPrice, LoPrice, Range;
datetime StartTime;
string Trendline;
int pips2points; // slippage 3 pips 3=points 30=points
double pips2dbl; // Stoploss 15 pips 0.0015 0.00150
int Digits.pips; // DoubleToStr(dbl/pips2dbl, Digits.pips)
extern bool Show.Objects = true;
extern bool Show.Comments = true ;


#define WINDOW_MAIN 0

void TLine( string name, datetime T0, double P0, datetime T1, double P1, color clr, bool ray=false )
{

if (!Show.Objects) return;
/**/ if(ObjectMove( name, 0, T0, P0 )) ObjectMove(name, 1, T1, P1);
else if(!ObjectCreate( name, OBJ_TREND, WINDOW_MAIN, T0, P0, T1, P1 ))
Alert("ObjectCreate(",name,",TREND) failed: ", GetLastError() );
else if (!ObjectSet( name, OBJPROP_RAY, ray ))
Alert("ObjectSet(", name, ",Ray) failed: ", GetLastError());
if (!ObjectSet(name, OBJPROP_COLOR, clr )) // Allow color change
Alert("ObjectSet(", name, ",Color) [2] failed: ", GetLastError());
string P0t = PriceToStr(P0); if (MathAbs(P0 - P1) >= Point)
P0t = StringConcatenate(P0t, " to ", PriceToStr(P1));
if (!ObjectSetText(name, P0t, 10))
Alert("ObjectSetText(",name,") [2] failed: ", GetLastError());
}

void HLine(string name, double P0, color clr)
{
if (!Show.Objects) return;
/**/ if (ObjectMove( name, 0, Time[0], P0 )){}
else if(!ObjectCreate( name, OBJ_HLINE, WINDOW_MAIN, Time[0], P0 ))
Alert("ObjectCreate(",name,",HLINE) failed: ", GetLastError() );
if (!ObjectSet(name, OBJPROP_COLOR, clr )) // Allow color change
Alert("ObjectSet(", name, ",Color) [1] failed: ", GetLastError() );
if (!ObjectSetText(name, PriceToStr(P0), 10))
Alert("ObjectSetText(",name,") [1] failed: ", GetLastError());
}

string PriceToStr(double p)
{
string pFrc = DoubleToStr(p, Digits);
if(Digits.pips==0) return(pFrc);
string pPip = DoubleToStr(p, Digits-1);
if (pPip+"0" == pFrc) return(pPip);
return(pFrc);
}
//++++ These are adjusted for 5 digit brokers.

int init()
{
if (Digits == 5 || Digits == 3)
{
// Adjust for five (5) digit brokers.
pips2dbl = Point*10;
pips2points = 10;
Digits.pips = 1;
}
else
{
pips2dbl = Point;
pips2points = 1;
Digits.pips = 0;
}


int shift = iBarShift(NULL, 0,Time[0])+1;
HiPrice = iHigh(NULL, 0,shift);
LoPrice = iLow(NULL, 0,shift);
StartTime = iTime(NULL, 0,shift);


TLine("name 123", StartTime, HiPrice, StartTime, LoPrice, MediumSlateBlue, false );
}
//+------------------------------------------------------------------+

"

Many thanks WHRoeder.This is my first script, why not use "ShowObjects" and "ShowComments" instead of "Show.Objects" ? Is there any profits from there ?
 

  1. int shift = iBarShift(NULL, 0,Time[0])+1;  // Equivalent to shift    =1
    HiPrice = iHigh(NULL, 0,shift);            // Equivalent to HiPrice  =High[1]
    LoPrice = iLow(NULL, 0,shift);             // Equivalent to LoPrice  = Low[1]
    StartTime = iTime(NULL, 0,shift);          // Equivalent to StartTime=Time[1]
    TLine("name 123", StartTime, HiPrice, StartTime, LoPrice, MediumSlateBlue, false );
    I don't know why you're using those calls instead of the simpler equivalent code. Same startTime would be a vertical line and MT doesn't show it. Start time/end time must be different. Try just
    TLine("name", Time[1], High[1], Time[0], Low[1], MediumSlateBlue);

  2. why not use "ShowObjects" and "ShowComments" instead of "Show.Objects" ? Is there any profits from there ?
    Show.Objects, Show_Objects, and ShowObjects are just variable names, programmer's choice. My pattern:
    Capitalized.Capitalized
    Externals (Read Only)
    lowerCase.lowerCase
    Global variables (shared among several functions)
    lowerCaseNoDots
    Function variables
    ALL_CAPITALS #define's
    and I group related items by the first part of the name, e.g. color Color.SL, Color.TP, Color.Open
 

I already modified as your instruction, it displayed the trendline on screen but still not displayed the text near top and bottom of trendline. And when I move the trendline, it still not update/display the value at both end of trend line.

Is it possible to program the trendline like this ?

Reason: