Understood! Instead of deleting the line at a break of structure candle close, you want to shorten the swing line so it ends exactly at the bar (candle) that closes beyond (breaks) it — i.e., the line is truncated at that bar’s time.
How to achieve this:
- Detect which candle closed beyond the swing line price.
- Adjust the horizontal trendline’s second point's time to that candle’s close time (instead of infinitely right).
- This way, the line extends from the swing candle up to the BOS candle, then ends.
Here is an updated version integrating this logic:
#property copyright "ChatGPT"
#property link ""
#property version "1.03"
#property strict
input int SwingLookBack = 5; // Bars to look on each side to detect swing
#define LINE_COLOR clrWhite
#define LINE_STYLE STYLE_SOLID
#define LINE_WIDTH 1
datetime lastBarTime = 0;
// Store swing prices and start times globally for line adjustment
double lastSwingHighPrice = 0.0;
datetime lastSwingHighTime = 0;
double lastSwingLowPrice = 0.0;
datetime lastSwingLowTime = 0;
bool IsSwingHigh(const MqlRates &rates[], int index, int lookBack)
{
double centerHigh = rates[index].high;
for(int i = index - lookBack; i <= index + lookBack; i++)
{
if(i < 0 || i >= ArraySize(rates)) continue;
if(i == index) continue;
if(rates[i].high >= centerHigh) return false;
}
return true;
}
bool IsSwingLow(const MqlRates &rates[], int index, int lookBack)
{
double centerLow = rates[index].low;
for(int i = index - lookBack; i <= index + lookBack; i++)
{
if(i < 0 || i >= ArraySize(rates)) continue;
if(i == index) continue;
if(rates[i].low <= centerLow) return false;
}
return true;
}
void DrawHorizontalRay(string name, double price, datetime timeStart, datetime timeEnd, bool rayRight, bool rayLeft)
{
if(ObjectFind(0, name) < 0)
{
ObjectCreate(0, name, OBJ_TREND, 0, timeStart, price, timeEnd, price);
}
else
{
ObjectMove(0, name, 0, timeStart, price);
ObjectMove(0, name, 1, timeEnd, price);
}
ObjectSetInteger(0, name, OBJPROP_COLOR, LINE_COLOR);
ObjectSetInteger(0, name, OBJPROP_STYLE, LINE_STYLE);
ObjectSetInteger(0, name, OBJPROP_WIDTH, LINE_WIDTH);
ObjectSetInteger(0, name, OBJPROP_BACK, false);
ObjectSetInteger(0, name, OBJPROP_RAY_RIGHT, rayRight);
ObjectSetInteger(0, name, OBJPROP_RAY_LEFT, rayLeft);
}
int OnInit()
{
lastSwingHighPrice = 0.0;
lastSwingLowPrice = 0.0;
lastSwingHighTime = 0;
lastSwingLowTime = 0;
return(INIT_SUCCEEDED);
}
void OnDeinit(const int reason)
{
ObjectDelete(0, "SwingHighLine");
ObjectDelete(0, "SwingLowLine");
}
void OnTick()
{
MqlRates rates[];
int copied = CopyRates(_Symbol, _Period, 0, 100, rates);
if(copied <= 0)
return;
// Only on new bar
if(lastBarTime == rates[0].time)
return;
lastBarTime = rates[0].time;
int limit = copied - 1;
int recentSwingHighIndex = -1;
int recentSwingLowIndex = -1;
for(int i = limit - SwingLookBack; i >= 0 && i > limit - 50; i--)
{
if(IsSwingHigh(rates, i, SwingLookBack))
{
recentSwingHighIndex = i;
break;
}
}
for(int i = limit - SwingLookBack; i >= 0 && i > limit - 50; i--)
{
if(IsSwingLow(rates, i, SwingLookBack))
{
recentSwingLowIndex = i;
break;
}
}
// --------- Process Swing High Line ---------
if(lastSwingHighPrice != 0.0)
{
// Find first candle that closes above swing high price, starting from swing candle +1 to latest closed candle at rates[1]
int bosIndex = -1;
for(int i = recentSwingHighIndex - 1; i >= 1; i--)
{
if(rates[i].close > lastSwingHighPrice)
{
bosIndex = i;
break;
}
}
if(bosIndex != -1)
{
// Shorten line to the bar that closed beyond swing line price
DrawHorizontalRay("SwingHighLine", lastSwingHighPrice, lastSwingHighTime, rates[bosIndex].time, false, false);
}
else
{
// No BOS => ray to right infinite
datetime future = lastSwingHighTime + 60*60*24*365*10;
DrawHorizontalRay("SwingHighLine", lastSwingHighPrice, lastSwingHighTime, future, true, false);
}
}
else if(recentSwingHighIndex >= 0)
{
// No current swing high => draw new swing line
lastSwingHighPrice = rates[recentSwingHighIndex].high;
lastSwingHighTime = rates[recentSwingHighIndex].time;
datetime future = lastSwingHighTime + 60*60*24*365*10;
DrawHorizontalRay("SwingHighLine", lastSwingHighPrice, lastSwingHighTime, future, true, false);
}
else
{
// No swing high found, delete line
ObjectDelete(0, "SwingHighLine");
lastSwingHighPrice = 0.0;
}
// --------- Process Swing Low Line ---------
if(lastSwingLowPrice != 0.0)
{
// Find first candle that closes below swing low price, starting from swing candle +1 to latest closed candle at rates[1]
int bosIndex = -1;
for(int i = recentSwingLowIndex - 1; i >= 1; i--)
{
if(rates[i].close < lastSwingLowPrice)
{
bosIndex = i;
break;
}
}
if(bosIndex != -1)
{
// Shorten line to the bar that closed below swing low price
DrawHorizontalRay("SwingLowLine", lastSwingLowPrice, lastSwingLowTime, rates[bosIndex].time, false, false);
}
else
{
datetime future = lastSwingLowTime + 60*60*24*365*10;
DrawHorizontalRay("SwingLowLine", lastSwingLowPrice, lastSwingLowTime, future, true, false);
}
}
else if(recentSwingLowIndex >= 0)
{
lastSwingLowPrice = rates[recentSwingLowIndex].low;
lastSwingLowTime = rates[recentSwingLowIndex].time;
datetime future = lastSwingLowTime + 60*60*24*365*10;
DrawHorizontalRay("SwingLowLine", lastSwingLowPrice, lastSwingLowTime, future, true, false);
}
else
{
ObjectDelete(0, "SwingLowLine");
lastSwingLowPrice = 0.0;
}
}
Explanation of main changes:
Test this on your MT5 chart — you should now see:
- Swing lines starting from a swing candle
- Extending right until the candle that closes beyond the swing price
- No extension past the BOS candle.
If you want me to help add alerts on BOS, mark BOS candles, or other enhancements, just ask!