SetChannelData
Publishes the latest status data to a channel. This function is used for inter-live trading communication, allowing the current live trading instance to broadcast its status data to a channel for other live trading instances to subscribe and retrieve.
SetChannelData(data)Examples
-
Channel Broadcaster Example - Publishing BTC Price Market Data
javascriptfunction main() { var updateId = 0 var robotId = _G() // Get current live trading bot ID while(true) { // Get actual market price var ticker = exchange.GetTicker("BTC_USDT") if (!ticker) { Sleep(5000) continue } // Prepare current channel state data var channelState = { robotId: robotId, updateId: ++updateId, timestamp: Date.now(), symbol: "BTC_USDT", lastPrice: ticker.Last, volume: ticker.Volume, high: ticker.High, low: ticker.Low } // Publish latest state on channel (overwrites old state) SetChannelData(channelState) // Display current channel state LogStatus("Channel Broadcaster [Bot ID: " + robotId + "]\n" + "Update ID: #" + channelState.updateId + "\n" + "Time: " + _D(channelState.timestamp) + "\n" + "Symbol: " + channelState.symbol + "\n" + "Last Price: $" + channelState.lastPrice.toFixed(2) + "\n" + "Volume: " + channelState.volume.toFixed(4) + "\n" + "High: $" + channelState.high.toFixed(2) + "\n" + "Low: $" + channelState.low.toFixed(2)) Sleep(60000) // Update channel state every minute } }pythondef main(): updateId = 0 robotId = _G() # Get current live trading bot ID while True: # Get actual market price ticker = exchange.GetTicker("BTC_USDT") if not ticker: Sleep(5000) continue # Prepare current channel state data channelState = { "robotId": robotId, "updateId": updateId + 1, "timestamp": time.time() * 1000, "symbol": "BTC_USDT", "lastPrice": ticker["Last"], "volume": ticker["Volume"], "high": ticker["High"], "low": ticker["Low"] } updateId += 1 # Publish latest state on channel (overwrites old state) SetChannelData(channelState) # Display current channel state LogStatus("Channel Broadcaster [Bot ID: {}]\n".format(robotId) + "Update ID: #{}\n".format(channelState["updateId"]) + "Time: {}\n".format(_D(channelState["timestamp"])) + "Symbol: {}\n".format(channelState["symbol"]) + "Last Price: ${:.2f}\n".format(channelState["lastPrice"]) + "Volume: {:.4f}\n".format(channelState["volume"]) + "High: ${:.2f}\n".format(channelState["high"]) + "Low: ${:.2f}".format(channelState["low"])) Sleep(60000) # Update channel state every minutec++ -
Cross-platform sending example - Simulating external platform (e.g., TradingView) sending data to FMZ live trading
javascript// This example demonstrates how to use HttpQuery to send HTTP POST requests, simulating external platforms sending data to FMZ live trading // In actual scenarios, external platforms (such as TradingView's Webhook alert URL, third-party trading systems, etc.) would directly call the FMZ API endpoint function main() { let uuid = "6BC42A119B5DBFA2188A8279DA3B5C30" let robotId = 123456 // Target live trading ID (the live trading instance that needs to receive data) let baseUrl = "https://www.fmz.com" while (true) { // Prepare the data to send (can be JSON, text, or other formats) let sendData = { "action": "buy", "symbol": "BTC_USDT", "price": 50000, "timestamp": Date.now() } // Construct HTTP POST request let options = { method: "POST", body: JSON.stringify(sendData) // body can be JSON string, plain text, etc. } let url = `${baseUrl}/api/v1?method=pub&robot=${robotId}&channel=${uuid}` // Send data let ret = HttpQuery(url, options) Log("Simulated external platform sending data, result:", ret) Sleep(10000) // Send every 10 seconds } }python# This example demonstrates how to use HttpQuery to send HTTP POST requests, simulating external platforms sending data to FMZ live trading # In actual scenarios, external platforms (such as TradingView's Webhook alert URL, third-party trading systems, etc.) would directly call the FMZ API endpoint import json def main(): uuid = "6BC42A119B5DBFA2188A8279DA3B5C30" robotId = 123456 # Target live trading ID (the live trading instance that needs to receive data) baseUrl = "https://www.fmz.com" while True: # Prepare the data to send (can be JSON, text, or other formats) sendData = { "action": "buy", "symbol": "BTC_USDT", "price": 50000, "timestamp": time.time() * 1000 } # Construct HTTP POST request options = { "method": "POST", "body": json.dumps(sendData) # body can be JSON string, plain text, etc. } url = "{}/api/v1?method=pub&robot={}&channel={}".format(baseUrl, robotId, uuid) # Send data ret = HttpQuery(url, options) Log("Simulated external platform sending data, result:", ret) Sleep(10000) # Send every 10 secondsc++
Returns
| Type | Description |
null value | The function has no return value. |
Arguments
| Name | Type | Required | Description |
data | object / array / string / number / bool / null value | Yes | The data to be published to the channel, can be any data structure that can be |
See Also
Remarks
The SetChannelData() function is a non-blocking call that returns immediately after invocation without waiting for data transmission to complete.
Each live trading instance has an independent channel, and the channel ID is the live trading ID (which can be obtained through the _G() function).
Only the latest status data is saved on the channel. Each call to SetChannelData() overwrites the previously published data rather than appending historical messages.
Channel data can be broadcast across live trading instances, across dockers, and across servers. Multiple live trading instances can subscribe to the same channel.
The subscriber uses the GetChannelData() function to subscribe to channel data.
Channel communication is applicable to live trading environments; this feature may be limited in the backtesting system.
The byte length of the data parameter data after JSON serialization must not exceed 1024 bytes. Exceeding this limit may cause data publishing to fail. It is recommended to transmit only necessary status information and avoid transmitting overly large data objects.
Published data should be used reasonably according to the memory and network bandwidth of hardware devices. Avoid publishing overly large data objects.
In addition to being subscribed by other live trading instances within the FMZ platform, data published by the SetChannelData() function also supports cross-platform data sending functionality. External platforms (such as TradingView Webhook alerts, third-party trading systems, monitoring software, etc.) can send data to a specified FMZ live trading instance via HTTP POST requests.
Cross-platform data sending method: External systems send data to the FMZ platform API endpoint via HTTP POST request: https://www.fmz.com/api/v1?method=pub&robot={robotId}&channel={uuid}, where robotId is the target live trading ID, and uuid is a 32-character channel identifier. The data to be sent is passed in the request body and can be in JSON format, text, or other formats. Note: A live trading instance must first subscribe to the UUID channel before external systems can successfully send data; the broadcast data will be sent to all live trading instances under the docker where the robotId live trading instance is located, and all live trading instances under the same docker that have subscribed to the UUID channel can receive the data.