Struggling with getting vaule from iFractals

 

Hi, 

Can anyone help me with understanding how to get the value from iFractal please? It returns type double which I assume to be the price of the fractal but I'm just getting 0? The logic seems sound, so I'm assuming it's my misunderstanding of iFractal.

My current code 


//+------------------------------------------------------------------+
//| Global Variables                                                 |
//+------------------------------------------------------------------+

double fractalUpper = DBL_MIN;
double fractalLower = DBL_MAX;

datetime lastBar = 0;

//**********************************************************************

int OnInit()
{

   ChartSetInteger(0, CHART_MODE, CHART_CANDLES); //Show candlesticks
   ChartSetInteger(0, CHART_SHOW_GRID, false); //Turn off grid

   return INIT_SUCCEEDED;
}

void OnTick()
{
   if (lastBar != iTime(Symbol(), PERIOD_H4, 0))
   {
      fractal();
      comment();
      lastBar = iTime(Symbol(), PERIOD_H4, 0);
   }
}


//+------------------------------------------------------------------+
//| Functions                                                        |
//+------------------------------------------------------------------+

void fractal()
{
   fractalUpper = iFractals(Symbol(), PERIOD_H4, MODE_UPPER, 0);
   fractalLower = iFractals(Symbol(), PERIOD_H4, MODE_LOWER, 0);
}

void comment()
{
   Comment("Last upper price: ", fractalUpper,
           "\nLast lower price: ", fractalLower);
}


Thank you

 
The value is non-zero only if there is a Swing HL in that index.
 
Yashar Seyyedin #:
The value is non-zero only if there is a Swing HL in that index.

Ahh I see. Thank you.

I've changed the shift to 3, which will be the location of the fractal from the current bar and everything works. Thanks for your help :)


I just need to add some logic to update only if the number is non-zero as it changes back to zero once it moves onto the next bar.

 
This seems to work fine.

// //+------------------------------------------------------------------+
// //| Global Variables                                                 |
// //+------------------------------------------------------------------+

double currentFractalUpper = DBL_MIN;
double currentFractalLower = DBL_MAX;
double lastFractalUpper, lastFractaLower;

datetime lastBar = 0;

int OnInit()
{
   //--- Display as candlesticks
   ChartSetInteger(0, CHART_MODE, CHART_CANDLES);
   ChartSetInteger(0, CHART_SHOW_GRID, false);

   return INIT_SUCCEEDED;
}

void OnTick()
{
   if (lastBar != iTime(Symbol(), PERIOD_H4, 0))
   {
      fractal();
      comment();

      lastBar = iTime(Symbol(), PERIOD_H4, 0);
   }
}

//**//**//**//*//*//**//**//**//**//**//*//*//**//**//**//**//**//**

//+------------------------------------------------------------------+
//| Functions                                                        |
//+------------------------------------------------------------------+

void fractal()
{

   currentFractalUpper = iFractals(Symbol(), PERIOD_H4, MODE_UPPER, 3);
   currentFractalLower = iFractals(Symbol(), PERIOD_H4, MODE_LOWER, 3);

   if (currentFractalUpper > 0)
   {
      lastFractalUpper = currentFractalUpper;
   }

   if (currentFractalLower > 0)
   {
      lastFractaLower = currentFractalLower;
   }
}

void comment()
{
   Comment("Last Fractal High: ", lastFractalUpper,
           "\nLast Fractal Low: ", lastFractaLower);
}
 
I've made a few changes and tried to get a dot drawn at the price of the fractal. Something a little odd is happening. It's drawing the dots and giving the correct datetime and price, but then including another price which I'm not asking for and drawing the dot in that price location.

// //+------------------------------------------------------------------+
// //| Global Variables                                                 |
// //+------------------------------------------------------------------+

double currentFractalUpper = DBL_MIN;
double currentFractalLower = DBL_MAX;
double lastFractalUpper, lastFractalLower;

string fractalPoint = "NULL";
datetime timeBar = 0;

datetime lastBar = 0;

int OnInit()
{
   //--- Display as candlesticks
   ChartSetInteger(0, CHART_MODE, CHART_CANDLES);
   ChartSetInteger(0, CHART_SHOW_GRID, false);

   return INIT_SUCCEEDED;
}

void OnTick()
{

   datetime timeNow = TimeCurrent(); // Current date and time

   if (lastBar != iTime(Symbol(), PERIOD_H4, 0))
   {
      fractal(timeNow);
      comment();

      lastBar = iTime(Symbol(), PERIOD_H4, 0);
   }
}

//**//**//**//*//*//**//**//**//**//**//*//*//**//**//**//**//**//**

//+------------------------------------------------------------------+
//| Functions                                                        |
//+------------------------------------------------------------------+

void fractal(datetime timeNow)
{

   currentFractalUpper = iFractals(Symbol(), PERIOD_H4, MODE_UPPER, 3);
   currentFractalLower = iFractals(Symbol(), PERIOD_H4, MODE_LOWER, 3);

   string fractalName;

   if (currentFractalUpper > 0)
   {
      lastFractalUpper = currentFractalUpper;

      timeBar = iTime(Symbol(), PERIOD_H4, 3);

      fractalName = "FU Time: " + TimeToStr(timeBar) + "\nFU Price: " + lastFractalUpper;

      drawFractal(lastFractalUpper, fractalName);

      Print("FU time: ", TimeToStr(iTime(Symbol(), PERIOD_H4, 3), TIME_DATE | TIME_MINUTES)); //debug
   }

   if (currentFractalLower > 0)
   {

      lastFractalLower = currentFractalLower;

      timeBar = iTime(Symbol(), PERIOD_H4, 3);

      fractalName = "FL Time: " + TimeToStr(timeBar) + "\nFL Price: " + lastFractalLower;

      drawFractal(lastFractalLower, fractalName);

      Print("FL time: ", TimeToStr(iTime(Symbol(), PERIOD_H4, 3), TIME_DATE | TIME_MINUTES)); //debug
   }

   fractalName = "NULL"; //debug
}

void comment()
{
   Comment("Last swing high price: ", lastFractalUpper,
           "\nLast swing low price: ", lastFractalLower);
}

// //+------------------------------------------------------------------+
// //| Draw marker above / below fractal                                |
// //+------------------------------------------------------------------+

void drawFractal(double fractalPrice, string fractalName)
{

   ObjectCreate(fractalName, OBJ_ARROW, 0, timeBar, fractalPrice); 
   ObjectSet(fractalName, OBJPROP_ARROWCODE, 159);                 // Code for an dot
   ObjectSet(fractalName, OBJPROP_SELECTABLE, false);              // Make the objectnot selectable
   ObjectSet(fractalName, OBJPROP_SELECTED, false);                // Deselect the object

   Print("Time check: ", TimeToStr(timeBar)); //debug
}


The dot is drawn at the price location that I've highlighed with a red arrow, but I have no clue where it's coming from. The other values are correct in that it is a fractal at that point in time with the FU Price being the correct price of that fractal.

Wrong coord