Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathmargin.py
More file actions
Latest commit
103 lines (88 loc) · 4.78 KB
/
Copy pathmargin.py
File metadata and controls
103 lines (88 loc) · 4.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# !/usr/bin/env python
# coding: utf-8
importlogging
importrandom
fromdecimalimportDecimalasD, ROUND_UP, getcontext
fromgate_apiimportApiClient, Configuration, Loan, MarginApi, Order, RepayRequest, SpotApi, Transfer, WalletApi
fromgate_api.exceptionsimportGateApiException
fromconfigimportRunConfig
logger=logging.getLogger(__name__)
defmargin_demo(run_config):
# type: (RunConfig) -> None
currency_pair='BTC_USDT'
currency=currency_pair.split("_")[1]
# Initialize API client
# Setting host is optional. It defaults to https://api.gateio.ws/api/v4
config=Configuration(key=run_config.api_key, secret=run_config.api_secret, host=run_config.host_used)
spot_api=SpotApi(ApiClient(config))
margin_api=MarginApi(ApiClient(config))
wallet_api=WalletApi(ApiClient(config))
# retrieve currency pair last price
tickers=spot_api.list_tickers(currency_pair=currency_pair)
assertlen(tickers) ==1
last_price=tickers[0].last
logger.info("currency pair %s last price %s", currency_pair, last_price)
pairs=margin_api.list_margin_currency_pairs()
pair=next(pforpinpairsifp.id==currency_pair)
loan_amount=D("0") ifnotpair.min_quote_amountelseD(pair.min_quote_amount)
ifpair.min_base_amount:
min_loan_amount=D(pair.min_base_amount) *D(last_price)
ifloan_amount<min_loan_amount:
loan_amount=min_loan_amount
logger.info("minimum loan amount in currency pair %s: %s %s", currency_pair, str(loan_amount), currency)
getcontext().prec=8
getcontext().rounding=ROUND_UP
# example to lend
funding_accounts=margin_api.list_funding_accounts(currency=currency)
lend_amount=loan_amount+D(random.random())
iflen(funding_accounts) ==1andD(funding_accounts[0].available) >=lend_amount:
lending_loan=Loan(amount=str(lend_amount), auto_renew=False, days=10, currency=currency, rate="0.002",
side='lend')
created_loan=margin_api.create_loan(lending_loan)
logger.info("place a lending loan %s with currency %s, rate %s, amount %s", created_loan.id,
created_loan.currency, created_loan.rate, created_loan.amount)
loan_result=margin_api.get_loan(created_loan.id, 'lend')
ifloan_result.status=='loaned':
records=margin_api.list_loan_records(loan_result.id)
forrinrecords:
logger.info("loan %s is borrowed with record id %s, amount %s, status: %s", r.loan_id, r.id, r.amount,
r.status)
else:
margin_api.cancel_loan(created_loan.id, currency)
assertpair.leverage
margin=loan_amount/ (pair.leverage-1)
accounts=margin_api.list_margin_accounts(currency_pair=currency_pair)
assertlen(accounts) ==1
available=D(accounts[0].quote.available)
logger.info("available margin balance of currency %s in currency pair %s: %s", currency, currency_pair,
str(available))
ifmargin>available:
transfer=Transfer(currency_pair=currency_pair, currency=currency, amount=str(margin-available),
_from='spot', to='margin')
wallet_api.transfer(transfer)
logger.info("transferred %s %s to margin account", transfer.amount, transfer.currency)
# borrow with minimum rate
borrow_amount=loan_amount+D(random.random())
min_rate_item=min(
filter(lambdax: x.rateandD(x.amount) >borrow_amount, margin_api.list_funding_book(currency)),
key=lambdax: D(x.rate)
)
loan=Loan(side='borrow', currency=currency, rate=min_rate_item.rate, amount=str(borrow_amount),
days=min_rate_item.days, currency_pair=currency_pair)
borrowed=margin_api.create_loan(loan)
logger.info("borrowed %s %s in currency pair %s with rate %s, id %s", borrowed.amount, borrowed.currency,
borrowed.currency_pair, borrowed.rate, borrowed.id)
assertborrowed.status=='loaned'
# create margin order
order_amount=spot_api.get_currency_pair(currency_pair).min_quote_amount
order=Order(account='margin', currency_pair=currency_pair, price=last_price, amount=order_amountor"1",
side='sell')
try:
created_order=spot_api.create_order(order)
logger.info("margin order created with id %s, status %s", created_order.id, created_order.status)
exceptGateApiExceptionasex:
logger.error("failed to create margin order: %s", ex)
repay_request=RepayRequest(mode='all', currency=currency, currency_pair=currency_pair)
margin_api.repay_loan(borrowed.id, repay_request)
forrinmargin_api.list_loan_repayments(borrowed.id):
logger.info("loan %s repaid %s with interest %s", borrowed.id, r.principal, r.interest)