Hello fellow traders I was wondering if @mrtools, @Kvak or any other coderscould help with this code for identifying wedges. I'm no coder and was wondering if at all possible that either of you could take a look and see if it can be completed. Here is the code below....
Code: Select all
/+------------------------------------------------------------------+
//| Wedge Detector.mq4 |
//| ChatGPT |
//| 2023-03-16 |
//+------------------------------------------------------------------+
#property indicator_chart_window
#property indicator_buffers 2
#property indicator_color1 Red // Bearish Wedge
#property indicator_color2 LimeGreen // Bullish Wedge
//--- input parameters
extern int WedgePeriod = 14; // Period for calculating the wedges
extern double WedgeSlope = 0.05; // Minimum slope required for a valid wedge
//--- buffers
double BearishWedgeBuffer[];
double BullishWedgeBuffer[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init()
{
//--- indicator buffers mapping
SetIndexBuffer(0, BearishWedgeBuffer);
SetIndexStyle(0, DRAW_ARROW, STYLE_SOLID, 2);
SetIndexArrow(0, 233);
SetIndexLabel(0, "Bearish Wedge");
SetIndexBuffer(1, BullishWedgeBuffer);
SetIndexStyle(1, DRAW_ARROW, STYLE_SOLID, 2);
SetIndexArrow(1, 234);
SetIndexLabel(1, "Bullish Wedge");
//---
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function |
//+------------------------------------------------------------------+
int start()
{
int i, limit;
double trend1, trend2, slope1, slope2, slope3, slope4, high, low;
//--- determine the limit of bars to be calculated
limit = Bars - WedgePeriod - 1;
if (limit < 1) return(0);
//--- main loop
for (i = limit; i >= 0; i--)
{
//--- calculate the trend lines
trend1 = iMA(NULL, 0, WedgePeriod, 0, MODE_EMA, PRICE_HIGH, i);
trend2 = iMA(NULL, 0, WedgePeriod, 0, MODE_EMA, PRICE_LOW, i);
slope1 = (iHigh(NULL, 0, i + WedgePeriod) - trend1) / WedgePeriod;
slope2 = (trend1 - iLow(NULL, 0, i + WedgePeriod)) / WedgePeriod;
slope3 = (iLow(NULL, 0, i + WedgePeriod) - trend2) / WedgePeriod;
slope4 = (trend2 - iHigh(NULL, 0, i + WedgePeriod)) / WedgePeriod;
high = iHigh(NULL, 0, i);
low = iLow(NULL, 0, i);
//--- detect bullish wedge
if (slope1 < -WedgeSlope && slope2 > WedgeSlope && high > trend1 && low > trend2)
{
BullishWedgeBuffer[i] = low - 10 * Point;
}
else
{
BullishWedgeBuffer[i] = EMPTY_VALUE;
}
//--- detect bearish wedge
if (slope3 > WedgeSlope && slope4 < -WedgeSlope && high < trend2 && low < trend1)
{
BearishWedgeBuffer[i] = high + 10 * Point;
}
else
{
BearishWedgeBuffer[i] =
// Bullish Rising Wedge Indicator
double A, B, C, D, E, F, G, H, I, J, K, L, M, N;
double x1, y1, x2, y2, x3, y3, x4, y4, x5, y5, x6, y6;
double SlopeAB, SlopeCD, SlopeEF, SlopeGH, SlopeIJ;
double p1, p2, p3, p4, p5, p6;
int i, shift;
void OnInit()
{
SetIndexStyle(0,DRAW_LINE);
SetIndexBuffer(0,NULL);
}
void 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[])
{
shift = Bars-IndicatorCounted();
for(i=shift; i>=0; i--)
{
A = High[i+2];
B = Time[i+2];
C = High[i+1];
D = Time[i+1];
E = High[i];
F = Time[i];
G = Low[i+2];
H = Low[i+1];
I = Low[i];
J = High[i+3];
K = High[i+4];
L = Low[i+3];
M = Low[i+4];
N = Low[i+5];
SlopeAB = (A-C)/(B-D);
SlopeCD = (C-E)/(D-F);
SlopeEF = (G-I)/(F-H);
SlopeGH = (G-K)/(H-J);
SlopeIJ = (L-N)/(J-M);
if(SlopeAB<SlopeCD && SlopeEF<SlopeGH && SlopeGH<SlopeIJ && MathAbs(SlopeAB)>MathAbs(SlopeEF))
{
x1 = B;
y1 = A;
x2 = D;
y2 = C;
x3 = F;
y3 = E;
x4 = H;
y4 = G;
x5 = J;
y5 = K;
x6 = M;
y6 = L;
p1 = iCustom(NULL,0,"Point",x1,y1);
p2 = iCustom(NULL,0,"Point",x2,y2);
p3 = iCustom(NULL,0,"Point",x3,y3);
p4 = iCustom(NULL,0,"Point",x4,y4);
p5 = iCustom(NULL,0,"Point",x5,y5);
p6 = iCustom(NULL,0,"Point",x6,y6);
if(p1!=0 && p2!=0 && p3!=0 && p4!=0 && p5!=0 && p6!=0)
{
SetIndexShift(0,shift);
IndicatorBuffer[0][shift] = 0;
for(int j=0;j<=6;j++)
{
ObjectCreate("line"+j,OBJ_TREND,0,x1-j*(x1-x6)/6,y1+j*(y6-y1)/6,x6-j*(x1-x6)/6,y6+j*(y6-y1)/6);
ObjectSet("line"+j,
#property indicator_chart_window
extern int wedge_size = 20;
extern int look_back = 100;
void find_wedges()
{
double top[]; // array for wedge tops
double bottom[]; // array for wedge bottoms
int top_count = 0; // number of wedge tops found
int bottom_count = 0; // number of wedge bottoms found
for (int i = look_back; i >= wedge_size; i--)
{
double highest_high = High[i];
double lowest_low = Low[i];
int last_peak_index = 0;
int last_trough_index = 0;
bool is_rising_wedge = false;
bool is_falling_wedge = false;
// look for the highest high and lowest low within wedge size
for (int j = i; j >= i - wedge_size; j--)
{
if (High[j] > highest_high)
{
highest_high = High[j];
last_peak_index = j;
}
if (Low[j] < lowest_low)
{
lowest_low = Low[j];
last_trough_index = j;
}
}
// check if the wedge is rising or falling
if (highest_high - Low[last_trough_index] < High[last_peak_index] - lowest_low)
{
is_rising_wedge = true;
}
else
{
is_falling_wedge = true;
}
// check if the wedge has been broken
bool is_wedge_broken = false;
for (int j = last_peak_index; j >= last_trough_index; j--)
{
if (High[j] > highest_high || Low[j] < lowest_low)
{
is_wedge_broken = true;
break;
}
}
// add the wedge to the array if it hasn't been broken
if (!is_wedge_broken)
{
if (is_rising_wedge)
{
top[top_count] = highest_high;
bottom[bottom_count] = lowest_low;
top_count++;
bottom_count++;
}
else if (is_falling_wedge)
{
top[top_count] = highest_high;
bottom[bottom_count] = lowest_low;
top_count++;
bottom_count++;
}
}
}
// plot the wedges
for (int i = 0; i < top_count; i++)
{
ObjectCreate("Rising Wedge "+i, OBJ_TREND, 0, Time[top_count-i-1], top[i], Time[bottom_count-i-1], bottom[i]);
ObjectSet("Rising Wedge "+i, OBJPROP_COLOR, Green);
}
for (int i = 0; i < bottom_count; i++)
{
ObjectCreate("Falling Wedge "+i, OBJ_TREND, 0, Time[bottom_count-i-1], bottom[i], Time[top_count-i-1], top[i]);
ObjectSet("Falling Wedge "+i, OBJPROP_COLOR, Red);
}
}
int init()
{
return(0);
}
int start()
{
find_wedges();
return(0);
}
// --- indicator parameters
extern int LookBackBars=500; // Number of bars to look back for wedge formation
extern double WedgeThreshold=0.1; // Minimum slope of wedge pattern
extern color BullWedgeColor=Green; // Color of bullish wedge pattern
extern color BearWedgeColor=Red; // Color of bearish wedge pattern
// --- indicator buffers
double BullishWedge[];
double BearishWedge[];
// --- indicator initialization function
int init()
{
// --- indicator buffers mapping
SetIndexBuffer(0, BullishWedge);
SetIndexBuffer(1, BearishWedge);
// --- set indicator labels
SetIndexLabel(0, "Bullish Wedge");
SetIndexLabel(1, "Bearish Wedge");
// --- set indicator colors
SetIndexStyle(0, DRAW_ARROW, 0, 2, BullWedgeColor);
SetIndexStyle(1, DRAW_ARROW, 0, 2, BearWedgeColor);
return(0);
}
// --- indicator calculation function
int start()
{
// --- get the number of bars on the chart
int limit=Bars-LookBackBars-1;
// --- loop through the bars to find wedge patterns
for(int i=limit; i>=0; i--)
{
// --- calculate the slope of the wedge
double slope=(iMA(NULL, 0, 5, 0, MODE_LWMA, PRICE_CLOSE, i)-iMA(NULL, 0, 30, 0, MODE_LWMA, PRICE_CLOSE, i))/(30-5);
// --- check if the slope meets the threshold for a wedge pattern
if(MathAbs(slope)<=WedgeThreshold)
{
// --- check if the wedge is bullish or bearish
if(slope>0)
{
// --- set the bullish wedge buffer to the low of the wedge pattern
BullishWedge[i]=Low[i]-Point*2;
}
else
{
// --- set the bearish wedge buffer to the high of the wedge pattern
BearishWedge[i]=High[i]+Point*2;
}
}
}
return(0);
}