horizontal_distance
Download scriptThis tool is designed to provide traders with real-time spatial awareness by calculating the exact distance between the current market price and specific historical price levels marked on a chart.
Core Functionality
The script operates by identifying horizontal lines that you have manually placed on your trading chart. Instead of requiring you to perform manual calculations, it automatically scans these lines and performs the following actions:
- Automated Detection: The tool looks for any horizontal lines you have drawn to represent key levels (such as support, resistance, or target prices).
- Dynamic Distance Calculation: For every line detected, the script calculates the "gap" between that specific price level and the current market closing price.
- Visual Feedback: It places a text label directly above each horizontal line. This label clearly displays the distance in points, allowing you to see at a glance how far the current price is from your chosen targets or barriers.
User Experience
From a user perspective, the workflow is straightforward:
- Mark Your Levels: You place your horizontal lines on the chart where you identify important price zones.
- Instant Tracking: The script automatically populates those lines with "Distance" markers.
- Real-Time Updates: As the market moves, these distance values update automatically, providing a continuous measurement of how much room remains before the price hits your designated levels.
Clean Management
To ensure your chart remains organized and uncluttered, the script includes an automatic cleanup feature. When the tool is removed or deactivated from the chart, it identifies and deletes all the temporary text labels it created, leaving your original horizontal lines exactly as you placed them without any leftover "ghost" markings.
#property copyright "neo"
#property link "http://www.metaquotes.net"
#property indicator_chart_window
int init()
{
return(0);
}
int deinit()
{
int j,count;
int objtot=ObjectsTotal();
for (j=objtot-1; j>=0; j--)
{
if(StringSubstr(ObjectName(j),0,4)=="illu" && ObjectType(ObjectName(j))==OBJ_TEXT) ObjectDelete(ObjectName(j));
}
return(0);
}
int start()
{
double price1;
int j,count,k=0;
int objtot=ObjectsTotal();
string nameH[],null_string;
ArrayResize(nameH,objtot);
//----------------- filter out all horizontal lines
for (j=0; j<objtot; j++)
{
if(StringSubstr(ObjectName(j),0,9)=="Horizonta" && ObjectType(ObjectName(j))==OBJ_HLINE)
{
nameH[k]=ObjectName(j);
k ++;
}
}
if(k<1)
{
Comment("Need to add some horizonal lines !!! ");
return(0);
}
ArrayResize(nameH,k);
for(j=0; j<k; j++)
{
price1=ObjectGet(nameH[j],OBJPROP_PRICE1);
ObjectCreate("illu"+nameH[j], OBJ_TEXT, 0,Time[WindowFirstVisibleBar()-15],price1+8*Point );
ObjectSetText("illu"+nameH[j],"Distance: "+DoubleToStr(MathAbs(price1-Close[0])/Point,0),14,"Times New Roman",Orange);
}
return(0);
}
Comments