This repository was archived by the owner on Jul 16, 2026. It is now read-only.
- Notifications
You must be signed in to change notification settings - Fork 128
Expand file tree
/
Copy pathspot.py
More file actions
Latest commit
56 lines (47 loc) · 2.24 KB
/
Copy pathspot.py
File metadata and controls
56 lines (47 loc) · 2.24 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
# !/usr/bin/env python
# coding: utf-8
importlogging
fromdecimalimportDecimalasD
fromgate_apiimportApiClient, Configuration, Order, SpotApi
fromconfigimportRunConfig
logger=logging.getLogger(__name__)
defspot_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))
pair=spot_api.get_currency_pair(currency_pair)
logger.info("testing against currency pair: "+currency_pair)
min_amount=pair.min_base_amount
# get last price
tickers=spot_api.list_tickers(currency_pair=currency_pair)
assertlen(tickers) ==1
last_price=tickers[0].last
# make sure balance is enough
order_amount=D(min_amount) *2
accounts=spot_api.list_spot_accounts(currency=currency)
assertlen(accounts) ==1
available=D(accounts[0].available)
logger.info("Account available: %s %s", str(available), currency)
ifavailable<order_amount:
logger.error("Account balance not enough")
return
order=Order(amount=str(order_amount), price=last_price, side='buy', currency_pair=currency_pair)
logger.info("place a spot %s order in %s with amount %s and price %s", order.side, order.currency_pair,
order.amount, order.price)
created=spot_api.create_order(order)
logger.info("order created with id %s, status %s", created.id, created.status)
ifcreated.status=='open':
order_result=spot_api.get_order(created.id, currency_pair)
logger.info("order %s filled %s, left: %s", order_result.id, order_result.filled_total, order_result.left)
result=spot_api.cancel_order(order_result.id, currency_pair)
ifresult.status=='cancelled':
logger.info("order %s cancelled", result.id)
else:
trades=spot_api.list_my_trades(currency_pair, order_id=created.id)
assertlen(trades) >0
fortintrades:
logger.info("order %s filled %s with price %s", t.order_id, t.amount, t.price)