-
Notifications
You must be signed in to change notification settings - Fork 0
/
money.py
95 lines (70 loc) · 2.88 KB
/
money.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
import requests
# This will serve as a library for the input and output of money
# first we will check to see if we have enough money to make the trade.
# Then we will lookup the price and either buy or sell at that price
# amount should always be in USD
def buy(moneytype, amount):
# check to see if we have enough money to make the trade
if checkbal("USD") < amount:
return False
# get the price of the coin you want to buy
r = requests.get("https://api.binance.com/api/v3/ticker/price?symbol=" + str(moneytype).upper() + "USDT")
price = float(r.json()["price"])
# read the money file and store it in a list
with open("money.txt", "r") as f:
contents = f.readlines()
# modify the list
isnthere = True
for line in range(len(contents)):
if "USD" in contents[line]:
contents[line + 1] = str(float(contents[line + 1]) - amount) + '\n'
if moneytype in contents[line]:
contents[line + 1] = str(float(contents[line + 1]) + amount/price) + '\n'
isnthere = False
if isnthere:
contents.append(str(moneytype) + ":\n")
contents.append("0.0\n")
# write that list back into the file
with open("money.txt", "w") as f:
for line in contents:
f.write(line)
print("\nBought $" + str(amount) + " worth of " + str(moneytype) + "\n")
return price
#sell the type and the amount of that type
def sell(moneytype, amount):
if checkbal(moneytype) < amount:
return False
# get the price of the coin you want to buy
r = requests.get("https://api.binance.com/api/v3/ticker/price?symbol=" + str(moneytype).upper() + "USDT")
price = float(r.json()["price"])
# read the money file and store it in a list
with open("money.txt", "r") as f:
contents = f.readlines()
# modify the list
for line in range(len(contents)):
if "USD" in contents[line]:
contents[line + 1] = str(float(contents[line + 1]) + amount*price) + '\n'
if moneytype in contents[line]:
contents[line + 1] = str(float(contents[line + 1]) - amount) + '\n'
# write that list back into the file
with open("money.txt", "w") as f:
for line in contents:
f.write(line)
print("\nSold " + str(amount) + " " + str(moneytype) + "\n")
return price
# Quick function to check the balance of a type of money
def checkbal(moneytype):
with open("money.txt", 'r') as f:
for line in f:
if moneytype in line:
return float(next(f))
return 0.0
def sellall(moneytype):
sell(moneytype, checkbal(moneytype))
def buyall(moneytype):
buy(moneytype, checkbal("USD"))
# sellall("ETH")
# sellall("BTC")
# sellall("XRP")
# sellall("BNB")
# sellall("ADA")