从零开始搭建链上dex自动化价差套利程序(5)

从这篇开始介绍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

ParameterDescription
marketMarket of the order.
sideEither BUY or SELL.
typeThe type of order. This can be MARKET, LIMIT, STOP_LIMIT, TRAILING_STOP or TAKE_PROFIT.
postOnlyWhether the order should be canceled if it would fill immediately on reaching the matching-engine.
sizeSize of the order, in base currency (i.e. an ETH-USD position of size 1 represents 1 ETH).
priceWorst accepted price of the base asset in USD.
limitFeeIs the highest accepted fee for the trade. See below for more information.
expirationTime 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.
clientIdUnique 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.
signatureSignature 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']
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

半事无意

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值