Search code examples
fibonaccimql4metatrader4mt4

How to automatically calculate fibonnacci levels from yesterday/prev day in MQL4?


how do I calculate the fibo levels from yesterday/previous day.

This is how far I am currently:

 int shift   = iBarShift( NULL, PERIOD_D1, Time[0] ) + 1;   // yesterday
 HiPrice     = iHigh(     NULL, PERIOD_D1, shift);
 LoPrice     = iLow (     NULL, PERIOD_D1, shift);
 StartTime   = iTime(     NULL, PERIOD_D1, shift);

 if ( TimeDayOfWeek( StartTime ) == 0    /* Sunday */ )
 {                                       // Add fridays high and low
      HiPrice = MathMax( HiPrice, iHigh( NULL, PERIOD_D1, shift + 1 ) );
      LoPrice = MathMin( LoPrice, iLow(  NULL, PERIOD_D1, shift + 1 ) );
 }
 Range = HiPrice - LoPrice;

I think now I should have all values necessary for calculating it.

I am not sure on how I now can calculate the different levels now:
23.6 38.2 50.0 61.8 76.4 and -23.6 -38.2 -50.0 -61.8 -76.4 -100


Solution

  • All necessary Fibo-levels can be added manually as an array - this is the easiest way as far as I know. Then simply loop over such array and
    +values are ( high + array[i] / 100 * range ),
    values below the fibo - ( low - array[i] / 100 * range ),
    where
    array[] = { 23.6, 38.2, .. } ( only positive values are enough )