首页 > EA > EA编程

【外汇EA编程学习】锁仓顺势加码EA源码

小明学外汇EA编程 2019-09-09 23:07:28 交流微信号:FX263cn 外汇EA编程   锁仓顺势



//+------------------------------------------------------------------+
//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,"对冲单已生成, 系统将止盈改为零,需要手动开锁!");
  }  
}       
    
}

全球外汇中文网Fx263.com  ,阅读更多请登录www.Fx263.com

【版权声明】

本文仅代表作者观点,不代表本站立场。Fx263.com对文中陈述、观点判断保持中立,不对所包含内容的准确性、可靠性或完整性提供任何明示或暗示的保证,且不构成任何投资建议,请读者仅作参考,并自行承担全部风险与责任。Fx263.com作为信息内容发布平台,页面展示内容的目的在于传播更多信息,不代表Fx263.com立场;本站会员及自媒体人所发的稿件所载明的信息与本网无关,如文章涉及版权,请联系本站处理。

【风险提示】

请通过正规渠道参与外汇保证金交易。目前通过网络平台提供、参与外汇保证金交易均属非法。请提高意识,谨防损失!外汇、贵金属和差价合约(OTC场外交易)是杠杆产品,存在较高的风险,可能会导致亏损您的投资本金,请理性投资。

 

Fx263.com - 全球财经中文网

官方交流微信号:FX263cn注明来意

重要声明 | 本站内容不适用于中国大陆地区访客。

Powered By Fx263.com - 版权所有

大中华地区全球财经中文网

本站内容仅供参考盈亏自负

FX263网 - 全球财经中文网