-
Notifications
You must be signed in to change notification settings - Fork 2
/
buy_usdc.py
112 lines (92 loc) · 3.44 KB
/
buy_usdc.py
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
"""
A simple script that tries to buy an amount of USDC with ARS in small chunks
config.json requires the value api_key like
{"api_key": "...."}
Example buy 1000 USDC pay max 180 pesos per USD doing 100 USDC chunks:
python buy_usdc.py config.json 1000 180 100
"""
import json
import os
import sys
import time
import logging
import ccxt
from dotenv import load_dotenv
import requests
from utils import retry
PAIR = "USDC/ARS"
NORMAL_WAIT = 10
WAIT_BETWEEN_TRADES = 30
MIN_ORDER = 10
@retry(times=5, exceptions=(ccxt.errors.RequestTimeout, ccxt.errors.DDoSProtection))
def fetch_order_book(exchange, pair):
return exchange.fetch_order_book(pair)
@retry(times=5, exceptions=(ccxt.errors.RequestTimeout, ccxt.errors.DDoSProtection))
def create_limit_buy_order(exchange, pair, chunk_to_buy, price_to_buy):
return exchange.create_limit_buy_order(pair, chunk_to_buy, price_to_buy)
@retry(times=5, exceptions=(ccxt.errors.RequestTimeout, ccxt.errors.DDoSProtection))
def fetch_order(exchange, order_id, pair):
return exchange.fetch_order(order_id, pair)
def run(API_KEY, amount_to_buy, limit, chunk):
ripio = ccxt.ripio({'apiKey': API_KEY})
# markets = ripio.load_markets()
total_bought = 0
while total_bought < amount_to_buy:
orderbook = fetch_order_book(ripio, PAIR)
if len(orderbook['asks']) == 0:
time.sleep(NORMAL_WAIT)
continue
top_price, top_amount = orderbook['asks'][0]
print("current top %s @ %s" % (top_amount, top_price))
if top_price < limit:
chunk_to_buy = min(top_amount, chunk)
chunk_to_buy = max(chunk_to_buy, MIN_ORDER)
price_to_buy = top_price
else:
chunk_to_buy = chunk
price_to_buy = limit
order = create_limit_buy_order(ripio, PAIR, chunk_to_buy, price_to_buy)
order_id = order['id']
while True:
order = fetch_order(ripio, order_id, PAIR)
if order['status'] == 'canceled':
print('Order %s got canceled !!!' % order_id)
break
elif order['status'] != "closed":
if order['status'] == 'pending creation':
print('pending creation')
time.sleep(NORMAL_WAIT)
continue
try:
orderbook = fetch_order_book(ripio, PAIR)
top_price, top_amount = orderbook['asks'][0]
print("status: %s current top %s @ %s" % (order['status'], top_amount, top_price))
except:
logging.exception('exploto')
time.sleep(NORMAL_WAIT)
else:
completed = order['amount']
total_bought += completed
print("Completed %s @ %s" % (order['amount'], top_price))
print("Total %s" % total_bought)
print()
time.sleep(WAIT_BETWEEN_TRADES)
break
print("WOOHOO DONE COMPRE: %s USDC" % total_bought)
if __name__ == "__main__":
load_dotenv()
API_KEY = os.environ.get('API_KEY', None)
if not API_KEY:
print('missing API_KEY')
sys.exit(2)
amount = float(sys.argv[1])
limit = float(sys.argv[2])
chunk = float(sys.argv[3])
print("""CONFIRM:
Buying: %s USDC
Limit price: %s ARS
Chunk size: %s USDC
""" % (amount, limit, chunk))
if input("confirm y/n: ") == "y":
print()
run(API_KEY, amount, limit, chunk)