Undeclared Identifier Error

 

Hi, I tried compiling this code for an EA but kept getting an error. Please help.

   if (secondCandleAfterBullish)
   {
      if (!PositionGetInteger(POSITION_TYPE))
      {
         Trade.Buy(magicNumber, 0.1, Ask, 0, 0, 0, "Buy", 0, clrNONE);
      }
   }

   if (secondCandleAfterBearish)
   {
      if (!PositionGetInteger(POSITION_TYPE))
      {
         Trade.Sell(magicNumber, 0.1, Bid, 0, 0, 0, "Sell", 0, clrNONE);
      }
   }

   if (PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_BUY)
   {
      Trade.PositionModify(PositionGetInteger(POSITION_TICKET), stopLoss, takeProfit);
   }
   else if (PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_SELL)
   {
      Trade.PositionModify(PositionGetInteger(POSITION_TICKET), takeProfit, stopLoss);
   }
  }

here is the error i get.

'Trade' - undeclared identifier ATM.mq5 53      10
'Trade' - undeclared identifier ATM.mq5 61      10
'Trade' - undeclared identifier ATM.mq5 67      7
'Trade' - undeclared identifier ATM.mq5 71      7
 
LuckyJ:

Hi, I tried compiling this code for an EA but kept getting an error. Please help.

here is the error i get.

Send your full code please.

I suppose you have declared « CTrade trade », and not « CTrade Trade ».

Here, the error tells you you have not declared the CTrade instance called « Trade ».
 
//+------------------------------------------------------------------+
//|                                                       pinbar.mq5 |
//|                        Copyright 2023, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#include <Trade\Trade.mqh>
CTrade Trade; // Declare a CTrade object

input double stopLossPct = 1.0;
input double takeProfitPct = 2.0;

input int magicNumber = 12345; // Magic number to identify trades

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
   double bodySizePct = MathAbs(iOpen(Symbol(), Period(), 0) - iClose(Symbol(), Period(), 0)) / (iHigh(Symbol(), Period(), 0) - iLow(Symbol(), Period(), 0)) * 100;
   double candleMiddle = (iHigh(Symbol(), Period(), 0) - iLow(Symbol(), Period(), 0)) / 2;
   bool aboveMiddle = iOpen(Symbol(), Period(), 0) > candleMiddle && iClose(Symbol(), Period(), 0) > candleMiddle;
   bool belowMiddle = iOpen(Symbol(), Period(), 0) < candleMiddle && iClose(Symbol(), Period(), 0) < candleMiddle;

   bool bullishPinbarCondition = bodySizePct < 50 && belowMiddle;
   bool bearishPinbarCondition = bodySizePct < 50 && aboveMiddle;

   double bullishPinbarHigh = bullishPinbarCondition ? iHigh(Symbol(), Period(), 0) : 0;
   double bearishPinbarLow = bearishPinbarCondition ? iLow(Symbol(), Period(), 0) : 0;

   bool secondCandleAfterBullish = bullishPinbarHigh > 0 && iClose(Symbol(), Period(), 1) > bullishPinbarHigh;
   bool secondCandleAfterBearish = bearishPinbarLow > 0 && iClose(Symbol(), Period(), 1) < bearishPinbarLow;

   double stopLoss = PositionGetDouble(POSITION_PRICE_OPEN) * (1 - stopLossPct / 100);
   double takeProfit = PositionGetDouble(POSITION_PRICE_OPEN) * (1 + takeProfitPct / 100);

   if (secondCandleAfterBullish)
   {
      if (!PositionGetInteger(POSITION_TYPE))
      {
         trade.Buy(magicNumber, 0.1, Ask, 0, 0, 0, "Buy", 0, clrNONE);
      }
   }

   if (secondCandleAfterBearish)
   {
      if (!PositionGetInteger(POSITION_TYPE))
      {
         trade.Sell(magicNumber, 0.1, Bid, 0, 0, 0, "Sell", 0, clrNONE);
      }
   }

   if (PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_BUY)
   {
      trade.PositionModify(PositionGetInteger(POSITION_TICKET), stopLoss, takeProfit);
   }
   else if (PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_SELL)
   {
      trade.PositionModify(PositionGetInteger(POSITION_TICKET), takeProfit, stopLoss);
   }
  }
//+------------------------------------------------------------------+
Zaky Hamdoun #:
Send your full code please.

I suppose you have initialized « CTrade trade », and not « CTrade Trade ».

Here, the error tells you you have not initialized the CTrade instance called « Trade ».

Here's the full code

 
LuckyJ #:

Here's the full code

Object declaration
CTrade Trade; // Declare a CTrade object

Usage:
         trade.Buy(magicNumber, 0.1, Ask, 0, 0, 0, "Buy", 0, clrNONE);
Do you notice the error?



 
Dominik Christian Egert #:
Object declaration

Usage:
Do you notice the error?




Yeah I tried it but I'm still getting the "'trade' - undeclared identifier" error
 
LuckyJ #:
Yeah I tried it but I'm still getting the "'trade' - undeclared identifier" error
if you declare it using "Trade" you also must reference it using "Trade", not "trade".

Else, if that is not your error, you have shown the wrong code....