Skip to content
← Back to the research library
MQL5 SOURCE COLLECTION / Mean Reversion

Fibonacci & ZigZag Retracement

Builds retracement triggers from the two most recent non-zero ZigZag points and draws the corresponding Fibonacci object.

MT5 / MQL5Original sourceSource issue noted

H1 ZigZag / tick entries · Published 28 October 2026

Source inspectionLimited static review
MetaEditor compilationNot performed
Backtest / live recordNot supplied

How the source is structured

  1. On each new H1 bar, scan the ZigZag buffer for two recent pivot values.
  2. Calculate a 61.8% retracement trigger for a long or short entry.
  3. Attempt an order when price crosses the trigger, then mark that trigger as consumed.

Default inputs

As supplied; these are not optimized or recommended settings.
Volume0.1 lot
Entry / stop / target levels61.8 / 100 / 0
ZigZag periodH1
Depth / deviation / backstep12 / 5 / 3
BEFORE YOU TEST

Source-review notes

These observations come from reading this uploaded version. They are not compiler output or a complete code audit. The download retains the source as supplied.

SOURCE LINES 63–65

The buy target uses FiboEntryLevel instead of FiboTpLevel, making the calculated target equal to the trigger.

SOURCE LINES 71–73

The sell stop and target add the level percentage to the full swing instead of multiplying the swing by it.

SOURCE LINES 49–51, 87–94

Buffer-copy results and order success are not checked before consuming values or triggers. Recent ZigZag pivots also need stability checks in a backtest.

Dependencies

Examples\ZigZag.ex5 is referenced by iCustom. That indicator is not included in this upload.

Source & attribution

No author or license declaration appears in the supplied source.

Included in the Edge Quant source collection with existing notices preserved. Free download does not imply a new license grant.

Original filename: Fibonacci.mq5
Encoding: UTF-8

Inspect the original source

The browser preview is decoded for reading; the download keeps the original file bytes.

Direct .mq5 file ↓
Expand full MQL5 source · 113 lines
#include <Trade/Trade.mqh>input double Lots = 0.1;input double FiboEntryLevel = 61.8;input double FiboSlLevel = 100;input double FiboTpLevel = 0;input ENUM_TIMEFRAMES ZigZagTimeframe = PERIOD_H1;input int ZigZagDepth = 12;input int ZigZagDeviation = 5;input int ZigZagBackStep = 3;CTrade trade;int handleZigZag;datetime timestamp;double triggerBuy;double slBuy;double tpBuy;double triggerSell;double slSell;double tpSell;int OnInit(){   handleZigZag = iCustom(_Symbol,ZigZagTimeframe,"Examples\\ZigZag.ex5",ZigZagDepth,ZigZagDeviation,ZigZagBackStep);   return(INIT_SUCCEEDED); }void OnDeinit(const int reason){   }void OnTick(){   static double zigZagLast = 0;   static datetime zigZagLastTime = 0;   static double zigZagLastLast = 0;   static datetime zigZagLastLastTime = 0;   datetime time0 = iTime(_Symbol,ZigZagTimeframe,0);   if(timestamp != time0){      timestamp = time0;               zigZagLast = 0;         zigZagLastLast = 0;         for(int i = 1; i <1000; i++){            double zigZag[];            CopyBuffer(handleZigZag,MAIN_LINE,i,1,zigZag);            if(zigZag[0] != EMPTY_VALUE && zigZag[0] != 0){               if(zigZagLast ==0){               zigZagLast = zigZag[0];               zigZagLastTime = iTime(_Symbol,ZigZagTimeframe,i);               }else{                  zigZagLastLast = zigZag[0];                  zigZagLastLastTime = iTime(_Symbol,ZigZagTimeframe,i);               }            }            if(zigZagLast != 0 && zigZagLastLast != 0){               if(zigZagLast > zigZagLastLast && triggerBuy < DBL_MAX){               if(triggerSell >= DBL_MAX) triggerSell = 0;               triggerBuy = zigZagLast - (zigZagLast - zigZagLastLast) * FiboEntryLevel / 100;               slBuy = zigZagLast - (zigZagLast - zigZagLastLast) * FiboSlLevel / 100;               tpBuy = zigZagLast - (zigZagLast - zigZagLastLast) * FiboEntryLevel / 100;                              createFibo("FiboBuy",zigZagLastLastTime,zigZagLastLast,zigZagLastTime,zigZagLast);             }             if(zigZagLast < zigZagLastLast && triggerSell < DBL_MAX){               if(triggerBuy >=DBL_MAX) triggerBuy = 0;               triggerSell = zigZagLast + (zigZagLastLast - zigZagLast) * FiboEntryLevel / 100;               slSell = zigZagLast + (zigZagLastLast - zigZagLast) + FiboSlLevel / 100;               tpSell = zigZagLast + (zigZagLastLast - zigZagLast) + FiboTpLevel / 100;                              createFibo("FiboSell",zigZagLastLastTime,zigZagLastLast,zigZagLastTime,zigZagLast);             }                          break;           }         }       }              double bid = SymbolInfoDouble(_Symbol,SYMBOL_BID);       double ask = SymbolInfoDouble(_Symbol,SYMBOL_ASK);              if(triggerBuy > 0 && triggerBuy < DBL_MAX && ask < triggerBuy){         trade.Buy(Lots,_Symbol,ask,slBuy,tpBuy);         triggerBuy = DBL_MAX;         if(zigZagLast < zigZagLastLast) triggerBuy = 0;       }       if(triggerSell > 0 && triggerSell < DBL_MAX && bid > triggerSell){         trade.Sell(Lots,_Symbol,ask,slSell,tpSell);         triggerSell = DBL_MAX;         if(zigZagLast > zigZagLastLast) triggerBuy = 0;       }      }void createFibo(string prefix, datetime zigZagLastLastTime, double zigZagLastLast, datetime zigZagLastTime, double zigZagLast){   ObjectsDeleteAll(0,prefix);      string objName = prefix+TimeToString(zigZagLastLastTime);   ObjectCreate(0,objName,OBJ_FIBO,0,zigZagLastLastTime,zigZagLastLast,zigZagLastTime,zigZagLast);      for(int i = 0; i < ObjectGetInteger(0,objName,OBJPROP_LEVELS); i++){      if(prefix=="FiboBuy") ObjectSetInteger(0,objName,OBJPROP_LEVELCOLOR,i,clrBlue);      if(prefix=="FiboBSell") ObjectSetInteger(0,objName,OBJPROP_LEVELCOLOR,i,clrRed);      ObjectSetInteger(0,objName,OBJPROP_LEVELSTYLE,i,STYLE_DASH);      ObjectSetInteger(0,objName,OBJPROP_LEVELWIDTH,i,1);     }     if(prefix=="FiboBuy")ObjectSetInteger(0,objName,OBJPROP_COLOR,clrBlue);     if(prefix=="FiboSell")ObjectSetInteger(0,objName,OBJPROP_COLOR,clrRed);}

SHA-256: 753d8e666db708fe2e6cbb9ea2619877455d48de23cd9d949765f65779c620e9

Educational source material, not investment advice. No profitability, execution quality or live-readiness claim is made for this upload.