从这篇开始介绍dydx以及apex的Private API调用
dydx
参考文档:
1.Programmatic Trading on dYdX:https://medium.com/dydxderivatives/programatic-trading-on-dydx-4c74b8e86d88
2.https://github.com/dydxprotocol/dydx-v3-python/blob/master/examples/onboard.py
在主网上需要获取STARK key来执行Private API
from dydx3 import Client
# initial dydx client
client = Client(host='https://api.dydx.exchange',
eth_private_key='', #换成钱包私钥
)
# Set STARK key.
stark_key_pair_with_y_coordinate = client.onboarding.derive_stark_key()
client.stark_private_key = stark_key_pair_with_y_coordinate['private_key']
(public_x, public_y) = (
stark_key_pair_with_y_coordinate['public_key'],
stark_key_pair_with_y_coordinate['public_key_y_coordinate'],
)
# Onboard the account.
onboarding_response = client.onboarding.create_user(
stark_public_key=public_x,
stark_public_key_y_coordinate=public_y,
)
print('onboarding_response', onboarding_response)
# Query a private endpoint.
accounts_response = client.private.get_accounts()
print('accounts_response', accounts_response.data)
Create A New Order
Create Order
from dydx3.constants import MARKET_BTC_USD
from dydx3.constants import ORDER_SIDE_SELL
from dydx3.constants import ORDER_TYPE_LIMIT
from dydx3.constants import TIME_IN_FORCE_GTT
placed_order = client.private.create_order(
position_id=1, # required for creating the order signature
market=MARKET_BTC_USD,
side=ORDER_SIDE_SELL,
order_type=ORDER_TYPE_LIMIT,
post_only=False,
size='100',
price='18000',
limit_fee='0.015',
expiration_epoch_seconds=1613988637,
time_in_force=TIME_IN_FORCE_GTT,
)
HTTP Request
POST v3/orders
Description: Create a new order.
Request
Parameter | Description |
---|---|
market | Market of the order. |
side | Either BUY or SELL. |
type | The type of order. This can be MARKET , LIMIT , STOP_LIMIT , TRAILING_STOP or TAKE_PROFIT . |
postOnly | Whether the order should be canceled if it would fill immediately on reaching the matching-engine. |
size | Size of the order, in base currency (i.e. an ETH-USD position of size 1 represents 1 ETH). |
price | Worst accepted price of the base asset in USD. |
limitFee | Is the highest accepted fee for the trade. See below for more information. |
expiration | Time at which the order will expire if not filled. This is the Good-Til-Time and is accurate to a granularity of about 15 seconds. |
timeInForce | (Optional) One of GTT (Good til time), FOK (Fill or kill) or IOC (Immediate or cancel). This will default to GTT . |
cancelId | (Optional) The id of the order that is being replaced by this one. |
triggerPrice | (Optional) The triggerPrice at which this order will go to the matching-engine. |
trailingPercent | (Optional) The percent that the triggerPrice trails the index price of the market. |
reduceOnly | (Optional) Whether the order should be reduce-only. Only supported on FOK (Fill or kill) or IOC (Immediate or cancel) orders. |
clientId | Unique id of the client associated with the order. Must be <= 40 characters. When using the client, if not included, will be randomly generated by the client. |
signature | Signature for the order, signed with the account’s STARK private key. When using the client, if not included, will be done by the client. For more information see above. |
发交易:
import time
from dydx3 import Client
from dydx3.constants import MARKET_BTC_USD
from dydx3.constants import ORDER_SIDE_BUY
from dydx3.constants import ORDER_TYPE_LIMIT
# 初始化 dydx 客户端
client = Client(host='https://api.dydx.exchange',
eth_private_key='your private eth key',
)
# Set STARK key.
stark_key_pair_with_y_coordinate = client.onboarding.derive_stark_key()
client.stark_private_key = stark_key_pair_with_y_coordinate['private_key']
(public_x, public_y) = (
stark_key_pair_with_y_coordinate['public_key'],
stark_key_pair_with_y_coordinate['public_key_y_coordinate'],
)
# # Onboard the account.
# onboarding_response = client.onboarding.create_user(
# stark_public_key=public_x,
# stark_public_key_y_coordinate=public_y,
# )
# print('onboarding_response', onboarding_response)
# Get our position ID.
account_response = client.private.get_account()
position_id = account_response.data['account']['positionId']
# Post an bid at a price that is unlikely to match.
order_params = {
'position_id': position_id,
'market': MARKET_BTC_USD,
'side': ORDER_SIDE_BUY,
'order_type': ORDER_TYPE_LIMIT,
'post_only': True,
'size': '0.0777',
'price': '20',
'limit_fee': '0.0015',
'expiration_epoch_seconds': time.time() + 100,
}
order_response = client.private.create_order(**order_params)
order_id = order_response['order']['id']
print('order_response',order_response.data)
# Cancel all orders.
client.private.cancel_all_orders()
bug:
1.VPN地区不能设在美国
2.'Order expiration cannot be less than 1 minute(s) in the future'
'expiration_epoch_seconds': time.time() + 100 #这一行一开始写的+5,不行,改成+100就行
3.get position_id error
position_id = account_response['account']['positionId'] => position_id = account_response.data['account']['positionId']