My ea only placed once a buy trade, and only once a sell trade

 

Can't figure out how to debug it.

Someone has any idea?

//+------------------------------------------------------------------+
//|                                            8-21EMAs Strategy.mq4 |
//|                        Copyright 2022, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2022, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict
#include <Custom Functions.mqh>

//inputs

input int StopLoss         = 30;
input int TakeProfit       = 60;

input int FastMaPeriod     = 8;
input int FastMaShift      = 0;
input int FastMaMethod     = 1;
input int FastMaApplied    = 0;

input int SlowMaPeriod     = 21;
input int SlowMaShift      = 0;
input int SlowMaMethod     = 1;
input int SlowMaApplied    = 0;

input int MagicNumber      = 123;
input int Slippage         = 3;
input double LotSize       = 0.1;


//vars

int BuyTicket;
int SellTicket;

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
   
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
   double   FastMaValue        = iMA(Symbol(), 0, FastMaPeriod, FastMaShift, FastMaMethod, FastMaApplied, 2);
   double   SlowMaValue        = iMA(Symbol(), 0, SlowMaPeriod, SlowMaShift, SlowMaMethod, SlowMaApplied, 2);
   
   bool     conditionB         = FastMaValue > SlowMaValue;
   bool     conditionBEC       = Close[1] > High[2] &&
                                 Close[1] > Open[1] &&
                                 Close[2] < Open[2];
   bool     conditionS         = FastMaValue < SlowMaValue;
   bool     conditionSEC       = Close[1] < Low[2] &&
                                 Close[1] < Open[1] &&
                                 Close[2] > Open[2];
   
   if(conditionB)//buying
   {
      if(BuyTicket == 0)
        {
         if(conditionBEC)
           {
            OpenMarketOrder(0);
           }
        }
   }
  
   if(conditionS)//selling
   {
      if(SellTicket == 0)
        {
         if(conditionSEC)
           {
            OpenMarketOrder(1);
           }
        }
   }
  }
//+------------------------------------------------------------------+



int OpenMarketOrder(int direction)
{
   if(direction == 0)
     {
      double bsl     = 0;
      double btp     = 0;
      if(StopLoss != 0)    bsl = Ask - (StopLoss * PipValue());
      if(TakeProfit != 0)  btp = Ask + (TakeProfit * PipValue());
      BuyTicket = OrderSend(Symbol(), direction, LotSize, Ask, Slippage, bsl, btp, "Buy Order", MagicNumber);
      return BuyTicket;
     }
    
    if(direction == 1)
     {
      double ssl     = 0;
      double stp     = 0;
      if(StopLoss != 0)    ssl = Bid + (StopLoss * PipValue());
      if(TakeProfit != 0)  stp = Bid - (TakeProfit * PipValue());
      SellTicket = OrderSend(Symbol(), direction, LotSize, Bid, Slippage, ssl, stp, "Sell Order", MagicNumber);
      return SellTicket;
     }
    return(0);
}
 
  1. Shele: Can't figure out how to debug it.

    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)

  2.       if(StopLoss != 0)    bsl = Ask - (StopLoss * PipValue());
          if(TakeProfit != 0)  btp = Ask + (TakeProfit * PipValue());

    You buy at the Ask and sell at the Bid. Pending Buy Stop orders become market orders when hit and open at 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).

Reason: