Code Library / Fibonacci

MQL5 Auto Fibonacci

Auto Fibonacci retracement from recent swing high/low.

Language: MQL5 · Category: Fibonacci · Tags: Fibonacci, retracement, MQL5, auto, swing

Source code

struct Fib { double ratio; double price; };
void CalcFib(Fib &f[],int lookback) {
   double h[],l[]; CopyHigh(_Symbol,PERIOD_CURRENT,0,lookback,h); CopyLow(_Symbol,PERIOD_CURRENT,0,lookback,l);
   double hi=h[ArrayMaximum(h)],lo=l[ArrayMinimum(l)],r=hi-lo;
   double ratios[]={0,0.236,0.382,0.5,0.618,0.786,1.0};
   ArrayResize(f,7);
   for(int i=0;i<7;i++){f[i].ratio=ratios[i];f[i].price=hi-r*ratios[i];}
}

How to use this snippet

This is a building block, not a finished trading system. Paste it into your own MQL5 project, compile it, and test it on a demo account with your broker's real spreads before relying on it. If MetaEditor reports errors, the compilation error fixer can help.

Related snippets