求助帖

 

文科生,想写个辅助平仓的ea,但看了一套教学视频还是云里雾里,特此请教大大帮忙。EA思路如下:


1.MT4平台

2.监控浮盈, 例如,当账户总浮盈达到  $10(金额可自行设置) 的时候全部平仓并删除所有挂单。



先行谢过!

 
//+------------------------------------------------------------------+
//|                                                    AutoClose.mq4 |
//|                                     Copyright 2022, WX:billabiao |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2022, WX:billabiao"
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict
input double close_equity=10;//设定平仓:
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
   
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
   
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
      autoClose();
      Comment( DoubleToStr(close_equity ));
  }
//+------------------------------------------------------------------+


void autoClose()
{
   double equity=AccountEquity();
   double balance=AccountBalance();
   
   double dif=equity-balance;
   if( dif< close_equity) return ;
   
   for(;;){
   int i,n=OrdersTotal();
   
   bool find=false;
   for(i=0;i<n;i++)
   {
      if(!OrderSelect( i,SELECT_BY_POS,MODE_TRADES))
         continue;
         
      string strSymbol=OrderSymbol();
      if(OrderType()==OP_BUY )
      {
         find=true;
         double bid=SymbolInfoDouble(strSymbol,SYMBOL_BID);
         if(!OrderClose(OrderTicket(),OrderLots(),bid,3,Violet))
            continue;
      }
      else if(OrderType()==OP_SELL  )
      {
         find=true;
         double ask=SymbolInfoDouble(strSymbol,SYMBOL_ASK);
         if(!OrderClose(OrderTicket(),OrderLots(),ask ,3,Violet))
            continue;
      }
      else{
         find=true;
         if(!OrderDelete(OrderTicket())){
            continue;
         }
      }
      
   }
   if( find==false) break;
   }
   return ;
   
   
}

Discover new MetaTrader 5 opportunities with MQL5 community and services
Discover new MetaTrader 5 opportunities with MQL5 community and services
  • 2023.02.01
  • www.mql5.com
MQL5: language of trade strategies built-in the MetaTrader 5 Trading Platform, allows writing your own trading robots, technical indicators, scripts and libraries of functions
 
Yong Biao Zou #:
//+------------------------------------------------------------------+
//|                                                    AutoClose.mq4 |
//|                                     Copyright 2022, WX:billabiao |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2022, WX:billabiao"
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict
input double close_equity=10;//设定平仓:
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
   
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
   
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
      autoClose();
      Comment( DoubleToStr(close_equity ));
  }
//+------------------------------------------------------------------+


void autoClose()
{
   double equity=AccountEquity();
   double balance=AccountBalance();
   
   double dif=equity-balance;
   if( dif< close_equity) return ;
   
   for(;;){
   int i,n=OrdersTotal();
   
   bool find=false;
   for(i=0;i<n;i++)
   {
      if(!OrderSelect( i,SELECT_BY_POS,MODE_TRADES))
         continue;
         
      string strSymbol=OrderSymbol();
      if(OrderType()==OP_BUY )
      {
         find=true;
         double bid=SymbolInfoDouble(strSymbol,SYMBOL_BID);
         if(!OrderClose(OrderTicket(),OrderLots(),bid,3,Violet))
            continue;
      }
      else if(OrderType()==OP_SELL  )
      {
         find=true;
         double ask=SymbolInfoDouble(strSymbol,SYMBOL_ASK);
         if(!OrderClose(OrderTicket(),OrderLots(),ask ,3,Violet))
            continue;
      }
      else{
         find=true;
         if(!OrderDelete(OrderTicket())){
            continue;
         }
      }
      
   }
   if( find==false) break;
   }
   return ;
   
   
}

腻害