if not na(phLiquidity) and liquidityBool
liquidity newLiquidity = liquidity.new()
newLiquidity.value := high[liquidity_len]
newLiquidity.barStart := time[liquidity_len]
newLiquidity.barEnd := time
newLiquidity.broken := false
newLiquidity.liquidityLine := line.new(x1 = newLiquidity.barStart, y1 = newLiquidity.value, x2 = newLiquidity.barEnd, y2 = newLiquidity.value, xloc = xloc.bar_time, color = downColor, width = 1)
array.push(bearishLiquidity, newLiquidity)
if array.size(bearishLiquidity) > 7
deletedLiquidity = array.shift(bearishLiquidity)
deletedLiquidity.liquidityLine.delete()
if not na(plLiquidity) and liquidityBool
liquidity newLiquidity = liquidity.new()
newLiquidity.value := low[liquidity_len]
newLiquidity.barStart := time[liquidity_len]
newLiquidity.barEnd := time
newLiquidity.broken := false
newLiquidity.liquidityLine := line.new(x1 = newLiquidity.barStart, y1 = newLiquidity.value, x2 = newLiquidity.barEnd, y2 = newLiquidity.value, xloc = xloc.bar_time, color = upColor, width = 1)
array.push(bullishLiquidity, newLiquidity)
if array.size(bullishLiquidity) > 7
deletedLiquidity = array.shift(bullishLiquidity)
deletedLiquidity.liquidityLine.delete()
Assistant
User
I WANT ALL THIS , COMPLETE , TO WORK ON METATRADER5
Assistant
The provided Pine Script snippet is detecting liquidity sweeps on a chart by identifying pivot highs and pivot lows, then storing these as custom liquidity structures containing lines on the chart.
I see you have set up:
Input parameters under "Liquidity Settings" (toggle show, length of liquidity detection, colors).
A custom type liquidity that includes value, start/end time, line, broken status, and a label for sweep.
Two arrays holding bullish and bearish liquidity points.
Logic to detect pivot highs/lows and create liquidity lines on the chart.
Logic to keep only recent 7 liquidity lines and delete older ones' line objects.
Here are some suggestions/notes to improve the script or fix potential issues:
liquidity instance creation with liquidity.new()
PineScript doesn't have a constructor or new() method on user-defined types; you usually declare variables directly.
Instead of this:
liquidity newLiquidity = liquidity(
value = high[liquidity_len],
barStart = time[liquidity_len],
barEnd = time,
liquidityLine = line.new( ... ),
broken = false,
sweep = na // Initialize to na if necessary
)
Handling the label sweep field
You declare the field in the type but never assign to or manipulate it. If you intend to create labels identifying the sweep, you need to create and manage them in the code. Otherwise, leave it out or handle later.
Clarify your input parameters usage liquidity_len in the call ta.pivothigh(high, liquidity_len, liquidity_len) means the number of bars to the left and right of the high pivot to confirm it. Typical values are 5-15, so OK.
Use var for persistent arrays, check for scope
You used var array<liquidity> to create persistent containers — perfect.
Line Deletion
When you remove older liquidity objects, you delete their line with:
pinescript
deletedLiquidity.liquidityLine.delete()
This works, but ensure that liquidityLine is always a valid line object (not na) to avoid errors.
Optional: Drawing labels or alerts on liquidity sweeps
Performance or memory
You keep only the last 7 liquidity objects to limit overhead, which is reasonable.
Corrected Example (simplified and with proper liquidity struct creation):
pinescript
//@version=5
indicator("Liquidity Sweeps", overlay=true)
// === LIQUIDITY SETTINGS ===
liquidityBool = input.bool(true, 'Show Liquidity Sweeps', group = 'Liquidity Settings')
liquidity_len = input.int(15, 'Liquidity Length', minval=5, group = 'Liquidity Settings')
upColor = input.color(color.new(color.yellow, 15), title='Bullish Liquidity Color', group = 'Liquidity Settings')
downColor = input.color(color.new(color.yellow, 15), title='Bearish Liquidity Color', group = 'Liquidity Settings')
// === DATA STRUCTURES ===
type liquidity
float value
int barStart
int barEnd
line liquidityLine
bool broken
label sweep
var array<liquidity> bullishLiquidity = array.new<liquidity>()
var array<liquidity> bearishLiquidity = array.new<liquidity>()
// === LIQUIDITY DETECTION ===
phLiquidity = ta.pivothigh(high, liquidity_len, liquidity_len)
plLiquidity = ta.pivotlow(low, liquidity_len, liquidity_len)
if not na(phLiquidity) and liquidityBool
line phLine = line.new(x1=time[liquidity_len], y1=high[liquidity_len], x2=time, y2=high[liquidity_len], xloc=xloc.bar_time, color=downColor, width=1)
liquidity newLiquidity = liquidity(
value = high[liquidity_len],
barStart = time[liquidity_len],
barEnd = time,
liquidityLine = phLine,
broken = false,
sweep = na
)
array.push(bearishLiquidity, newLiquidity)
if array.size(bearishLiquidity) > 7
liquidity oldLiquidity = array.shift(bearishLiquidity)
if oldLiquidity.liquidityLine
oldLiquidity.liquidityLine.delete()
if not na(plLiquidity) and liquidityBool
line plLine = line.new(x1=time[liquidity_len], y1=low[liquidity_len], x2=time, y2=low[liquidity_len], xloc=xloc.bar_time, color=upColor, width=1)
liquidity newLiquidity = liquidity(
value = low[liquidity_len],
barStart = time[liquidity_len],
barEnd = time,
liquidityLine = plLine,
broken = false,
sweep = na
)
array.push(bullishLiquidity, newLiquidity)
if array.size(bullishLiquidity) > 7
liquidity oldLiquidity = array.shift(bullishLiquidity)
if oldLiquidity.liquidityLine
oldLiquidity.liquidityLine.delete()
If you want to add functionality for marking broken liquidity lines (for example, detecting price crossing the line and marking broken := true) and plotting labels/sweeps, you can extend this later.
Let me know if you want help adding that logic or alerts!
Metatrader 5 (MT5) uses the MQL5 language (C++-like), not Pine Script, so this Pine Script code cannot run directly in MT5. You will need to translate your Pine Script liquidity sweeps indicator into MQL5 code.
What you want in MT5:
Detect pivot highs and pivot lows (liquidity points) using a lookback length parameter.
Store these liquidity points in arrays or object lists (with value, start time, end time).
Draw horizontal lines at liquidity values from barStart to current bar time.
Detect when these liquidity lines are "broken" (price crosses these levels).
Optionally label sweeps.
Clean up old lines to keep the chart clean.
How to do it in MQL5:
Below is a starting example of an MQL5 indicator that:
Inputs length and colors.
Detects pivot highs and lows.
Creates horizontal lines for bullish and bearish liquidity.
Keeps recent lines and deletes old lines.
Detects "broken" lines.
(For simplicity, this example uses bar indexes instead of time for x1/x2.)
mql5
//+------------------------------------------------------------------+
//| LiquiditySweeps.mq5 |
//| Sample Liquidity Sweep indicator |
//+------------------------------------------------------------------+
#property indicator_chart_window
//--- input parameters
input int LiquidityLength = 15;
input color BullishColor = clrYellow;
input color BearishColor = clrYellow;
input int MaxLines = 7;
//--- struct to hold liquidity data
struct Liquidity
{
double value;
int barStart;
int barEnd;
long lineHandle;
bool broken;
};
//--- global arrays holding liquidity points
Liquidity bullishLiquidity[];
Liquidity bearishLiquidity[];
//+------------------------------------------------------------------+
//| Find pivot high at bar i |
//+------------------------------------------------------------------+
bool IsPivotHigh(int i, int length)
{
double pivot = High[i];
for (int j = i - length; j <= i + length; j++)
{
if (j == i) continue;
if (j < 0 || j >= Bars) continue;
if (High[j] >= pivot) return false;
}
return true;
}
//+------------------------------------------------------------------+
//| Find pivot low at bar i |
//+------------------------------------------------------------------+
bool IsPivotLow(int i, int length)
{
double pivot = Low[i];
for (int j = i - length; j <= i + length; j++)
{
if (j == i) continue;
if (j < 0 || j >= Bars) continue;
if (Low[j] <= pivot) return false;
}
return true;
}
//+------------------------------------------------------------------+
//| Create new liquidity line |
//+------------------------------------------------------------------+
long CreateLiquidityLine(int barStart, int barEnd, double price, color clr)
{
string name = "liqLine_" + IntegerToString(barStart) + "_" + DoubleToString(price, _Digits);
// Draw a horizontal line from barStart to barEnd at price
// Since MT5 lines are objects, create a line object
if (ObjectFind(0, name) != -1) ObjectDelete(0, name); // delete if exists
long handle = ObjectCreate(0, name, OBJ_TREND, 0, Time[barStart], price, Time[barEnd], price);
ObjectSetInteger(0, name, OBJPROP_COLOR, clr);
ObjectSetInteger(0, name, OBJPROP_WIDTH, 1);
ObjectSetInteger(0, name, OBJPROP_RAY_RIGHT, false); // no ray right
ObjectSetInteger(0, name, OBJPROP_BACK, true);
return handle;
}
//+------------------------------------------------------------------+
//| Delete liquidity line |
//+------------------------------------------------------------------+
void DeleteLiquidityLine(long handle)
{
// Find the object by handle and delete by name
// MQL5 ObjectDelete by name only, so store names alternatively
// We'll track names in the struct
// But here for simplicity, find object by time and price from struct; better to store names explicitly
}
//+------------------------------------------------------------------+
//| Update liquidity lines and detect broken lines |
//+------------------------------------------------------------------+
void OnCalculateLiquidity()
{
int limit = Bars - LiquidityLength - 1; // safe limit for checking pivots
// Check new pivot highs and add
for (int i = limit; i >= limit - 5; i--) // check last few bars, decrease for realtime updates
{
if (IsPivotHigh(i, LiquidityLength))
{
double val = High[i];
// Create line from bar i to current bar
int startBar = i;
int endBar = 0; // current latest bar index (0)
string lineName = "bearishLine_" + IntegerToString(startBar);
if (ObjectFind(0, lineName) == -1) // Draw only if not exist
{
long handle = ObjectCreate(0, lineName, OBJ_TREND, 0, Time[startBar], val, Time[endBar], val);
ObjectSetInteger(0, lineName, OBJPROP_COLOR, BearishColor);
ObjectSetInteger(0, lineName, OBJPROP_WIDTH, 1);
// Add to array
Liquidity newL;
newL.value = val;
newL.barStart = startBar;
newL.barEnd = endBar;
newL.lineHandle = 0; // unused, can be replaced by object name
newL.broken = false;
ArrayResize(bearishLiquidity, ArraySize(bearishLiquidity) + 1);
bearishLiquidity[ArraySize(bearishLiquidity)-1] = newL;
// Manage max lines
if (ArraySize(bearishLiquidity) > MaxLines)
{
// Delete oldest line
string oldLineName = "bearishLine_" + IntegerToString(bearishLiquidity[0].barStart);
ObjectDelete(0, oldLineName);
// Remove first element from array
for (int idx=0; idx<ArraySize(bearishLiquidity)-1; idx++)
bearishLiquidity[idx] = bearishLiquidity[idx+1];
ArrayResize(bearishLiquidity, ArraySize(bearishLiquidity)-1);
}
}
}
}
// Check new pivot lows and add
for (int i = limit; i >= limit - 5; i--)
{
if (IsPivotLow(i, LiquidityLength))
{
double val = Low[i];
int startBar = i;
int endBar = 0; // current bar
string lineName = "bullishLine_" + IntegerToString(startBar);
if (ObjectFind(0, lineName) == -1)
{
long handle = ObjectCreate(0, lineName, OBJ_TREND, 0, Time[startBar], val, Time[endBar], val);
ObjectSetInteger(0, lineName, OBJPROP_COLOR, BullishColor);
ObjectSetInteger(0, lineName, OBJPROP_WIDTH, 1);
// Add to array
Liquidity newL;
newL.value = val;
newL.barStart = startBar;
newL.barEnd = endBar;
newL.lineHandle = 0;
newL.broken = false;
ArrayResize(bullishLiquidity, ArraySize(bullishLiquidity)+1);
bullishLiquidity[ArraySize(bullishLiquidity)-1] = newL;
// Manage max lines
if (ArraySize(bullishLiquidity) > MaxLines)
{
string oldLineName = "bullishLine_" + IntegerToString(bullishLiquidity[0].barStart);
ObjectDelete(0, oldLineName);
// Remove first element
for (int idx=0; idx<ArraySize(bullishLiquidity)-1; idx++)
bullishLiquidity[idx] = bullishLiquidity[idx+1];
ArrayResize(bullishLiquidity, ArraySize(bullishLiquidity)-1);
}
}
}
}
// Check if price broke any bullish liquidity lines (for example price closing below liquidity line value)
// Mark as broken by changing color or line style
for (int i=0; i<ArraySize(bullishLiquidity); i++)
{
if (!bullishLiquidity[i].broken)
{
if (Close[0] < bullishLiquidity[i].value)
{
bullishLiquidity[i].broken = true;
string lineName = "bullishLine_" + IntegerToString(bullishLiquidity[i].barStart);
ObjectSetInteger(0, lineName, OBJPROP_COLOR, clrRed);
ObjectSetInteger(0, lineName, OBJPROP_STYLE, STYLE_DOT);
}
}
}
// Similar check for bearish liquidity (if price closing above liquidity line)
for (int i=0; i<ArraySize(bearishLiquidity); i++)
{
if (!bearishLiquidity[i].broken)
{
if (Close[0] > bearishLiquidity[i].value)
{
bearishLiquidity[i].broken = true;
string lineName = "bearishLine_" + IntegerToString(bearishLiquidity[i].barStart);
ObjectSetInteger(0, lineName, OBJPROP_COLOR, clrRed);
ObjectSetInteger(0, lineName, OBJPROP_STYLE, STYLE_DOT);
}
}
}
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
const int prev_calculated,
const int begin,
const double &price[])
{
// On every tick or bar event update liquidity detection
OnCalculateLiquidity();
return(rates_total);
}
Notes:
Pivot detection is done by checking highs/lows against neighbors.
The limited loop (last 5 bars) is to detect pivots recently formed (adjust as needed).
lineHandle field is unused here since we managed objects by their names (based on bar index).
Lines are drawn from the pivot time to current bar. For actual times, use Time[] properly.
When price crosses liquidity level, the line changes color to red and style to dotted, indicating "broken".
Lines older than MaxLines are deleted and removed from the arrays.
This is an example and might need optimizing and adjustments for your exact use case.
Migration from PineScript to MQL5 involves reworking data structures, data access, and drawing methods using MQL5 native calls.
If you want, I can also help polish this example or add labels and alerts as per your request.
Feel free to ask for:
Full code with labels for sweeps.
Alerts when liquidity lines break.
Optimization tips for large datasets.
Integration with Expert Advisor trading actions.
In summary:
Your Pine approach concepts can be implemented in MQL5, but it requires rewriting logic with native syntax and APIs. The sample above is a solid basis to build your "liquidity sweeps" indicator on MT5.