Set Trailing Step According to Order Swap and Minimum Stop allowed

 

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 B:

//+------------------------------------------------------------------+
//| 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.

Documentation on MQL5: Constants, Enumerations and Structures / Trade Constants / Trade Orders in DOM
Documentation on MQL5: Constants, Enumerations and Structures / Trade Constants / Trade Orders in DOM
  • www.mql5.com
For equity securities, the Depth of Market window is available, where you can see the current Buy and Sell orders. Desired direction of a trade operation, required amount and requested price are specified for each order. To obtain...
Reason: