Download MetaTrader 5

Drawing Line Start from specific point

To add comments, please log in or register
fxtrader_
247
fxtrader_ 2015.10.17 21:58 
Please can someone help me to get what I need.
I know how to draw a Line in the chart, But my problem is how can I make it start from the point I need and drawing to the right side.

For example :

I need the line to start from the high of candle 3 and go to the right side to candle 0

Any help will be highly appreciated


honest_knave
Moderator
2406
honest_knave 2015.10.17 22:08  
FxTrader_:

I need the line to start from the high of candle 3 and go to the right side to candle 0


   string name="myline";
   ObjectCreate(0,name,OBJ_TREND,0,Time[2],High[2],Time[0],High[2]);
   ObjectSetInteger(0,name,OBJPROP_RAY,false);
fxtrader_
247
fxtrader_ 2015.10.17 22:12  
wow that's all, thank you very much...
fxtrader_
247
fxtrader_ 2015.10.17 22:46  
Sorry But i note that when i change the time frame the line disappear?
Also when I change the number in the time or from high to low nothing change.
honest_knave
Moderator
2406
honest_knave 2015.10.17 23:45  
FxTrader_:
Sorry But i note that when i change the time frame the line disappear?
Also when I change the number in the time or from high to low nothing change.

Well, it was just the most basic of code snippets. You'll need to show your code about how you're implementing it.

Simple example:

#property strict
#property indicator_chart_window

string name="MyLine";

int OnInit()
  {
   return(INIT_SUCCEEDED);
  }


void OnDeinit(const int reason)
  {
   ObjectDelete(name);
  }


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[])
  {
   static bool IsDrawn=false;
   if(!IsDrawn)
     {
      ObjectCreate(0,name,OBJ_TREND,0,Time[2],High[2],Time[0],High[2]);
      ObjectSetInteger(0,name,OBJPROP_RAY,false);
      IsDrawn=true;
     }
   return(rates_total);
  }
Keith Watford
Moderator
9640
Keith Watford 2015.10.18 00:41  
FxTrader_:
Sorry But i note that when i change the time frame the line disappear?
Also when I change the number in the time or from high to low nothing change.

Does it disappear or is it simply outside of the chart range?

Check Objects list for the chart (Ctrl B)

If you draw a line on the H1 chart from 04:00 to 07:00 and then change to H4, the line is still there, but you can't see it as a line because it is totally contained within the 04:00 H4 candle.

Unless you delete the object in OnDeinit, changing the code will do nothing. This is because the object already exists when you try to create it again. You cannot create an object with the same name as one that already exists.

Check whether the object exists and if it does, move it with ObjectMove() or ObjectSet(). 

Keith Watford
Moderator
9640
Keith Watford 2015.10.18 00:45  
honest_knave:

Well, it was just the most basic of code snippets. You'll need to show your code about how you're implementing it.

Simple example:

 

   static bool IsDrawn=false;
   if(!IsDrawn)
     {
      ObjectCreate(0,name,OBJ_TREND,0,Time[2],High[2],Time[0],High[2]);
      ObjectSetInteger(0,name,OBJPROP_RAY,false);
      IsDrawn=true;
     }

 

 The only problem is that when an indicator is re-initialised, the static bool will be re-set to false.

My opinion, it is better to use ObjectFind() 

fxtrader_
247
fxtrader_ 2015.10.18 06:45  
Dears GumRai  and honest_knave,
Thank you very much for assistance.
actually there is some limitation. as this line will be add for specific situation.

For example :
If the candle cross the moving average we will start draw the line in that candle in both side( Hi / Low ). and limit the length of it to  4-5 candle long.

1. The line need to be in the chart as long as the indicator attached to the chart for all across.
2.  specify which period the indicator will work in.
3. applity to change color of line

appreciate your help.
honest_knave
Moderator
2406
honest_knave 2015.10.18 09:00  
GumRai:

Unless you delete the object in OnDeinit, changing the code will do nothing. This is because the object already exists when you try to create it again. You cannot create an object with the same name as one that already exists.

Check whether the object exists and if it does, move it with ObjectMove() or ObjectSet(). 

Hello GumRai.

Lots of ways to skin the proverbial cat. Alternatively, you can just ObjectCreate() and let it fail if the object already exists. Set time and price separately from ObjectCreate() by using ObjectSetInteger() and ObjectSetDouble() on the next lines and you dispense with any conditional statements. They'll all work.

My example was just a very basic example of implementation. 

 

GumRai:

 The only problem is that when an indicator is re-initialised, the static bool will be re-set to false.

My opinion, it is better to use ObjectFind() 

 

The line gets deleted in OnDeinit() so the static needs to be reset to false on re-initialisation. Checking a bool every tick is much more efficient than evaluating ObjectFind() every tick, hence why I went that route. But both would work.

 

FxTrader_:

1. The line need to be in the chart as long as the indicator attached to the chart for all across.

 Hello FxTrader_

You need to delete the object in OnDeinit() if you want to remove the line when the indicator is removed - see my example before. The line will stay on the chart until you delete it, but as GumRai correctly pointed out you may not be able to see the line easily on all timeframes.

 

FxTrader_:

2.  specify which period the indicator will work in.

 This will specify which timeframes your line appears on

      ObjectSetInteger(0,name,OBJPROP_TIMEFRAMES,OBJ_PERIOD_M15|OBJ_PERIOD_H1);

 

You can check Period() before any calculations to restrict what timeframes the indicator works on. 

 

If you want to access data from other timeframes (basically make the indicator calculations independent of the chart timeframe), you should look into iBarShift(), iClose() etc 

 

FxTrader_:
3. applity to change color of line

 

      ObjectSetInteger(0,name,OBJPROP_COLOR,clrCyan);
fxtrader_
247
fxtrader_ 2015.10.18 09:54  
Thanks again, Your instruction help me alot.

But how to limit the length of the line.

For example:
I need it to draw above 5 candle and stop draw after fifth candle.
fxtrader_
247
fxtrader_ 2015.10.18 10:05  

This is the full code that i'm trying to add the line for.

Code you please help make it work..

#property strict
#property indicator_chart_window

         extern bool  AlertM1  = true;                  // M1 Alert
         extern bool  AlertM5  = true;                  // M5 Alert
         extern bool  AlertM15 = true;                  // M15 Alert
         extern bool  AlertM30 = true;                  // M30 Alert
         extern bool  AlertH1  = true;                  // H1 Alert
         extern bool  AlertH4  = true;                  // H4 Alert
         extern bool  AlertD1  = true;                  // D1 Alert
         extern bool  AlertW1  = true;                  // W1 Alert
         extern bool  AlertMN1 = true;                  // MN1 Alert
         
         extern bool  Alert_If_Touch_D1  = false;        // Alert If Touch D1
         extern bool  Alert_If_Touch_W1  = false;        // Alert If Touch W1
         extern bool  Alert_If_Touch_MN1 = false;        // Alert If Touch MN1

         extern int   MA_Period = 34;          // MA Period
         extern int   MA_Shift  = 0 ;          // MA Shift
         extern int   MA_Method = 0 ;          // 0=sma, 1=ema, 2=smma, 3=lwma
         extern int   MA_Price  = 0 ;          // 0=close, 1=open, 2=high, 3=low, 4=median, 5=typical, 6=weighted
         
         

         string name="MyLine";
         string name2="MyLine2";


int OnInit()
  {
   return(INIT_SUCCEEDED);
  }


void OnDeinit(const int reason)
  {
   ObjectDelete(name);
   
   
  }


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[])
  {
  
  
         double  Current_MA_H1    = iMA(Symbol(),PERIOD_H1,MA_Period,MA_Shift,MA_Method,MA_Price,0 ),
                 Previous_MA_1_H1   = iMA(Symbol(),PERIOD_H1,MA_Period,MA_Shift,MA_Method,MA_Price,1 ),
                 Previous_MA_2_H1   = iMA(Symbol(),PERIOD_H1,MA_Period,MA_Shift,MA_Method,MA_Price,2 ),
                 Previous_MA_3_H1   = iMA(Symbol(),PERIOD_H1,MA_Period,MA_Shift,MA_Method,MA_Price,3 );

  
  
  
  
   static bool IsDrawn=false;
   if(!IsDrawn)
     {
     
     
       double M1_BUY_CONDITION  = (Close[3] < Previous_MA_3_H1 && Close[2] > Previous_MA_2_H1 && Close[1] > Close[2] && Volume[0] ==1 );
       double M1_SELL_CONDITION = (Close[3] > Previous_MA_3_H1 && Close[2] < Previous_MA_2_H1 && Close[1] < Close[2] && Volume[0] ==1 );
             
         if (M1_BUY_CONDITION  ) 

            ObjectCreate(0,M1_BUY_CONDITION,OBJ_TREND,0,Time[0],High[0],Time[0],High[0]);
            ObjectSetInteger(0,M1_BUY_CONDITION,OBJPROP_RAY,false);
            ObjectSetInteger(0,M1_BUY_CONDITION,OBJPROP_TIMEFRAMES,OBJ_PERIOD_M15|OBJ_PERIOD_H1);
            ObjectSetInteger(0,M1_BUY_CONDITION,OBJPROP_COLOR,clrRed);
      

      IsDrawn=true;
     }
     
     
   return(rates_total);
  }
12
To add comments, please log in or register