锁仓顺势加码EA源码
2012-02-17 07:59:15 来源: 作者:
//+------------------------------------------------------------------+
//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,"对冲单已生成, 系统将止盈改为零,需要手动开锁!");
}
}
}