MQL4 - Set Trailing Step According to Order Swap and Minimum Stop allowed

帅比萌擦擦* 提交于 2020-06-17 16:17:23

问题


I am using a swap strategy with two account with different brokers (hedged accounts). I have the code that sync between the two account (via two brokers).

When i close a BUY order with broker A, my EA send Close message and the SELL order on broker B gets closed. i get profit from (swap-commission) on broker B.

What i need, instead of closing the order on broker B do the following:

  1. Get the total swap of the SELL order.
  2. Set the Trailing stop loss by the (swap-commission) value, e.g if the (swap-commission) is $50, set the stop loss so if it gets hit the loss will be $50 max.
  3. Check the minimum allowed stop loss by broker, and if the needed step (calculated on step 2) is above, do not set trailing stop , just close the order with the (swap-commission) profit.

I only have the trailing stop function that gets trigger when the BUY order on broker A is closed:

//+------------------------------------------------------------------+
//| Trailing stop loss                                               |
//+------------------------------------------------------------------+
void Trail1()
{
double PointValue;
  for (int i = 0; i < OrdersTotal(); i++) 
  {
      OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
      if (OrderSymbol() != Symbol()) continue; // Skipping positions in other currency pairs
      Print("Swap for the order #",OrderSwap());

      //Calculate the point value in case there are extra digits in the quotes
      if (MarketInfo(OrderSymbol(), MODE_POINT) == 0.00001) PointValue = 0.0001;
      else if (MarketInfo(OrderSymbol(), MODE_POINT) == 0.001) PointValue = 0.01;
      else PointValue = MarketInfo(OrderSymbol(), MODE_POINT);


      //Normalize trailing stop value to the point value
      double TSTP = TrailingStop * PointValue;

      if (OrderType() == OP_BUY)
      {
         if (OrderStopLoss() < Bid - TSTP)
         {
            if (!OrderModify(OrderTicket(), OrderOpenPrice(), Bid - TSTP, OrderTakeProfit(), OrderExpiration(), clrNONE))
               Print("Error setting Buy trailing stop: ", GetLastError());
         }
      }
      else if (OrderType() == OP_SELL)
      {
         if ((OrderStopLoss() > Ask + TSTP) || (OrderStopLoss() == 0))
         {
            if (!OrderModify(OrderTicket(), OrderOpenPrice(), Ask + TSTP, OrderTakeProfit(), OrderExpiration(), clrNONE))
               Print("Error setting Sell trailing stop: ", GetLastError());
         }
      }
    }
    }
// --------------- ----------------------------------------------------------- ------------------------

Please help.

来源:https://stackoverflow.com/questions/61068665/mql4-set-trailing-step-according-to-order-swap-and-minimum-stop-allowed

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!