Just one trade at time

 

Hi to all the community,

I'l like to set my EA to take one trade at time because is entering a position so many times at the time (my EA is based on take positions at the crossing of two MA, so each time the lines crosses it takes a trade, I'd like to take the trade just one time when the lines crosses.

Thank you in advance.

 
//+------------------------------------------------------------------+
//+ Trade Count                                                      |
//+------------------------------------------------------------------+
int TradeCount(ENUM_ORDER_TYPE Type)
  {
//----

   int   Cnt = 0;

   for(int i = OrdersTotal() - 1; i >= 0; i--)
   {
      if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES) == false)
      {
         Print("Error in orders!");

         break;
      }

      if(OrderSymbol() == _Symbol && OrderMagicNumber() == MagicNum)
      {
         if(OrderType() == Type)
         {
            Cnt++;
         }
      }
   }

//----
   return(Cnt);
  }
Use this to check for your order and if return 1 means there is a buy/sell already so that you cannot enter anymore
 
miguel.guerra21: I'l like to set my EA to take one trade at time because is entering a position so many times at the time (my EA is based on take positions at the crossing of two MA, so each time the lines crosses it takes a trade, I'd like to take the trade just one time when the lines crosses.
  1. You can count how many order you have open as Dua Yong Rew suggests, that would prevent opening the second on a cross, uncross, recross scenario.
  2. You don't show your code, so we can only guess. A) you are looking at bar zero which recrosses as the bar forms, or B) you don't have any new bar code and are seeing the same cross each tick.
 
Dua Yong Rew:
Use this to check for your order and if return 1 means there is a buy/sell already so that you cannot enter anymore

Thank you Dua Yong, this script have to be inside the OnTradeTransaction() function?
 
miguel.guerra21:

Thank you Dua Yong, this script have to be inside the OnTradeTransaction() function?

ontick()
 
you using mt4 or mt5? the code is meant for mt4. please modify to mt5 if necessary.
 
Dua Yong Rew:
you using mt4 or mt5? the code is meant for mt4. please modify to mt5 if necessary.

Thank you again Dua, I'm coding in mql5.
 
//+------------------------------------------------------------------+
//| Trade Count                                                      |
//+------------------------------------------------------------------+
int TradeCount(ENUM_TIMEFRAMES TimeFrame)
  {
//---

   int      Cnt;
   ulong    Ticket;
   datetime DT[1];

   Cnt = 0;

   if(CopyTime(_Symbol, TimeFrame, 0, 1, DT) <= 0)
   {
      Cnt = -1;
   }
   else
   {
      HistorySelect(DT[0], TimeCurrent());

      for(int i = HistoryDealsTotal() - 1; i >= 0; i--)
      {
         Ticket = HistoryDealGetTicket(i);

         if(HistoryDealGetString(Ticket, DEAL_SYMBOL) == _Symbol)
         {
            if(HistoryDealGetInteger(Ticket, DEAL_ENTRY) == DEAL_ENTRY_IN)
            {
               Cnt++;
            }
         }
      }
   }

//---
   return(Cnt);
  }
modify this code then
 
whroeder1:
  1. You can count how many order you have open as Dua Yong Rew suggests, that would prevent opening the second on a cross, uncross, recross scenario.
  2. You don't show your code, so we can only guess. A) you are looking at bar zero which recrosses as the bar forms, or B) you don't have any new bar code and are seeing the same cross each tick.

Thank you whroeder1, do you think this code could help me?
//--- Do we have enough bars to work with
   if(Bars(_Symbol,_Period)<60) // if total bars is less than 60 bars
     {
      Alert("We have less than 60 bars, EA will now exit!!");
      return;
     }  

// We will use the static Old_Time variable to serve the bar time.
// At each OnTick execution we will check the current bar time with the saved one.
// If the bar time isn't equal to the saved time, it indicates that we have a new tick.

   static datetime Old_Time;
   datetime New_Time[1];
   bool IsNewBar=false;

// copying the last bar time to the element New_Time[0]
   int copied=CopyTime(_Symbol,_Period,0,1,New_Time);
   if(copied>0) // ok, the data has been copied successfully
     {
      if(Old_Time!=New_Time[0]) // if old time isn't equal to new bar time
        {
         IsNewBar=true;   // if it isn't a first call, the new bar has appeared
         if(MQL5InfoInteger(MQL5_DEBUGGING)) Print("We have new bar here ",New_Time[0]," old time was ",Old_Time);
         Old_Time=New_Time[0];            // saving bar time
        }
     }
   else
     {
      Alert("Error in copying historical times data, error =",GetLastError());
      ResetLastError();
      return;
     }

//--- EA should only check for new trade if we have a new bar
   if(IsNewBar==false)
     {
      return;
     }
 
//--- Do we have enough bars to work with
   int Mybars=Bars(_Symbol,_Period);
   if(Mybars<60) // if total bars is less than 60 bars
     {
      Alert("We have less than 60 bars, EA will now exit!!");
      return;
     }
 
miguel.guerra21: Thank you whroeder1, do you think this code could help me?
Help you with what? All you posted was new bar code for Mt5. Nothing to do with your original question.