手机版

锁仓顺势加码EA源码

阅读 :
外_汇_邦 WaiHuiBang.com

//+------------------------------------------------------------------+
//2006.12.20  V2.0  增加了对手动订单的主动止盈功能
//2007.01.03  V2.1  对于已经成交的对冲单,系统不能自动平仓或删除,需要手动平仓.
//2007.01.09  V2.2  系统自动生成对冲单后,如果手动订单设置了止盈,则取消止盈.
#property copyright ""
#property link      ""
#include <stdlib.mqh>
extern bool USE_ATR=true;
extern int PercentATR=40; //40% ATR
extern double HedgingLevel=30;
extern bool AutoTakeProfit=false;
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
  {
//----
Hedge_Assistante();   
//----
   return(0);
  }
//+------------------------------------------------------------------+
void Hedge_Assistante()
{
int i,Tic,Ticket,Hedge,Type;
double TP;
string msg="/n"+"ATR(14)="+iATR(Symbol(),PERIOD_D1,14,0)+"/n"+"dHigh:"+iHigh(Symbol(),PERIOD_D1,0)+"   dLow:"+
            iLow(Symbol(),PERIOD_D1,0)+"/n"+"AtrHi:"+(iLow(Symbol(),PERIOD_D1,0)+iATR(Symbol(),PERIOD_D1,14,0))+
            "   AtrLo:"+(iHigh(Symbol(),PERIOD_D1,0)-iATR(Symbol(),PERIOD_D1,14,0));
Comment( msg,Red,10);
//1)仅对于BUY/SELL 手动订单进行止盈, BUY/SELL STOP 和BUY/SELL LIMIT 的手动订单不会进行止盈
//2)GBP/USD 止盈 17点, 其他货币对止盈 10点
if(AutoTakeProfit) 
  { for(i=0;i<OrdersTotal();i++)
    {
     if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) continue;
     if(Symbol()==OrderSymbol() && OrderMagicNumber()==0 && OrderProfit()!=0) //找到手动订单
         { 
         if(Symbol()=="GBPUSD") TP=17; else TP=10; // GBPUSD takeprofit is 17, other pairs takeprofit is 10
         if(OrderType()==OP_BUY ) 
              { 
               if (Bid-OrderOpenPrice()>=TP*Point) 
                 {
                 if(OrderClose(OrderTicket(),OrderLots(),Bid,2,0)==true)
                   {Alert("Good Job! 自动止盈@",Bid,"  ",OrderTicket(),"   ",OrderTakeProfit()," - ",OrderOpenPrice(),
                    " = ",OrderTakeProfit()-OrderOpenPrice()," X ",OrderLots()," = ",OrderProfit());
                    return(0);
                    }   
                 else Alert(OrderTicket()," Close Order err---->",ErrorDescription(GetLastError()));
                 }    
                 
                     
               if (OrderTakeProfit()-OrderOpenPrice()<(TP-1)*Point || OrderTakeProfit()-OrderOpenPrice()>(TP+1)*Point)
                 {  
                 if(OrderModify(OrderTicket(),OrderOpenPrice(),OrderStopLoss(),OrderOpenPrice()+TP*Point,0,0)==true)
                 {Alert(OrderTicket(),"  ",Symbol(),"  Buy=",OrderOpenPrice(),"  设置止盈@",OrderOpenPrice()+TP*Point);
                  Sleep(10000);
                 }
                 else Alert(OrderTicket(),"  ",Symbol()," Modify TakeProfit error---->",ErrorDescription(GetLastError()));  
                 }
              } 
           
          if(OrderType()==OP_SELL) 
              { 
              if (OrderOpenPrice()-Ask>=TP*Point) 
                {
                if(OrderClose(OrderTicket(),OrderLots(),Ask,2,0)==true)
                   {Alert("Good Job! 自动止盈@",Ask,"  ",OrderTicket(),"   ",OrderOpenPrice()," - ",OrderTakeProfit(),
                    " = ",OrderOpenPrice()-OrderTakeProfit()," X ",OrderLots()," = ",OrderProfit());
                    return(0);  
                  }
                else  Alert(OrderTicket(),"  ",Symbol()," Modify TakeProfit error---->",ErrorDescription(GetLastError()));  
                }
             
              if (OrderOpenPrice()-OrderTakeProfit()>(TP+1)*Point ||OrderOpenPrice()-OrderTakeProfit()<(TP-1)*Point) 
                   {
                    if(OrderModify(OrderTicket(),OrderOpenPrice(),OrderStopLoss(),OrderOpenPrice()-TP*Point,0,0)==true)
                    {Alert(OrderTicket(),"  ",Symbol(),"  Sell=",OrderOpenPrice(),"  设置止盈@",OrderOpenPrice()-TP*Point);
                    Sleep(10000);
                    }
                    else Alert(OrderTicket(),"  ",Symbol()," Modify TakeProfit error---->   ",ErrorDescription(GetLastError()));  
                  }
               } 
           }   
       }
    }
//-------------------------------------------------------------------------

if (USE_ATR) HedgingLevel=iATR(NULL,PERIOD_D1,14,0)*PercentATR/100;
else HedgingLevel=HedgingLevel*Point;
//Check all manual order, if no it's countpart hedging order, then creat one
for (i=0;i<OrdersTotal();i++)
{if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break; 
  if(Symbol()==OrderSymbol() && OrderMagicNumber()==0 && OrderProfit()!=0/*Actived!*/ ) //it's a manual Order 
   {
     Hedge=0;
     Ticket=OrderTicket();
     Type=OrderType();
    double Lots=OrderLots(),OpenPrice=OrderOpenPrice();  
     for (int j=i;j<OrdersTotal();j++)
     {if (OrderSelect(j,SELECT_BY_POS,MODE_TRADES)==false) break;
      if (Ticket==OrderMagicNumber()) Hedge++;
     }
    if (Hedge==0) //no hedge order, creat one
     {
      if (Type==OP_BUY) 
         {
         Tic=OrderSend(Symbol(),OP_SELLSTOP,Lots, NormalizeDouble(OpenPrice-HedgingLevel,Digits),0,0,0,"Hedging "+DoubleToStr(Ticket,0),Ticket,0,Green);
         if( Tic>0) Alert(Ticket,"的对冲单:",Tic," 生成  SELL STOP:",NormalizeDouble(OpenPrice-HedgingLevel,Digits),"  ",Lots,"Lots");   
         }
      if (Type==OP_SELL) 
         { 
         Tic=OrderSend(Symbol(),OP_BUYSTOP,Lots,NormalizeDouble(OpenPrice+HedgingLevel,Digits),0,0,0,"Hedging "+DoubleToStr(Ticket,0),Ticket,0,Red);
         if( Tic>0) Alert(Ticket,"的对冲单:",Tic," 生成 BUY STOP:",NormalizeDouble(OpenPrice+HedgingLevel,Digits),"  ",Lots,"Lots");   
         }     
     }
    }  
   }             
  
//Check  all hedge order, if no it's countpart manual order, then delete the hedge Order
for (i=0;i<OrdersTotal();i++)
  {if  (OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break;
   if  (Symbol()==OrderSymbol() && OrderMagicNumber() !=0) //it's a hedge OrderClose
     {Hedge=0;
      Ticket=OrderTicket();
      int Magic=OrderMagicNumber();   
      for (j=0;j<OrdersTotal();j++) //check all manual OrderClose
        {if (OrderSelect(j,SELECT_BY_POS,MODE_TRADES)==false) break;      
         if (Magic==OrderTicket()) Hedge++;//use Hedge Order's Magic# compare with Maunal order's Ticket#
        }     
      if (Hedge==0 && OrderType()>1/*exclde type OP_BUY && OP_SELL   */ ) OrderDelete(Ticket);//no it's countpart manual order exist, delete this hedge OrderClose     
     }
  }

//v2.2  revise manul order TP=0 if Hedge order have created.
for (i=0;i<OrdersTotal();i++)

  if(OrderSelect(i,SELECT_BY_POS)==false) continue;
  if(Symbol()==OrderSymbol() && OrderMagicNumber()!=0 && OrderType()<2) //Actived HedgeOrder founded!
  {
    Ticket=OrderMagicNumber();
    if( OrderSelect(Ticket,SELECT_BY_TICKET)==false) break;
    if( OrderTakeProfit()!=0) 
    if(OrderModify(Ticket,OrderOpenPrice(),0,0,0,0)) Alert(Ticket,"对冲单已生成, 系统将止盈改为零,需要手动开锁!");
  }  
}       
    
}

 

 

 

外_汇_邦 WaiHuiBang.com
本文标题:锁仓顺势加码EA源码 - MT4平台MQL4编程学习
本文地址:https://www.waihuibang.com/fxschool/autotrading/mql4/40682.html

相关文章

  • 外汇程序化交易的八个湿货

    投机就像山岳一般古老。毋庸费言,外汇零售市场的属性,必然会让国内的大多数外汇交易者,以中短期投机的方式居多,长期价值投资的偏少。而作为波动市场的王者,外汇货币给了手工交易和程序化交易最丰富的可能性,此种优势,其...

    MQL4编程学习
  • 外汇EA功能性的脚本-含源码

    能显示你在图表上画的水平线距离现价的点数,只要把它拖到你使的图表中即可!我写过的脚本太多了。比如在图表上画一条水平线然后系统判断所处位置自动挂单,如果移动线的位置挂单的价格也会跟着改变。如果订单盈利后只...

    MQL4编程学习
  • 新版MQL4错误代码中文释义(含新增错误代码)

    新版MQL4增加了许多内容,于是,自定义了一个错误代码函数,并重新翻译了错误代码的中文意思。作为手册查看也是不错的。...

    MQL4编程学习
  • 外汇EA,我最爱、亦是我最怕的东西
    外汇EA,我最爱、亦是我最怕的东西

    外汇EA是我的最爱,也是我的最怕。这几年,交易技术与EA编程技术齐头并进,EA编程技术自觉算是比较老到了,交易却离修成正果似乎还遥遥无期。编写过N多的EA,只要是有个想法,突发奇想的、冥思苦想的、蓄谋已久...

    MQL4编程学习
  • 我对EA优化的方法
    我对EA优化的方法

    MT中仅有参数优化,但对于一个EA来说,参数优化仅仅是一个步骤. 我的想法是通过发现那些对EA有优化作用的指标及其参数范围来优化EA.简单讲讲我的思路和做法. 主要思路是在要优化的EA中插入一段代码,这段代码用来...

    MQL4编程学习
你可能感兴趣