0% found this document useful (0 votes)
30 views23 pages

Nowy Dokument Tekstowy462782

Uploaded by

xaviereqyt
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views23 pages

Nowy Dokument Tekstowy462782

Uploaded by

xaviereqyt
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 23

//+------------------------------------------------------------------+

//| ZigZagPAStrategy.mq5 |
//| Converted from Pine Script V4.1 |
//| |
//+------------------------------------------------------------------+
#property copyright "Copyright 2025"
#property link ""
#property version "1.00"
#property description "ZigZag Pattern Recognition Strategy"
#property indicator_chart_window
#property indicator_buffers 9
#property indicator_plots 9

// Indicator buffers
double ZigZagBuffer[];
double Fib0000Buffer[];
double Fib0236Buffer[];
double Fib0382Buffer[];
double Fib0500Buffer[];
double Fib0618Buffer[];
double Fib0764Buffer[];
double Fib1000Buffer[];
double EntryBuffer[];

// Input parameters
input bool UseHA = false; // Use Heikken Ashi Candles
input bool UseAltTF = true; // Use Alt Timeframe
input string AltTF = "H1"; // Alt Timeframe
input bool ShowPatterns = true; // Show Patterns
input bool ShowFib0000 = true; // Display Fibonacci 0.000
input bool ShowFib0236 = true; // Display Fibonacci 0.236
input bool ShowFib0382 = true; // Display Fibonacci 0.382
input bool ShowFib0500 = true; // Display Fibonacci 0.500
input bool ShowFib0618 = true; // Display Fibonacci 0.618
input bool ShowFib0764 = true; // Display Fibonacci 0.764
input bool ShowFib1000 = true; // Display Fibonacci 1.000

// Fixed lot size and take profit in pips


input double FixedLotSize = 0.1; // Fixed lot size for all trades
input int FixedTP = 20; // Fixed take profit in pips

// Global variables
double x, a, b, c, d;
double xab, xad, abc, bcd;
double fib_range;
int zigzag_handle;
bool new_pattern_detected = false;
string current_pattern = "";
bool is_bullish = false;

//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
// Initialize buffers
SetIndexBuffer(0, ZigZagBuffer, INDICATOR_DATA);
SetIndexBuffer(1, Fib0000Buffer, INDICATOR_DATA);
SetIndexBuffer(2, Fib0236Buffer, INDICATOR_DATA);
SetIndexBuffer(3, Fib0382Buffer, INDICATOR_DATA);
SetIndexBuffer(4, Fib0500Buffer, INDICATOR_DATA);
SetIndexBuffer(5, Fib0618Buffer, INDICATOR_DATA);
SetIndexBuffer(6, Fib0764Buffer, INDICATOR_DATA);
SetIndexBuffer(7, Fib1000Buffer, INDICATOR_DATA);
SetIndexBuffer(8, EntryBuffer, INDICATOR_DATA);

// Set buffer properties


PlotIndexSetInteger(0, PLOT_LINE_WIDTH, 2);
PlotIndexSetInteger(0, PLOT_LINE_COLOR, clrBlack);

// Label and colors for Fibonacci lines


PlotIndexSetString(1, PLOT_LABEL, "Fib 0.000");
PlotIndexSetInteger(1, PLOT_LINE_COLOR, clrBlack);

PlotIndexSetString(2, PLOT_LABEL, "Fib 0.236");


PlotIndexSetInteger(2, PLOT_LINE_COLOR, clrRed);

PlotIndexSetString(3, PLOT_LABEL, "Fib 0.382");


PlotIndexSetInteger(3, PLOT_LINE_COLOR, clrOlive);

PlotIndexSetString(4, PLOT_LABEL, "Fib 0.500");


PlotIndexSetInteger(4, PLOT_LINE_COLOR, clrLime);

PlotIndexSetString(5, PLOT_LABEL, "Fib 0.618");


PlotIndexSetInteger(5, PLOT_LINE_COLOR, clrTeal);

PlotIndexSetString(6, PLOT_LABEL, "Fib 0.764");


PlotIndexSetInteger(6, PLOT_LINE_COLOR, clrBlue);

PlotIndexSetString(7, PLOT_LABEL, "Fib 1.000");


PlotIndexSetInteger(7, PLOT_LINE_COLOR, clrBlack);

// Set indicator name


IndicatorSetString(INDICATOR_SHORTNAME, "ZigZag PA Strategy V4.1");

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 insufficient data
if(rates_total < 5) return(0);

int limit = rates_total - prev_calculated;


if(limit > rates_total - 5) limit = rates_total - 5;
// Calculate zigzag
CalculateZigZag(rates_total, limit, open, high, low, close);

// Calculate patterns
if(rates_total > 5)
{
for(int i = limit; i < rates_total; i++)
{
// Find zigzag points
int zigzagPoints[5] = {0};
int pointCount = 0;

for(int j = i; j >= 0 && pointCount < 5; j--)


{
if(ZigZagBuffer[j] != 0)
{
zigzagPoints[pointCount] = j;
pointCount++;
}
}

// Skip if we don't have enough zigzag points yet


if(pointCount < 5) continue;

// Assign X, A, B, C, D points
d = ZigZagBuffer[zigzagPoints[0]];
c = ZigZagBuffer[zigzagPoints[1]];
b = ZigZagBuffer[zigzagPoints[2]];
a = ZigZagBuffer[zigzagPoints[3]];
x = ZigZagBuffer[zigzagPoints[4]];

// Calculate ratios
xab = MathAbs(b-a)/MathAbs(x-a);
xad = MathAbs(a-d)/MathAbs(x-a);
abc = MathAbs(b-c)/MathAbs(a-b);
bcd = MathAbs(c-d)/MathAbs(b-c);

// Check for patterns


CheckPatterns(i);

// Calculate Fibonacci levels


fib_range = MathAbs(d-c);

// Set Fibonacci levels


if(ShowFib0000) Fib0000Buffer[i] = (d > c) ? d-(fib_range*0.000) : d+
(fib_range*0.000);
if(ShowFib0236) Fib0236Buffer[i] = (d > c) ? d-(fib_range*0.236) : d+
(fib_range*0.236);
if(ShowFib0382) Fib0382Buffer[i] = (d > c) ? d-(fib_range*0.382) : d+
(fib_range*0.382);
if(ShowFib0500) Fib0500Buffer[i] = (d > c) ? d-(fib_range*0.500) : d+
(fib_range*0.500);
if(ShowFib0618) Fib0618Buffer[i] = (d > c) ? d-(fib_range*0.618) : d+
(fib_range*0.618);
if(ShowFib0764) Fib0764Buffer[i] = (d > c) ? d-(fib_range*0.764) : d+
(fib_range*0.764);
if(ShowFib1000) Fib1000Buffer[i] = (d > c) ? d-(fib_range*1.000) : d+
(fib_range*1.000);
}
}

return(rates_total);
}

//+------------------------------------------------------------------+
//| Calculate ZigZag |
//+------------------------------------------------------------------+
void CalculateZigZag(const int rates_total, const int limit,
const double &open[], const double &high[],
const double &low[], const double &close[])
{
int direction = 0;

for(int i = limit; i < rates_total; i++)


{
bool isUp = close[i] >= open[i];
bool isDown = close[i] <= open[i];

if(i > 0)
{
// Logic for detecting zigzag points
if(isUp && isDown && direction != -1)
{
// Find highest in the last 2 bars
double highest = MathMax(high[i], high[i-1]);
ZigZagBuffer[i] = highest;
direction = -1;
}
else if(isDown && isUp && direction != 1)
{
// Find lowest in the last 2 bars
double lowest = MathMin(low[i], low[i-1]);
ZigZagBuffer[i] = lowest;
direction = 1;
}
else
{
ZigZagBuffer[i] = 0;
}
}
}
}

//+------------------------------------------------------------------+
//| Check for patterns |
//+------------------------------------------------------------------+
void CheckPatterns(int bar)
{
// Reset pattern detection
new_pattern_detected = false;
current_pattern = "";
is_bullish = false;

// Check bullish patterns


if(IsBullishABCD() || IsBullishBat() || IsBullishAltBat() ||
IsBullishButterfly() ||
IsBullishGartley() || IsBullishCrab() || IsBullishShark() || IsBullish5O() ||
IsBullishWolf() || IsBullishHnS() || IsBullishConTria() || IsBullishExpTria()
||
IsBullishAntiBat() || IsBullishAntiButterfly() || IsBullishAntiGartley() ||
IsBullishAntiCrab() || IsBullishAntiShark())
{
if(ShowPatterns)
{
CreatePatternLabel(bar, current_pattern, true);
}

// Check for entry conditions


CheckEntryConditions(bar, true);
}

// Check bearish patterns


if(IsBearishABCD() || IsBearishBat() || IsBearishAltBat() ||
IsBearishButterfly() ||
IsBearishGartley() || IsBearishCrab() || IsBearishShark() || IsBearish5O() ||

IsBearishWolf() || IsBearishHnS() || IsBearishConTria() || IsBearishExpTria()


||
IsBearishAntiBat() || IsBearishAntiButterfly() || IsBearishAntiGartley() ||
IsBearishAntiCrab() || IsBearishAntiShark())
{
if(ShowPatterns)
{
CreatePatternLabel(bar, current_pattern, false);
}

// Check for entry conditions


CheckEntryConditions(bar, false);
}
}

//+------------------------------------------------------------------+
//| Create pattern label |
//+------------------------------------------------------------------+
void CreatePatternLabel(int bar, string pattern, bool bullish)
{
if(new_pattern_detected)
{
string label_name = "Pattern_" + IntegerToString(bar);

if(ObjectFind(0, label_name) != -1)


ObjectDelete(0, label_name);

ObjectCreate(0, label_name, OBJ_TEXT, 0,


iTime(_Symbol, PERIOD_CURRENT, bar),
bullish ? iLow(_Symbol, PERIOD_CURRENT, bar) - 10*_Point :
iHigh(_Symbol, PERIOD_CURRENT, bar) + 10*_Point);

ObjectSetString(0, label_name, OBJPROP_TEXT, pattern);


ObjectSetInteger(0, label_name, OBJPROP_COLOR, bullish ? clrGreen :
clrMaroon);
ObjectSetInteger(0, label_name, OBJPROP_FONTSIZE, 10);
ObjectSetInteger(0, label_name, OBJPROP_ANCHOR, bullish ? ANCHOR_TOP :
ANCHOR_BOTTOM);
}
}
//+------------------------------------------------------------------+
//| Check entry conditions |
//+------------------------------------------------------------------+
void CheckEntryConditions(int bar, bool bullish)
{
double last_fib_entry1 = CalculateLastFib(0.236); // Always use 0.236 entry
level

if(bullish)
{
// Bullish entry condition
if(Close[bar] <= last_fib_entry1)
{
EntryBuffer[bar] = Low[bar] - 5*_Point;
if(bar == Bars-1) // Current bar
{
// Fixed lot size and 20 pips TP
double tp_price = Ask + FixedTP * _Point * 10;
double sl_price = 0; // No SL for now

ExecuteBuyOrder(FixedLotSize, tp_price, sl_price);


}
}
}
else
{
// Bearish entry condition
if(Close[bar] >= last_fib_entry1)
{
EntryBuffer[bar] = High[bar] + 5*_Point;
if(bar == Bars-1) // Current bar
{
// Fixed lot size and 20 pips TP
double tp_price = Bid - FixedTP * _Point * 10;
double sl_price = 0; // No SL for now

ExecuteSellOrder(FixedLotSize, tp_price, sl_price);


}
}
}
}

//+------------------------------------------------------------------+
//| Calculate Fibonacci level |
//+------------------------------------------------------------------+
double CalculateLastFib(double rate)
{
return (d > c) ? d-(fib_range*rate) : d+(fib_range*rate);
}

//+------------------------------------------------------------------+
//| Execute buy order |
//+------------------------------------------------------------------+
void ExecuteBuyOrder(double volume, double tp_level, double sl_level)
{
string comment = "buy " + current_pattern;

MqlTradeRequest request = {};


MqlTradeResult result = {};

request.action = TRADE_ACTION_DEAL;
request.symbol = _Symbol;
request.volume = volume;
request.type = ORDER_TYPE_BUY;
request.price = SymbolInfoDouble(_Symbol, SYMBOL_ASK);
request.tp = tp_level;
request.sl = sl_level;
request.deviation = 5;
request.type_filling = ORDER_FILLING_FOK;
request.comment = comment;

bool success = OrderSend(request, result);

if(!success)
Print("OrderSend error: ", GetLastError());
else
Print("Order placed successfully. Ticket: ", result.order);
}

//+------------------------------------------------------------------+
//| Execute sell order |
//+------------------------------------------------------------------+
void ExecuteSellOrder(double volume, double tp_level, double sl_level)
{
string comment = "sell " + current_pattern;

MqlTradeRequest request = {};


MqlTradeResult result = {};

request.action = TRADE_ACTION_DEAL;
request.symbol = _Symbol;
request.volume = volume;
request.type = ORDER_TYPE_SELL;
request.price = SymbolInfoDouble(_Symbol, SYMBOL_BID);
request.tp = tp_level;
request.sl = sl_level;
request.deviation = 5;
request.type_filling = ORDER_FILLING_FOK;
request.comment = comment;

bool success = OrderSend(request, result);

if(!success)
Print("OrderSend error: ", GetLastError());
else
Print("Order placed successfully. Ticket: ", result.order);
}

//+------------------------------------------------------------------+
//| Pattern recognition functions - FULL IMPLEMENTATION |
//+------------------------------------------------------------------+

//--- ABCD Pattern


bool IsBullishABCD()
{
bool _abc = abc >= 0.382 && abc <= 0.886;
bool _bcd = bcd >= 1.13 && bcd <= 2.618;
bool result = _abc && _bcd && (d < c);

if(result && !new_pattern_detected)


{
new_pattern_detected = true;
current_pattern = "AB=CD";
is_bullish = true;
}

return result;
}

bool IsBearishABCD()
{
bool _abc = abc >= 0.382 && abc <= 0.886;
bool _bcd = bcd >= 1.13 && bcd <= 2.618;
bool result = _abc && _bcd && (d > c);

if(result && !new_pattern_detected)


{
new_pattern_detected = true;
current_pattern = "AB=CD";
is_bullish = false;
}

return result;
}

//--- BAT Pattern


bool IsBullishBat()
{
bool _xab = xab >= 0.382 && xab <= 0.5;
bool _abc = abc >= 0.382 && abc <= 0.886;
bool _bcd = bcd >= 1.618 && bcd <= 2.618;
bool _xad = xad <= 0.618 && xad <= 1.000;
bool result = _xab && _abc && _bcd && _xad && (d < c);

if(result && !new_pattern_detected)


{
new_pattern_detected = true;
current_pattern = "Bat";
is_bullish = true;
}

return result;
}

bool IsBearishBat()
{
bool _xab = xab >= 0.382 && xab <= 0.5;
bool _abc = abc >= 0.382 && abc <= 0.886;
bool _bcd = bcd >= 1.618 && bcd <= 2.618;
bool _xad = xad <= 0.618 && xad <= 1.000;
bool result = _xab && _abc && _bcd && _xad && (d > c);

if(result && !new_pattern_detected)


{
new_pattern_detected = true;
current_pattern = "Bat";
is_bullish = false;
}

return result;
}

//--- Anti-BAT Pattern


bool IsBullishAntiBat()
{
bool _xab = xab >= 0.500 && xab <= 0.886;
bool _abc = abc >= 1.000 && abc <= 2.618;
bool _bcd = bcd >= 1.618 && bcd <= 2.618;
bool _xad = xad >= 0.886 && xad <= 1.000;
bool result = _xab && _abc && _bcd && _xad && (d < c);

if(result && !new_pattern_detected)


{
new_pattern_detected = true;
current_pattern = "Anti Bat";
is_bullish = true;
}

return result;
}

bool IsBearishAntiBat()
{
bool _xab = xab >= 0.500 && xab <= 0.886;
bool _abc = abc >= 1.000 && abc <= 2.618;
bool _bcd = bcd >= 1.618 && bcd <= 2.618;
bool _xad = xad >= 0.886 && xad <= 1.000;
bool result = _xab && _abc && _bcd && _xad && (d > c);

if(result && !new_pattern_detected)


{
new_pattern_detected = true;
current_pattern = "Anti Bat";
is_bullish = false;
}

return result;
}

//--- Alt BAT Pattern


bool IsBullishAltBat()
{
bool _xab = xab <= 0.382;
bool _abc = abc >= 0.382 && abc <= 0.886;
bool _bcd = bcd >= 2.0 && bcd <= 3.618;
bool _xad = xad <= 1.13;
bool result = _xab && _abc && _bcd && _xad && (d < c);

if(result && !new_pattern_detected)


{
new_pattern_detected = true;
current_pattern = "Alt Bat";
is_bullish = true;
}
return result;
}

bool IsBearishAltBat()
{
bool _xab = xab <= 0.382;
bool _abc = abc >= 0.382 && abc <= 0.886;
bool _bcd = bcd >= 2.0 && bcd <= 3.618;
bool _xad = xad <= 1.13;
bool result = _xab && _abc && _bcd && _xad && (d > c);

if(result && !new_pattern_detected)


{
new_pattern_detected = true;
current_pattern = "Alt Bat";
is_bullish = false;
}

return result;
}

//--- Butterfly Pattern


bool IsBullishButterfly()
{
bool _xab = xab <= 0.786;
bool _abc = abc >= 0.382 && abc <= 0.886;
bool _bcd = bcd >= 1.618 && bcd <= 2.618;
bool _xad = xad >= 1.27 && xad <= 1.618;
bool result = _xab && _abc && _bcd && _xad && (d < c);

if(result && !new_pattern_detected)


{
new_pattern_detected = true;
current_pattern = "Butterfly";
is_bullish = true;
}

return result;
}

bool IsBearishButterfly()
{
bool _xab = xab <= 0.786;
bool _abc = abc >= 0.382 && abc <= 0.886;
bool _bcd = bcd >= 1.618 && bcd <= 2.618;
bool _xad = xad >= 1.27 && xad <= 1.618;
bool result = _xab && _abc && _bcd && _xad && (d > c);

if(result && !new_pattern_detected)


{
new_pattern_detected = true;
current_pattern = "Butterfly";
is_bullish = false;
}

return result;
}

//--- Anti-Butterfly Pattern


bool IsBullishAntiButterfly()
{
bool _xab = xab >= 0.236 && xab <= 0.886;
bool _abc = abc >= 1.130 && abc <= 2.618;
bool _bcd = bcd >= 1.000 && bcd <= 1.382;
bool _xad = xad >= 0.500 && xad <= 0.886;
bool result = _xab && _abc && _bcd && _xad && (d < c);

if(result && !new_pattern_detected)


{
new_pattern_detected = true;
current_pattern = "Anti Butterfly";
is_bullish = true;
}

return result;
}

bool IsBearishAntiButterfly()
{
bool _xab = xab >= 0.236 && xab <= 0.886;
bool _abc = abc >= 1.130 && abc <= 2.618;
bool _bcd = bcd >= 1.000 && bcd <= 1.382;
bool _xad = xad >= 0.500 && xad <= 0.886;
bool result = _xab && _abc && _bcd && _xad && (d > c);

if(result && !new_pattern_detected)


{
new_pattern_detected = true;
current_pattern = "Anti Butterfly";
is_bullish = false;
}

return result;
}

//--- Gartley Pattern


bool IsBullishGartley()
{
bool _xab = xab >= 0.5 && xab <= 0.618;
bool _abc = abc >= 0.382 && abc <= 0.886;
bool _bcd = bcd >= 1.13 && bcd <= 2.618;
bool _xad = xad >= 0.75 && xad <= 0.875;
bool result = _xab && _abc && _bcd && _xad && (d < c);

if(result && !new_pattern_detected)


{
new_pattern_detected = true;
current_pattern = "Gartley";
is_bullish = true;
}

return result;
}

bool IsBearishGartley()
{
bool _xab = xab >= 0.5 && xab <= 0.618;
bool _abc = abc >= 0.382 && abc <= 0.886;
bool _bcd = bcd >= 1.13 && bcd <= 2.618;
bool _xad = xad >= 0.75 && xad <= 0.875;
bool result = _xab && _abc && _bcd && _xad && (d > c);

if(result && !new_pattern_detected)


{
new_pattern_detected = true;
current_pattern = "Gartley";
is_bullish = false;
}

return result;
}

//--- Anti-Gartley Pattern


bool IsBullishAntiGartley()
{
bool _xab = xab >= 0.500 && xab <= 0.886;
bool _abc = abc >= 1.000 && abc <= 2.618;
bool _bcd = bcd >= 1.500 && bcd <= 5.000;
bool _xad = xad >= 1.000 && xad <= 5.000;
bool result = _xab && _abc && _bcd && _xad && (d < c);

if(result && !new_pattern_detected)


{
new_pattern_detected = true;
current_pattern = "Anti Gartley";
is_bullish = true;
}

return result;
}

bool IsBearishAntiGartley()
{
bool _xab = xab >= 0.500 && xab <= 0.886;
bool _abc = abc >= 1.000 && abc <= 2.618;
bool _bcd = bcd >= 1.500 && bcd <= 5.000;
bool _xad = xad >= 1.000 && xad <= 5.000;
bool result = _xab && _abc && _bcd && _xad && (d > c);

if(result && !new_pattern_detected)


{
new_pattern_detected = true;
current_pattern = "Anti Gartley";
is_bullish = false;
}

return result;
}

//--- Crab Pattern


bool IsBullishCrab()
{
bool _xab = xab >= 0.500 && xab <= 0.875;
bool _abc = abc >= 0.382 && abc <= 0.886;
bool _bcd = bcd >= 2.000 && bcd <= 5.000;
bool _xad = xad >= 1.382 && xad <= 5.000;
bool result = _xab && _abc && _bcd && _xad && (d < c);
if(result && !new_pattern_detected)
{
new_pattern_detected = true;
current_pattern = "Crab";
is_bullish = true;
}

return result;
}

bool IsBearishCrab()
{
bool _xab = xab >= 0.500 && xab <= 0.875;
bool _abc = abc >= 0.382 && abc <= 0.886;
bool _bcd = bcd >= 2.000 && bcd <= 5.000;
bool _xad = xad >= 1.382 && xad <= 5.000;
bool result = _xab && _abc && _bcd && _xad && (d > c);

if(result && !new_pattern_detected)


{
new_pattern_detected = true;
current_pattern = "Crab";
is_bullish = false;
}

return result;
}

//--- Anti-Crab Pattern


bool IsBullishAntiCrab()
{
bool _xab = xab >= 0.250 && xab <= 0.500;
bool _abc = abc >= 1.130 && abc <= 2.618;
bool _bcd = bcd >= 1.618 && bcd <= 2.618;
bool _xad = xad >= 0.500 && xad <= 0.750;
bool result = _xab && _abc && _bcd && _xad && (d < c);

if(result && !new_pattern_detected)


{
new_pattern_detected = true;
current_pattern = "Anti Crab";
is_bullish = true;
}

return result;
}

bool IsBearishAntiCrab()
{
bool _xab = xab >= 0.250 && xab <= 0.500;
bool _abc = abc >= 1.130 && abc <= 2.618;
bool _bcd = bcd >= 1.618 && bcd <= 2.618;
bool _xad = xad >= 0.500 && xad <= 0.750;
bool result = _xab && _abc && _bcd && _xad && (d > c);

if(result && !new_pattern_detected)


{
new_pattern_detected = true;
current_pattern = "Anti Crab";
is_bullish = false;
}

return result;
}

//--- Shark Pattern


bool IsBullishShark()
{
bool _xab = xab >= 0.500 && xab <= 0.875;
bool _abc = abc >= 1.130 && abc <= 1.618;
bool _bcd = bcd >= 1.270 && bcd <= 2.240;
bool _xad = xad >= 0.886 && xad <= 1.130;
bool result = _xab && _abc && _bcd && _xad && (d < c);

if(result && !new_pattern_detected)


{
new_pattern_detected = true;
current_pattern = "Shark";
is_bullish = true;
}

return result;
}

bool IsBearishShark()
{
bool _xab = xab >= 0.500 && xab <= 0.875;
bool _abc = abc >= 1.130 && abc <= 1.618;
bool _bcd = bcd >= 1.270 && bcd <= 2.240;
bool _xad = xad >= 0.886 && xad <= 1.130;
bool result = _xab && _abc && _bcd && _xad && (d > c);

if(result && !new_pattern_detected)


{
new_pattern_detected = true;
current_pattern = "Shark";
is_bullish = false;
}

return result;
}

//--- Anti-Shark Pattern


bool IsBullishAntiShark()
{
bool _xab = xab >= 0.382 && xab <= 0.875;
bool _abc = abc >= 0.500 && abc <= 1.000;
bool _bcd = bcd >= 1.250 && bcd <= 2.618;
bool _xad = xad >= 0.500 && xad <= 1.250;
bool result = _xab && _abc && _bcd && _xad && (d < c);

if(result && !new_pattern_detected)


{
new_pattern_detected = true;
current_pattern = "Anti Shark";
is_bullish = true;
}
return result;
}

bool IsBearishAntiShark()
{
bool _xab = xab >= 0.382 && xab <= 0.875;
bool _abc = abc >= 0.500 && abc <= 1.000;
bool _bcd = bcd >= 1.250 && bcd <= 2.618;
bool _xad = xad >= 0.500 && xad <= 1.250;
bool result = _xab && _abc && _bcd && _xad && (d > c);

if(result && !new_pattern_detected)


{
new_pattern_detected = true;
current_pattern = "Anti Shark";
is_bullish = false;
}

return result;
}

//--- 5-O Pattern


bool IsBullish5O()
{
bool _xab = xab >= 1.13 && xab <= 1.618;
bool _abc = abc >= 1.618 && abc <= 2.24;
bool _bcd = bcd >= 0.5 && bcd <= 0.625;
bool _xad = xad >= 0.0 && xad <= 0.236;
bool result = _xab && _abc && _bcd && _xad && (d < c);

if(result && !new_pattern_detected)


{
new_pattern_detected = true;
current_pattern = "5-O";
is_bullish = true;
}

return result;
}

bool IsBearish5O()
{
bool _xab = xab >= 1.13 && xab <= 1.618;
bool _abc = abc >= 1.618 && abc <= 2.24;
bool _bcd = bcd >= 0.5 && bcd <= 0.625;
bool _xad = xad >= 0.0 && xad <= 0.236;
bool result = _xab && _abc && _bcd && _xad && (d > c);

if(result && !new_pattern_detected)


{
new_pattern_detected = true;
current_pattern = "5-O";
is_bullish = false;
}

return result;
}
//--- Wolf Wave Pattern
bool IsBullishWolf()
{
bool _xab = xab >= 1.27 && xab <= 1.618;
bool _abc = abc >= 0 && abc <= 5;
bool _bcd = bcd >= 1.27 && bcd <= 1.618;
bool _xad = xad >= 0.0 && xad <= 5;
bool result = _xab && _abc && _bcd && _xad && (d < c);

if(result && !new_pattern_detected)


{
new_pattern_detected = true;
current_pattern = "Wolf Wave";
is_bullish = true;
}

return result;
}

bool IsBearishWolf()
{
bool _xab = xab >= 1.27 && xab <= 1.618;
bool _abc = abc >= 0 && abc <= 5;
bool _bcd = bcd >= 1.27 && bcd <= 1.618;
bool _xad = xad >= 0.0 && xad <= 5;
bool result = _xab && _abc && _bcd && _xad && (d > c);

if(result && !new_pattern_detected)


{
new_pattern_detected = true;
current_pattern = "Wolf Wave";
is_bullish = false;
}

return result;
}

//--- Head and Shoulders Pattern


bool IsBullishHnS()
{
bool _xab = xab >= 2.0 && xab <= 10;
bool _abc = abc >= 0.90 && abc <= 1.1;
bool _bcd = bcd >= 0.236 && bcd <= 0.88;
bool _xad = xad >= 0.90 && xad <= 1.1;
bool result = _xab && _abc && _bcd && _xad && (d < c);

if(result && !new_pattern_detected)


{
new_pattern_detected = true;
current_pattern = "Head and Shoulders";
is_bullish = true;
}

return result;
}

bool IsBearishHnS()
{
bool _xab = xab >= 2.0 && xab <= 10;
bool _abc = abc >= 0.90 && abc <= 1.1;
bool _bcd = bcd >= 0.236 && bcd <= 0.88;
bool _xad = xad >= 0.90 && xad <= 1.1;
bool result = _xab && _abc && _bcd && _xad && (d > c);

if(result && !new_pattern_detected)


{
new_pattern_detected = true;
current_pattern = "Head and Shoulders";
is_bullish = false;
}

return result;
}

//--- Contracting Triangle Pattern


bool IsBullishConTria()
{
bool _xab = xab >= 0.382 && xab <= 0.618;
bool _abc = abc >= 0.382 && abc <= 0.618;
bool _bcd = bcd >= 0.382 && bcd <= 0.618;
bool _xad = xad >= 0.236 && xad <= 0.764;
bool result = _xab && _abc && _bcd && _xad && (d < c);

if(result && !new_pattern_detected)


{
new_pattern_detected = true;
current_pattern = "Contracting Triangle";
is_bullish = true;
}

return result;
}

bool IsBearishConTria()
{
bool _xab = xab >= 0.382 && xab <= 0.618;
bool _abc = abc >= 0.382 && abc <= 0.618;
bool _bcd = bcd >= 0.382 && bcd <= 0.618;
bool _xad = xad >= 0.236 && xad <= 0.764;
bool result = _xab && _abc && _bcd && _xad && (d > c);

if(result && !new_pattern_detected)


{
new_pattern_detected = true;
current_pattern = "Contracting Triangle";
is_bullish = false;
}

return result;
}

//--- Expanding Triangle Pattern


bool IsBullishExpTria()
{
bool _xab = xab >= 1.236 && xab <= 1.618;
bool _abc = abc >= 1.000 && abc <= 1.618;
bool _bcd = bcd >= 1.236 && bcd <= 2.000;
bool _xad = xad >= 2.000 && xad <= 2.236;
bool result = _xab && _abc && _bcd && _xad && (d < c);

if(result && !new_pattern_detected)


{
new_pattern_detected = true;
current_pattern = "Expanding Triangle";
is_bullish = true;
}

return result;
}

bool IsBearishExpTria()
{
bool _xab = xab >= 1.236 && xab <= 1.618;
bool _abc = abc >= 1.000 && abc <= 1.618;
bool _bcd = bcd >= 1.236 && bcd <= 2.000;
bool _xad = xad >= 2.000 && xad <= 2.236;
bool result = _xab && _abc && _bcd && _xad && (d > c);

if(result && !new_pattern_detected)


{
new_pattern_detected = true;
current_pattern = "Expanding Triangle";
is_bullish = false;
}

return result;
}

//+------------------------------------------------------------------+
//| Custom event handler functions |
//+------------------------------------------------------------------+
void OnChartEvent(const int id,
const long &lparam,
const double &dparam,
const string &sparam)
{
// Refresh the chart
if(id == CHARTEVENT_CHART_CHANGE)
{
ChartRedraw();
}
}

//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
// Initialize strategy parameters
IndicatorSetString(INDICATOR_SHORTNAME, "[STRATEGY][RS]ZigZag PA Strategy
V4.1");

// Set buffers
SetIndexBuffer(0, ZigZagBuffer, INDICATOR_DATA);
SetIndexBuffer(1, Fib0000Buffer, INDICATOR_DATA);
SetIndexBuffer(2, Fib0236Buffer, INDICATOR_DATA);
SetIndexBuffer(3, Fib0382Buffer, INDICATOR_DATA);
SetIndexBuffer(4, Fib0500Buffer, INDICATOR_DATA);
SetIndexBuffer(5, Fib0618Buffer, INDICATOR_DATA);
SetIndexBuffer(6, Fib0764Buffer, INDICATOR_DATA);
SetIndexBuffer(7, Fib1000Buffer, INDICATOR_DATA);
SetIndexBuffer(8, EntryBuffer, INDICATOR_DATA);

// Initialize arrays as empty


ArrayInitialize(ZigZagBuffer, 0);
ArrayInitialize(Fib0000Buffer, EMPTY_VALUE);
ArrayInitialize(Fib0236Buffer, EMPTY_VALUE);
ArrayInitialize(Fib0382Buffer, EMPTY_VALUE);
ArrayInitialize(Fib0500Buffer, EMPTY_VALUE);
ArrayInitialize(Fib0618Buffer, EMPTY_VALUE);
ArrayInitialize(Fib0764Buffer, EMPTY_VALUE);
ArrayInitialize(Fib1000Buffer, EMPTY_VALUE);
ArrayInitialize(EntryBuffer, EMPTY_VALUE);

// Set drawing styles


PlotIndexSetInteger(0, PLOT_DRAW_TYPE, DRAW_LINE);
PlotIndexSetInteger(0, PLOT_LINE_WIDTH, 2);
PlotIndexSetInteger(0, PLOT_LINE_COLOR, clrBlack);
PlotIndexSetString(0, PLOT_LABEL, "ZigZag");

// Set Fibonacci levels drawing styles


PlotIndexSetInteger(1, PLOT_DRAW_TYPE, DRAW_LINE);
PlotIndexSetInteger(1, PLOT_LINE_COLOR, clrBlack);
PlotIndexSetString(1, PLOT_LABEL, "Fib 0.000");

PlotIndexSetInteger(2, PLOT_DRAW_TYPE, DRAW_LINE);


PlotIndexSetInteger(2, PLOT_LINE_COLOR, clrRed);
PlotIndexSetString(2, PLOT_LABEL, "Fib 0.236");

PlotIndexSetInteger(3, PLOT_DRAW_TYPE, DRAW_LINE);


PlotIndexSetInteger(3, PLOT_LINE_COLOR, clrOlive);
PlotIndexSetString(3, PLOT_LABEL, "Fib 0.382");

PlotIndexSetInteger(4, PLOT_DRAW_TYPE, DRAW_LINE);


PlotIndexSetInteger(4, PLOT_LINE_COLOR, clrLime);
PlotIndexSetString(4, PLOT_LABEL, "Fib 0.500");

PlotIndexSetInteger(5, PLOT_DRAW_TYPE, DRAW_LINE);


PlotIndexSetInteger(5, PLOT_LINE_COLOR, clrTeal);
PlotIndexSetString(5, PLOT_LABEL, "Fib 0.618");

PlotIndexSetInteger(6, PLOT_DRAW_TYPE, DRAW_LINE);


PlotIndexSetInteger(6, PLOT_LINE_COLOR, clrBlue);
PlotIndexSetString(6, PLOT_LABEL, "Fib 0.764");

PlotIndexSetInteger(7, PLOT_DRAW_TYPE, DRAW_LINE);


PlotIndexSetInteger(7, PLOT_LINE_COLOR, clrBlack);
PlotIndexSetString(7, PLOT_LABEL, "Fib 1.000");

// Entry point style


PlotIndexSetInteger(8, PLOT_DRAW_TYPE, DRAW_ARROW);
PlotIndexSetInteger(8, PLOT_ARROW, 159);
PlotIndexSetInteger(8, PLOT_LINE_COLOR, clrMagenta);
PlotIndexSetString(8, PLOT_LABEL, "Entry Points");

return(INIT_SUCCEEDED);
}

//+------------------------------------------------------------------+
//| Deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
// Delete all objects created by this indicator
ObjectsDeleteAll(0, "Pattern_");

// Print deinitialization reason


Print("ZigZag PA Strategy deinitialized with reason code: ", reason);
}

//+------------------------------------------------------------------+
//| Enhanced Trade Management Function |
//+------------------------------------------------------------------+
void ManageOpenTrades()
{
// Check if we have any open positions
int total = PositionsTotal();

for(int i = total - 1; i >= 0; i--)


{
ulong ticket = PositionGetTicket(i);

if(PositionSelectByTicket(ticket))
{
// Get position properties
string symbol = PositionGetString(POSITION_SYMBOL);
int position_type = (int)PositionGetInteger(POSITION_TYPE);
double open_price = PositionGetDouble(POSITION_PRICE_OPEN);
double current_price = PositionGetDouble(POSITION_PRICE_CURRENT);
double take_profit = PositionGetDouble(POSITION_TP);
double stop_loss = PositionGetDouble(POSITION_SL);

// Only manage positions for the current symbol


if(symbol != _Symbol) continue;

// Check for trailing stop opportunities


if((position_type == POSITION_TYPE_BUY && current_price > open_price +
100*_Point) ||
(position_type == POSITION_TYPE_SELL && current_price < open_price -
100*_Point))
{
double new_sl = 0;

if(position_type == POSITION_TYPE_BUY)
new_sl = current_price - 50*_Point;
else
new_sl = current_price + 50*_Point;

// Only modify if new stop loss is better


if((position_type == POSITION_TYPE_BUY && (stop_loss == 0 || new_sl >
stop_loss)) ||
(position_type == POSITION_TYPE_SELL && (stop_loss == 0 || new_sl <
stop_loss)))
{
// Modify position with trailing stop
ModifyPosition(ticket, take_profit, new_sl);
}
}
}
}
}

//+------------------------------------------------------------------+
//| Modify position stop loss and take profit |
//+------------------------------------------------------------------+
bool ModifyPosition(const ulong ticket, const double tp, const double sl)
{
MqlTradeRequest request = {};
MqlTradeResult result = {};

request.action = TRADE_ACTION_SLTP;
request.position = ticket;
request.symbol = _Symbol;
request.tp = tp;
request.sl = sl;

bool success = OrderSend(request, result);

if(!success)
Print("Position modify error: ", GetLastError());
else
Print("Position modified successfully. Ticket: ", ticket);

return success;
}

//+------------------------------------------------------------------+
//| Calculate Fibonacci target levels for entries and exits |
//+------------------------------------------------------------------+
void CalculateFibonacciTargets()
{
// Calculate Fibonacci target levels based on the latest zigzag points
if(d != 0 && c != 0)
{
// Target 1
double target01_entry_level = CalculateLastFib(0.236);
double target01_tp_level = CalculateLastFib(0.618);
double target01_sl_level = CalculateLastFib(-0.236);

// Target 2
double target02_entry_level = CalculateLastFib(0.236);
double target02_tp_level = CalculateLastFib(1.618);
double target02_sl_level = CalculateLastFib(-0.236);

// For future implementation of target levels


// These can be used for trading signals
}
}

//+------------------------------------------------------------------+
//| Main trading logic controller |
//+------------------------------------------------------------------+
void TradingLogic(int bar)
{
// Check for pattern formations
CheckPatterns(bar);

// Calculate Fibonacci targets


CalculateFibonacciTargets();

// Check entry conditions - this is already implemented in


CheckEntryConditions()

// Manage existing trades


if(bar == 0) // Current bar
{
ManageOpenTrades();
}
}

//+------------------------------------------------------------------+
//| Optimized version of indicator calculation |
//+------------------------------------------------------------------+
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 insufficient data
if(rates_total < 5) return(0);

// Calculate start position


int start;
if(prev_calculated > 0)
start = prev_calculated - 1;
else
start = 4; // Need at least 5 bars for zigzag

// Calculate zigzag
CalculateZigZag(rates_total, start, open, high, low, close);

// Process each bar


for(int i = start; i < rates_total; i++)
{
// Find the latest zigzag points
int zigzagPoints[5] = {0};
int pointCount = 0;

for(int j = i; j >= 0 && pointCount < 5; j--)


{
if(ZigZagBuffer[j] != 0)
{
zigzagPoints[pointCount] = j;
pointCount++;
}
}
// Skip if we don't have enough zigzag points yet
if(pointCount < 5) continue;

// Assign X, A, B, C, D points
d = ZigZagBuffer[zigzagPoints[0]];
c = ZigZagBuffer[zigzagPoints[1]];
b = ZigZagBuffer[zigzagPoints[2]];
a = ZigZagBuffer[zigzagPoints[3]];
x = ZigZagBuffer[zigzagPoints[4]];

// Calculate ratios
xab = MathAbs(b-a)/MathAbs(x-a);
xad = MathAbs(a-d)/MathAbs(x-a);
abc = MathAbs(b-c)/MathAbs(a-b);
bcd = MathAbs(c-d)/MathAbs(b-c);

// Run trading logic


TradingLogic(i);

// Calculate Fibonacci levels


fib_range = MathAbs(d-c);

if(ShowFib0000) Fib0000Buffer[i] = (d > c) ? d-(fib_range*0.000) : d+


(fib_range*0.000);
if(ShowFib0236) Fib0236Buffer[i] = (d > c) ? d-(fib_range*0.236) : d+
(fib_range*0.236);
if(ShowFib0382) Fib0382Buffer[i] = (d > c) ? d-(fib_range*0.382) : d+
(fib_range*0.382);
if(ShowFib0500) Fib0500Buffer[i] = (d > c) ? d-(fib_range*0.500) : d+
(fib_range*0.500);
if(ShowFib0618) Fib0618Buffer[i] = (d > c) ? d-(fib_range*0.618) : d+
(fib_range*0.618);
if(ShowFib0764) Fib0764Buffer[i] = (d > c) ? d-(fib_range*0.764) : d+
(fib_range*0.764);
if(ShowFib1000) Fib1000Buffer[i] = (d > c) ? d-(fib_range*1.000) : d+
(fib_range*1.000);
}

return(rates_total);
}

You might also like