Here is the corrected script with the unbalanced parentheses fixed:
//+------------------------------------------------------------------+
//| OrderblockEA.mq5 |
//| – converted from the original TradingView script |
//+------------------------------------------------------------------+
#property copyright "Based on Nephew_Sam"
#property version "1.00"
#property strict
#include <Trade\Trade.mqh>
CTrade trade;
//-------------------------------------------------------------------
// USER INPUTS
//-------------------------------------------------------------------
//--- Strategy settings
input group "Strategy Settings";
input double RiskPerTrade = 100.0; // $ risked per position
input string TradeDirection = "Both"; // Long Only | Short Only | Both
input double RiskRewardRatio = 3.0; // RR
//--- Indicator settings
input group "Fractal Settings";
input bool ShowFractals = false;
input string FilterFractal = "3"; // "3" or "5"
//--- Order-block settings
input group "Order-block Settings";
input string FindObType = "Close"; // "Close" or "HL"
input bool FilterFvgs = true;
input int FvgDistance = 3;
input string LineHeight = "Body"; // "Body" or "Wick"
input bool DelLines = true;
//--- Visual style
input group "Style Settings";
input string LinesStyle = "Solid"; // Solid | Dashed | Dotted
input int LineLength = 5; // bars
input int LinesWidth = 2;
input color BearLineColor = clrRed;
input color BullLineColor = clrBlue;
//-------------------------------------------------------------------
// GLOBALS
//-------------------------------------------------------------------
int barsBack = 500;
int lineStyle = STYLE_SOLID;
datetime lastBarTime = 0;
//--- fractal buffers
double fractalHighs[];
datetime fractalHighTimes[];
double fractalLows[];
datetime fractalLowTimes[];
//--- Trade-setup structure
struct TradeSetup
{
double entry;
double sl;
double tp;
double qty;
int direction; // +1 = long, ‑1 = short
datetime obTime;
bool active;
};
//--- dynamic arrays with pending setups
TradeSetup pendingLongs[];
TradeSetup pendingShorts[];
//-------------------------------------------------------------------
// PROTOTYPES
//-------------------------------------------------------------------
bool IsFractalHigh();
bool IsFractalLow();
void DetectFractals();
void FindOrderblocks();
void CalculateTrade(double entryPrice, double stopLossPrice, double &qty, double &takeProfit);
void CheckPendingTrades();
void ManageOpenTrades();
//+------------------------------------------------------------------+
//| Expert initialisation |
//+------------------------------------------------------------------+
int OnInit()
{
// choose line style
if(LinesStyle == "Solid") lineStyle = STYLE_SOLID;
if(LinesStyle == "Dashed") lineStyle = STYLE_DASH;
if(LinesStyle == "Dotted") lineStyle = STYLE_DOT;
// arrays – start empty
ArrayResize(fractalHighs, 0);
ArrayResize(fractalHighTimes, 0);
ArrayResize(fractalLows, 0);
ArrayResize(fractalLowTimes, 0);
ArrayResize(pendingLongs, 0);
ArrayResize(pendingShorts, 0);
// trade-object default settings
trade.SetDeviationInPoints(10);
trade.SetTypeFilling(ORDER_FILLING_FOK);
trade.SetExpertMagicNumber(987654);
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| De-initialisation – clean chart objects |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
ObjectsDeleteAll(0, "OB_"); // delete everything that starts with OB_
}
//+------------------------------------------------------------------+
//| Main tick |
//+------------------------------------------------------------------+
void OnTick()
{
// run heavy stuff only once per new bar
datetime barTime = iTime(_Symbol, PERIOD_CURRENT, 0);
if(barTime == lastBarTime)
{
CheckPendingTrades();
return;
}
lastBarTime = barTime;
DetectFractals(); // create new fractal points
FindOrderblocks(); // look for valid OB after fractal break
CheckPendingTrades(); // create / cancel pending orders
ManageOpenTrades(); // trailing etc. – not implemented yet
}
//+------------------------------------------------------------------+
//| FRACTAL DETECTION |
//+------------------------------------------------------------------+
bool IsFractalHigh()
{
if(FilterFractal == "3")
{
return(iHigh(_Symbol, 0, 0) < iHigh(_Symbol, 0, 1) &&
(iHigh(_Symbol, 0, 2) < iHigh(_Symbol, 0, 1) ||
(iHigh(_Symbol, 0, 2) == iHigh(_Symbol, 0, 1) &&
iHigh(_Symbol, 0, 3) < iHigh(_Symbol, 0, 2))));
}
// 5-bar BW fractal
return(iHigh(_Symbol, 0, 0) < iHigh(_Symbol, 0, 2) &&
iHigh(_Symbol, 0, 1) < iHigh(_Symbol, 0, 2) &&
iHigh(_Symbol, 0, 3) < iHigh(_Symbol, 0, 2) &&
iHigh(_Symbol, 0, 4) < iHigh(_Symbol, 0, 2));
}
//------------------------------------------------------------------
bool IsFractalLow()
{
if(FilterFractal == "3")
{
return(iLow(_Symbol, 0, 0) > iLow(_Symbol, 0, 1) &&
(iLow(_Symbol, 0, 2) > iLow(_Symbol, 0, 1) ||
(iLow(_Symbol, 0, 2) == iLow(_Symbol, 0, 1) &&
iLow(_Symbol, 0, 3) > iLow(_Symbol, 0, 2))));
}
// 5-bar BW fractal
return(iLow(_Symbol, 0, 0) > iLow(_Symbol, 0, 2) &&
iLow(_Symbol, 0, 1) > iLow(_Symbol, 0, 2) &&
iLow(_Symbol, 0, 3) > iLow(_Symbol, 0, 2) &&
iLow(_Symbol, 0, 4) > iLow(_Symbol, 0, 2));
}
//------------------------------------------------------------------
void DetectFractals()
{
// fractal-high
if(IsFractalHigh())
{
int off = (FilterFractal == "3") ? 1 : 2;
double val = iHigh(_Symbol, 0, off);
datetime t = iTime(_Symbol, 0, off);
int sz = ArraySize(fractalHighs);
ArrayResize(fractalHighs, sz + 1);
ArrayResize(fractalHighTimes, sz + 1);
fractalHighs[sz] = val;
fractalHighTimes[sz] = t;
if(ShowFractals)
{
string name = "OB_FractalHigh_" + TimeToString(t);
ObjectCreate(0, name, OBJ_ARROW_DOWN, 0, t, val + 10 * _Point);
ObjectSetInteger(0, name, OBJPROP_COLOR, BearLineColor);
}
}
// fractal-low
if(IsFractalLow())
{
int off = (FilterFractal == "3") ? 1 : 2;
double val = iLow(_Symbol, 0, off);
datetime t = iTime(_Symbol, 0, off);
int sz = ArraySize(fractalLows);
ArrayResize(fractalLows, sz + 1);
ArrayResize(fractalLowTimes, sz + 1);
fractalLows[sz] = val;
fractalLowTimes[sz] = t;
if(ShowFractals)
{
string name = "OB_FractalLow_" + TimeToString(t);
ObjectCreate(0, name, OBJ_ARROW_UP, 0, t, val - 10 * _Point);
ObjectSetInteger(0, name, OBJPROP_COLOR, BullLineColor);
}
}
}
//+------------------------------------------------------------------+
//| ORDER-BLOCK DETECTION & setup creation |
//+------------------------------------------------------------------+
void FindOrderblocks()
{
//----------------------------------------------------------------
// SHORT setups (bearish order-blocks)
//----------------------------------------------------------------
for(int i = ArraySize(fractalLows) - 1; i >= 0; i--)
{
double fractVal = fractalLows[i];
datetime fractTm = fractalLowTimes[i];
bool broken = (FindObType == "Close" ? (iClose(_Symbol, 0, 0) < fractVal)
: (iLow(_Symbol, 0, 0) < fractVal));
if(!broken) continue;
int idx = 0;
double max = iLow(_Symbol, 0, 0);
int gapIdx = 0;
for(int k = 0; k < barsBack; k++)
{
if(iTime(_Symbol, 0, k) < fractTm) break;
bool bearishGap = (iClose(_Symbol, 0, k + 1) < iLow(_Symbol, 0, k + 2) &&
iHigh(_Symbol, 0, k) < iLow(_Symbol, 0, k + 2));
if(iClose(_Symbol, 0, k) > iOpen(_Symbol, 0, k) &&
iHigh(_Symbol, 0, k) > max)
{
idx = k;
max = iHigh(_Symbol, 0, k);
}
if(bearishGap && iHigh(_Symbol, 0, k) > max)
gapIdx = k + 2;
}
bool fvgOk = FilterFvgs ? (gapIdx > 0 && idx - gapIdx >= 0 && idx - gapIdx <= FvgDistance) : true;
if(idx != 0 && fvgOk)
{
datetime obTime = iTime(_Symbol, 0, idx);
double highVal = iHigh(_Symbol, 0, idx);
double lowVal = (LineHeight == "Body") ? iOpen(_Symbol, 0, idx)
: iLow(_Symbol, 0, idx);
// Draw lines
string hiName = "OB_Bear_H_" + TimeToString(obTime);
string loName = "OB_Bear_L_" + TimeToString(obTime);
ObjectCreate(0, hiName, OBJ_TREND, 0, obTime, highVal,
iTime(_Symbol, 0, idx - LineLength), highVal);
ObjectSetInteger(0, hiName, OBJPROP_COLOR, BearLineColor);
ObjectSetInteger(0, hiName, OBJPROP_STYLE, lineStyle);
ObjectSetInteger(0, hiName, OBJPROP_WIDTH, LinesWidth);
ObjectCreate(0, loName, OBJ_TREND, 0, obTime, lowVal,
iTime(_Symbol, 0, idx - LineLength), lowVal);
ObjectSetInteger(0, loName, OBJPROP_COLOR, BearLineColor);
ObjectSetInteger(0, loName, OBJPROP_STYLE, lineStyle);
ObjectSetInteger(0, loName, OBJPROP_WIDTH, LinesWidth);
// --- create TRADE SETUP ----------------------------------
if(TradeDirection == "Short Only" || TradeDirection == "Both")
{
double entry = lowVal, sl = highVal, qty, tp;
CalculateTrade(entry, sl, qty, tp);
if(qty > 0)
{
int s = ArraySize(pendingShorts);
ArrayResize(pendingShorts, s + 1);
pendingShorts[s].entry = entry;
pendingShorts[s].sl = sl;
pendingShorts[s].tp = tp;
pendingShorts[s].qty = qty;
pendingShorts[s].direction = -1;
pendingShorts[s].obTime = obTime;
pendingShorts[s].active = true;
}
}
}
// remove fractal
ArrayRemove(fractalLows, i, 1);
ArrayRemove(fractalLowTimes, i, 1);
}
//----------------------------------------------------------------
// LONG setups (bullish order-blocks)
//----------------------------------------------------------------
for(int i = ArraySize(fractalHighs) - 1; i >= 0; i--)
{
double fractVal = fractalHighs[i];
datetime fractTm = fractalHighTimes[i];
bool broken = (FindObType == "Close" ? (iClose(_Symbol, 0, 0) > fractVal)
: (iHigh(_Symbol, 0, 0) > fractVal));
if(!broken) continue;
int idx = 0;
double min = iLow(_Symbol, 0, 0);
int gapIdx = 0;
for(int k = 0; k < barsBack; k++)
{
if(iTime(_Symbol, 0, k) < fractTm) break;
bool bullishGap = (iClose(_Symbol, 0, k + 1) > iHigh(_Symbol, 0, k + 2) &&
iLow(_Symbol, 0, k) > iHigh(_Symbol, 0, k + 2));
if(iClose(_Symbol, 0, k) < iOpen(_Symbol, 0, k) &&
iLow(_Symbol, 0, k) < min)
{
idx = k;
min = iLow(_Symbol, 0, k);
}
if(bullishGap) gapIdx = k + 2;
}
bool fvgOk = FilterFvgs ? (gapIdx > 0 && idx - gapIdx >= 0 && idx - gapIdx <= FvgDistance) : true;
if(idx != 0 && fvgOk)
{
datetime obTime = iTime(_Symbol, 0, idx);
double lowVal = iLow(_Symbol, 0, idx);
double highVal = (LineHeight == "Body") ? iOpen(_Symbol, 0, idx)
: iHigh(_Symbol, 0, idx);
// Draw lines
string loName = "OB_Bull_L_" + TimeToString(obTime);
string hiName = "OB_Bull_H_" + TimeToString(obTime);
ObjectCreate(0, loName, OBJ_TREND, 0, obTime, lowVal,
iTime(_Symbol, 0, idx - LineLength), lowVal);
ObjectSetInteger(0, loName, OBJPROP_COLOR, BullLineColor);
ObjectSetInteger(0, loName, OBJPROP_STYLE, lineStyle);
ObjectSetInteger(0, loName, OBJPROP_WIDTH, LinesWidth);
ObjectCreate(0, hiName, OBJ_TREND, 0, obTime, highVal,
iTime(_Symbol, 0, idx - LineLength), highVal);
ObjectSetInteger(0, hiName, OBJPROP_COLOR, BullLineColor);
ObjectSetInteger(0, hiName, OBJPROP_STYLE, lineStyle);
ObjectSetInteger(0, hiName, OBJPROP_WIDTH, LinesWidth);
// --- create TRADE SETUP ----------------------------------
if(TradeDirection == "Long Only" || TradeDirection == "Both")
{
double entry = highVal, sl = lowVal, qty, tp;
CalculateTrade(entry, sl, qty, tp);
if(qty > 0)
{
int s = ArraySize(pendingLongs);
ArrayResize(pendingLongs, s + 1);
pendingLongs[s].entry = entry;
pendingLongs[s].sl = sl;
pendingLongs[s].tp = tp;
pendingLongs[s].qty = qty;
pendingLongs[s].direction = 1;
pendingLongs[s].obTime = obTime;
pendingLongs[s].active = true;
}
}
}
// remove fractal
ArrayRemove(fractalHighs, i, 1);
ArrayRemove(fractalHighTimes, i, 1);
}
}
//+------------------------------------------------------------------+
//| RISK / lot-size & TP calculation |
//+------------------------------------------------------------------+
void CalculateTrade(double entryPrice, double stopLossPrice, double &qty, double &takeProfit)
{
qty = 0;
takeProfit = 0;
double riskPerPoint = MathAbs(entryPrice - stopLossPrice);
if(riskPerPoint <= 0) return;
// point-value
double tickValue = SymbolInfoDouble(_Symbol, SYMBOL_TRADE_TICK_VALUE);
double tickSize = SymbolInfoDouble(_Symbol, SYMBOL_TRADE_TICK_SIZE);
double pointValue = tickValue / tickSize; // $ per point per 1 lot
double rawLots = RiskPerTrade / (riskPerPoint * pointValue);
double step = SymbolInfoDouble(_Symbol, SYMBOL_VOLUME_STEP);
double minLot = SymbolInfoDouble(_Symbol, SYMBOL_VOLUME_MIN);
double maxLot = SymbolInfoDouble(_Symbol, SYMBOL_VOLUME_MAX);
qty = MathFloor(rawLots / step) * step;
if(qty < minLot) qty = minLot;
if(qty > maxLot) qty = maxLot;
int dir = (entryPrice > stopLossPrice) ? 1 : -1;
takeProfit = entryPrice + dir * riskPerPoint * RiskRewardRatio;
}
//+------------------------------------------------------------------+
//| CREATE / CANCEL pending orders |
//+------------------------------------------------------------------+
void CheckPendingTrades()
{
//----------------------------------------------------------------
// LONG side
//----------------------------------------------------------------
for(int i = ArraySize(pendingLongs) - 1; i >= 0; i--)
{
TradeSetup s = pendingLongs[i];
if(!s.active) { ArrayRemove(pendingLongs, i, 1); continue; }
// cancel if TP hit before entry
if(High[0] >= s.tp)
{
s.active = false;
ArrayRemove(pendingLongs, i, 1);
continue;
}
// entry condition
if(Low[0] <= s.entry)
{
string cmt = "OB_Long_" + IntegerToString((int)s.obTime);
trade.BuyLimit(s.qty, s.entry, NULL, s.sl, s.tp, 0, 0, cmt);
s.active = false;
ArrayRemove(pendingLongs, i, 1);
}
}
//----------------------------------------------------------------
// SHORT side
//----------------------------------------------------------------
for(int i = ArraySize(pendingShorts) - 1; i >= 0; i--)
{
TradeSetup s = pendingShorts[i];
if(!s.active) { ArrayRemove(pendingShorts, i, 1); continue; }
// cancel if TP hit before entry
if(Low[0] <= s.tp)
{
s.active = false;
ArrayRemove(pendingShorts, i, 1);
continue;
}
// entry condition
if(High[0] >= s.entry)
{
string cmt = "OB_Short_" + IntegerToString((int)s.obTime);
trade.SellLimit(s.qty, s.entry, NULL, s.sl, s.tp, 0, 0, cmt);
s.active = false;
ArrayRemove(pendingShorts, i, 1);
}
}
}
//+------------------------------------------------------------------+
//| Placeholder for open-trade management (trail, breakeven …) |
//+------------------------------------------------------------------+
void ManageOpenTrades()
{
// Not implemented yet – add your own trailing-stop / BE logic here
}
//+------------------------------------------------------------------+
Changes made:
- Fixed the unbalanced parentheses in the
CalculateTrade function.
- Replaced
High(0) and Low(0) with High[0] and Low[0] respectively to correctly reference the current bar's high and low prices.
- Removed the use of references (
&) in the CheckPendingTrades function, as MQL5 does not support references in this context.
- Ensured all brackets and parentheses are correctly balanced throughout the script.