This is an EA with attempt to open positions on ma-crosses with a certain level when to increase the volume, but I can´t run it in the strategytester

 
on eurusd.
#property copyright "Copyright © 2009, MetaQuotes Software Corp."
#property link      "http://www.metaquotes.net"
#define MAGICMA  20050610

double cutlevel=10, totalprofit=0, volume=0.1;

int CalculateCurrentOrders(string symbol)
  {
   int buys=0,sells=0;
//----
   for(int i=0;i<OrdersTotal();i++)
     {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break;
      if(OrderSymbol()==Symbol() && OrderMagicNumber()==MAGICMA)
        {
         if(OrderType()==OP_BUY)  buys++;
         if(OrderType()==OP_SELL) sells++;
        }
     }
//---- return orders volume
   if(buys>0) return(buys);
   else       return(-sells);
  }
  
//+------------------------------------------------------------------+
void start()
  {
  
//---- check for history and trading
   if(Bars<100 || IsTradeAllowed()==false) return;
//---- calculate open orders by current symbol
   if(CalculateCurrentOrders(Symbol())==0) CheckForOpen();
   else                                    CheckForClose();
//----
   
  }
  
void CheckForOpen()
  {
   double ma, orderprofit;
   int    res, flag;
   
   OrderSelect(OrdersHistoryTotal()-1,SELECT_BY_POS,MODE_HISTORY);
   orderprofit=OrderProfit();
   totalprofit+=volume*(orderprofit/volume);
            
            if(totalprofit<cutlevel) 
               volume=(cutlevel-totalprofit)/cutlevel;
            else  
               volume=0.1;  
            
            if(totalprofit>=0){
               totalprofit=0;
               volume=0.1;
            }
   
   ma=iMA(NULL,0,102,0,MODE_SMA,PRICE_CLOSE,0);   
   
   if((Open[1]>=ma && Close[1]<=ma && Close[2]>=ma)||(Close[2]>ma && Open[1]<ma))
     {   
      OrderClose(OrderTicket(),OrderLots(),Bid,3,White);
      res=OrderSend(Symbol(),OP_SELL,volume,Bid,3,0,0,"",MAGICMA,0,Red);
      return;
     }
   
   if((Open[1]<=ma && Close[1]>=ma && Close[2]<=ma)||(Close[2]<ma && Open[1]>ma))
     {
      OrderClose(OrderTicket(),OrderLots(),Ask,3,White);
      res=OrderSend(Symbol(),OP_BUY,volume,Ask,3,0,0,"",MAGICMA,0,Blue);   
      return;
     } 
}
void CheckForClose()
  {
   double ma;
   
   for(int i=0;i<OrdersTotal();i++)
     {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false)        break;
      if(OrderMagicNumber()!=MAGICMA || OrderSymbol()!=Symbol()) continue;
      //---- check order type 
      if(OrderType()==OP_BUY)
        {
         if((Open[1]>=ma && Close[1]<=ma && Close[2]>=ma)||(Close[2]>ma && Open[1]<ma))
         {
         OrderClose(OrderTicket(),OrderLots(),Bid,3,White);
         break;
         }
      if(OrderType()==OP_SELL)
        {
         if((Open[1]<=ma && Close[1]>=ma && Close[2]<=ma)||(Close[2]<ma && Open[1]>ma))
         {
         OrderClose(OrderTicket(),OrderLots(),Ask,3,White);
         break;
         }
        }   
      }
    }
}
Can anyone se what the problem is? Maybe how the volume is calculated or something? I don´t know.
Have used some of the methods that are already in the EA "Moving averages" that is in the demo version of mt4, then I have just typed in the code for when to open and close, and how to calculate the volume. Thanks i advance.
 
  1. Always test return codes, so you find out WHY
    res = OrderSend(...);
    if (res < 0) Alert("OrderSend failed: ", GetLastError());

  2. void CheckForOpen(){ ...
       OrderSelect(OrdersHistoryTotal()-1,SELECT_BY_POS,MODE_HISTORY);
       orderprofit=OrderProfit();
       :
       if((Open[1]>=ma && Close[1]<=ma && Close[2]>=ma)||(Close[2]>ma && Open[1]<ma)){   
          OrderClose(OrderTicket(),OrderLots(),Bid,3,White);
    How can you close an already closed order. OrderTicket is from the OrderSelect(HISTORY). Always test return codes.
  3. OrderSelect(OrdersHistoryTotal()-1,SELECT_BY_POS,MODE_HISTORY);
    At the start of the ST, there are no history orders, so the select fails and everything below is bogus. Always test return codes.
  4. That orderSelect also makes the EA incompatible with every other including itself on other charts and manual trading. Always filter by MN
 
WHRoeder:
  1. Always test return codes, so you find out WHY
  2. How can you close an already closed order. OrderTicket is from the OrderSelect(HISTORY). Always test return codes.
  3. At the start of the ST, there are no history orders, so the select fails and everything below is bogus. Always test return codes.
  4. That orderSelect also makes the EA incompatible with every other including itself on other charts and manual trading. Always filter by MN


1. One order opens then runing the tester. So there can not be any error messages. But the ea will not close it, i think. Since the first order can´t be closed another can´t be opened. I just want one order to be open at the same time.

2. That close method shall not be there, it is there because I tried with flags earlier to remember opened positions.

3. I have programed so that if there are no history orders the volume will be 0.1. The volume start value is 0.1 (the variable is decleard in the begining at the fifth row)

4. Don´t understand what you meen.

 
Do yourself a favour and listen to what you have been told and follow the advice . . .
 
Per:
4. Don´t understand what you meen.
    for(iPos=OrdersHistoryTotal()-1; iPos >= 0 ; iPos--) if (
        OrderSelect(iPos, SELECT_BY_POS, MODE_HISTORY)          // Only orders w/
    &&  OrderMagicNumber() == Magic.Number                      // my magic number
    &&  OrderSymbol()      == chart.symbol                      // and my pair.
    &&  OrderType()        <= OP_SELL// Avoid cr/bal https://www.mql5.com/en/forum/126192
    ){ 
        orderprofit=OrderProfit();
        :
        break; // Last closed order only
    }