137 lines
5.4 KiB
Plaintext
137 lines
5.4 KiB
Plaintext
//+------------------------------------------------------------------+
|
|
//| VPattern.mq5 |
|
|
//| Copyright 2023, MetaQuotes Software Corp. |
|
|
//| https://www.metaquotes.net/ |
|
|
//+------------------------------------------------------------------+
|
|
#property copyright "Copyright 2023, MetaQuotes Software Corp."
|
|
#property link "https://www.metaquotes.net/"
|
|
#property version "1.00"
|
|
#property indicator_chart_window
|
|
#property indicator_buffers 4
|
|
#property indicator_plots 2
|
|
#property indicator_label1 "V Pattern Top"
|
|
#property indicator_type1 DRAW_ARROW
|
|
#property indicator_color1 clrGreen
|
|
#property indicator_width1 2
|
|
#property indicator_label2 "V Pattern Bottom"
|
|
#property indicator_type2 DRAW_ARROW
|
|
#property indicator_color2 clrRed
|
|
#property indicator_width2 2
|
|
|
|
//--- Input parameters
|
|
input int LookbackPeriod=20; // Bars to analyze for pattern
|
|
input double MinDeclinePercent=2.0; // Minimum decline percentage
|
|
input double MinRisePercent=2.0; // Minimum rise percentage
|
|
input bool ShowAlerts=true; // Show alert messages
|
|
input color ArrowUpColor=clrGreen; // Up arrow color
|
|
input color ArrowDownColor=clrRed; // Down arrow color
|
|
|
|
//--- Indicator buffers
|
|
double VTopBuffer[];
|
|
double VBottomBuffer[];
|
|
double TempHighBuffer[];
|
|
double TempLowBuffer[];
|
|
|
|
//+------------------------------------------------------------------+
|
|
//| Custom indicator initialization function |
|
|
//+------------------------------------------------------------------+
|
|
int OnInit()
|
|
{
|
|
//--- indicator buffers mapping
|
|
SetIndexBuffer(0, VTopBuffer, INDICATOR_DATA);
|
|
SetIndexBuffer(1, VBottomBuffer, INDICATOR_DATA);
|
|
SetIndexBuffer(2, TempHighBuffer, INDICATOR_CALCULATIONS);
|
|
SetIndexBuffer(3, TempLowBuffer, INDICATOR_CALCULATIONS);
|
|
|
|
//--- setting arrow codes
|
|
PlotIndexSetInteger(0, PLOT_ARROW, 233); // Up arrow
|
|
PlotIndexSetInteger(1, PLOT_ARROW, 234); // Down arrow
|
|
|
|
//--- setting arrow colors
|
|
PlotIndexSetInteger(0, PLOT_LINE_COLOR, ArrowUpColor);
|
|
PlotIndexSetInteger(1, PLOT_LINE_COLOR, ArrowDownColor);
|
|
|
|
//--- setting buffer values as EMPTY_VALUE
|
|
ArrayInitialize(VTopBuffer, EMPTY_VALUE);
|
|
ArrayInitialize(VBottomBuffer, EMPTY_VALUE);
|
|
|
|
//--- name for DataWindow and indicator subwindow label
|
|
IndicatorSetString(INDICATOR_SHORTNAME, "V-Pattern Detector ("+string(LookbackPeriod)+")");
|
|
|
|
return(INIT_SUCCEEDED);
|
|
}
|
|
//+------------------------------------------------------------------+
|
|
//| 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 minimum bars required
|
|
if(rates_total < LookbackPeriod)
|
|
return(0);
|
|
|
|
//--- start calculation from bar determined by prev_calculated
|
|
int start;
|
|
if(prev_calculated == 0)
|
|
start = LookbackPeriod;
|
|
else
|
|
start = prev_calculated - 1;
|
|
|
|
//--- main calculation loop
|
|
for(int i=start; i<rates_total && !IsStopped(); i++)
|
|
{
|
|
// Reset buffers
|
|
VTopBuffer[i] = EMPTY_VALUE;
|
|
VBottomBuffer[i] = EMPTY_VALUE;
|
|
|
|
// Find the lowest low in the lookback period
|
|
int lowestBar = iLowest(NULL, 0, MODE_LOW, LookbackPeriod, i-LookbackPeriod+1);
|
|
double lowestLow = low[lowestBar];
|
|
|
|
// Find the highest high before the lowest low
|
|
int highBeforeLow = iHighest(NULL, 0, MODE_HIGH, lowestBar-(i-LookbackPeriod+1)+1, i-LookbackPeriod+1);
|
|
double highestBefore = high[highBeforeLow];
|
|
|
|
// Find the highest high after the lowest low
|
|
int highAfterLow = iHighest(NULL, 0, MODE_HIGH, i-lowestBar, lowestBar+1);
|
|
double highestAfter = high[highAfterLow];
|
|
|
|
// Calculate percentage moves
|
|
double declinePercent = (highestBefore - lowestLow) / highestBefore * 100;
|
|
double risePercent = (highestAfter - lowestLow) / lowestLow * 100;
|
|
|
|
// Check if we have a V pattern
|
|
if(declinePercent >= MinDeclinePercent && risePercent >= MinRisePercent)
|
|
{
|
|
// Check if the low is in the middle third of the lookback period
|
|
int lowPosition = lowestBar - (i-LookbackPeriod+1);
|
|
if(lowPosition > LookbackPeriod/3 && lowPosition < 2*LookbackPeriod/3)
|
|
{
|
|
// Mark the pattern on chart
|
|
VTopBuffer[highBeforeLow] = high[highBeforeLow];
|
|
VBottomBuffer[lowestBar] = low[lowestBar];
|
|
VTopBuffer[highAfterLow] = high[highAfterLow];
|
|
|
|
// Show alert if enabled
|
|
if(ShowAlerts && i == rates_total-1)
|
|
{
|
|
string alertText = StringFormat("V-Pattern detected! Decline: %.2f%%, Rise: %.2f%%",
|
|
declinePercent, risePercent);
|
|
Alert(alertText);
|
|
Comment(alertText);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
return(rates_total);
|
|
}
|
|
//+------------------------------------------------------------------+ |