问题
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:
- Get the total swap of the SELL order.
- 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.
- 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