简体   繁体   English

MQL4 自动停止

[英]MQL4 automatic stop

My Martingale EA should be stopped automatically if the previously manually set limit has been reached or exceeded.如果达到或超过之前手动设置的限制,我的 Martingale EA 应该会自动停止。 The last trade should expire normally and not be interrupted.最后一笔交易应正常到期且不会中断。

So far, I've tried the following code, without interruption, etc. the code works fine.到目前为止,我已经尝试了以下代码,没有中断等。代码工作正常。 I also heard about RemoveExpert, but I do not know how to do it.我也听说过 RemoveExpert,但我不知道该怎么做。 Am grateful for any help:)非常感谢您的帮助:)

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);
}
   if(tick_counter>=ticks_to_close)
 {
  ExpertRemove();
  Print(TimeCurrent(),": ",__FUNCTION__," expert advisor will be unloaded");
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM