> ## Documentation Index
> Fetch the complete documentation index at: https://docs.qfex.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Public Trades

QFEX provides a **realtime stream of executed trades** across all symbols via websocket.

## How it works

Connect to the public websocket:

* URL: **wss\://mds.qfex.com**

Then send a subscribe message:

```json theme={null}
{
  "type": "subscribe",
  "channels": ["trade"],
  "symbols": ["AAPL-USD", "US500-USD"]
}
```

## Sample Code

<CodeGroup>
  ```python python theme={null}
  # Python (websocket-client)
  # pip install websocket-client
  import json
  import websocket

  def on_open(ws):
      sub = {
          "type": "subscribe",
          "channels": ["trade"],
          "symbols": ["US500-USD"]  # or ["*"]
      }
      ws.send(json.dumps(sub))

  def on_message(ws, message):
      data = json.loads(message)
      print("Trade update:", json.dumps(data, indent=2))

  def on_error(ws, error):
      print("Error:", error)

  def on_close(ws, close_status_code, close_msg):
      print("Closed:", close_status_code, close_msg)

  if __name__ == "__main__":
      ws = websocket.WebSocketApp(
          "wss://mds.qfex.com",
          on_open=on_open,
          on_message=on_message,
          on_error=on_error,
          on_close=on_close,
      )
      ws.run_forever()
  ```

  ```javascript node theme={null}
  // Node.js (ws)
  // npm i ws
  import WebSocket from "ws";

  const ws = new WebSocket("wss://mds.qfex.com");

  ws.on("open", () => {
    const sub = {
      type: "subscribe",
      channels: ["trade"],
      symbols: ["US500-USD"], // or ["*"]
    };
    ws.send(JSON.stringify(sub));
  });

  ws.on("message", (msg) => {
    try {
      const data = JSON.parse(msg.toString());
      console.log("Trade update:", data);
    } catch (e) {
      console.error("Parse error:", e);
    }
  });

  ws.on("error", (err) => console.error("WS error:", err));
  ws.on("close", (code, reason) =>
    console.log("Closed:", code, reason?.toString())
  );
  ```

  ```go go theme={null}
  // Go (gorilla/websocket)
  // go get github.com/gorilla/websocket
  package main

  import (
  	"fmt"
  	"log"
  	"os"
  	"os/signal"

  	"github.com/gorilla/websocket"
  )

  func main() {
  	c, _, err := websocket.DefaultDialer.Dial("wss://mds.qfex.com", nil)
  	if err != nil {
  		log.Fatal("dial:", err)
  	}
  	defer c.Close()

  	sub := map[string]interface{}{
  		"type":     "subscribe",
  		"channels": []string{"trade"},
  		"symbols":  []string{"US500-USD"}, // or []string{"*"}
  	}
  	if err := c.WriteJSON(sub); err != nil {
  		log.Fatal("write:", err)
  	}

  	done := make(chan struct{})
  	go func() {
  		defer close(done)
  		for {
  			_, message, err := c.ReadMessage()
  			if err != nil {
  				log.Println("read:", err)
  				return
  			}
  			fmt.Printf("Trade update: %s\n", message)
  		}
  	}()

  	// keep running until Ctrl+C
  	interrupt := make(chan os.Signal, 1)
  	signal.Notify(interrupt, os.Interrupt)
  	<-interrupt
  }
  ```

  ```java java theme={null}
  // Java (OkHttp WebSocket)
  // Gradle: implementation("com.squareup.okhttp3:okhttp:4.12.0")
  import java.util.concurrent.TimeUnit;
  import okhttp3.*;

  public class TradeWs {
    public static void main(String[] args) {
      OkHttpClient client = new OkHttpClient.Builder()
          .pingInterval(20, TimeUnit.SECONDS)
          .build();

      Request request = new Request.Builder()
          .url("wss://mds.qfex.com")
          .build();

      WebSocketListener listener = new WebSocketListener() {
        @Override public void onOpen(WebSocket webSocket, Response response) {
          String sub = "{\"type\":\"subscribe\",\"channels\":[\"trade\"],\"symbols\":[\"US500-USD\"]}";
          webSocket.send(sub);
        }
        @Override public void onMessage(WebSocket webSocket, String text) {
          System.out.println("Trade update: " + text);
        }
        @Override public void onFailure(WebSocket webSocket, Throwable t, Response response) {
          System.err.println("WS error: " + t.getMessage());
        }
      };

      client.newWebSocket(request, listener);

      // Keep JVM alive
      try { Thread.sleep(Long.MAX_VALUE); } catch (InterruptedException ignored) {}
    }
  }
  ```
</CodeGroup>

## Example Response

```json theme={null}
{
  "type": "trade",
  "trade_id": "ac928c66-ca53-498f-9c13-a110027a60e8",
  "sequence": 50,
  "time": "2014-11-07T08:19:27.028459Z",
  "symbol": "AAPL-USD",
  "size": "5.23512",
  "price": "400.23",
  "side": "sell",
  "execution_type": "NEW"
}
```

Please see [Enums](/api-reference/enums) for further details on `execution_type` and others.

### Unsubscribe (optional)

```json theme={null}
{ "type": "unsubscribe", "channels": ["trade"], "symbols": ["US500-USD"] }
```

***

**Notes**

* Messages are pushed in realtime as trades occur.
* Supports multiple symbols or a wildcard `*`.
* Fields mirror REST and gRPC trade objects.
