Nowy Dokument Tekstowy462782
Nowy Dokument Tekstowy462782
//| 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
// 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);
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);
// Calculate patterns
if(rates_total > 5)
{
for(int i = limit; i < rates_total; i++)
{
// Find zigzag points
int zigzagPoints[5] = {0};
int pointCount = 0;
// 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);
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;
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;
//+------------------------------------------------------------------+
//| Create pattern label |
//+------------------------------------------------------------------+
void CreatePatternLabel(int bar, string pattern, bool bullish)
{
if(new_pattern_detected)
{
string label_name = "Pattern_" + IntegerToString(bar);
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
//+------------------------------------------------------------------+
//| 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;
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;
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;
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;
if(!success)
Print("OrderSend error: ", GetLastError());
else
Print("Order placed successfully. Ticket: ", result.order);
}
//+------------------------------------------------------------------+
//| Pattern recognition functions - FULL IMPLEMENTATION |
//+------------------------------------------------------------------+
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);
return result;
}
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);
return result;
}
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);
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);
return result;
}
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);
return result;
}
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);
return result;
}
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);
return result;
}
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);
return result;
}
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);
return result;
}
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);
return result;
}
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);
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);
return result;
}
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);
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);
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);
return result;
}
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);
return result;
}
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);
return result;
}
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);
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);
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
// Delete all objects created by this indicator
ObjectsDeleteAll(0, "Pattern_");
//+------------------------------------------------------------------+
//| Enhanced Trade Management Function |
//+------------------------------------------------------------------+
void ManageOpenTrades()
{
// Check if we have any open positions
int total = PositionsTotal();
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);
if(position_type == POSITION_TYPE_BUY)
new_sl = current_price - 50*_Point;
else
new_sl = current_price + 50*_Point;
//+------------------------------------------------------------------+
//| 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;
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);
//+------------------------------------------------------------------+
//| Main trading logic controller |
//+------------------------------------------------------------------+
void TradingLogic(int bar)
{
// Check for pattern formations
CheckPatterns(bar);
//+------------------------------------------------------------------+
//| 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 zigzag
CalculateZigZag(rates_total, start, open, high, low, close);
// 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);
return(rates_total);
}