-
Notifications
You must be signed in to change notification settings - Fork 7
/
dca.py
52 lines (40 loc) · 1.66 KB
/
dca.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
import json
from binance.client import Client
config = json.load(open("config.json"))
quote_asset, amount, fee = config["quote_asset"], config["amount"], config["fee"]
client = Client(config["keys"]["api"], config["keys"]["secret"])
def fetch_balance(asset):
return float(client.get_asset_balance(asset=asset)["free"])
def place_order(ticker, amount):
if config["testing"]:
create_order = client.create_test_order
else:
create_order = client.create_order
res = create_order(
symbol=f"{ticker}{quote_asset}".upper(),
side=Client.SIDE_BUY,
type=Client.ORDER_TYPE_MARKET,
quoteOrderQty=round(amount, 3)
)
if config["testing"]:
print("Real orders cannot be made in testing mode")
else:
print(f"Amount Bought: {res['executedQty']}\nPrice: ${1 / float(res['executedQty']) * float(res['cummulativeQuoteQty'])}\nTotal Spent: ${res['cummulativeQuoteQty']}\n\n")
try:
# check API key validity
balance = fetch_balance(quote_asset)
print(f"${quote_asset} Balance:", balance)
print(
f"Purchase Amount: ${amount:.2f}\nFee Rate: ${fee / 100 * amount:.2f}\n")
except:
exit("invalid binance API key or config settings")
if amount > balance:
exit(f"insufficient ${quote_asset} balance to run DCA")
if sum(config["coins"].values()) > 100:
exit("asset allocation cannot exceed 100%")
coins = config["coins"]
for coin in coins:
purchasing_amount = coins[coin] / 100 * amount
total_purchase_amount = purchasing_amount + (fee / 100 * purchasing_amount)
print(f"Attempting to buy ${total_purchase_amount:.2f} of ${coin.upper()}")
place_order(coin, total_purchase_amount)