如果我增加其计算的条件数量,我的第一个EA会MQL4超交易

时间:2018-09-26 13:55:03

标签: mql4

我创建了一个EA交易,可以计算一些条件,并为每个条件提供多头或空头价值(+或-0.1手),然后将所有条件相加,得出净多头或空头头寸。

然后它应该要么开一个新订单,要么关闭一个未平仓订单,以改变先前的未平仓头寸(仓位),以匹配交易条件根据每个新柱的变化计算出的目标值(总和);这是通过使用ontick函数完成的。

如果我仅使用一个或两个条件,则可以正常工作,但是当我增加条件数量(总和可以大于0.2手或小于-0.2手)时,它会以1的价格进行交易,然后平仓应该然后再进行一次过度交易,这不应该进入一个令人讨厌的循环。

我不明白为什么它可以在两个条件下完美地工作,但不能在两个以上的条件下正常工作。我想知道是否有人对任何输入表示感谢。

这是我第一次尝试EA,最好的问候是Ken。

//+------------------------------------------------------------------+
//|                                                        test.mq4  |
//|                                     Copyright 2018, K T          |
//|                                          https://                |
//+------------------------------------------------------------------+
#property copyright "Copyright 2018, K T"
#property link      ""
#property version   "1.00"
#property strict
#property description "SIMPLE TRADER"

//INPUT PARAMETERS
input double   lots=0.1;
input int      MagicNumber=1111;//MAGICNO MUST BE UNIQUE
input int      condno=1; //1=3/10 10 2=10 10/20 3=ALL
input double   test=0.0;      
input int      spread=1;
input int      slippage=10;
input string   simble="FTSE100(£)"; 
input int      period=PERIOD_M1; 
input double   TP=200;
input double   SL=100;
input int      ma1input=3;
input int      ma2input=10;
input int      ma3input=20;
input int      sleep=0;


//ONINIT TEST FOR CORRECR CONDITIONS
int OnInit()
  {
  string simble1=simble;
  int period1=period;
  if (simble1==Symbol()&& period1==Period() && Ask-Bid<=spread)
  {
  Alert ("CORRECT SYMBOL ", simble," mlots=", mlots(),"sum= ",sum());
  Alert ("CORRECT TIME T", period);
  return(INIT_SUCCEEDED);
  }
  else
  {
  Alert ("INIT FAILED");
  Alert ("wrong symbol, time or spread too wide");

  return(INIT_FAILED);
  }
  }

//ON EVERY TICK
void OnTick()
   {
   double sum1 = sum();
   double mlots1 = mlots();

   if(sum1 == mlots1)
   {
   neutral();
   return;
   }
   else if(sum1>mlots1 && mlots1>=0 && mlots1!= sum1)
   {
   openbuy();
   return;
   }

   else if(sum1>mlots1 && mlots1<0 && mlots1!= sum1)
   {
   closesell();
   return;
   }

   else if(sum1<mlots1 && mlots1<=0 && mlots1!= sum1)
   {
   opensell();
   return;
   }

   else if(sum1<mlots1 && mlots1>0 && mlots1!= sum1)
   {
   closebuy();
   return;
   }

   else 

   return;   
   }

//NEUTRAL
void  neutral()
   {
   Alert("T", period, " EQUILIBRIUM ", simble,"/ sum=",sum(),"/ mlots=" ,mlots());
   return;
   }

//OPEN BUY
void openbuy()
  {

   double TakeProfitLevel;
   double StopLossLevel;
   TakeProfitLevel = Bid + TP*Point*10;   //0.00001 * 10 = 0.0001
   StopLossLevel = Bid - SL*Point*10;

   if(mlots()!= sum())
   {
   OrderSend(simble, OP_BUY, lots, Ask, slippage*10, StopLossLevel, TakeProfitLevel, "BUY", MagicNumber);//notice that slippage also has to be multiplied by 10
   Alert(MagicNumber," T", period, " OPENBUYFCTION = ", simble,"/ sum=",sum(),"/ mlots=" ,mlots());
   Sleep(sleep);
   return;
   }
   else
   {
   return;
   }
  }


//OPEN SELL
void opensell()
  {
  double TakeProfitLevel;
  double StopLossLevel;

   //here we are assuming that the TakeProfit and StopLoss are entered in Pips
   TakeProfitLevel = Ask - TP*Point*10;   //0.00001 * 10 = 0.0001
   StopLossLevel = Ask + SL*Point*10;

   if (mlots()!= sum())
   {
   OrderSend(simble, OP_SELL, lots, Bid, slippage*10, StopLossLevel, TakeProfitLevel, "SELL", MagicNumber);   //notice that slippage also has to be multiplied by 10
   Alert(MagicNumber," T", period, " OPENSELLFCTION = ", simble,"/ sum=",sum(),"/ mlots=" ,mlots());
   Sleep(sleep);
   return;
   }
   else
   {
   return;
   }
   }


//CLOSE BUY
void closebuy()
  {
  if(mlots()==sum())
  {
  neutral();
  return;
  }
  int low=OrderTicket();
  int i=OrdersTotal();
  for (i = OrdersTotal(); i >=0; i--) 
  {
  if (OrderSymbol() != Symbol() || OrderMagicNumber() != MagicNumber) continue;
  if (i<=OrdersTotal() && OrderType()== OP_BUY && OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber )
  bool closed = OrderClose( low, OrderLots(), Bid, slippage, White);
  Alert("Ticket= ", low);
  Alert(MagicNumber," T", period, " CLOSEBUYFCTION = ", simble,"/ sum=",sum(),"/ mlots=" ,mlots());
  Sleep(sleep);
  return;
  }  

  }

//CLOSE SELL
void closesell()
  {
  if(mlots()==sum())neutral();
  int low=OrderTicket();
  int i=OrdersTotal();
  for (i = OrdersTotal(); i >=0; i--) 
  {
  if (OrderSymbol() != Symbol() || OrderMagicNumber() != MagicNumber) continue;
  if (i<=OrdersTotal() && OrderType()== OP_SELL && OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber )
  bool closed = OrderClose( low, OrderLots(), Ask, slippage, White);
  Alert("Ticket= ", low);
  Alert(MagicNumber," T", period, " CLOSESELLFCTION = ", simble,"/ sum=",sum(),"/ mlots=" ,mlots());
  Sleep(sleep);
  return;
  }  

  }


//CONDITION 1
double cond1()   //cond 1 3ema 10ma
   {
   double   ma1;
   double   ma2;
   ma1=iMA(NULL,0,ma1input,0,MODE_EMA,PRICE_CLOSE,1);//3EMA
   ma2=iMA(NULL,0,ma2input,0,MODE_SMA,PRICE_CLOSE,1);//10MA
   if(ma1>=ma2)
      {
      return(lots);
      }
   else
      {
      return(lots*-1);
      }
      }

//CONDITION 2
double cond2()   //cond 2 10ema 10ma
   {
   double   ma1;
   double   ma2;
   ma1=iMA(NULL,0,ma1input,0,MODE_EMA,PRICE_CLOSE,1);//3EMA
   ma2=iMA(NULL,0,ma2input,0,MODE_EMA,PRICE_CLOSE,1);//10EMA
   if(ma1>=ma2)
      {
      return(lots);
      }
   else       
      {
      return(lots*-1);
      }
      }

//CONDITION 3
double cond3()   //cond 3 10ema 20ema
   {
   double   ma1;
   double   ma2;
   ma1=iMA(NULL,0,ma2input,0,MODE_EMA,PRICE_CLOSE,1);//10EMA
   ma2=iMA(NULL,0,ma3input,0,MODE_EMA,PRICE_CLOSE,1);//20EMA
   if(ma1>=ma2)
      {
      return(lots);
      }
   else
      {
      return(lots*-1);
      }
      }


//CONDITION 4
double cond4()   //cond 4 10ma 20Ema
   {
   double   ma1;
   double   ma2;
   ma1=iMA(NULL,0,ma2input,0,MODE_SMA,PRICE_CLOSE,1);//10MA
   ma2=iMA(NULL,0,ma3input,0,MODE_EMA,PRICE_CLOSE,1);//20EMA
   if(ma1>=ma2)
      {
      return(lots);
      }
   else       
      {
      return(lots*-1);
      }
      }
//SELECTING CONDITIONS TO BE INCLUDED
double   sum()
      {
      if(condno==1)
      {
      return(cond1() + cond2());
      }
      else if(condno==2)
      {
      return(cond3() + cond4());
      }
      else if (condno==3)
      {
      return(cond1() + cond2()+ cond3() + cond4() );//IF MORE THAN TWO CONDITIONS USED IT OVERTRADES B ONE AND THEN CLOSES ERROR TRADE
      }
      else
      return(cond1() + cond2());
      }

//COUNT OF ALL OPEN TRADES  
double mlots()
   {
   double mlots1 = BuyTotalMagicOpen() + SellTotalMagicOpen();
   double mlots2 = mlots1/10;
   return (mlots2);
   }

//COUNT BUY ORDERS   
double BuyTotalMagicOpen() 
   {
   int OrderCount = 0;
   for (int i = OrdersTotal() - 1; i >= 0; i--) 
   {
   OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
   if (OrderSymbol() != Symbol() || OrderMagicNumber() != MagicNumber) continue;
   if (OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber)
   if (OrderType() == OP_BUY) OrderCount++;
   }
return (OrderCount);
   }

//COUNT SELL ORDERS
double SellTotalMagicOpen() 
   {
   int OrderCount = 0;
   for (int i = OrdersTotal() - 1; i >= 0; i--) 
   {
   OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
   if (OrderSymbol() != Symbol() || OrderMagicNumber() != MagicNumber) continue;
   if (OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber)
   if (OrderType() == OP_SELL) OrderCount++;
   }
return (OrderCount*-1);
   }

//CLOSE ALL MAGICNUMBER REFRESHRATES BUSYSLEEP MODE
void CloseThis() {
for (int i = OrdersTotal(); i >=0; i--) {
    OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
    while(IsTradeContextBusy()) Sleep(100);
    RefreshRates();
    if (OrderType() == OP_BUY && Symbol() == OrderSymbol() 
     && MagicNumber == OrderMagicNumber()) {
        bool closed = OrderClose( OrderTicket(), OrderLots(), Bid, slippage, Red);
        Alert("T", period, " BUY CLOSED ON DEINIT");
    }
    if (OrderType() == OP_SELL && Symbol() == OrderSymbol()
     && MagicNumber == OrderMagicNumber()) {
        bool closed = OrderClose( OrderTicket(), OrderLots(), Ask, slippage, Green);
        Alert("T", period, " SELL CLOSED ON DEINIT");
    }
}
}

//DEINIT
void OnDeinit(const int reason)//MAGIC NO
  {
 CloseThis();
 Alert("T", period, " DEINIT SUCCEEDED");
  }
//

0 个答案:

没有答案