文档库 最新最全的文档下载
当前位置:文档库 › 注释了一个mt4止损策略代码

注释了一个mt4止损策略代码

注释了一个mt4止损策略代码
注释了一个mt4止损策略代码

//+----------------------------------------------------------------------------+

// TrailStop functi** - 移动止损,追踪止损

// 可选择两种方式:

// 1.相当于移动一个以上TrailStop单位的时候,把止损移动到一个TrailStop的位置,就是无视TrailStop之内的杂波

// 2.相当于移动多的2*TrailStop的时候,才把止损移动到一个TrailStop的位置,相当于无视2*TrailStop之内的杂波

//+----------------------------------------------------------------------------+

void TrailStop(int MargicNum, int TrailMode, double TrailStart, double TrailStop){ RefreshRates();

if(OrdersTotal()>0){

for(int i=0; i

OrderSelect(i,SELECT_BY_POS,MODE_TRADES);

if(OrderType()<=OP_SELL && OrderMagicNumber()==MargicNum){

//**买单

if(OrderType()==OP_BUY){

//**当前的获利点数超过开始跟踪的点数

if( NormalizeDouble((Bid-OrderOpenPrice()),Digits)>=NormalizeDouble(TrailStart*Point,Digits) ){

if(TrailMode==1){//--第一种方式

//**如果当前止损价格小于当前价格减去TrailStop的点数,相当于移动一个以上TrailStop 单位的时候,把止损移动到一个TrailStop的位置,就是无视TrailStop之内的杂波

if(NormalizeDouble(OrderStopLoss(),Digits)

}

}

if(TrailMode==2){//---第二种方式

//**当前价格-TrailStop大于止损价格+TrailStop,相当于移动多的2*TrailStop才把止损移动到一个TrailStop的位置,相当于无视2*TrailStop之内的杂波

if(Bid-TrailStop*Point >= OrderStopLoss()+TrailStop*Point){

OrderModify(OrderTicket(),OrderOpenPrice(),NormalizeDouble(Bid-TrailStop*Point,Digits),Ord erTakeProfit(),0,Blue);

}

}

}

}else{

//**卖单

if( NormalizeDouble((OrderOpenPrice()-Ask),Digits)>=NormalizeDouble(TrailStart*Point,Digits )){

if(TrailMode==1){//--第一种方式

if( (NormalizeDouble(OrderStopLoss(),Digits) > NormalizeDouble(Ask+TrailStop*Point,Digits)) || OrderStopLoss()==0 ){

OrderModify(OrderTicket(),OrderOpenPrice(),NormalizeDouble(Ask+TrailStop*Point,Digits),Or

derTakeProfit(),0,Red);

}

}

if(TrailMode==2){//---第二种方式

if((Ask+TrailStop*Point <=OrderStopLoss()-TrailStop*Point) || OrderStopLoss()==0 ){ OrderModify(OrderTicket(),OrderOpenPrice(),NormalizeDouble(Ask+TrailStop*Point,Digits),Or derTakeProfit(),0,Blue);

}

}

}

}

}

}

}

}

//+------------------------------------------------------------------+

#property library

//+----------------------------------------------------------------------------+

// BreakEven functi** -- 盈亏平衡点

// 移动止损修改订单---当盈利多少点点止损改为开盘价

//+----------------------------------------------------------------------------+

void BreakEven(int MargicNum, double BreakEven){

//**刷新预定义变量和系列数组的数据。

RefreshRates();

//**获取订单总数

if(OrdersTotal()>0){

//**循环遍历订单

for(int i=0; i

OrderSelect(i,SELECT_BY_POS,MODE_TRADES);

//**订单类型是OP_BUY和OP_SELL && 根据魔术号码确认是此EA开的仓

if(OrderType()<=OP_SELL && OrderMagicNumber()==MargicNum){

//**如果是买单

if(OrderType()==OP_BUY){

//NormalizeDouble() 给出环绕浮点值的精确度。返回双重类型的正常化

//OrderOpenPrice() 对于当前选择定单返回开价格

//Digits 返回当前货币对的汇率小数位

//**当前的获利点数超过盈利点数

if( NormalizeDouble((Bid-OrderOpenPrice()),Digits)>=NormalizeDouble(BreakEven*Point,Digit s)){

//OrderStopLoss() 对于当前选择定单返回止损值

//**止损在开盘价下方

if( (OrderOpenPrice()-OrderStopLoss())>0 || (OrderStopLoss()==0))

//OrderTicket() 对于当前选择定单返回定单编号

//OrderTakeProfit() 对于当前选择定单返回赢利值

//**设置止损到开盘价的位置

OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice(),OrderTakeProfit(),0,Blue);

}

}else{

//**卖单

if( NormalizeDouble((OrderOpenPrice()-Ask),Digits)>=NormalizeDouble(BreakEven*Point,Digit s)){

if(((OrderOpenPrice()-OrderStopLoss())<0) || (OrderStopLoss()==0))

OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice(),OrderTakeProfit(),0,Red);

}

}

}

}

}

}

相关文档