How to draw a vertical line *but* keeps its position *related to* the screen

 

Hi there,

How could I draw a vertical line but keeps its position *related to* the screen?

So when dragging the candle lines this special line won't get moved and will stay at it's original drawn position.

Thanks.



 

Make an indicator that positions the line.

See:

WindowFirstVisibleBar()

WindowBarsPerChart()

From those functions and the position you want, you can determine which bar -- the time -- to which to move your line as you scroll (and refresh)

 
Thanks will give it a try~
 

I sort of get this working(the line is replaced with the arrow for simplicity), but for some reason the code put in start() will only execute once. (I put this for the indicator)

How can I make it execute on every tick?

The idea is quite simple -- create the arrow for the first time, then when panning the bars since it's a tick-based detection, the arrow will move accordingly.

#property indicator_chart_window
string SYMM_ARROW = "symm_arrow";

int start()
{
   if (FindObject(SYMM_ARROW) < 0)
   {
      ObjectCreate(SYMM_ARROW, OBJ_ARROW, 0, 0, 0);
      ObjectSet(SYMM_ARROW, OBJPROP_ARROWCODE, SYMBOL_ARROWUP);
   }
   ObjectMove(SYMM_ARROW, 0, Time[WindowFirstVisibleBar()-15], Close[WindowFirstVisibleBar()-15]);
   return(0);

}

int FindObject(string Name)
{
  if (StringLen(Name) <= 0)
    return(-1);
  else
    return(ObjectFind(Name));
}
 

You seem to be moving your arrow to a price that (on EURUSD for example) would currently be about 13,700 pips below zero.

Try

ObjectMove(SYMM_ARROW, 0, Time[WindowFirstVisibleBar()-15], Close[WindowFirstVisibleBar() -0.0015]);

 

Hi phy thanks for the reply :D

I believe my code is correct, since WindowFirstVisibleBar() will return the on-screen first available bar's index (an integer) and then offset back 15 (same as the Time[]), pointing to the Close position.

The only problem I have is the code just won't update per tick; it only executes once (when hitting F5 in MetaEditor) :(



 

My error, thinking in terms of price, not bar on the second parameter.

Your code seems to work fine in the tester.

 

How did you test it in the tester? It only works for EA doesn't it?

Or is there a way to simulate offline ticks?

(I start suspecting that the reason it doesn't work is due to the Close Market...)

 

Create a new EA, don't modify anything, name it Dummy.

Run it in visual mode. Slow it down with the slider.

Add an indicator, like Moving Average. It shows on the chart.

Add your indicator, it shows on the chart.

If you like, save a template, name it Dummy.

When Dummy EA is tested it will apply Dummy Template with your addtional settings.

 

Thanks for the step-by-step tips... they work :D

Really hope mt4 support user-events natively, using a event-driven system.

This will make this kind of probing way easier and more efficient.

 

In fact, Sector Positioning Relative to a Chart in the link below is what I'm looking for:

https://book.mql4.com/functions/objects

However seems that only Labels are native supported, and the scale won't get changed with the chart zoom unless done in code, bringing the same issue above.

The 'coordinates' can't be exchanged easily -- price/time vs window(x,y) in pixels.

Reason: