Adding trade limits for the day

 

Good afternoon everybody,

I recently created an EA and is working exactly as expected. The signals are being delivered correctly and it is opening them accordingly. Everything is perfect.

Now, there is one fault which I already have it contemplated on my manual trading and it is that it only has to open one trade a day. ONLY ONE TRADE.

Due to this, the performance of my EA is not as good as the manual trading. I would like to add this condition.

I will attach the code in here:

//+------------------------------------------------------------------+
//|                                                   Test - tma.mq4 |
//|                        Copyright 2023, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2023, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict


/////////Indicator Inputs//////////////
input int Body_Candle_Pips = 5;
input int DFS = 10;
input int InitialHour = 14;
input int InitialMinute = 00;
input int FinalHour = 17;
input int FinalMinute = 00;
input int RSIThreshhold = 50;

/////////Risk Management//////////////


input int InpTakeProfitPts = 100;
input int InpStopLossPts = 100;

input double InpOrderSize = 1.0;
input string InpTradeComment = __FILE__;
input int InpMagicNumber = 2000001;

double TakeProfit;
double StopLoss;

//Identify Buffer Names

const string IndicatorName = "TMA Overlay Strategy - test";
const int BufferBuy = 0;
const int BufferSell = 1;


//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
   double point = SymbolInfoDouble(Symbol(), SYMBOL_POINT);
   TakeProfit = InpTakeProfitPts*point;
   StopLoss = InpStopLossPts*point;
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
   
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
   if(!NewBar()) return;
   
   //Perform any calculations and analysis here
   
   double BuyCustom = iCustom(Symbol(), Period(), IndicatorName, Body_Candle_Pips, DFS, InitialHour, InitialMinute, FinalHour, FinalMinute, RSIThreshhold, BufferBuy, 1);
   double SellCustom = iCustom(Symbol(), Period(), IndicatorName, Body_Candle_Pips, DFS, InitialHour, InitialMinute, FinalHour, FinalMinute, RSIThreshhold, BufferSell, 1);
   
   //Execute strategy here
   bool buyCondition = false;
   bool sellCondition = false;
   
   if(BuyCustom != EMPTY_VALUE && NoTradesToday()){
      buyCondition = true;
   } 
   
   if(SellCustom != EMPTY_VALUE){
      sellCondition = true;
   }
  
  
   if(buyCondition){
   
      OrderOpen(ORDER_TYPE_BUY, StopLoss, TakeProfit);
   } else
   
   if(sellCondition){
   
      OrderOpen(ORDER_TYPE_SELL, StopLoss, TakeProfit);
   }
   
   
  }
//+------------------------------------------------------------------+


bool NewBar() {

   static datetime prevTime = 0;
   datetime currentTime = iTime(Symbol(), Period(), 0);
   if(currentTime != prevTime) {
      prevTime = currentTime;
      return(true);
   }
   return(false);

}


bool OrderOpen(ENUM_ORDER_TYPE orderType, double stopLoss, double takeProfit) {

   int ticket;
   double openPrice;
   double stopLossPrice;
   double takeProfitPrice;
   
   if(orderType == ORDER_TYPE_BUY){
      openPrice = SymbolInfoDouble(Symbol(), SYMBOL_ASK);
      stopLossPrice = openPrice - stopLoss;
      takeProfitPrice = openPrice + takeProfit;
   } else
   
   if(orderType == ORDER_TYPE_SELL){
      openPrice = SymbolInfoDouble(Symbol(), SYMBOL_BID);
      stopLossPrice = openPrice + stopLoss;
      takeProfitPrice = openPrice - takeProfit;     
   } else {
   
   return(false);
   }
   
   ticket = OrderSend(Symbol(), orderType, InpOrderSize, openPrice, 0, stopLossPrice, takeProfitPrice, InpTradeComment);
   return(ticket > 0);

}

bool NoTradesToday()
  {
   datetime today = iTime(NULL,Period(),0);

   for(int i=OrdersHistoryTotal()-1; i>=0; i--)
     {
      if(!OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)) continue;
      if(OrderSymbol()      != _Symbol)  continue;
      if(OrderMagicNumber() != InpMagicNumber) continue;
      if(OrderOpenTime()    >= today)    return(false);
     }

   for(int i=OrdersTotal()-1; i>=0; i--)           
     {
      if(!OrderSelect(i,SELECT_BY_POS))  continue;  
      if(OrderSymbol()      != _Symbol)  continue;  
      if(OrderMagicNumber() != InpMagicNumber) continue;  
      if(OrderOpenTime()    >= today)    return(false);
     }

   return(true);
  }
Discover new MetaTrader 5 opportunities with MQL5 community and services
Discover new MetaTrader 5 opportunities with MQL5 community and services
  • 2023.07.05
  • www.mql5.com
MQL5: language of trade strategies built-in the MetaTrader 5 Trading Platform, allows writing your own trading robots, technical indicators, scripts and libraries of functions
 
Your topic has been moved to the section: MQL4 e MetaTrader 4 — In the future, please consider which section is most appropriate for your query.
MQL5 forum: Expert Advisors and Automated Trading
MQL5 forum: Expert Advisors and Automated Trading
  • www.mql5.com
How to create an Expert Advisor (a trading robot) for Forex trading
 
bool NoTradesToday()
  {
   datetime today = iTime(NULL,Period(),0);

   for(int i=OrdersHistoryTotal()-1; i>=0; i--)
     {
      if(!OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)) continue;
      if(OrderSymbol()      != _Symbol)  continue;
      if(OrderMagicNumber() != InpMagicNumber) continue;
      if(OrderOpenTime()    >= today)    return(false);
     }

   for(int i=OrdersTotal()-1; i>=0; i--)           
     {
      if(!OrderSelect(i,SELECT_BY_POS))  continue;  
      if(OrderSymbol()      != _Symbol)  continue;  
      if(OrderMagicNumber() != InpMagicNumber) continue;  
      if(OrderOpenTime()    >= today)    return(false);
     }

   return(true);
  }

use

   datetime today = iTime(NULL,PERIOD_D1,0);