ZigZag Color PRO FIX V2 (Auto) |

Author: shuha5555-gifCreated Jul 21, 2026Updated Jul 21, 2026

//+------------------------------------------------------------------+ //| ZigZag Color PRO FIX V2 (Auto) | //+------------------------------------------------------------------+ #property strict

//--- Savdo kutubxonasini ulash #include <Trade\Trade.mqh> CTrade trade;

//==================================================== // INPUT PARAMETRLARI (SOZLAMALAR) //==================================================== input string _ind_settings = "--- ZigZag Sozlamalari ---"; input int InpDepth = 12; input int InpDeviation = 5; input int InpBackstep = 3;

input string _trade_settings = "--- Savdo Sozlamalari ---"; input double InpLot = 0.1; // Lot o'lchami input int InpStopLoss = 200; // Stop Loss (Punktda) input int InpTakeProfit = 400; // Take Profit (Punktda) input int InpMagic = 123456; // Robot ID raqami (Magic Number)

input string _trailing_settings = "--- Trailing Stop ---"; input bool UseTrailing = true; // Trailing Stopni yoqish/o'chirish input int TrailingStop = 150; // Trailing Stop masofasi (Punktda) input int TrailingStep = 30; // Trailing Stop qadami (Punktda)

//--- ZigZag handle int zzHandle;

//--- buffers double HighBuf[]; double LowBuf[];

//--- swing structure struct Swing { datetime time; double price; };

// Dinamik massivlar Swing highs[]; Swing lows[];

int hCount=0; int lCount=0; datetime lastBarTime;

//+------------------------------------------------------------------+ int OnInit() { // ZigZag handle olish zzHandle = iCustom(_Symbol,_Period,"ZigZagColor",InpDepth,InpDeviation,InpBackstep);

if(zzHandle==INVALID_HANDLE) { Print("ZigZag handle xatoligi! Grafikda 'ZigZagColor' borligini tekshiring."); return(INIT_FAILED); }

// Savdo obyekti sozlamalari trade.SetExpertMagicNumber(InpMagic);

lastBarTime = 0; return(INIT_SUCCEEDED); }

//+------------------------------------------------------------------+ void OnDeinit(const int reason) { // Grafikni tozalash for(int i=ObjectsTotal(0)-1; i>=0; i--) { string name=ObjectName(0,i); if(StringFind(name,"ZZ_")==0) ObjectDelete(0,name); } }

//+------------------------------------------------------------------+ void OnTick() { // TRAILING STOP (Har bir tikda tekshiriladi) if(UseTrailing) DoTrailingStop();

// FAQAT YANGI BAR OCHILGANDA SIGNAL TEKSHIRILADI datetime currentBarTime = iTime(_Symbol, _Period, 0); if(currentBarTime == lastBarTime || currentBarTime == 0) return;

// Bufer o'lchamlarini moslash ArrayResize(HighBuf, 300); ArrayResize(LowBuf, 300);

if(CopyBuffer(zzHandle,0,0,300,HighBuf)<=0) return; if(CopyBuffer(zzHandle,1,0,300,LowBuf)<=0) return;

ArraySetAsSeries(HighBuf, true); ArraySetAsSeries(LowBuf, true);

// Maksimal 20 ta element saqlash uchun joy ochamiz ArrayResize(highs, 20); ArrayResize(lows, 20);

hCount=0; lCount=0;

// Swinglarni yig'ish for(int i=0; i<300; i++) { if(hCount >= 20 && lCount >= 20) break; // Massiv to'lgan bo'lsa siklni to'xtatish

  datetime t = iTime(_Symbol,_Period,i);
  if(t == 0) continue; 

  if(HighBuf[i]!=0.0 && HighBuf[i]!=EMPTY_VALUE && hCount<20)
  {
     highs[hCount].time=t;
     highs[hCount].price=HighBuf[i];
     hCount++;
  }

  if(LowBuf[i]!=0.0 && LowBuf[i]!=EMPTY_VALUE && lCount<20)
  {
     lows[lCount].time=t;
     lows[lCount].price=LowBuf[i];
     lCount++;
  }

}

// Massivda kamida 2 ta nuqta borligini qat'iy tekshirish (Crash oldini olish) if(hCount<2 || lCount<2) return;

ArrayResize(highs, hCount); ArrayResize(lows, lCount);

// Obyektlarni yangilash UpdateGraphics();

//==================================================== // SAVDO MANTIQI (ORDER OCHISH) //====================================================

// Hozirgi trendni aniqlash string trend = "NEUTRAL"; if(highs[0].price > highs[1].price && lows[0].price > lows[1].price) trend = "BULLISH"; else if(highs[0].price < highs[1].price && lows[0].price < lows[1].price) trend = "BEARISH";

// Grafikda ochiq pozitsiyalarimiz borligini tekshirish int totalPositions = 0; for(int i=PositionsTotal()-1; i>=0; i--) { if(PositionGetSymbol(i) == _Symbol && PositionGetInteger(POSITION_MAGIC) == InpMagic) totalPositions++; }

// Agar ochiq pozitsiya bo'lmasa va yangi signal bo'lsa order ochish if(totalPositions == 0) { double ask = SymbolInfoDouble(_Symbol, SYMBOL_ASK); double bid = SymbolInfoDouble(_Symbol, SYMBOL_BID);

  // BUY SIGNAL: Trend Bullish va oxirgi nuqta Low bo'lsa
  if(trend == "BULLISH" && lows[0].time == iTime(_Symbol, _Period, 1))
  {
     double sl = (InpStopLoss > 0) ? ask - (InpStopLoss * _Point) : 0;
     double tp = (InpTakeProfit > 0) ? ask + (InpTakeProfit * _Point) : 0;
     
     if(sl > 0) sl = NormalizeDouble(sl, _Digits);
     if(tp > 0) tp = NormalizeDouble(tp, _Digits);
     
     trade.Buy(InpLot, _Symbol, ask, sl, tp, "ZZ_Buy");
     lastBarTime = currentBarTime; // Signal bajarilgach bar vaqtini mustahkamlash
  }
  // SELL SIGNAL: Trend Bearish va oxirgi nuqta High bo'lsa
  else if(trend == "BEARISH" && highs[0].time == iTime(_Symbol, _Period, 1))
  {
     double sl = (InpStopLoss > 0) ? bid + (InpStopLoss * _Point) : 0;
     double tp = (InpTakeProfit > 0) ? bid - (InpTakeProfit * _Point) : 0;
     
     if(sl > 0) sl = NormalizeDouble(sl, _Digits);
     if(tp > 0) tp = NormalizeDouble(tp, _Digits);
     
     trade.Sell(InpLot, _Symbol, bid, sl, tp, "ZZ_Sell");
     lastBarTime = currentBarTime; // Signal bajarilgach bar vaqtini mustahkamlash
  }

} }

//+------------------------------------------------------------------+ //| TRAILING STOP FUNKSIYASI | //+------------------------------------------------------------------+ void DoTrailingStop() { for(int i=PositionsTotal()-1; i>=0; i--) { if(PositionGetSymbol(i) == _Symbol && PositionGetInteger(POSITION_MAGIC) == InpMagic) { ulong ticket = PositionGetInteger(POSITION_TICKET); double posPrice = PositionGetDouble(POSITION_PRICE_OPEN); double posSL = PositionGetDouble(POSITION_SL); double posTP = PositionGetDouble(POSITION_TP); long type = PositionGetInteger(POSITION_TYPE);

     double bid = SymbolInfoDouble(_Symbol, SYMBOL_BID);
     double ask = SymbolInfoDouble(_Symbol, SYMBOL_ASK);
     
     if(type == POSITION_TYPE_BUY)
     {
        if(bid - posPrice > TrailingStop * _Point)
        {
           if(posSL < bid - TrailingStop * _Point)
           {
              if(bid - TrailingStop * _Point - posSL > TrailingStep * _Point || posSL == 0)
              {
                 double newSL = NormalizeDouble(bid - TrailingStop * _Point, _Digits);
                 trade.PositionModify(ticket, newSL, posTP);
              }
           }
        }
     }
     else if(type == POSITION_TYPE_SELL)
     {
        if(posPrice - ask > TrailingStop * _Point)
        {
           if(posSL > ask + TrailingStop * _Point || posSL == 0)
              {
              if(posSL - (ask + TrailingStop * _Point) > TrailingStep * _Point || posSL == 0)
              {
                 double newSL = NormalizeDouble(ask + TrailingStop * _Point, _Digits);
                 trade.PositionModify(ticket, newSL, posTP);
              }
           }
        }
     }
  }

} }

//+------------------------------------------------------------------+ //| GRAFIKDA CHIZIQLARNI YANGILASH FUNKSIYASI | //+------------------------------------------------------------------+ void UpdateGraphics() { // Massivlar xavfsizligini yana bir bor tekshirish if(hCount < 2 || lCount < 2) return;

for(int i=ObjectsTotal(0)-1; i>=0; i--) { string name=ObjectName(0,i); if(StringFind(name,"ZZ_")==0) ObjectDelete(0,name); }

// Trendline Bull/Bear ObjectCreate(0,"ZZ_BullTrend",OBJ_TREND,0,lows[1].time,lows[1].price,lows[0].time,lows[0].price); ObjectSetInteger(0,"ZZ_BullTrend",OBJPROP_COLOR,clrDodgerBlue); ObjectSetInteger(0,"ZZ_BullTrend",OBJPROP_RAY_RIGHT,false); // True o'rniga False qilindi (Grafik chiroyli ko'rinishi uchun) ObjectSetInteger(0,"ZZ_BullTrend",OBJPROP_WIDTH,2);

ObjectCreate(0,"ZZ_BearTrend",OBJ_TREND,0,highs[1].time,highs[1].price,highs[0].time,highs[0].price); ObjectSetInteger(0,"ZZ_BearTrend",OBJPROP_COLOR,clrRed); ObjectSetInteger(0,"ZZ_BearTrend",OBJPROP_RAY_RIGHT,false); // True o'rniga False qilindi ObjectSetInteger(0,"ZZ_BearTrend",OBJPROP_WIDTH,2);

// Strelkalar for(int i=0; i<hCount; i++) { string name="ZZ_H_"+IntegerToString(i); ObjectCreate(0,name,OBJ_ARROW,0,highs[i].time,highs[i].price); ObjectSetInteger(0,name,OBJPROP_ARROWCODE,234); ObjectSetInteger(0,name,OBJPROP_COLOR,clrRed); } for(int i=0; i<lCount; i++) { string name="ZZ_L_"+IntegerToString(i); ObjectCreate(0,name,OBJ_ARROW,0,lows[i].time,lows[i].price); ObjectSetInteger(0,name,OBJPROP_ARROWCODE,233); ObjectSetInteger(0,name,OBJPROP_COLOR,clrDodgerBlue); }

// Fibonachchi string fibo="ZZ_FIBO"; double p1, p2; datetime t1, t2; if(lows[0].time > highs[0].time) { p1 = highs[0].price; t1 = highs[0].time; p2 = lows[0].price; t2 = lows[0].time; } else { p1 = lows[0].price; t1 = lows[0].time; p2 = highs[0].price; t2 = highs[0].time; } ObjectCreate(0,fibo,OBJ_FIBO,0,t1,p1,t2,p2); ObjectSetInteger(0,fibo,OBJPROP_COLOR,clrGold); ObjectSetInteger(0,fibo,OBJPROP_RAY_RIGHT,false);

// Trend Label string label="ZZ_LABEL"; ObjectCreate//+------------------------------------------------------------------+ //| ZigZag Color PRO FIX V2 (Auto) | //+------------------------------------------------------------------+ #property strict

//--- Savdo kutubxonasini ulash #include <Trade\Trade.mqh> CTrade trade;

//==================================================== // INPUT PARAMETRLARI (SOZLAMALAR) //==================================================== input string _ind_settings = "--- ZigZag Sozlamalari ---"; input int InpDepth = 12; input int InpDeviation = 5; input int InpBackstep = 3;

input string _trade_settings = "--- Savdo Sozlamalari ---"; input double InpLot = 0.1; // Lot o'lchami input int InpStopLoss = 200; // Stop Loss (Punktda) input int InpTakeProfit = 400; // Take Profit (Punktda) input int InpMagic = 123456; // Robot ID raqami (Magic Number)

input string _trailing_settings = "--- Trailing Stop ---"; input bool UseTrailing = true; // Trailing Stopni yoqish/o'chirish input int TrailingStop = 150; // Trailing Stop masofasi (Punktda) input int TrailingStep = 30; // Trailing Stop qadami (Punktda)

//--- ZigZag handle int zzHandle;

//--- buffers double HighBuf[]; double LowBuf[];

//--- swing structure struct Swing { datetime time; double price; };

// Dinamik massivlar Swing highs[]; Swing lows[];

int hCount=0; int lCount=0; datetime lastBarTime;

//+------------------------------------------------------------------+ int OnInit() { // ZigZag handle olish zzHandle = iCustom(_Symbol,_Period,"ZigZagColor",InpDepth,InpDeviation,InpBackstep);

if(zzHandle==INVALID_HANDLE) { Print("ZigZag handle xatoligi! Grafikda 'ZigZagColor' borligini tekshiring."); return(INIT_FAILED); }

// Savdo obyekti sozlamalari trade.SetExpertMagicNumber(InpMagic);

lastBarTime = 0; return(INIT_SUCCEEDED); }

//+------------------------------------------------------------------+ void OnDeinit(const int reason) { // Grafikni tozalash for(int i=ObjectsTotal(0)-1; i>=0; i--) { string name=ObjectName(0,i); if(StringFind(name,"ZZ_")==0) ObjectDelete(0,name); } }

//+------------------------------------------------------------------+ void OnTick() { // TRAILING STOP (Har bir tikda tekshiriladi) if(UseTrailing) DoTrailingStop();

// FAQAT YANGI BAR OCHILGANDA SIGNAL TEKSHIRILADI datetime currentBarTime = iTime(_Symbol, _Period, 0); if(currentBarTime == lastBarTime || currentBarTime == 0) return;

// Bufer o'lchamlarini moslash ArrayResize(HighBuf, 300); ArrayResize(LowBuf, 300);

if(CopyBuffer(zzHandle,0,0,300,HighBuf)<=0) return; if(CopyBuffer(zzHandle,1,0,300,LowBuf)<=0) return;

ArraySetAsSeries(HighBuf, true); ArraySetAsSeries(LowBuf, true);

// Maksimal 20 ta element saqlash uchun joy ochamiz ArrayResize(highs, 20); ArrayResize(lows, 20);

hCount=0; lCount=0;

// Swinglarni yig'ish for(int i=0; i<300; i++) { if(hCount >= 20 && lCount >= 20) break; // Massiv to'lgan bo'lsa siklni to'xtatish

  datetime t = iTime(_Symbol,_Period,i);
  if(t == 0) continue; 

  if(HighBuf[i]!=0.0 && HighBuf[i]!=EMPTY_VALUE && hCount<20)
  {
     highs[hCount].time=t;
     highs[hCount].price=HighBuf[i];
     hCount++;
  }

  if(LowBuf[i]!=0.0 && LowBuf[i]!=EMPTY_VALUE && lCount<20)
  {
     lows[lCount].time=t;
     lows[lCount].price=LowBuf[i];
     lCount++;
  }

}

// Massivda kamida 2 ta nuqta borligini qat'iy tekshirish (Crash oldini olish) if(hCount<2 || lCount<2) return;

ArrayResize(highs, hCount); ArrayResize(lows, lCount);

// Obyektlarni yangilash UpdateGraphics();

//==================================================== // SAVDO MANTIQI (ORDER OCHISH) //====================================================

// Hozirgi trendni aniqlash string trend = "NEUTRAL"; if(highs[0].price > highs[1].price && lows[0].price > lows[1].price) trend = "BULLISH"; else if(highs[0].price < highs[1].price && lows[0].price < lows[1].price) trend = "BEARISH";

// Grafikda ochiq pozitsiyalarimiz borligini tekshirish int totalPositions = 0; for(int i=PositionsTotal()-1; i>=0; i--) { if(PositionGetSymbol(i) == _Symbol && PositionGetInteger(POSITION_MAGIC) == InpMagic) totalPositions++; }

// Agar ochiq pozitsiya bo'lmasa va yangi signal bo'lsa order ochish if(totalPositions == 0) { double ask = SymbolInfoDouble(_Symbol, SYMBOL_ASK); double bid = SymbolInfoDouble(_Symbol, SYMBOL_BID);

  // BUY SIGNAL: Trend Bullish va oxirgi nuqta Low bo'lsa
  if(trend == "BULLISH" && lows[0].time == iTime(_Symbol, _Period, 1))
  {
     double sl = (InpStopLoss > 0) ? ask - (InpStopLoss * _Point) : 0;
     double tp = (InpTakeProfit > 0) ? ask + (InpTakeProfit * _Point) : 0;
     
     if(sl > 0) sl = NormalizeDouble(sl, _Digits);
     if(tp > 0) tp = NormalizeDouble(tp, _Digits);
     
     trade.Buy(InpLot, _Symbol, ask, sl, tp, "ZZ_Buy");
     lastBarTime = currentBarTime; // Signal bajarilgach bar vaqtini mustahkamlash
  }
  // SELL SIGNAL: Trend Bearish va oxirgi nuqta High bo'lsa
  else if(trend == "BEARISH" && highs[0].time == iTime(_Symbol, _Period, 1))
  {
     double sl = (InpStopLoss > 0) ? bid + (InpStopLoss * _Point) : 0;
     double tp = (InpTakeProfit > 0) ? bid - (InpTakeProfit * _Point) : 0;
     
     if(sl > 0) sl = NormalizeDouble(sl, _Digits);
     if(tp > 0) tp = NormalizeDouble(tp, _Digits);
     
     trade.Sell(InpLot, _Symbol, bid, sl, tp, "ZZ_Sell");
     lastBarTime = currentBarTime; // Signal bajarilgach bar vaqtini mustahkamlash
  }

} }

//+------------------------------------------------------------------+ //| TRAILING STOP FUNKSIYASI | //+------------------------------------------------------------------+ void DoTrailingStop() { for(int i=PositionsTotal()-1; i>=0; i--) { if(PositionGetSymbol(i) == _Symbol && PositionGetInteger(POSITION_MAGIC) == InpMagic) { ulong ticket = PositionGetInteger(POSITION_TICKET); double posPrice = PositionGetDouble(POSITION_PRICE_OPEN); double posSL = PositionGetDouble(POSITION_SL); double posTP = PositionGetDouble(POSITION_TP); long type = PositionGetInteger(POSITION_TYPE);

     double bid = SymbolInfoDouble(_Symbol, SYMBOL_BID);
     double ask = SymbolInfoDouble(_Symbol, SYMBOL_ASK);
     
     if(type == POSITION_TYPE_BUY)
     {
        if(bid - posPrice > TrailingStop * _Point)
        {
           if(posSL < bid - TrailingStop * _Point)
           {
              if(bid - TrailingStop * _Point - posSL > TrailingStep * _Point || posSL == 0)
              {
                 double newSL = NormalizeDouble(bid - TrailingStop * _Point, _Digits);
                 trade.PositionModify(ticket, newSL, posTP);
              }
           }
        }
     }
     else if(type == POSITION_TYPE_SELL)
     {
        if(posPrice - ask > TrailingStop * _Point)
        {
           if(posSL > ask + TrailingStop * _Point || posSL == 0)
              {
              if(posSL - (ask + TrailingStop * _Point) > TrailingStep * _Point || posSL == 0)
              {
                 double newSL = NormalizeDouble(ask + TrailingStop * _Point, _Digits);
                 trade.PositionModify(ticket, newSL, posTP);
              }
           }
        }
     }
  }

} }

//+------------------------------------------------------------------+ //| GRAFIKDA CHIZIQLARNI YANGILASH FUNKSIYASI | //+------------------------------------------------------------------+ void UpdateGraphics() { // Massivlar xavfsizligini yana bir bor tekshirish if(hCount < 2 || lCount < 2) return;

for(int i=ObjectsTotal(0)-1; i>=0; i--) { string name=ObjectName(0,i); if(StringFind(name,"ZZ_")==0) ObjectDelete(0,name); }

// Trendline Bull/Bear ObjectCreate(0,"ZZ_BullTrend",OBJ_TREND,0,lows[1].time,lows[1].price,lows[0].time,lows[0].price); ObjectSetInteger(0,"ZZ_BullTrend",OBJPROP_COLOR,clrDodgerBlue); ObjectSetInteger(0,"ZZ_BullTrend",OBJPROP_RAY_RIGHT,false); // True o'rniga False qilindi (Grafik chiroyli ko'rinishi uchun) ObjectSetInteger(0,"ZZ_BullTrend",OBJPROP_WIDTH,2);

ObjectCreate(0,"ZZ_BearTrend",OBJ_TREND,0,highs[1].time,highs[1].price,highs[0].time,highs[0].price); ObjectSetInteger(0,"ZZ_BearTrend",OBJPROP_COLOR,clrRed); ObjectSetInteger(0,"ZZ_BearTrend",OBJPROP_RAY_RIGHT,false); // True o'rniga False qilindi ObjectSetInteger(0,"ZZ_BearTrend",OBJPROP_WIDTH,2);

// Strelkalar for(int i=0; i<hCount; i++) { string name="ZZ_H_"+IntegerToString(i); ObjectCreate(0,name,OBJ_ARROW,0,highs[i].time,highs[i].price); ObjectSetInteger(0,name,OBJPROP_ARROWCODE,234); ObjectSetInteger(0,name,OBJPROP_COLOR,clrRed); } for(int i=0; i<lCount; i++) { string name="ZZ_L_"+IntegerToString(i); ObjectCreate(0,name,OBJ_ARROW,0,lows[i].time,lows[i].price); ObjectSetInteger(0,name,OBJPROP_ARROWCODE,233); ObjectSetInteger(0,name,OBJPROP_COLOR,clrDodgerBlue); }

// Fibonachchi string fibo="ZZ_FIBO"; double p1, p2; datetime t1, t2; if(lows[0].time > highs[0].time) { p1 = highs[0].price; t1 = highs[0].time; p2 = lows[0].price; t2 = lows[0].time; } else { p1 = lows[0].price; t1 = lows[0].time; p2 = highs[0].price; t2 = highs[0].time; } ObjectCreate(0,fibo,OBJ_FIBO,0,t1,p1,t2,p2); ObjectSetInteger(0,fibo,OBJ

Source: deepseek-ai/DeepSeek-Coder