python - 嗨,我一直在尝试使用 python 开发自己的交易机器人,有人可以向我说明如何从 deriv 获取实时价格
问题描述
如果程序将被托管,我需要使用套接字流 API 或您知道的任何工具从我的经纪人(Deriv)获取历史和实时价格到我的系统服务器。先感谢您
解决方案
我使用 huobi api 来构建我的机器人。下面是一些工作代码:
import requests
import json
import hmac
import hashlib
import base64
from urllib.parse import urlencode
import time
base_uri = 'api.huobi.pro'
AccessKeyId = 'xxxxxx'
SecretKey = 'xxxxxx'
account_id = '11111111'
def get_url(method, endpoint):
timestamp = str(datetime.utcnow().isoformat())[0:19]
params = urlencode({'AccessKeyId': AccessKeyId, 'SignatureMethod': 'HmacSHA256', 'SignatureVersion': '2', 'Timestamp': timestamp})
pre_signed_text = method + '\n' + base_uri + '\n' + endpoint + '\n' + params
hash_code = hmac.new(SecretKey.encode(), pre_signed_text.encode(), hashlib.sha256).digest()
signature = urlencode({'Signature': base64.b64encode(hash_code).decode()})
return 'https://' + base_uri + endpoint + '?' + params + '&' + signature
def get_all_open_orders():
url = get_url('GET', '/v1/order/openOrders')
response = requests.request('GET', url, headers={}, data={})
return json.loads(response.text)['data']
def place_new_order(data): #for both buy/sell
url = get_url('POST', '/v1/order/orders/place')
response = requests.request('POST', url, headers={'Content-Type': 'application/json'}, data=data)
return json.loads(response.text)
def cancel_order(order_id):
url = get_url('POST', '/v1/order/orders/{}/submitcancel'.format(order_id))
response = requests.request('POST', url, headers={}, data={})
return json.loads(response.text)
获取当前价格
SYMBOL = 'xrphusd'
url = 'https://api.huobi.pro/market/history/kline?period=1min&size=1&symbol=' + SYMBOL
response = requests.request('GET', url, headers={}, data={})
print(json.loads(response.text)['data'][0])
下订单(可以是'buy-limit'或'sell-limit')
price = "1.25"
quantity = "100"
payload = {"account-id": str(account_id), "amount": quantity, "price": price, "source": "api", "symbol": SYMBOL, "type": "buy-limit"}
status = place_new_order(json.dumps(payload))
print(status)
推荐阅读
- python - 多范围部分的数学
- javascript - 打字稿/反应。错误类型'{键:数字;}' 不可分配给类型 ILink(SFC 组件)
- xslt-grouping - XSLT 不同的节点组合
- javascript - Angular 5 - 导入 Bootstrap 3 轮播
- python - 在 Python 中将十六进制数字写入文件
- sql - 选择行直到并包括列中第一个值的最后一行
- symfony - Symfony Twig 中的优化引导进度条
- python - scapy "TypeError: 'IP' 对象不可调用"
- javascript - 过渡在第一次创建图表时不起作用
- javascript - Ionic 3 - 手动触发模糊事件