个人MT4外汇开户_锁仓顺势加码EA源码

作者:MT4 发布时间:2021-09-22 15:04

//+------------------------------------------------------------------+
//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));
Co妹妹ent( 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 TakeProf外汇开户赠金it 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,"对于冲双已经天生, 体系将行亏改成整,必要手动启锁!");
  }  
}       
    
}