How to only have 1 open position at a time.

 

Hi I am new to coding and I am trying to code it such that I would only have 1 position at a time.

If I have 1 lot in the long position, and a sell signal appears, it would sell 2 lots and would net a 1 lot in the short position.

OrdersTotal only allows me to see total current orders but not if the orders are currently long or short.


Is there a way to make a TotalShort and TotalLong or a way to make it such that there would only be 1 position opened at a time.

 
TereYeo: Hi I am new to coding and I am trying to code it such that I would only have 1 position at a time.

If I have 1 lot in the long position, and a sell signal appears, it would sell 2 lots and would net a 1 lot in the short position.

OrdersTotal only allows me to see total current orders but not if the orders are currently long or short.

Is there a way to make a TotalShort and TotalLong or a way to make it such that there would only be 1 position opened at a time.

It will be difficult to help you, if you don't show the relevant code with which you having difficulty implementing!

In MQL5, the individual hedged positions are available to you as "Positions", not "Orders", but only if you are using a "hedgeing" account and not a "netting" account.

In the off chance that you are actually working with MQL4, please note that moderators will move the thread to the MQL4 section at the end of the page, because this section is really a MQL5 section.

 
TereYeo:

Hi I am new to coding and I am trying to code it such that I would only have 1 position at a time.

If I have 1 lot in the long position, and a sell signal appears, it would sell 2 lots and would net a 1 lot in the short position.

OrdersTotal only allows me to see total current orders but not if the orders are currently long or short.


Is there a way to make a TotalShort and TotalLong or a way to make it such that there would only be 1 position opened at a time.


Use something like that below:

//+------------------------------------------------------------------+
//| Expert TotalOrdersCount function
//+------------------------------------------------------------------+
int TotalOrdersCount(char type)
{
   int result = 0;
   
   for( i=0;   i<OrdersTotal() && !IsStopped();    i++)
   {
      bool ordselect = OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
      if(OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber && OrderType() == type)
      result++;
   }

//---
   return(result);
}
Reason: