Fractal indicator into EA question

 

After convert fractal indicator into EA, the order didnt trigger at all.

I want it trigger buy when candle close above last fractal high, sell when candle close below last fractal low.

here is the coding, where did i done wrong??? please help

coding:



//+------------------------------------------------------------------+

//|                           Fractals Break EA -                          |

//+------------------------------------------------------------------+





// Input parameters

extern double LotSize = 0.01;               // Trading lot size

extern double TakeProfit = 600.0;           // Take profit in points

extern double StopLoss = 300.0;             // Stop loss in points

extern int FractalPeriod = 25;              // Fractal period



double val1[];

double val2[];

bool buySignal = false;

bool sellSignal = false;

bool isOrderOpen = false;



// Arrow displacements

extern double UpperArrowDisplacement = 1.0;  // Upper arrow displacement

extern double LowerArrowDisplacement = 1.0;  // Lower arrow displacement



//+------------------------------------------------------------------+

//|                                                                  |

//+------------------------------------------------------------------+

int init()

{

   SetIndexBuffer(0, val1);

   SetIndexStyle(0, DRAW_ARROW);

   SetIndexArrow(0, 159);

   SetIndexBuffer(1, val2);

   SetIndexStyle(1, DRAW_ARROW);

   SetIndexArrow(1, 159);



   return (0);

}



int deinit()

{

   return (0);

}



//+------------------------------------------------------------------+

//|                                                                  |

//+------------------------------------------------------------------+

int start()

{

   int half = FractalPeriod / 2;

   int i, limit, counted_bars = IndicatorCounted();



   if (counted_bars < 0)

      return (-1);

   if (counted_bars > 0)

      counted_bars--;

   limit = MathMin(MathMax(Bars - counted_bars, FractalPeriod), Bars - 1);



   for (i = limit; i >= 0; i--)

   {

      bool found = true;

      double compareTo = High[i];



      for (int k = 1; k <= half; k++)

      {

         if ((i + k) < Bars && High[i + k] > compareTo)

         {

            found = false;

            break;

         }

         if ((i - k) >= 0 && High[i - k] >= compareTo)

         {

            found = false;

            break;

         }

      }



      if (found)

         val1[i] = High[i] + iATR(NULL, 0, 20, i) * UpperArrowDisplacement;

      else

         val1[i] = val1[i + 1];



      found = true;

      compareTo = Low[i];



      for (k = 1; k <= half; k++)

      {

         if ((i + k) < Bars && Low[i + k] < compareTo)

         {

            found = false;

            break;

         }

         if ((i - k) >= 0 && Low[i - k] <= compareTo)

         {

            found = false;

            break;

         }

      }



      if (found)

         val2[i] = Low[i] - iATR(NULL, 0, 20, i) * LowerArrowDisplacement;

      else

         val2[i] = val2[i + 1];

   }



   // Check for buy and sell signals

   if (Close[0] > val1[1] && Close[1] <= val1[1])

   {

      buySignal = true;

      sellSignal = false;

   }

   else if (Close[0] < val2[1] && Close[1] >= val2[1])

   {

      buySignal = false;

      sellSignal = true;

   }

   else

   {

      buySignal = false;

      sellSignal = false;

   }



   // Execute trade based on signals

   if (!isOrderOpen && buySignal)

   {

      OpenBuyOrder();

   }

   else if (!isOrderOpen && sellSignal)

   {

      OpenSellOrder();

   }



   return (0);

}



//+------------------------------------------------------------------+

//|                                                                  |

//+------------------------------------------------------------------+

void OpenBuyOrder()

{

   double slPrice = NormalizeDouble(val2[1] - StopLoss * Point, Digits);

   double tpPrice = NormalizeDouble(val1[1] + TakeProfit * Point, Digits);



   int ticket = OrderSend(Symbol(), OP_BUY, LotSize, Ask, 10, slPrice, tpPrice);

   if (ticket > 0)

      isOrderOpen = true;

}



//+------------------------------------------------------------------+

//|                                                                  |

//+------------------------------------------------------------------+

void OpenSellOrder()

{

   double slPrice = NormalizeDouble(val1[1] + StopLoss * Point, Digits);

   double tpPrice = NormalizeDouble(val2[1] - TakeProfit * Point, Digits);



   int ticket = OrderSend(Symbol(), OP_SELL, LotSize, Bid, 10, slPrice, tpPrice);

   if (ticket > 0)

      isOrderOpen = true;

}


 
johntsai: After convert fractal indicator into EA,

Don't try to do that. There are no buffers, no IndicatorCounted() or prev_calculated. No way to know if older bars have changed or been added (history update.)

Just get the value(s) of the indicator(s) into EA/indicator (using iCustom) and do what you want with it.
          (MT4) Detailed explanation of iCustom - MQL4 programming forum (2017)