如何使收支平衡在一个条目中触发多次

时间:2019-06-19 15:51:01

标签: mql4

我现在试图使“盈亏平衡代码”触发不止一次, 示例EA的条目为1.28000,止损为1.28500 如果当前价格达到1.175000(50pips),则sl会收支平衡,例如达到1.28000(5pips)。

EA将在满足条件后不再进行更多修改。

因此,如果价格达到1.17000(100pips),则如何触发收支平衡,sl跌至(1.175000)(50点)

价格再次达到1.165000(150pips),sl升至1.17000(100pips)

我想做

BE_B_M(sl move to(example:5)) 

BE_B_T(price reach(example:50)) 

作为变量,每次价格达到目标变量时更改为下一个值 变成了

BE_B_M(sl move to(example:50)) and BE_B_T(price reach(example:100)) 

整个代码如下

extern double  BE_T_1      = 50;   
extern double  BE_M_1      = 5;  

extern double  BE_T_2      = 100;   
extern double  BE_M_2      = 50;

extern double  BE_T_3      = 150;   
extern double  BE_M_3      = 100;

double BE_S_M; 
double BE_S_T;

void MOVE_BE_1()

  {
   for(int b=OrdersTotal()-1;b>=0;b--)
     {

      if(OrderSelect(b,SELECT_BY_POS,MODE_TRADES))
         if(OrderMagicNumber()!=M_Number)continue;
      if(OrderSymbol()==Symbol())
         if(OrderType()==OP_BUY)
            if(Bid-OrderOpenPrice()>BE_S_T*Pips)
               if(OrderOpenPrice()>OrderStopLoss())
                  if(!OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice()+(BE_S_M*Pips),OrderTakeProfit(),0,CLR_NONE))
                     Print("eror");
     }

   for(int s=OrdersTotal()-1;s>=0;s--)
     {
      if(OrderSelect(s,SELECT_BY_POS,MODE_TRADES))
         if(OrderMagicNumber()!=M_Number)continue;
      if(OrderSymbol()==Symbol())
         if(OrderType()==OP_SELL)
            if(OrderOpenPrice()-Ask>BE_S_T*Pips)
               if(OrderOpenPrice()<OrderStopLoss())
                  if(!OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice()-(BE_S_M*Pips),OrderTakeProfit(),0,CLR_NONE))
                     Print("eror");
     }

  }

我希望价格会在进入后每50点出现sl

1 个答案:

答案 0 :(得分:1)

在这里,您可以将所有3个收支平衡点放在一个函数上。

注意:for-loopOP_BUY都可以使用1个OP_SELL

这是我的OnInit()

// Global variable
double point;

int OnInit()
  {
   if(Digits == 5 || Digits == 3) point=Point*10;
   else point=Point;
   return(INIT_SUCCEEDED);
  }

这里是BreakEven()函数

//+------------------------------------------------------------------+
//| Break even the trade at 3  levels                                |
//+------------------------------------------------------------------+
void BreakEven()
  {
   // get the stop level for that symbol
   double stopLevel = SymbolInfoInteger(Symbol(),SYMBOL_TRADE_STOPS_LEVEL)*Point;

   for(int i=OrdersTotal()-1;i>=0;i--)
     {
      if(!OrderSelect(i,SELECT_BY_POS,MODE_TRADES)) continue;
      if(OrderMagicNumber()!=M_Number)continue;
      if(OrderSymbol()!=Symbol())continue;

      if(OrderType()==OP_BUY)
        {
         double profitPips=Bid-OrderOpenPrice();
         double newSL=OrderStopLoss();

         if(profitPips>=BE_T_1*point && OrderStopLoss()<OrderOpenPrice()) // Break Even
           {
            newSL=OrderOpenPrice()+BE_M_1*point;
           }
         else if(profitPips>=BE_T_3*point) // 150/100
           {
            newSL=OrderOpenPrice()+BE_M_3*point;
           }
         else if(profitPips>=BE_T_2*point) // 100/50
           {
            newSL=OrderOpenPrice()+BE_M_2*point;
           }

         if(newSL>=OrderStopLoss()+Point && newSL<Bid-stopLevel)
            if(!OrderModify(OrderTicket(),OrderOpenPrice(),NormalizeDouble(newSL,Digits),OrderTakeProfit(),0))
               Print("Error at BE: ",GetLastError());
        }
      else if(OrderType()==OP_SELL)
        {
         double profitPips=OrderOpenPrice()-Ask;
         double newSL=OrderStopLoss();
         if(profitPips>=BE_T_1*point && (OrderStopLoss()>OrderOpenPrice() || OrderStopLoss()==0)) // Break Even
           {
            newSL=OrderOpenPrice()-BE_M_1*point;
           }
         else if(profitPips>=BE_T_3*point) // 150/100
           {
            newSL=OrderOpenPrice()-BE_M_3*point;
           }
         else if(profitPips>=BE_T_2*point) // 100/50
           {
            newSL=OrderOpenPrice()-BE_M_2*point;
           }

         if(newSL<=OrderStopLoss()-Point && newSL>Ask+stopLevel)
            if(!OrderModify(OrderTicket(),OrderOpenPrice(),NormalizeDouble(newSL,Digits),OrderTakeProfit(),0))
               Print("Error at BE: ",GetLastError());
        }
     }

  }

我自己没有在交易中对此进行测试,但是应该可以。