MQL4自动停止

时间:2019-10-07 17:52:21

标签: mql4 trading mql

如果达到或超过了先前手动设置的限制,我的Martingale EA应该自动停止。最后一笔交易应正常到期,并且不被中断。

到目前为止,我已经尝试了以下代码,没有中断,等等。代码工作正常。 我也听说过RemoveExpert,但是我不知道该怎么做。非常感谢您的帮助:)

enum tradeD{Buy=1, Sell=2};
input tradeD   TradeType   = Buy;
input double   Initial_Lot = 0.01;
input double   Lot_Factor  = 2;
input int      Max_Spread  = 30;
input double   Pct_SL      = 0.1;  // Percentage Stop Loss
input double   Pct_TP      = 0.1;  // Percentage Take Profit
input int      Magic_Number= 73726;
input int      interrupt   = 1,0000;   
input bool     StopEA      = false;


 int OnInit()
  {
     return(INIT_SUCCEEDED);
  }
void OnDeinit(const int reason)
  {

  }
 int start()
 {
  if (StopEA == true) { return(0); } //This line will skip the execution of EA.
 }
  void OnTick()
  {
   if(OpenOrders()==0 && (SymbolInfoInteger(Symbol(),SYMBOL_SPREAD)<Max_Spread||Max_Spread==0)){
      if(TradeType==Buy){
      double flot=Initial_Lot;
      double etp=Ask+(Ask*(Pct_TP/100));
      etp=NormalizeDouble(etp,Digits);
      double esl=Ask-(Ask*(Pct_TP/100));
      esl=NormalizeDouble(esl,Digits);

      if (ask <= interrupt) { StopEA = true; return(0); }

         if(lastProfit()<0){
            flot=NormalizeDouble(lastLot()*Lot_Factor,2);
         }
        int snd=OrderSend(Symbol(),OP_BUY,flot,Ask,10,esl,etp,NULL,Magic_Number,0,clrAliceBlue); 
      }
      if(TradeType==Sell){
      double flot=Initial_Lot;
      double etp=Bid-(Bid*(Pct_TP/100));
      etp=NormalizeDouble(etp,Digits);
      double esl=Bid+(Bid*(Pct_TP/100));
      esl=NormalizeDouble(esl,Digits);

      if (bid >= interrupt) { StopEA = true; return(0); }

         if(lastProfit()<0){
            flot=NormalizeDouble(lastLot()*Lot_Factor,2);
         }
        int snd=OrderSend(Symbol(),OP_SELL,flot,Bid,10,esl,etp,NULL,Magic_Number,0,clrAliceBlue); 
      }
   }

   //checkProfit();
  }
int checkProfit(){
   int total=0;
   for(int i=OrdersTotal()+5; i >= 0; i--)
      {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)){

      string ordsymB  = OrderSymbol();
      int ordtypeB    =OrderType();
      int magicnB     = OrderMagicNumber();
      int magicn=magicnB;
      double ordProfit=OrderProfit();
      int tick=OrderTicket();
      double oLots=OrderLots();

         if(ordsymB==Symbol() && magicnB==Magic_Number){
            double pctBalT=AccountBalance()*(Pct_TP/100);
            double pctBalS=AccountBalance()*(Pct_SL/100);

            if(ordProfit>=pctBalT){
               int clo=OrderClose(tick,oLots,Close[0],10,clrAntiqueWhite);
            }
            if(ordProfit<0 && MathAbs(ordProfit)>=pctBalS){
               int clo=OrderClose(tick,oLots,Close[0],10,clrAntiqueWhite);
            }
            }
         }
      }
return (total);
}

 int OpenOrders(){
   int total=0;
   for(int i=OrdersTotal()+5; i >= 0; i--)
      {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)){

      string ordsymB  = OrderSymbol();
      int ordtypeB    =OrderType();
      int magicnB     = OrderMagicNumber();
      int magicn=magicnB;

         if(ordsymB==Symbol() && magicnB==Magic_Number){
            total++;
            }
         }
      }
return (total);
}


double lastProfit(){
   double total=0;
   datetime lastTime=0;
   for(int i=OrdersHistoryTotal(); i >= 0; i--)
      {
      if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)){
      string ordSym=OrderSymbol();
      int ordType=OrderType();
      datetime ordTime=OrderOpenTime();
      double ordLot=OrderLots();
      double ordOp=OrderOpenPrice();
      int ordMag=OrderMagicNumber();
      double ordProfit=OrderProfit();

         if(ordSym==Symbol() && ordTime>lastTime && ordMag==Magic_Number){
            lastTime=ordTime;
            total=ordProfit;
         }
     }
  }
return(total);
}

double lastLot(){
   double total=0;
   datetime lastTime=0;
   for(int i=OrdersHistoryTotal(); i >= 0; i--)
      {
      if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)){
      string ordSym=OrderSymbol();
      int ordType=OrderType();
      datetime ordTime=OrderOpenTime();
      double ordLot=OrderLots();
      double ordOp=OrderOpenPrice();
      int ordMag=OrderMagicNumber();

         if(ordSym==Symbol() && ordTime>lastTime && ordMag==Magic_Number){
            lastTime=ordTime;
            total=ordLot;
         }
     }
  }
return(total);
}

1 个答案:

答案 0 :(得分:0)

   if(tick_counter>=ticks_to_close)
 {
  ExpertRemove();
  Print(TimeCurrent(),": ",__FUNCTION__," expert advisor will be unloaded");
 }