//--- Input parameters
input int InpPeriodADX = 14; // Period for ADX
input int InpPeriodATR = 14; // Period for ATR
input int InpPeriodBB = 20; // Period for Bollinger Bands
input double InpBBDeviation = 2.0; // Deviation for Bollinger Bands
input int InpPeriodRSI = 14; // Period for RSI
input int InpThresholdRSI = 10; // RSI Threshold range around 50 for consolidation
input ENUM_TIMEFRAMES InpTimeframe = PERIOD_CURRENT; // Timeframe
//--- Set indicator levels (Optional: Add threshold lines)
IndicatorSetInteger(INDICATOR_LEVELS, 2);
// Set other indicator levels if needed
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
//--- Release indicator handles
if (ATR_Handle != INVALID_HANDLE)
IndicatorRelease(ATR_Handle);
if (ADX_Handle != INVALID_HANDLE)
IndicatorRelease(ADX_Handle);
if (BB_Handle != INVALID_HANDLE)
IndicatorRelease(BB_Handle);
if (RSI_Handle != INVALID_HANDLE)
IndicatorRelease(RSI_Handle);
//--- Delete all rectangle objects created by this indicator
long total_objects = ObjectsTotal(0, -1, -1);
for (int i = total_objects - 1; i >= 0; i--)
{
string obj_name = ObjectName(0, i);
if (StringFind(obj_name, "Consolidation_", 0) != -1)
{
ObjectDelete(0, obj_name);
}
}
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
const int prev_calculated,
const datetime &time[],
const double &open[],
const double &high[],
const double &low[],
const double &close[],
const long &tick_volume[],
const long &volume[],
const int &spread[])
{
//--- Check for sufficient bars
int required_bars = MathMax(MathMax(InpPeriodADX, InpPeriodATR), MathMax(InpPeriodBB, InpPeriodRSI)) + 10; // Extra buffer
if (rates_total < required_bars)
return(0);
//--- Determine the start position for calculation
int start = 0;
if (prev_calculated > 0)
start = prev_calculated - 1;
else
start = 0; // Start from the beginning
//--- Declare dynamic threshold variables
double ADX_MA, ATR_MA;
int threshold_period = InpPeriodADX; // Example period for MA
//--- Calculate Moving Averages for dynamic thresholds
//--- ADX Moving Average
ADX_MA = 0.0;
if (CopyBuffer(ADX_Handle, 0, 0, threshold_period, ADXBuffer) > 0)
{
for (int i = 0; i < threshold_period; i++)
{
ADX_MA += ADXBuffer[i];
}
ADX_MA /= threshold_period;
}
else
{
Print("Failed to copy ADX buffer for MA calculation. Error: ", GetLastError());
return(prev_calculated);
}
//--- ATR Moving Average
ATR_MA = 0.0;
if (CopyBuffer(ATR_Handle, 0, 0, threshold_period, ATRBuffer) > 0)
{
for (int i = 0; i < threshold_period; i++)
{
ATR_MA += ATRBuffer[i];
}
ATR_MA /= threshold_period;
}
else
{
Print("Failed to copy ATR buffer for MA calculation. Error: ", GetLastError());
return(prev_calculated);
}
//--- Loop through each bar
for (int i = start; i < rates_total; i++)
{
//--- Calculate MPO
if (ATRBuffer[i] != 0.0)
MPOBuffer[i] = (ADXBuffer[i] / ATRBuffer[i]) / 100.0;
else
MPOBuffer[i] = 0.0;
code
//--- Assign Bollinger Bands
BBUpperBuffer[i] = BBUpperRaw[i];
BBLowerBuffer[i] = BBLowerRaw[i];
//--- Consolidation Logic:
// 1. MPO below a certain dynamic threshold (indicating low market phase)
// 2. Bollinger Bands are narrow (low volatility)
// 3. RSI is near the neutral level (e.g., between 50 - InpThresholdRSI/2 and 50 + InpThresholdRSI/2)
// 4. ADX below a dynamic threshold
bool isLowMPO = MPOBuffer[i] < (ADX_MA / ATR_MA) * 0.8; // Adjust multiplier as needed
bool isNarrowBB = (BBUpperRaw[i] - BBLowerRaw[i]) < ATR_MA * 0.5; // Example: BB width less than half ATR MA
bool isNeutralRSI = (RSIBuffer[i] > (50 - InpThresholdRSI / 2)) && (RSIBuffer[i] < (50 + InpThresholdRSI / 2));
bool isADXLow = ADXBuffer[i] < ADX_MA * 1.2; // Adjust multiplier as needed
bool isConsolidation = isLowMPO && isNarrowBB && isNeutralRSI && isADXLow;
if (isConsolidation)
{
if (!inConsolidation)
{
// Consolidation starts
inConsolidation = true;
consolidationStartTime = time[i];
consolidationHigh = high[i];
consolidationLow = low[i];
}
else
{
// Update high and low for the consolidation period
if (high[i] > consolidationHigh) consolidationHigh = high[i];
if (low[i] < consolidationLow) consolidationLow = low[i];
}
}
else
{
if (inConsolidation)
{
// Consolidation ends
datetime consolidationEndTime = time[i];
inConsolidation = false;
// Create rectangle object
string objName = "Consolidation_" + IntegerToString(consolidationStartTime);
ObjectDelete(0, objName); // Delete if already exists
bool created = ObjectCreate(0, objName, OBJ_RECTANGLE, 0,
consolidationStartTime, consolidationHigh,
consolidationEndTime, consolidationLow);
if (created)
{
// Set properties
ObjectSetInteger(0, objName, OBJPROP_COLOR, clrYellow);
ObjectSetInteger(0, objName, OBJPROP_STYLE, STYLE_SOLID);
ObjectSetInteger(0, objName, OBJPROP_WIDTH, 1);
ObjectSetInteger(0, objName, OBJPROP_FILL, true);
ObjectSetInteger(0, objName, OBJPROP_BACK, true); // Draw in background
ObjectSetInteger(0, objName, OBJPROP_TRANSPARENCY, 50); // Transparency 0-255
}
else
{
Print("Failed to create rectangle object. Error: ", GetLastError());
}
}
}
'OBJPROP_TRANSPARENCY' - undeclared identifier consolidation.mq5 313 45
'ObjectSetInteger' - no one of the overloads can be applied to the function call consolidation.mq5 313 16
2 errors, 1 warnings 3 2
The errors you're encountering in your MQL5 code are related to the OBJPROP_TRANSPARENCY identifier and the usage of the ObjectSetInteger function. Here's a detailed breakdown of the issues and how to resolve them:
Cause:
The OBJPROP_TRANSPARENCY property should be predefined in MQL5. However, if the compiler doesn't recognize it, it may be due to:
A typo in the property name.
Using an outdated version of the MQL5 compiler that doesn't support this property.
Missing necessary includes or definitions (though standard properties are typically available by default).
Solution:
Check for Typos:
Ensure that OBJPROP_TRANSPARENCY is spelled correctly in your code. MQL5 identifiers are case-insensitive, but any deviation in spelling can cause issues.
Update MetaTrader:
Make sure you are using the latest version of MetaTrader 5. Older versions might not recognize newer properties like OBJPROP_TRANSPARENCY.
Use Numeric Value as a Workaround:
If updating isn't feasible or doesn't resolve the issue, you can use the numeric value corresponding to OBJPROP_TRANSPARENCY. According to the MQL5 documentation, OBJPROP_TRANSPARENCY is defined as 73.
mql
ObjectSetInteger(0, objName, 73, 50); // 73 corresponds to OBJPROP_TRANSPARENCY
Define the Property Manually (Not Recommended):
As a last resort, you can manually define the property if it's missing. However, this is generally not recommended as it can lead to inconsistencies.
Error 2: 'ObjectSetInteger' - no one of the overloads can be applied to the function call
Cause:
This error suggests that the parameters passed to ObjectSetInteger do not match any of the function's expected signatures. The ObjectSetInteger function expects the following parameters:
mql
bool ObjectSetInteger(
long chart_id, // Chart identifier, usually 0 for the current chart
string object_name, // Name of the object
ENUM_OBJECT_PROPERTY property, // Property to set
long value // Value to assign to the property
);
Solution:
Ensure Correct Parameter Types:
Verify that each parameter passed to ObjectSetInteger matches the expected type:
chart_id: Should be a long. Using 0 for the current chart is correct.
object_name: Should be a string.
property: Should be an enumerated ENUM_OBJECT_PROPERTY value.
Alternative Function:
If the issue persists, consider using the ObjectSet function, which is more flexible but less type-safe.
mql
ObjectSet(0, objName, OBJPROP_TRANSPARENCY, 50);
Verify Object Type:
Ensure that the object type you're working with (OBJ_RECTANGLE in this case) supports the OBJPROP_TRANSPARENCY property. Most standard objects do, but custom or unsupported objects might not.
Final Corrected Code Snippet
Here's how you can modify the problematic section of your code to address both errors:
mql
// Create rectangle object
string objName = "Consolidation_" + IntegerToString(consolidationStartTime);
ObjectDelete(0, objName); // Delete if already exists
bool created = ObjectCreate(0, objName, OBJ_RECTANGLE, 0,
consolidationStartTime, consolidationHigh,
consolidationEndTime, consolidationLow);
if (created)
{
// Set properties
ObjectSetInteger(0, objName, OBJPROP_COLOR, clrYellow);
ObjectSetInteger(0, objName, OBJPROP_STYLE, STYLE_SOLID);
ObjectSetInteger(0, objName, OBJPROP_WIDTH, 1);
// For boolean properties, use ObjectSetInteger with 1 (true) or 0 (false)
ObjectSetInteger(0, objName, OBJPROP_FILL, 1); // true
ObjectSetInteger(0, objName, OBJPROP_BACK, 1); // true
// Set transparency (using numeric value if OBJPROP_TRANSPARENCY is undefined)
ObjectSetInteger(0, objName, OBJPROP_TRANSPARENCY, 50); // If OBJPROP_TRANSPARENCY is defined
// If not defined, use:
// ObjectSetInteger(0, objName, 73, 50);
}
else
{
Print("Failed to create rectangle object. Error: ", GetLastError());
}
Additional Recommendations
Use ObjectSetInteger for Integer Properties:
Properties like OBJPROP_COLOR, OBJPROP_STYLE, OBJPROP_WIDTH, and OBJPROP_TRANSPARENCY are integers and should be set using ObjectSetInteger.
Use ObjectSetBoolean for Boolean Properties:
Alternatively, for boolean properties like OBJPROP_FILL and OBJPROP_BACK, you can use ObjectSetBoolean for better type safety.
Always check the return value of object creation and setting functions to handle errors gracefully.
Optimization:
Consider minimizing the number of CopyBuffer calls by organizing your data fetching efficiently, especially for large datasets.
By addressing the undeclared OBJPROP_TRANSPARENCY identifier and ensuring correct usage of the ObjectSetInteger function, your indicator should compile without errors. If issues persist after these changes, please provide additional details or error messages for further assistance.