One Position opened for each Symbol at the same time ?

 
Hi guys ...

I'm stuck in problems by programming trading criteria for my EA.




Criteria is only checked as a new bar is formed .So my start() function (after variable declaration) begins with :


if( Volume[0] == 1 )
{
     ....



in this if-block i set a for-loop to check if there is already a position opened for this symbol. There should only be one position opened for each symbol at the same time.

total= Orderstotal();
for(i=0;i<total;i++)

{

 test = OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
  
   if( test == false )
   {
     err=GetLastError();
     Print("error(",err,"): ",ErrorDescription(err));  
  
   }

   if(OrderSymbol() == Symbol() )
      {return(0);}
  

   else{
               my code




By Using upper code no positions are opened and no Error Code is written to the Journal.
Letting out the for-loop positions are opened as desired, but multiple for one symbol.
 
try using Time[0] instead of Volume[0]

if(time != Time[0])
{
time = Time[0];
...
}
 
cytomek:
Criteria is only checked as a new bar is formed .So my start() function (after variable declaration) begins with :
if( Volume[0] == 1 )

A check on Volume[0] is only reliable in backtesting, not in live/demo forward trading. This question comes up a lot, e.g. https://www.mql5.com/en/forum/124312 where BarrowBoy includes a link through to a more complete version of what fxcourt has suggested: https://www.mql5.com/en/forum/109887

 
Thanks for advice so far.

I changed Volume with Time like BarrowBoy did, but it seems that wasn the problem.
It still does not open any positions.

I think the problem is in the for-loop where the Orderselect function is.
When i cut out this part of code everything works fine except the undesired fact that it allows to hold multiple open positions for one symbol at the same time.


Does somebody know a general code for avoiding this fact?


Greets
cytomek
 
cytomek:
When i cut out this part of code everything works fine except the undesired fact that it allows to hold multiple open positions for one symbol at the same time.

Need to see the full code. From your extract it looks as though you may, in effect, only be checking the first open order, not all open orders. You need to do a complete loop through the orders looking for any which match your symbol, and only then place a new order once you've done a complete scan. For example:

int CountOfPendingAndOpenOrders = 0;
for (int i = 0; i < OrdersTotal(); i++) {
   if (OrderSelect(i, SELECT_BY_POS)) {
      if (Symbol() == OrderSymbol()) {
         if (OrderMagicNumber() == <your magic number>) {
            CountOfPendingAndOpenOrders++;
         }
      }
   }
}

if (CountOfPendingAndOpenOrders == 0) {
   ...No open or pending orders from your EA. Place new orders here.
}
 
Simplify
int CountOfPendingAndOpenOrders = 0;
for (int i = OrdersTotal()-1; i >=0; i--) if (
   OrderSelect(i, SELECT_BY_POS)
&& OrderSymbol()      == Symbol() 
&& OrderMagicNumber() == <your magic number>) {
   CountOfPendingAndOpenOrders++;
}
Is your Time variable must be static or external
int start (){                                static datetime Time0;
bool newBar = Time[shift] > Time0;   if (!newBar) return;    Time0 = Time[shift];
 
WHRoeder:
Simplify [...]

It's far from clear to me in what way your revised code is intended to be simpler. (Or faster, for that matter, given that MQ4 doesn't exit early.) It also adds a counterintuitive loop from OrdersTotal() downwards which is entirely unnecessary in context.

The thing which would arguably simplify the code and also definitely save execution time is to exit from the loop when you find the first matching order, because cytomek is only interested in the existence of one or more orders, not the number of orders. But the speed gain is trivial, so I left in the full count as an example.

Reason: