[后退] [返回首页] 复盘侠MTCommander(MTDriver)用户手册 [切换到电脑版] [复制网址]

为了让大家尽快掌握在复盘软件中使用EA,下面讲解一个例子.

EA功能:追踪止损,固定止损,固定止盈.
EA运行方法:当做一个指标,拖拽到窗口K线图中即可.

为了节省时间,具体的代码我们已经写好了,请大家打开指标中,找到"EA_MTD追踪止损",并拖拽到窗口K线图中.


设置好参数以后,就可以了.


然后我们开始演示一下,在行情前进的时候,他的执行过程.
我们在
2015.10.22 07:15:00这个时间下1手sell单子.





到了2015.10.22 13:10:00
这个时间,可以看到已经触发追踪止损(360点),也就是止损价在1.13423,如下图:


随着行情的前进,追踪止损开始不断下移,见下图红色虚线就是止损价线:



最终止损于价格1.11129,采用此追踪止损,把握住了很大的一波日内行情.

还有其他的固定止损和止盈,也是一样的方法,大家可以加载此EA自行验证.如果参数为0,就表示不使用.比如刚才的演示,止损和止盈都是0,表示未采用.而只有追踪止损功能.

完整的代码如下:(大家也可以在最新版的MT4找到)

#include <MTDinc.mqh>
//+------------------------------------------------------------------+
//|                           复盘EA演示:   追踪止损和固定止损止盈
//+------------------------------------------------------------------+

//---- input parameters
extern double       止赢=240;
extern double       止损=168;
extern double       追踪止损=360;//移动止损的具体价格偏移值,如果值为0表示不起作用
extern int       Magic=0;
extern string    参数说明="单位为点数";

string id_pre="MTD_zzzs_";
//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {
//----
   deleteOBJ_StartsWith(id_pre);
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   deleteOBJ_StartsWith(id_pre);
//----
   return(0);
  }
 
int ids=0; 


 
 
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
  {
//----
    double myPoint=Point();
    //Alert(DoubleToStr(myPoint,8));
   
    ids=0;
    DrawOneMessage("EA -自动追踪止损运行中 , "+Symbol(),DeepPink,12);
    //ids++;
   
   
    //追踪止损
    if(追踪止损>0){
       _MoveStop(Magic, 追踪止损*myPoint,0*myPoint);//其中追踪止损单位为点数,追踪止损*myPoint=实际的止损值   
       DrawOneMessage("追踪止损= "+追踪止损+"点="+DoubleToStr(追踪止损*myPoint,Digits),clrYellow,12);
    }else{
       DrawOneMessage("追踪止损= "+追踪止损+"点(未启用)",clrGray,12);
    }
   
    //设置止赢
    if(止赢>0){
       _TakeProfit(Magic,止赢*myPoint);
       DrawOneMessage("首次止赢= "+止赢+"点="+DoubleToStr(止赢*myPoint,Digits),clrYellow,12);
    }else{
       DrawOneMessage("首次止赢= "+止赢+"点(未启用)",clrGray,12);
    }
   
   
    //设置止损
    if(止损>0){
       _StopLoss(Magic,止损*myPoint);
       DrawOneMessage("首次止损= "+止损+"点="+DoubleToStr(止损*myPoint,Digits),clrYellow,12);
    }else{
       DrawOneMessage("首次止损= "+止损+"点(未启用)",clrGray,12);
    }
  
   
    DrawOneMessage("Magic     =  "+Magic,clrYellow,12);
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| _MoveStop 移动止损函数 function                                               |
//+------------------------------------------------------------------+
int _MoveStop(int MAGIC, double MOVE,double STEP)//_MoveStop(MAGIC, MOVE);
  {
//----
   //Alert("追踪止损MOVE="+MOVE);
   if (MOVE<=0) return(0);
   double MoveStopPrice;
   for ( int z = MTDOrdersTotal() - 1; z >= 0; z -- )
   {
     if ( !MTDOrderSelect( z, SELECT_BY_POS ) )
     {
       Print("MTDOrderSelect(", z, ",SELECT_BY_POS) - Error #",GetLastError() );
       continue;
     }
     if (MTDOrderSymbol()!=Symbol())continue;
     if (MTDOrderMagicNumber() != MAGIC )continue;
     switch (MTDOrderType())
     {
       case OP_BUY   : 
       {
        
         MoveStopPrice=NormalizeDouble(MTDBid()-MOVE,Digits);//
         //Alert(MoveStopPrice+" , "+MTDOrderTicket());
         if (MoveStopPrice>MTDOrderOpenPrice() && (MoveStopPrice>MTDOrderStopLoss() || MTDOrderStopLoss()==0)){  //追踪止损的价格>开仓价 ,并且追踪止损的价格>当前止损价,就移动止损
           if(!MTDOrderModify(MTDOrderTicket(),MTDOrderOpenPrice(),MoveStopPrice+STEP,MTDOrderTakeProfit(),MTDOrderExpiration())) {   //
             Alert("MoveStop_MTDOrderModify Error #",GetLastError());
             return(-1);
           }
         }
         continue;
       }
       case OP_SELL:
       {
         MoveStopPrice=NormalizeDouble(MTDAsk()+MOVE,Digits);
         //Alert(MTDAsk()+","+MOVE+";"+MoveStopPrice+" , "+MTDOrderTicket());
         if (MoveStopPrice<MTDOrderOpenPrice() && (MoveStopPrice<MTDOrderStopLoss() || MTDOrderStopLoss()==0))
         {
           if(!MTDOrderModify(MTDOrderTicket(),MTDOrderOpenPrice(),MoveStopPrice-STEP,MTDOrderTakeProfit(),MTDOrderExpiration())){     //
             Alert("MoveStop_MTDOrderModify Error #",GetLastError());
             return(-1);
           }
         }
         continue;
       }
       default: continue;
     }
   }
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| 止盈函数 function                                               |
//+------------------------------------------------------------------+
int _TakeProfit(int MAGIC, double TP)//
  {
//----
   if (TP<=0) return(0);
   double TakeProfit;
   int OrdersTotal4TakeProfit=MTDOrdersTotal();
   for ( int z = OrdersTotal4TakeProfit - 1; z >= 0; z -- )
   {
     if ( !MTDOrderSelect( z, SELECT_BY_POS ) )
     {
       Print("MTDOrderSelect(", z, ",SELECT_BY_POS) - Error #",GetLastError() );
       continue;
     }
     if (MTDOrderSymbol()!=Symbol())continue;
     if (MTDOrderMagicNumber() != MAGIC )continue;
     switch (MTDOrderType())
     {
       case OP_BUY   : 
       {
         TakeProfit=NormalizeDouble(MTDOrderOpenPrice()+TP,Digits);
         if (MTDOrderTakeProfit()==0.0)
         {
           if(!MTDOrderModify(MTDOrderTicket(),MTDOrderOpenPrice(),MTDOrderStopLoss(),TakeProfit,MTDOrderExpiration()))
           {
             Alert("TakeProfit_MTDOrderModify Error #",GetLastError());
           }
         }
         continue;
       }
       case OP_SELL:
       {
         TakeProfit=NormalizeDouble(MTDOrderOpenPrice()-TP,Digits);
         if (MTDOrderTakeProfit()==0.0)
         {
           if(!MTDOrderModify(MTDOrderTicket(),MTDOrderOpenPrice(),MTDOrderStopLoss(),TakeProfit,MTDOrderExpiration()))
           {
             Alert("TakeProfit_MTDOrderModify Error #",GetLastError());
           }
         }
         continue;
       }
       default: continue;
     }
   }
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| 止损函数 function                                               |
//+------------------------------------------------------------------+
int _StopLoss(int MAGIC, double SL)//_MoveStop(MAGIC, MOVE);
  {
//----
   if (SL<=0) return(0);//0表示不执行止损
   double StopLoss;
   int OrdersTotal4StopLoss=MTDOrdersTotal();

   for ( int z = OrdersTotal4StopLoss - 1; z >= 0; z -- )
   {
     if ( !MTDOrderSelect( z, SELECT_BY_POS ) )
     {
       Print("MTDOrderSelect(", z, ",SELECT_BY_POS) - Error #",GetLastError() );
       continue;
     }
     if (MTDOrderSymbol()!=Symbol())continue;
     if (MTDOrderMagicNumber() != MAGIC )continue;
     switch (MTDOrderType())
     {
       case OP_BUY   : 
       {
         StopLoss=NormalizeDouble(MTDOrderOpenPrice()-SL,Digits);
         //Alert(DoubleToStr(StopLoss,Digits));
         if (MTDOrderStopLoss()==0.0)
         {
           if(!MTDOrderModify(MTDOrderTicket(),MTDOrderOpenPrice(),StopLoss,MTDOrderTakeProfit(),MTDOrderExpiration()))
           {
             Alert("StopLoss_MTDOrderModify Error #",GetLastError());
           }
         }
         continue;
       }
       case OP_SELL:
       {
         StopLoss=NormalizeDouble(MTDOrderOpenPrice()+SL,Digits);
         if (MTDOrderStopLoss()==0.0)
         {
           if(!MTDOrderModify(MTDOrderTicket(),MTDOrderOpenPrice(),StopLoss,MTDOrderTakeProfit(),MTDOrderExpiration()))
           {
             Alert("StopLoss_MTDOrderModify Error #",GetLastError());
           }
         }
         continue;
       }
       default: continue;
     }
   }
//----
   return(0);
  }
 

//+------------------------------------------------------------------+
void Drawlabel(int x, int y,string text, color fcolor=Lime,int fsize=12)
{
     
      string id=id_pre+ids;
      string font="Arial";
      ObjectDelete(id);
      ObjectCreate( id, OBJ_LABEL, 0, 0, 0 );
      ObjectSetText(id,text,fsize,font,fcolor);
      ObjectSet( id, OBJPROP_XDISTANCE, x );
      ObjectSet( id, OBJPROP_YDISTANCE, y );
      ObjectSet( id, OBJPROP_BACK, true );
      //ObjectSet( id, OBJPROP_CORNER, corner );
      ObjectSet( id, OBJPROP_CORNER, 0);
}
 
void DrawOneMessage(string text, color fcolor,int fsize){
   int x=365,y=66;
   y=y+20*ids;
   Drawlabel(x,y,text,fcolor,fsize);


   ids=ids+1;
}

//删除xx开头的对象
void deleteOBJ_StartsWith(string findText){
  string ObjName;
  //int ObjType;
  double OBJ_ARROW_PRICE1=0;
  int obj_count=ObjectsTotal();
  for (int i = 0; i < obj_count; i++)
  {
    ObjName = ObjectName(i);
    int index=StringFind(ObjName, findText, 0);
    if(index==0){
      if(ObjectDeleteFromName(ObjName)==true){
         i=i-1;
         obj_count=ObjectsTotal();
      }
    }   
  }
  //return (0);

//删除具体的某个对象
bool ObjectDeleteFromName(string ObjName){
         bool result=true;
         int try_again=10;//
         while(try_again>0){
            if(ObjectDelete(ObjName)==true){
               try_again=0;
               result=true;
            }else{
               try_again=try_again-1;
               result=false;
            }           
         }
         return (result);
}




[后退] [返回首页] 复盘侠MTCommander(MTDriver)用户手册 [切换到电脑版] [复制网址]