SetChannelData

在频道上发布最新状态数据。该函数用于实盘间通信,可将当前实盘的状态数据广播至频道,供其他实盘订阅获取。

函数无返回值。 空值

SetChannelData(data)

需要发布到频道的数据,可以是任何支持JSON序列化的数据结构,通常为包含实盘状态信息的对象。 data true object / array / string / number / bool / 空值

function main() {
    var updateId = 0
    var robotId = _G()  // 获取当前实盘ID

    while(true) {
        // 获取实时市场价格
        var ticker = exchange.GetTicker("BTC_USDT")
        if (!ticker) {
            Sleep(5000)
            continue
        }

        // 准备当前频道状态数据
        var channelState = {
            robotId: robotId,
            updateId: ++updateId,
            timestamp: Date.now(),
            symbol: "BTC_USDT",
            lastPrice: ticker.Last,
            volume: ticker.Volume,
            high: ticker.High,
            low: ticker.Low
        }

        // 在频道上发布最新状态(覆盖旧状态)
        SetChannelData(channelState)

        // 显示当前频道状态
        LogStatus("频道广播端 [实盘ID: " + robotId + "]\n" +
                  "更新ID: #" + channelState.updateId + "\n" +
                  "时间: " + _D(channelState.timestamp) + "\n" +
                  "交易对: " + channelState.symbol + "\n" +
                  "最新价: $" + channelState.lastPrice.toFixed(2) + "\n" +
                  "成交量: " + channelState.volume.toFixed(4) + "\n" +
                  "最高价: $" + channelState.high.toFixed(2) + "\n" +
                  "最低价: $" + channelState.low.toFixed(2))

        Sleep(60000)  // 每分钟更新一次频道状态
    }
}```
```python
def main():
    updateId = 0
    robotId = _G()  # 获取当前实盘ID

    while True:
        # 获取实时市场价格
        ticker = exchange.GetTicker("BTC_USDT")
        if not ticker:
            Sleep(5000)
            continue

        # 准备当前频道状态数据
        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

        # 在频道上发布最新状态(覆盖旧状态)
        SetChannelData(channelState)

        # 显示当前频道状态
        LogStatus("频道广播端 [实盘ID: {}]\n".format(robotId) +
                  "更新ID: #{}\n".format(channelState["updateId"]) +
                  "时间: {}\n".format(_D(channelState["timestamp"])) +
                  "交易对: {}\n".format(channelState["symbol"]) +
                  "最新价: ${:.2f}\n".format(channelState["lastPrice"]) +
                  "成交量: {:.4f}\n".format(channelState["volume"]) +
                  "最高价: ${:.2f}\n".format(channelState["high"]) +
                  "最低价: ${:.2f}".format(channelState["low"]))

        Sleep(60000)  # 每分钟更新一次频道状态```
```cpp

频道广播端示例 - 发布BTC价格行情数据

SetChannelData()函数为非阻塞调用,调用后立即返回,不会等待数据传输完成。 每个实盘都拥有独立的频道,频道ID即为实盘ID(可通过_G()函数获取)。 频道仅保存最新的状态数据,每次调用SetChannelData()会覆盖之前发布的数据,而非追加历史消息。 频道数据支持跨实盘、跨托管者、跨服务器广播,多个实盘可订阅同一频道。 订阅端使用GetChannelData()函数订阅频道数据。 频道通信适用于实盘环境,在回测系统中该功能可能受限。 传入的数据参数data经JSON序列化后的字节长度不得超过1024字节,超出限制可能导致数据发布失败。建议仅传输必要的状态信息,避免传输过大的数据对象。 发布的数据应根据硬件设备的内存、网络带宽合理使用,避免发布过大的数据对象。

{@fun/Global/GetChannelData GetChannelData} {@fun/Global/G _G}