DislikedGood Day,
Please can you assist me with this Mt5 indicator that i am trying to code? its based on the below parameters:
1. Fair Value Gaps
2. Liquidity
3. Trend
4. Support and resistance
5. When all parameters are met, will display a Entry and Exit point based on 50 pips
Below is the code which has many errors when i try compilingIgnored
Inserted Code
//+------------------------------------------------------------------+
//| KG System
//| Combines Fair Value Gaps, Liquidity Zones, Support/Resistance, |
//| Trend filter and non-repainting entry/exit arrows with Entry/TP |
//| levels (TP set at 50 pips by default) |
//+------------------------------------------------------------------+
#property indicator_chart_window
#property indicator_buffers 2
#property indicator_plots 2
//--- Plot settings for entry/exit arrows
// Plot 0: Entry (up arrow), Plot 1: Exit (down arrow)
#property indicator_label1 "Entry Arrow"
#property indicator_type1 DRAW_ARROW
#property indicator_color1 clrGreen
#property indicator_style1 STYLE_SOLID
#property indicator_width1 2
#property indicator_arrow1 233 // Wingdings up arrow
#property indicator_label2 "Exit Arrow"
#property indicator_type2 DRAW_ARROW
#property indicator_color2 clrRed
#property indicator_style2 STYLE_SOLID
#property indicator_width2 2
#property indicator_arrow2 234 // Wingdings down arrow
//--- indicator buffers
double EntryArrowBuffer[];
double ExitArrowBuffer[];
//--- input parameters
input int TrendMAPeriod = 50; // Moving average period for trend detection
input int FVGLookback = 2; // Use three candles (current, plus 2 previous) for FVG detection
input double ArrowOffsetPoints = 10; // Offset for arrow placement (in points)
input int LiquidityPeriod = 50; // Period for average tick volume calculation
input double LiquidityMultiplier = 1.5; // Multiplier for liquidity detection
input int TakeProfitPips = 50; // Take profit in pips
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
//--- Indicator buffers mapping
SetIndexBuffer(0, EntryArrowBuffer, INDICATOR_DATA);
SetIndexBuffer(1, ExitArrowBuffer, INDICATOR_DATA);
//--- Initialize buffers with EMPTY_VALUE so no arrow is drawn by default
ArrayInitialize(EntryArrowBuffer, EMPTY_VALUE);
ArrayInitialize(ExitArrowBuffer, EMPTY_VALUE);
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
const int prev_calculated,
const int begin,
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[])
{
// Need enough bars for calculations
if(rates_total < TrendMAPeriod+10)
return(0);
// Determine pip value based on symbol digits.
int digits = (int)SymbolInfoInteger(_Symbol, SYMBOL_DIGITS);
double pipValue = _Point;
if(digits == 3 || digits == 5)
pipValue = _Point * 10;
else
pipValue = _Point;
//-------------------------------------------------------------------------
// 1. Trend Filter & Non-Repainting Entry/Exit Arrows with Entry/TP Labels
// (Using bullish/bearish crossover of Close vs. SMA)
//-------------------------------------------------------------------------
for(int i = rates_total - 2; i >= 1; i--)
{
// Calculate current and previous moving average values
double ma_current = iMA(NULL, 0, TrendMAPeriod, 0, MODE_SMA, PRICE_CLOSE, i);
double ma_previous = iMA(NULL, 0, TrendMAPeriod, 0, MODE_SMA, PRICE_CLOSE, i+1);
// Bullish Signal: Price crosses above MA
if(close[i] > ma_current && close[i+1] <= ma_previous)
{
double entryPrice = low[i] - ArrowOffsetPoints * _Point;
double TP = entryPrice + TakeProfitPips * pipValue;
EntryArrowBuffer[i] = entryPrice;
ExitArrowBuffer[i] = EMPTY_VALUE;
// Create or update text label showing Entry and TP
string objName = "EntryTP_Buy_" + IntegerToString(i);
if(ObjectFind(0, objName) < 0)
{
ObjectCreate(0, objName, OBJ_TEXT, 0, time[i], entryPrice);
ObjectSetInteger(0, objName, OBJPROP_COLOR, clrWhite);
ObjectSetInteger(0, objName, OBJPROP_FONTSIZE, 10);
ObjectSetString(0, objName, OBJPROP_TEXT, "Buy\nEntry: " +
DoubleToString(entryPrice, digits) + "\nTP: " + DoubleToString(TP, digits));
}
else
{
ObjectSetDouble(0, objName, OBJPROP_TIME, time[i]);
ObjectSetDouble(0, objName, OBJPROP_PRICE, entryPrice);
ObjectSetString(0, objName, OBJPROP_TEXT, "Buy\nEntry: " +
DoubleToString(entryPrice, digits) + "\nTP: " + DoubleToString(TP, digits));
}
}
// Bearish Signal: Price crosses below MA
else if(close[i] < ma_current && close[i+1] >= ma_previous)
{
double entryPrice = high[i] + ArrowOffsetPoints * _Point;
double TP = entryPrice - TakeProfitPips * pipValue;
ExitArrowBuffer[i] = entryPrice;
EntryArrowBuffer[i] = EMPTY_VALUE;
// Create or update text label showing Entry and TP
string objName = "EntryTP_Sell_" + IntegerToString(i);
if(ObjectFind(0, objName) < 0)
{
ObjectCreate(0, objName, OBJ_TEXT, 0, time[i], entryPrice);
ObjectSetInteger(0, objName, OBJPROP_COLOR, clrWhite);
ObjectSetInteger(0, objName, OBJPROP_FONTSIZE, 10);
ObjectSetString(0, objName, OBJPROP_TEXT, "Sell\nEntry: " +
DoubleToString(entryPrice, digits) + "\nTP: " + DoubleToString(TP, digits));
}
else
{
ObjectSetDouble(0, objName, OBJPROP_TIME, time[i]);
ObjectSetDouble(0, objName, OBJPROP_PRICE, entryPrice);
ObjectSetString(0, objName, OBJPROP_TEXT, "Sell\nEntry: " +
DoubleToString(entryPrice, digits) + "\nTP: " + DoubleToString(TP, digits));
}
}
else
{
EntryArrowBuffer[i] = EMPTY_VALUE;
ExitArrowBuffer[i] = EMPTY_VALUE;
}
}
//-------------------------------------------------------------------------
// 2. Fair Value Gaps (FVG) Detection
// Loop through candles and check a three-candle pattern.
//-------------------------------------------------------------------------
for(int i = rates_total - FVGLookback - 1; i >= 0; i--)
{
// Bullish FVG detection: low[i+2] > high[i]
if(low[i+2] > high[i])
{
string objName = "FVG_Bull_" + IntegerToString(i);
if(ObjectFind(0, objName) < 0)
{
ObjectCreate(0, objName, OBJ_RECTANGLE, 0, time[i], high[i], time[i+2], low[i+2]);
ObjectSetInteger(0, objName, OBJPROP_COLOR, clrAqua);
ObjectSetInteger(0, objName, OBJPROP_STYLE, STYLE_SOLID);
ObjectSetInteger(0, objName, OBJPROP_WIDTH, 1);
ObjectSetInteger(0, objName, OBJPROP_BACK, true);
}
else
{
ObjectSetDouble(0, objName, OBJPROP_TIME1, time[i]);
ObjectSetDouble(0, objName, OBJPROP_PRICE1, high[i]);
ObjectSetDouble(0, objName, OBJPROP_TIME2, time[i+2]);
ObjectSetDouble(0, objName, OBJPROP_PRICE2, low[i+2]);
}
}
// Bearish FVG detection: high[i+2] < low[i]
if(high[i+2] < low[i])
{
string objName = "FVG_Bear_" + IntegerToString(i);
if(ObjectFind(0, objName) < 0)
{
ObjectCreate(0, objName, OBJ_RECTANGLE, 0, time[i], low[i], time[i+2], high[i+2]);
ObjectSetInteger(0, objName, OBJPROP_COLOR, clrMagenta);
ObjectSetInteger(0, objName, OBJPROP_STYLE, STYLE_SOLID);
ObjectSetInteger(0, objName, OBJPROP_WIDTH, 1);
ObjectSetInteger(0, objName, OBJPROP_BACK, true);
}
else
{
ObjectSetDouble(0, objName, OBJPROP_TIME1, time[i]);
ObjectSetDouble(0, objName, OBJPROP_PRICE1, low[i]);
ObjectSetDouble(0, objName, OBJPROP_TIME2, time[i+2]);
ObjectSetDouble(0, objName, OBJPROP_PRICE2, high[i+2]);
}
}
}
//-------------------------------------------------------------------------
// 3. Support and Resistance Detection (using a simple fractal method)
//-------------------------------------------------------------------------
for(int i = 1; i < rates_total - 1; i++)
{
// Resistance detection: candle high greater than its neighbors
if(high[i] > high[i-1] && high[i] > high[i+1])
{
string resName = "Resistance_" + IntegerToString(i);
if(ObjectFind(0, resName) < 0)
{
ObjectCreate(0, resName, OBJ_HLINE, 0, time[i], high[i]);
ObjectSetInteger(0, resName, OBJPROP_COLOR, clrRed);
ObjectSetInteger(0, resName, OBJPROP_STYLE, STYLE_DOT);
}
else
{
ObjectSetDouble(0, resName, OBJPROP_PRICE, high[i]);
}
}
// Support detection: candle low lower than its neighbors
if(low[i] < low[i-1] && low[i] < low[i+1])
{
string supName = "Support_" + IntegerToString(i);
if(ObjectFind(0, supName) < 0)
{
ObjectCreate(0, supName, OBJ_HLINE, 0, time[i], low[i]);
ObjectSetInteger(0, supName, OBJPROP_COLOR, clrGreen);
ObjectSetInteger(0, supName, OBJPROP_STYLE, STYLE_DOT);
}
else
{
ObjectSetDouble(0, supName, OBJPROP_PRICE, low[i]);
}
}
}
//-------------------------------------------------------------------------
// 4. Liquidity Zone Detection (using volume)
//-------------------------------------------------------------------------
double volSum = 0;
int volCount = 0;
for(int i = rates_total - LiquidityPeriod; i < rates_total; i++)
{
volSum += tick_volume[i];
volCount++;
}
double avgVolume = (volCount > 0 ? volSum/volCount : 0);
for(int i = rates_total - LiquidityPeriod; i < rates_total; i++)
{
if(tick_volume[i] > avgVolume * LiquidityMultiplier)
{
string liqName = "LiquidityZone_" + IntegerToString(i);
if(ObjectFind(0, liqName) < 0)
{
ObjectCreate(0, liqName, OBJ_ARROW, 0, time[i], high[i] + ArrowOffsetPoints * _Point);
ObjectSetInteger(0, liqName, OBJPROP_COLOR, clrYellow);
ObjectSetInteger(0, liqName, OBJPROP_ARROWCODE, 159); // circle-like arrow code
}
else
{
ObjectSetDouble(0, liqName, OBJPROP_TIME, time[i]);
ObjectSetDouble(0, liqName, OBJPROP_PRICE, high[i] + ArrowOffsetPoints * _Point);
}
}
}
return(rates_total);
}
//+------------------------------------------------------------------+ And you will leave pain to take care of itself.
1