Mr.ROSH:为什么加了净多单或净空单数量限制代码后,无法交易?是代码写得有问题吗?请指正谢谢!

 

extern int MagicNumber=123456;

extern int K1 = 19;
extern int D1 = 7;
extern int S1 = 9;
extern double a = 30;

extern int TakeProfit = 10;

extern double Lots = 0.1;
extern int maxopen = 5;
extern int maxlots = 20;

int myBars;
double k0, d0, k1, d1;
string text="EA10#, M1";
//=============================================================================================================
int start()
{
k0 =iStochastic(NULL,0,K1,D1,S1,MODE_SMA,0,MODE_MAIN,1);
d0 =iStochastic(NULL,0,K1,D1,S1,MODE_SMA,0,MODE_SIGNAL, 1);
k1 =iStochastic(NULL,0,K1,D1,S1,MODE_SMA,0,MODE_MAIN,2);
d1 =iStochastic(NULL,0,K1,D1,S1,MODE_SMA,0,MODE_SIGNAL, 2);
//==================================================================================================
int res =0;
int buyNumber =0;
int sellNumber=0;
for (int i=OrdersTotal()-1;i>=0;i--)
{
if (MagicNumber==0 || (OrderMagicNumber()==MagicNumber && MagicNumber!=0))
{
switch(OrderType())
{
case OP_BUY: buyNumber++;break;
case OP_SELL: sellNumber++;break;
}
}
}
res=buyNumber-sellNumber || sellNumber-buyNumber;
return(res);

//==================================================================================================
if(Bars!=myBars)
{
myBars=Bars;
if( res>maxopen && OrdersTotal()>maxlots) return(0);

if( k1<a && d1<a && k1<d1 && k0>d0 )
{ OrderSend(Symbol(), OP_BUY, Lots,Ask, 3, 0,Ask+TakeProfit*Point, text,2006, 3, Red);
PlaySound("alert. wav");
}

if( k1>100-a && d1>100-a && k1>d1 && k0<d0 )
{ OrderSend(Symbol(), OP_SELL, Lots, Bid,3, 0,Bid-TakeProfit*Point, text, 2006, 3,Blue);
PlaySound("alert. wav");
}
return(0);
}
}

 
Please, use MQL4 button.
extern int MagicNumber=123456;
 
extern int       K1        = 19;
extern int       D1        = 7;
extern int       S1        = 9;
extern double    a         = 30;
 
extern int   TakeProfit    = 10;
 
extern double  Lots        = 0.1;
extern int    maxopen      = 5;
extern int    maxlots      = 20;
 
int myBars;
double k0, d0, k1, d1;
string text="EA10#, M1";
//============================================================================================================= 
int start()  
{
   k0   =iStochastic(NULL,0,K1,D1,S1,MODE_SMA,0,MODE_MAIN,1);
   d0   =iStochastic(NULL,0,K1,D1,S1,MODE_SMA,0,MODE_SIGNAL,1);
   k1   =iStochastic(NULL,0,K1,D1,S1,MODE_SMA,0,MODE_MAIN,2);
   d1   =iStochastic(NULL,0,K1,D1,S1,MODE_SMA,0,MODE_SIGNAL,2);
//==================================================================================================
int res =0;
int buyNumber =0;
int sellNumber=0;
   for (int i=OrdersTotal()-1;i>=0;i--)
         {
         if (MagicNumber==0 || (OrderMagicNumber()==MagicNumber && MagicNumber!=0))
             {
               switch(OrderType())
                {
                 case OP_BUY: buyNumber++;break;
                 case OP_SELL: sellNumber++;break;
                }
             }
         }
   res=buyNumber-sellNumber || sellNumber-buyNumber;
   return(res);
 
//==================================================================================================
   if(Bars!=myBars)
      {
      myBars=Bars;
      if(  res>maxopen &&  OrdersTotal()>maxlots) return(0);
     
      if(  k1<a && d1<a && k1<d1 && k0>d0  )
            {  OrderSend(Symbol(),OP_BUY,Lots,Ask,3,0,Ask+TakeProfit*Point,text,2006,3,Red);
               PlaySound("alert.wav");
            }
 
      if(  k1>100-a && d1>100-a && k1>d1 && k0<d0   )
            {  OrderSend(Symbol(),OP_SELL,Lots,Bid,3,0,Bid-TakeProfit*Point,text,2006,3,Blue);
               PlaySound("alert.wav");
            }
      return(0);
      }
}
 
Use
res=MathAbs(buyNumber-sellNumber);
See https://docs.mql4.com/cn/math/MathAbs