SeamlessConnWS
https://www.fmz.com/bbs-topic/4328
JavaScriptOther
Parameters
| Name | Default | What it controls |
|---|---|---|
| IsUsedWebSocket | false | Enable or NotWebSocket |
| Hook_GetTicker | false | HookMarket Data InterfaceGetTicker |
Source JavaScript
function WSConnecter_Huobi (method, symbol) {
// wss : wss://api.huobi.pro/ws
/* Subscribe
{
"sub": "market.btcusdt.detail",
}
*/
/* Subscribe string
'{"sub": "market.btcusdt.detail"}'
*/
var strSubscribe = ""
if (method == "GetTicker") {
symbol = symbol.toLowerCase()
symbol = symbol.replace("_", "")
strSubscribe = '{"sub": "market.' + symbol + '.detail"}' // symbol : btcusdt
} else {
throw "Unrealized:" + method + "Method!"
}
var self = {}
self.method = method
self.pingCount = 0
Log("DialFunction Link:", "wss://api.huobi.pro/ws|compress=gzip&mode=recv&reconnect=true&payload=" + strSubscribe)
Log("Subscribe Information:", strSubscribe)
self.client = Dial("wss://api.huobi.pro/ws|compress=gzip&mode=recv&reconnect=true&payload=" + strSubscribe)
self.client.write(strSubscribe)
self.Read = function () {
try {
var ret = self.client.read()
var objRet = JSON.parse(ret)
if (objRet.ping) {
var pong = {"pong" : objRet.ping}
// Log("huobiConn Received heartbeat, sendpong:", JSON.stringify(pong))
self.client.write(JSON.stringify(pong))
self.pingCount++
return null
} else {
/* Encapsulated asFMZoftickerStructure
{
"ch":"market.btcusdt.detail",
"ts":1569477791639,
"tick":{
"id":205028275148,
"low":8217,
"high":8624.87,
"open":8517.89,
"close":8386.36,
"vol":361804831.52344716,
"amount":43197.45945060976,
"version":205028275148,
"count":335763
}
}
*/
var ticker = {}
if (!objRet.tick) {
return null
}
ticker.Info = objRet.tick
ticker.High = objRet.tick.high
ticker.Low = objRet.tick.low
ticker.Last = objRet.tick.close
ticker.Volume = objRet.tick.vol
ticker.Buy = objRet.tick.close
ticker.Sell = objRet.tick.close
ticker.Time = objRet.ts
return ticker
}
} catch (e) {
Log("error:", e)
return null
}
}
self.Close = function () {
self.client.close()
Log("method:", self.method, "DisconnectwsLink")
}
return self
}
function WSConnecter_Binance (method, symbol) {
// wss : wss://stream.binance.com:9443
// /ws/<streamName>
// Stream Name: <symbol>@ticker
// /ws/btcusdt@ticker
var strSubscribe = ""
if (method == "GetTicker") {
symbol = symbol.toLowerCase()
symbol = symbol.replace("_", "")
strSubscribe = "/ws/" + symbol + "@ticker"
} else {
throw "Unrealized:" + method + "Method!"
}
var self = {}
self.pingCount = 0
self.method = method
Log("DialFunction Link:", "wss://stream.binance.com:9443" + strSubscribe + "|reconnect=true")
self.client = Dial("wss://stream.binance.com:9443" + strSubscribe + "|reconnect=true")
self.Read = function () {
try {
var ret = self.client.read()
var objRet = JSON.parse(ret)
if (objRet.ping) {
var pong = {"pong" : objRet.ping}
Log("binanceConn Received heartbeat, sendpong:", JSON.stringify(pong))
self.client.write(JSON.stringify(pong))
self.pingCount++
return null
} else {
/*
{
"e":"24hrTicker",
"E":1569479950378,
"s":"BTCUSDT",
"p":"-72.67000000",
"P":"-0.855",
"w":"8380.87678669",
"x":"8504.99000000",
"c":"8430.98000000",
"Q":"0.10865300",
"b":"8430.02000000", // Buy1
"B":"2.00000000",
"a":"8431.00000000",
"A":"0.24020700",
"o":"8503.65000000",
"h":"8629.96000000",
"l":"8215.64000000",
"v":"52091.18312400",
"q":"436569787.43499342",
"O":1569393550372,
"C":1569479950372,
"F":182689950,
"L":183168699,
"n":478750
}
*/
var ticker = {}
ticker.Info = objRet
ticker.Buy = objRet.b
ticker.Sell = objRet.a
ticker.High = objRet.h
ticker.Low = objRet.l
ticker.Volume = objRet.v
ticker.Last = objRet.c
ticker.Time = objRet.E
Log("Push Market Data:", ticker) // Test
return ticker
}
} catch (e) {
Log("error:", e)
return null
}
}
self.Close = function () {
self.client.close()
Log("DisconnectwsLink")
}
return self
}
var _DictConnectCreater = {
"Huobi" : WSConnecter_Huobi,
"Binance" : WSConnecter_Binance,
}
var _ConnMap = {}
function init () {
if (IsUsedWebSocket) {
var connectCreater = null
if (exchanges.length != 1) {
Log("Switch towsInterface only for exchange Exchange object (i.e., the first added exchange object))")
}
var isFound = false
for (var name in _DictConnectCreater) {
if (exchange.GetName() == name) {
connectCreater = _DictConnectCreater[name]
isFound = true
}
}
if (!isFound) {
throw "No implementation found"
}
if (Hook_GetTicker) {
var symbol = exchange.GetCurrency()
_ConnMap.GetTicker = connectCreater("GetTicker", symbol)
exchange.GetTicker = function () {
return _C(_ConnMap.GetTicker.Read)
}
}
// ...
}
}
/* Will Not Trigger
function onexit(){
for (var method in _ConnMap) {
_ConnMap[method].Close()
}
}
*/
// Test
function main () {
}Published by 发明者量化-小小梦 on the FMZ strategy square · last modified 2019-09-26. Collected in the Quant Trading Vault. Unvalidated research: re-test with your own data and costs before trusting it.
Build this with AgenKit
A spec is not a trading system. Paste this into your coding agent and AgenKit runs a specialist team — architecture, test-first build, risk gates, review — with paper trading on by default.
/agenkit build a production-grade trading bot with a backtest engine, risk controls and paper trading by default from strategies/SeamlessConnWS.md- npx agenkit install engineering-kit in your project
- Paste the command into your agent and approve each phase
- Get tests, backtest, risk controls and a paper-trading switch
Understand it first
Before you run this, read the lesson behind the idea and how to backtest it honestly. Specs like this one are the survivors of an unknown number of trials; deflate their results accordingly.