NTPClient UDP Exchange Server delay
A function to obtain the time of the remote NTP server, which is more common in high-frequency trading or calculation delays
A function to obtain the time of the remote NTP server, which is more common in high-frequency trading or calculation delays
Source JavaScript
function ntp_client() {
// compatible with backtesting
if (IsVirtual()) {
return new Date().getTime()
}
let client = Dial("udp://time.windows.com:123")
client.write('\x1b' + Array(47).fill('\0').join(''))
let buf = client.read()
client.close()
if (buf && buf.byteLength == 48) {
let dataView = new DataView(buf)
return (dataView.getUint32(40, false) - 2208988800) * 1000 + Math.floor((dataView.getUint32(44, false) / 4294967295) * 1000)
}
return null
}
// compare local time and binance delay
function localDelayBinance() {
let delay = 0
for (let i = 0; i < 6; i++) {
let ts = 0
// 1 - 4 ignore first time
if (i> 0 && i < 5) {
ts = new Date().getTime()
}
let res = HttpQuery("https://fapi.binance.com/fapi/v1/time")
let now = new Date().getTime()
if (ts > 0) {
delay += now - ts
}
if (i == 5) {
return now - JSON.parse(res).serverTime + Math.ceil(delay / 2 / 4)
}
}
}
function main() {
let timestamp = ntp_client()
Log(_D(timestamp) + '.' + timestamp%1000, timestamp)
}Published by 发明者量化 on the FMZ strategy square · last modified 2023-09-07. 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/NTPClient-UDP-Exchange-Server-delay.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.