advertisement
Multi-robot market quotes sharing solution
When using digital currency quantitative trading robots, when there are multiple robots running on one server, if you visit different exchanges, the problem is not serious at this time, and there will be no API request frequency problem. If you need to have multiple robots running at the same time and they all visit the same exchange with the same trading pair quantitative trading strategy. At this time, there will be some problems with limiting API request frequency. So how to solve the problem of multiple robots accessing interface with the least number of servers?
We can implement a market quote forwarding robot, and access the exchange interface to obtain market quotes and other data can only be completed by this robot. Other trading strategy robots can request data from this market forwarding robot.
Quote Forwarding Robot Example
It is only responsible for accessing the exchange market quotes interface to obtain data and provide market quotes to other robots. Written in Python, in the example we only get data from the K line and provide the exchange, which can be extended to increase the depth of the data, add market data, etc.
import _thread
import threads
import json
import math
from http.server import HTTPServer, BaseHTTPRequestHandler
from urllib.parse import parse_qs, urlparse
Records = None
lock = threading.RLock()
Counter = {}
def url2Dict(url):
query = urlparse(url).query
parameters = parse_qs(query)
result = {key: params[key][0] for key in parameters}
return result
Class Provider (BaseHTTPRequestHandler):
def do_GET(self):
Global registers, lock, counter
test:
self.send_response(200)
self.send_header("Content type", "application/json")
self.end_headers()
dictParam = url2Dict(self.path)
# Log("Service receives request, self.path:", self.path, "query parameter:", dictParam)
lock.acquire()
# Recording
if dictParam["robotId"] is not in the counter:
Counter[dictParam["robotId"]] = {"NumberOfRequests": 0}
Counter[dictParam["robotId"]]["NumberOfRequests"] += 1
lock.release()
# Write data response
self.wfile.write(json.dumps(Records).encode())
except BaseException as e:
Log("Do_GET error from provider, e:", e)
def createServer(host):
test:
server = HTTPServer(host, Provider)
Log("Starting server, listen on: %s:%s" % host)
server.serve_forever()
except BaseException as e:
Log("server creation error, e:", e)
raise exception ("stop")
main() definition:
Global Records, Counter
Reset registry(1)
test:
# _thread.start_new_thread(createServer, (("localhost", 9090), )) # local computer test
_thread.start_new_thread(createServer, (("0.0.0.0", 9090), )) # Test on VPS server
Registration("Start service", "#FF0000")
except BaseException as e:
Log("Error starting service!")
Log("Error message:", e)
raise exception ("stop")
while True:
r = exchange.GetRecords()
if not r:
Log("Error in market quote for line K", "#FF0000")
continue
more:
Records = r
# Counter
tbl = {
"type": "table",
"title": "Statistics",
"cols" : ["ID of the robot requesting data", "Number of requests"],
"rows": [],
}
for k in Counter:
tbl["rows"].append([k, Counter[k]["NumberOfRequests"]])
LogStatus(_D(), "Data collection!", "\n", "`" + json.dumps(tbl) + "`")
Sleep (500)
Request data robot strategy code
The robot that requests data is a trading strategy robot, but we use it for testing. We just write the requested data (K line data) and draw the data. You can write it in JavaScript. To make a drawing, you need to consult the "Line Drawing Library." Find and copy this class library on Strategy Square. After copying, you can select it in the template reference column on the strategy edit page.
var FuncGetRecords = exchange.GetRecords
exchange.GetRecords = function() {
//You can fill in the IP address of the device where the "quote forwarding robot" is located xxx.xxx.xxx.xxx
var ret = HttpQuery("http://xxx.xxx.xx
advertisement
Related Articles
advertisement