EA Trading Issue - Help Appreciated!

 

Hello! I'm a newbie to MQL4 and Meta Trader. I've programmed in C++ many moons ago, so I'm hoping I've got the bulk of my code correct. I've not yet written all the script, doing it in stages, I've currently written most of the "buy point" determination and order sending, I have not yet programmed anything to close an order. The script compiles with no issues. I have tried running both the strategy tester and real data debugger, the script appears to initialise okay, but then doesn't appear to do anything, no orders are ever placed. I'm hoping I have missed something simple.

My script is:

// Trade Size Management
double Lots = 0.1; // Lots: Basic lot size


// Miscellaneous
int Magic = 14092022; // Magic: Order magic number for EA tracking - think this will need changing

// Global Variables
int LastBars = 0;
double TakeProfit    =1000;
bool HaveLongPosition;
bool HaveShortPosition;
bool DirectionBullish = false;
bool DirectionBearish = false;
bool RenkoBull = false;
bool RenkoBear = false;
bool MACDLongBuy = false;
bool MACDShortSell = false;

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---

//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---

  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {

   DirectionTesting();

  }

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void DirectionTesting()
  {

  if((!IsTradeAllowed()) || (IsTradeContextBusy()) || (!IsConnected()) || ((!MarketInfo(Symbol(), MODE_TRADEALLOWED)) && (!IsTesting())))
   return;

// Trade only if a new bar has arrived.
 if(LastBars != Bars)
   LastBars = Bars;
  else
  return;

// Close conditions.
   bool BearishHAClose = false;
   bool BullishHAClose = false;

// Signals.
   bool Bullish = false;
   bool Bearish = false;

// Heiken Ashi indicator values.
   double HAOpenLatest, HAOpenPrevious, HACloseLatest, HAClosePrevious;

   HAOpenLatest = iCustom(NULL, 240, "Heiken Ashi", 2, 1);  
   HAOpenPrevious = iCustom(NULL, 240, "Heiken Ashi", 2, 2);
   HACloseLatest = iCustom(NULL, 240, "Heiken Ashi", 3, 1);
   HAClosePrevious = iCustom(NULL, 240, "Heiken Ashi", 3, 2);

// 4hr Heiken Ashi last bar trend direction.
// Bullish HA candle
   if(HAOpenLatest < HACloseLatest)
     {
      BullishHAClose = true;
     }
// Bearish HA candle
   else
      if(HAOpenLatest > HACloseLatest)
        {
         BearishHAClose = true;
        }

   bool EMABullish = false;
   bool EMABearish = false;

   double MACurrent;
   double MAPrevious;
   int    MovingPeriod  =20;
   int    MovingShift   =0;

//--- Moving Average Definition

   MACurrent=iMA(NULL,30,MovingPeriod,MovingShift,MODE_EMA,PRICE_CLOSE,0);
   MAPrevious=iMA(NULL,30,MovingPeriod,MovingShift,MODE_EMA,PRICE_CLOSE,1);

// Moving average trend determination
   if((Close[1]>MACurrent) && (MACurrent>MAPrevious))
     {
      EMABullish = true;
     }
   else
      if((Close[1]<MACurrent) && (MACurrent<MAPrevious))
        {
         EMABearish = true;
        }
      else
        {
         EMABullish = false;
         EMABearish = false;
        }

// Trend direction

   DirectionBullish = false;
   DirectionBearish = false;

   if(BullishHAClose && EMABullish)
     {
      DirectionBullish = true;
      BuySignals();
     }
   else
      if(BearishHAClose && EMABearish)
        {
         DirectionBearish = true;
         BuySignals();
        }
      else
        {
         DirectionBullish = false;
         DirectionBearish = false;
        }

   return;

  }

// Renko

void BuySignals()

  {

   double RenkoPipSize = 0.0005; // 5 pips
   double ReferencePrice = iClose(NULL, 1, 1);  // Most recent close
   double RenkoPriceChange = 0;
   RenkoBull = false;
   RenkoBear = false;

   while(MathAbs(RenkoPriceChange) < RenkoPipSize)
     {

      RenkoPriceChange = iClose(NULL, 1, 1) - ReferencePrice;

     }


   if(RenkoPriceChange > 0)
     {

      RenkoBull = true;

     }

   else
      if(RenkoPriceChange < 0)
        {

         RenkoBear = true;

        }

      else
        {

         RenkoBull = false;
         RenkoBear = false;
         DirectionTesting();

        }

// MACD

   double MACDOpenLevel =3;
   double MACDCloseLevel=2;
   double MACDCurrent,MACDPrevious,MACDPrevious2,MACDPrevious3,MACDPrevious4,MACDPrevious5,MACDPrevious6,MACDPrevious7,MACDPrevious8;
   double SignalCurrent,SignalPrevious,SignalPrevious2,SignalPrevious3,SignalPrevious4,SignalPrevious5,SignalPrevious6,SignalPrevious7,SignalPrevious8;

   MACDCurrent=iMACD(NULL,30,12,26,9,PRICE_CLOSE,MODE_MAIN,0);
   MACDPrevious=iMACD(NULL,30,12,26,9,PRICE_CLOSE,MODE_MAIN,1);
   MACDPrevious2=iMACD(NULL,30,12,26,9,PRICE_CLOSE,MODE_MAIN,2);
   MACDPrevious3=iMACD(NULL,30,12,26,9,PRICE_CLOSE,MODE_MAIN,3);
   MACDPrevious4=iMACD(NULL,30,12,26,9,PRICE_CLOSE,MODE_MAIN,4);
   MACDPrevious5=iMACD(NULL,30,12,26,9,PRICE_CLOSE,MODE_MAIN,5);
   MACDPrevious6=iMACD(NULL,30,12,26,9,PRICE_CLOSE,MODE_MAIN,6);
   MACDPrevious7=iMACD(NULL,30,12,26,9,PRICE_CLOSE,MODE_MAIN,7);
   MACDPrevious8=iMACD(NULL,30,12,26,9,PRICE_CLOSE,MODE_MAIN,8);
   SignalCurrent=iMACD(NULL,30,12,26,9,PRICE_CLOSE,MODE_SIGNAL,0);
   SignalPrevious=iMACD(NULL,30,12,26,9,PRICE_CLOSE,MODE_SIGNAL,1);
   SignalPrevious2=iMACD(NULL,30,12,26,9,PRICE_CLOSE,MODE_SIGNAL,2);
   SignalPrevious3=iMACD(NULL,30,12,26,9,PRICE_CLOSE,MODE_SIGNAL,3);
   SignalPrevious4=iMACD(NULL,30,12,26,9,PRICE_CLOSE,MODE_SIGNAL,4);
   SignalPrevious5=iMACD(NULL,30,12,26,9,PRICE_CLOSE,MODE_SIGNAL,5);
   SignalPrevious6=iMACD(NULL,30,12,26,9,PRICE_CLOSE,MODE_SIGNAL,6);
   SignalPrevious7=iMACD(NULL,30,12,26,9,PRICE_CLOSE,MODE_SIGNAL,7);
   SignalPrevious8=iMACD(NULL,30,12,26,9,PRICE_CLOSE,MODE_SIGNAL,8);

   MACDLongBuy = false;
   MACDShortSell = false;

// MACD Long Buy Point Determination - Past 4 hours switch allowed

   if(MACDCurrent>SignalCurrent && (MACDPrevious<SignalPrevious || MACDPrevious2<SignalPrevious2 || MACDPrevious3<SignalPrevious3 || MACDPrevious4<SignalPrevious4 || MACDPrevious5<SignalPrevious5 ||
                                    MACDPrevious6<SignalPrevious6 || MACDPrevious7<SignalPrevious7 || MACDPrevious8<SignalPrevious8) && MathAbs(MACDCurrent)>(MACDOpenLevel*Point))
     {

      MACDLongBuy = true;
     }

// MACD Short Sell Point Determination - Past 4 hours switch allowed

   else
      if(MACDCurrent<SignalCurrent && (MACDPrevious>SignalPrevious || MACDPrevious2>SignalPrevious2 || MACDPrevious3>SignalPrevious3 || MACDPrevious4>SignalPrevious4 || MACDPrevious5>SignalPrevious5 ||
                                       MACDPrevious6>SignalPrevious6 || MACDPrevious7>SignalPrevious7 || MACDPrevious8>SignalPrevious8) && MACDCurrent>(MACDOpenLevel*Point))
        {

         MACDShortSell = true;
        }
      else
        {
         MACDLongBuy = false;
         MACDShortSell = false;
         DirectionTesting();
        }
  }

void BuyDetermination()

  {
   int    cnt,ticket,total;

   if(DirectionBullish && RenkoBull && MACDLongBuy)
     {

      // Buy long function

      ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,3,0,Ask+TakeProfit*Point,"Long Buy",0,0,Green);
      if(ticket>0)
        {
         if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES))
            Print("BUY order opened : ",OrderOpenPrice());
         DirectionTesting();
        }
      else
         Print("Error opening BUY order : ",GetLastError());
      DirectionTesting();

     }

   else
      if(DirectionBearish && RenkoBear && MACDShortSell)
        {

         // Buy short function

         ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,3,0,Bid-TakeProfit*Point,"Short Sell",0,0,Red);
         if(ticket>0)
           {
            if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES))
               Print("SELL order opened : ",OrderOpenPrice());
            DirectionTesting();
           }
         else
            Print("Error opening SELL order : ",GetLastError());
         DirectionTesting();

        }

      else
        {

         DirectionTesting();

        }

  }


//---

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

Any help is hugely appreciated!

Many thanks,

James

 
JamesFleming: The script compiles with no issues. 
  1. void OnTick()

    It's a EA not a Script.

  2.  if(LastBars != Bars)
       LastBars = Bars;
      else
      return;

    For a new bar test, Bars is unreliable (a refresh/reconnect can change number of bars on chart), volume is unreliable (miss ticks), Price is unreliable (duplicate prices and The == operand. - MQL4 programming forum.) Always use time.
              MT4: New candle - MQL4 programming forum #3 (2014)
              MT5: Accessing variables - MQL4 programming forum #3 (2022)

    I disagree with making a new bar function, because it can only be called once per tick (second call returns false). A variable can be tested multiple times.
              Running EA once at the start of each bar - MQL4 programming forum (2011)

  3.    MACurrent=iMA(NULL,30,MovingPeriod,MovingShift,MODE_EMA,PRICE_CLOSE,0);
       MAPrevious=iMA(NULL,30,MovingPeriod,MovingShift,MODE_EMA,PRICE_CLOSE,1);

    You shouldn't be looking at bar zero.

  4. JamesFleming: but then doesn't appear to do anything, no orders are ever placed.
    void BuyDetermination()

    Perhaps you should call your function somewhere.

  5. ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,3,0,Ask+TakeProfit*Point,"Long Buy",0,0,Green);

    You buy at the Ask and sell at the Bid. Pending Buy Stop orders become market orders when hit by the Ask.

    1. Your buy order's TP/SL (or Sell Stop's/Sell Limit's entry) are triggered when the Bid / OrderClosePrice reaches it. Using Ask±n, makes your SL shorter and your TP longer, by the spread. Don't you want the specified amount used in either direction?

    2. Your sell order's TP/SL (or Buy Stop's/Buy Limit's entry) will be triggered when the Ask / OrderClosePrice reaches it. To trigger close to a specific Bid price, add the average spread.
                MODE_SPREAD (Paul) - MQL4 programming forum - Page 3 #25

    3. The charts show Bid prices only. Turn on the Ask line to see how big the spread is (Tools → Options (control+O) → charts → Show ask line.)
      Most brokers with variable spreads widen considerably at end of day (5 PM ET) ± 30 minutes. My GBPJPY shows average spread = 26 points, but average maximum spread = 134 (your broker will be similar).

  6. Risk depends on your initial stop loss, lot size, and the value of the symbol. It does not depend on margin and leverage. No SL means you have infinite risk (on leveraged symbols). Never risk more than a small percentage of your trading funds, certainly less than 2% per trade, 6% total.

    1. You place the stop where it needs to be — where the reason for the trade is no longer valid. E.g. trading a support bounce, the stop goes below the support.

    2. AccountBalance * percent/100 = RISK = OrderLots * (|OrderOpenPrice - OrderStopLoss| * DeltaPerLot + CommissionPerLot) (Note OOP-OSL includes the spread, and DeltaPerLot is usually around $10/PIP but it takes account of the exchange rates of the pair vs. your account currency.)

    3. Do NOT use TickValue by itself - DeltaPerLot and verify that MODE_TICKVALUE is returning a value in your deposit currency, as promised by the documentation, or whether it is returning a value in the instrument's base currency.
                MODE_TICKVALUE is not reliable on non-fx instruments with many brokers - MQL4 programming forum (2017)
                Is there an universal solution for Tick value? - Currency Pairs - General - MQL5 programming forum (2018)
                Lot value calculation off by a factor of 100 - MQL5 programming forum (2019)

    4. You must normalize lots properly and check against min and max.

    5. You must also check FreeMargin to avoid stop out

    6. For MT5, see 'Money Fixed Risk' - MQL5 Code Base (2017)

    Most pairs are worth about $10 per PIP. A $5 risk with a (very small) 5 PIP SL is $5/$10/5 or 0.1 Lots maximum.

 
William Roeder, thank you for your very helpful reply, it helps considerably! I’ll work through your points. 

Have a good day! 
Reason: