Facebook
From Tacky Iguana, 1 Year ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 65
  1. void OnTick()
  2. {
  3.     static int last_total_positions = -1; // The last total number of open positions
  4.     static MqlTradeRequest last_request = {0}; // The last trade request made
  5.     static MqlTradeResult last_result = {0}; // The last trade result received
  6.  
  7.     // Retrieve the total number of open positions
  8.     int total_positions = PositionsTotal();
  9.  
  10.     // Check if there are any open positions
  11.     if (total_positions > 0)
  12.     {
  13.         // Check if the total number of open positions has changed
  14.         if (total_positions != last_total_positions)
  15.         {
  16.             // Print the new total number of open positions in the Experts log
  17.             Print("Total open positions: ", total_positions);
  18.  
  19.             // Update the last total number of open positions received
  20.             last_total_positions = total_positions;
  21.         }
  22.  
  23.         // Loop through all open positions
  24.         for (int i = 0; i < total_positions; i++)
  25.         {
  26.             // Get the ticket number for the current position
  27.             ulong ticket = PositionGetTicket(i);
  28.  
  29.             // Check if we already made a request for this position
  30.             if (last_request.action == TRADE_ACTION_DEAL && last_request.order == ticket)
  31.             {
  32.                 // Check if we received a result for the previous request
  33.                 if (last_result.retcode == TRADE_RETCODE_DONE)
  34.                 {
  35.                     // Get the P/L for the current position from the trade result
  36.                     double profit = last_result.profit;
  37.  
  38.                     // Convert the P/L to a string
  39.                     string profit_str = DoubleToString(profit, Digits);
  40.  
  41.                     // Create the JSON payload for the WebSocket server
  42.                     string payload = "{"ticket": " + IntegerToString(ticket) + ", "profit": " + profit_str + "}";
  43.  
  44.                     // Send the P/L to the WebSocket server
  45.                     WebRequest(websocket_handle, "SEND", payload, "", "");
  46.  
  47.                     // Print the new P/L in the Experts log
  48.                     Print("Position ticket: ", ticket, ", P/L: ", profit_str);
  49.  
  50.                     // Reset the last request and result variables
  51.                     last_request.action = 0;
  52.                     last_request.order = 0;
  53.                     last_result.retcode = 0;
  54.                     last_result.order = 0;
  55.                     last_result.volume = 0;
  56.                     last_result.price = 0;
  57.                 }
  58.             }
  59.             else
  60.             {
  61.                 // Make a trade request to get the P/L for the current position
  62.                 MqlTradeRequest request;
  63.                 ZeroMemory(&request, sizeof(request));
  64.                 request.action = TRADE_ACTION_DEAL;
  65.                 request.symbol = PositionGetSymbol(i);
  66.                 request.order = ticket;
  67.                 request.type = ORDER_TYPE_SELL;
  68.                 request.volume = PositionGetDouble(POSITION_IDENTIFIER, POSITION_VOLUME);
  69.                 request.price = SymbolInfoDouble(request.symbol, SYMBOL_ASK);
  70.                 request.sl = PositionGetDouble(POSITION_IDENTIFIER, POSITION_SL);
  71.                 request.tp = PositionGetDouble(POSITION_IDENTIFIER, POSITION_TP);
  72.  
  73.                 // Send the trade request to the server
  74.                 int result = OrderSend(request, last_result);
  75.  
  76.                 // Check if the trade request was successful
  77.                 if (result > 0)
  78.                 {
  79.                     // Update the last trade request made
  80.                     last_request = request;
  81.                 }
  82.                 else
  83.                 {
  84.                     // Print the error message in the Experts log
  85.                     Print("Trade request error: ", ErrorDescription(GetLastError()));
  86.                 }
  87.             }
  88.         }
  89.     }
  90. }