Pinbar EA problems

 

Hello, I am trying to develop an EA that detects pinbars.

I am in the beginning stages of this, but I am having trouble with the code.. it is not detecting any pinbars even though there are obvious candles meeting the conditions.


Any idea what may be wrong with this base code?

#property strict

// Define parameters
input int    minWickLength = 5;     // Minimum length of the long wick in pips
input int    maxWickLength = 15;    // Maximum length of the long wick in pips
input double minWickRatio = 1.5;    // Minimum ratio of the long wick to the body
input double maxWickRatio = 12;    // Maximum ratio of the long wick to the body
input double minTailRatio = 0.1;    // Minimum ratio of the short tail to the long wick
input double maxTailRatio = 0.5;    // Maximum ratio of the short tail to the long wick

// Tolerance for double comparison
input double tolerance = 0.00001;

// Pinbar count
int pinbarCount = 0;

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
{
   return(INIT_SUCCEEDED);
}

//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
{
   // Get candlestick information for the previous candle
   double openPrev   = iOpen(_Symbol, PERIOD_CURRENT, 1);
   double closePrev  = iClose(_Symbol, PERIOD_CURRENT, 1);
   double highPrev   = iHigh(_Symbol, PERIOD_CURRENT, 1);
   double lowPrev    = iLow(_Symbol, PERIOD_CURRENT, 1);

   // Calculate wick and body lengths for the previous candle
   double wickLengthPips = MathAbs(highPrev - lowPrev) / _Point;
   double bodyLengthPips = MathAbs(openPrev - closePrev) / _Point;

   // Calculate ratios with additional checks
   double wickRatio = (wickLengthPips > tolerance && bodyLengthPips > tolerance) ? wickLengthPips / bodyLengthPips : 0.0;
   double tailRatio = (wickLengthPips > tolerance) ? (highPrev - MathMax(openPrev, closePrev)) / wickLengthPips : 0.0;

   Print("Previous Candle - Wick Length: ", wickLengthPips);
   Print("Previous Candle - Body Length: ", bodyLengthPips);
   Print("Previous Candle - Wick Ratio: ", wickRatio);
   Print("Previous Candle - Tail Ratio: ", tailRatio);

   // Check pin bar conditions
   bool isPinbar = IsPinbar(wickLengthPips, bodyLengthPips, wickRatio, tailRatio);

   if (isPinbar)
   {
      pinbarCount++;
      PrintPinbarStatus(true);
   }
   else
   {
      PrintPinbarStatus(false);
   }
}

//+------------------------------------------------------------------+
//| Function to check if the candle is a Pinbar                     |
//+------------------------------------------------------------------+
bool IsPinbar(double wickLength, double bodyLength, double wickRatio, double tailRatio)
{
   // Check pin bar conditions
   return (wickLength >= minWickLength && wickLength <= maxWickLength &&
           wickRatio >= minWickRatio && wickRatio <= maxWickRatio &&
           tailRatio >= minTailRatio && tailRatio <= maxTailRatio);
}

//+------------------------------------------------------------------+
//| Function to print Pinbar status                                 |
//+------------------------------------------------------------------+
void PrintPinbarStatus(bool isPinbar)
{
   if (isPinbar)
      Print("This candle is a Pinbar! Total Pinbars: ", pinbarCount);
   else
      Print("This candle is not a Pinbar. Total Pinbars: ", pinbarCount);
}
 
  1.    double wickLengthPips = MathAbs(highPrev - lowPrev) / _Point;
       double bodyLengthPips = MathAbs(openPrev - closePrev) / _Point;
    

    PIP, Point, or Tick are all different in general.
              Ticks, PIPs or points in the GUI. Make up your mind. - MQL4 programming forum #1 (2014)
              Percentage in point - Wikipedia

    Unless you manually adjust your SL/TP for each separate symbol, using Point means code breaks on 4 digit brokers (if any still exists), exotics (e.g. USDZAR where spread is over 500 points), and metals. Compute what a logical PIP is and use that, not points.
              How to manage JPY pairs with parameters? - MQL4 programming forum (2017)
              Slippage defined in index points - Expert Advisors and Automated Trading - MQL5 programming forum (2018)

  2. Marcel: .. it is not detecting any pinbars even though 

    Use the debugger or print out your variables, including _LastError and prices and find out why. Do you really expect us to debug your code for you?
              Code debugging - Developing programs - MetaEditor Help
              Error Handling and Logging in MQL5 - MQL5 Articles (2015)
              Tracing, Debugging and Structural Analysis of Source Code - MQL5 Articles (2011)
              Introduction to MQL5: How to write simple Expert Advisor and Custom Indicator - MQL5 Articles (2010)

 
Your topic has been moved to the section: Expert Advisors and Automated Trading
Please consider which section is most appropriate — https://www.mql5.com/en/forum/172166/page6#comment_49114893