← Back to Home
advertisement
Implement a quantitative trading robot timed start or stop gadget by using Python

Implement a quantitative trading robot timed start or stop gadget by using Python

As a powerful, flexible and easy-to-use quantitative trading platform for the entire network, the FMZ quantitative trading platform has very low barriers to use and the robot program consumes little resources. But we still hope that the robot can start when it needs to work and stop when it doesn't need to. For example, when trading programmatic and quantitative commodity futures, downtime accounts for the majority of the day. In this way, we hope that the robot only works at opening time and only works at opening time every day to save money. Isn't it exciting? To meet this requirement, we can use the Python language to write a strategy robot running on FMZ Quant Trading Platform, and let the robot control the start and stop of the robot at regular intervals through the extended API interface of FMZ Quant Trading Platform. If necessary, let's do it! In fact, the entire code is very simple. The API interface call example of the FMZ Quant Trading Platform extension can be used directly. Address: https://www.fmz.com/api#simple example Use the example function directly: def api (method, *args) The interface we need to call is also very simple. The following two interfaces are used (can be found in the FMZ document) RestartRobot Restart the robot interface and pass the parameters to the robot ID. Use a specific id to call: api('RestartRobot', id)StopRobot To stop the robot interface, the parameters are also the robot ID. Robot identification: To call the FMZ Quant Trading Platform extension API, you must use the FMZ Quant Trading Platform API KEY. You can generate your own API KEY in account management. We pass the API KEY as a strategy parameter. The screenshot to get the API KEY of FMZ Quant Trading Platform account: The rest is to write timing logic, which is also very simple. Set synchronization parameters: ["175708,14:55:33-15:10:33", ...] The ellipsis indicates that more settings can be set such as "175708,14:55:33-15:10:33". The time parameter is a JSON string, which will be parsed into a list in the strategy code. Each item in the list is a set of robot start/stop settings. Namely: "175708,14:55:33-15:10:33" They are separated by commas. The part before the comma 175708 is the robot ID and the part after the comma is the start/end time. In the example above, the robot with ID 175708 starts at 14:55:33 and stops at 15:10:33. Then, in the strategy, the robot will continue to rotate. Each circular rotation will first obtain the current time, and then judge whether to trigger the robot to start or stop according to the comparison between the current time and the synchronization time. If triggered, call api('RestartRobot', id) or api('StopRobot', id) to start and stop the robot. Complete strategy code: # -*- encoding: utf-8 -*- import time import json test: import md5 import urllib2 from urllib import urlen code except: import hashlib as md5 import urllib.request as urllib2 from urllib.parse import urlen code def api(method, *args): d = { 'version': '1.0', 'access_key': access key, 'method': method, 'args': json.dumps(list(args)), 'nonce': int(time.time() * 1000), } d['sign'] = md5.md5(('%s|%s|%s|%d|%s' % (d['version'], d['method'], d['args'], d['nonce'], secretkey)).encode('utf-8')).hexdigest() return json.loads(urllib2.urlopen('https://www.fmz.com/api/v1', urlencode(d).encode('utf-8')).read().decode('utf-8')) RobotParams = json.loads(strRobotParams) main() definition: global robot parameters arrParams = [] nowDay = 0 strPush = "" if isPushMsg: strPush = "@" for i in range (len(RobotParams)): parameter = {} arr = RobotParams[i].split(",") if len(arr) != 2: raise exception("String setting error: delimiter") parameter["id"] = array[0] parameter["isProcessOpenThisDay"] = False parameter["isProc
advertisement

Related Articles

advertisement