-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.py
More file actions
120 lines (96 loc) · 3.04 KB
/
Copy pathtest.py
File metadata and controls
120 lines (96 loc) · 3.04 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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# Modified code to handle KeyboardInterrupt gracefully and close the exchange connection.
import time
import os
import math
import ccxt.pro
from typing import List
from ccxt.base.types import OrderBook, Ticker, Balances, Order
import asyncio
from dotenv import load_dotenv
from utils import log_order, log_error
from config import EXCHANGE_CONFIGS
symbol = 'MYST/USDT'
exchange = ccxt.pro.mexc(EXCHANGE_CONFIGS["mexc"])
def process_orders_update(orders: List[Order]):
def process_order_update(order: Order):
print(order)
for order in orders:
process_order_update(order)
break
def common_handler(
orderbook: OrderBook = None,
ticker: Ticker = None,
balances: Balances = None,
orders: List[Order] = None
):
# https://docs.ccxt.com/#/ccxt.pro.manual
if orderbook:
# print(f"OrderBook: ask: {orderbook['asks'][0]}, bid: {orderbook['bids'][0]}")
pass
if ticker:
# print(f"Ticker: {ticker['datetime']}, last_price: {ticker['last']}")
pass
if balances:
print(f"{balances['balance']=}")
if orders:
process_orders_update(orders)
for order in orders:
log_order(order)
async def watch_order_book():
try:
while True:
orderbook = await exchange.watch_order_book(symbol)
common_handler(orderbook=orderbook)
except Exception as e:
print(type(e).__name__, str(e))
async def watch_ticker():
try:
while True:
ticker = await exchange.watch_ticker(symbol)
common_handler(ticker=ticker)
except Exception as e:
print(type(e).__name__, str(e))
async def watch_balance():
try:
while True:
balances = await exchange.watch_balance()
common_handler(balances=balances)
except Exception as e:
print(type(e).__name__, str(e))
async def watch_orders():
try:
while True:
orders = await exchange.watch_orders(symbol=symbol)
common_handler(orders=orders)
except Exception as e:
print(type(e).__name__, str(e))
async def market_buy():
def min_order_amount(self, price=None):
"""
Get the minimum amount of the base currency that can be traded in one order.
"""
price = exchange.fetch_ticker(self.s)['last']
min_cost = self.min_cost + self.min_price
return math.ceil(min_cost / price / self.min_amount) * self.min_amount
amount = min_order_amount()
order = await exchange.create_order(
symbol=symbol, type="market", side="buy", amount=amount
)
return order
async def main():
await exchange.load_markets()
try:
loops = [
watch_order_book(),
watch_balance(),
watch_orders(),
watch_ticker()
]
await asyncio.gather(*loops)
except KeyboardInterrupt:
pass
except Exception as e:
print("ERROR", type(e).__name__, str(e))
finally:
await exchange.close()
asyncio.run(main())