-
Notifications
You must be signed in to change notification settings - Fork 0
/
invest.py
223 lines (210 loc) · 8.75 KB
/
invest.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
import xml.etree.ElementTree as ET
from decimal import *
import collections
from sopel.module import commands, example, NOLIMIT
from sortedcontainers import SortedDict
from forex_python.converter import CurrencyCodes
from forex_python.converter import CurrencyRates
from forex_python.bitcoin import BtcConverter
from pymarketcap import Pymarketcap
from coinmarketcap import Market
import coinmarketcap
c = CurrencyRates()
b=BtcConverter()
m=Market()
cc=CurrencyCodes()
allcoins={}
curlist=list(c.get_rates("USD").keys())
#print(c.get_rates("USD"))
curlist.append("USD")
curlist.sort()
for cur in curlist:
allcoins[cur]=cc.get_currency_name(cur)
altcoinmap={}
#print (coinmarketcap.price("BTC"))
json=m.ticker(convert='EUR')
#print(json)
for currency in json:
altcoinmap[currency["symbol"]]=currency["id"]
allcoins[currency["symbol"]]=currency["name"]
#print(altcoinmap)
#print(json)
#print(m.ticker("bitcoin",convert="EUR")[0]["price_usd"]*100)
allcoins=SortedDict(allcoins)
wordhash={}
credithash={}
initialvalue=100000.00
investfile = '/home/bananapi/.sopel/modules/invest.xml'
def setup(bot):
e = ET.parse(investfile).getroot()
for atype in e.findall('user'):
wordhash[atype.get('name')]={}
for child in atype.iter('cur'):
wordhash[atype.get('name')][child.get('currency')]=child.get('value')
print(str(wordhash))
@commands('investstart','startinvest')
@example('.investstart','.startinvest')
def startinvest(bot,trigger):
"""Starts and/or resets an initial investment portfolio"""
user=trigger.nick
wordhash[user]={}
wordhash[user]["EUR"]=initialvalue
bot.reply("I created an investment portfolio for you. Your initial investment funds are "+str(initialvalue)+"EUR")
toxml()
@commands('curvalue(\s(.*))?')
@example('.curvalue','.curvalue nick')
def currentvalue(bot,trigger):
"""Prints the current value of the portfolio of the given user in EUR"""
user=trigger.nick
if trigger.group(3):
user=trigger.group(3)
if user not in wordhash:
bot.say("You need to create a new portfolio for "+user+" first. Use the .investstart command to get started!")
return
result=Decimal(0)
for curkey,val in wordhash[user].items():
result+=Decimal(curconvert(curkey,"EUR",Decimal(val)))
bot.reply("Current value of "+user+"'s whole portfolio in EUR is "+str("%.2f "%result)+"EUR")
print(str(wordhash))
@commands('investstatus(\s(.*))?')
@example('.investstatus','.investstatus nick')
def investstatus(bot,trigger):
user=trigger.nick
if (trigger.group(3)):
user=trigger.group(3)
if user not in wordhash:
return
for curkey,val in sorted(wordhash[user].items()):
if curkey=="EUR":
bot.say(str("%.2f "%float(val))+curkey)
else:
bot.say(str("%.8f "%float(val))+curkey)
@commands('sell\s([0-9]+\.?[0-9]?[0-9]?[0-9]?[0-9]?[0-9]?[0-9]?[0-9]?[0-9]?)\s([A-z]+)')
@example('.sell 10.56 BTC')
def sell(bot,trigger):
"""Sells the specified amount in the specified currency from the virtual portfolio"""
amount=Decimal(trigger.group(2))
currency=trigger.group(3).upper()
user=trigger.nick
if not user in wordhash:
bot.say("You need to create a new portfolio first. Use the .investstart command to get started!")
return
btcineur=Decimal(curconvert(currency,"EUR",amount))
if amount>Decimal(wordhash[user][currency]):
bot.reply("You do not have enough "+currency+" to sell this amount. You only have "+str(wordhash[user][currency]))
else:
wordhash[user]["EUR"]=Decimal(wordhash[user]["EUR"])+btcineur
wordhash[user][currency]=Decimal(wordhash[user][currency])-amount
bot.reply("You sold "+str(amount)+currency+" and got "+str("%.2f "%(btcineur))+" EUR!")
bot.reply("Your EUR balance is now: "+str("%.2f "%wordhash[user]["EUR"])+"EUR")
toxml()
@commands('buy\s([0-9]+\.?[0-9]?[0-9]?[0-9]?[0-9]?[0-9]?[0-9]?[0-9]?[0-9]?)\s([A-z]+)')
@example('.buy 5 BTC')
def buy(bot,trigger):
"""Buys the specified amount of money in the specified currency given enough budget is available in the portfolio"""
amount=Decimal(trigger.group(2))
currency=trigger.group(3).upper()
user=trigger.nick
if user not in wordhash:
bot.say("You need to create a new portfolio first. Use the .investstart command to get started!")
return
euramount=Decimal(wordhash[user]["EUR"])
btcineur=curconvert(currency,"EUR",amount)
if btcineur>euramount:
bot.reply("You do not have enough EUR to buy "+str(amount)+" "+currency)
else:
wordhash[user]["EUR"]=Decimal(wordhash[user]["EUR"])-btcineur
if not currency in wordhash[user]:
wordhash[user][currency]=Decimal(0)
wordhash[user][currency]=Decimal(wordhash[user][currency])+Decimal(amount)
bot.reply("You purchased "+str(amount)+currency+". Your "+currency+" balance is now "+str(wordhash[user][currency])+currency)
bot.reply("Your EUR balance is now: "+str("%.2f "%wordhash[user]["EUR"])+"EUR")
toxml();
def toxml():
root = ET.Element('data')
for key in wordhash:
user=ET.SubElement(root,'user')
user.set("name",key)
for curkey,val in wordhash[key].items():
curtag=ET.SubElement(user,'cur')
curtag.set('currency',curkey)
curtag.set('value',str(val))
tree=ET.ElementTree(root)
tree.write(investfile)
def curconvert(fromcur,tocur,amount):
if amount==None:
amount=Decimal(1.0)
if fromcur==tocur:
return amount
elif fromcur=="SATOSHI" and tocur in curlist:
return b.convert_btc_to_cur(amount,tocur)/100000000
elif fromcur=="BTC" and tocur in curlist:
return b.convert_btc_to_cur(amount,tocur)
elif fromcur in curlist and tocur=="SATOSHI":
return b.convert_to_btc(amount,fromcur)*100000000
elif fromcur in curlist and tocur=="BTC":
return b.convert_to_btc(amount,fromcur)
elif fromcur in curlist and tocur in curlist:
return c.convert(fromcur,tocur,amount)
elif fromcur not in curlist and fromcur in altcoinmap and tocur in curlist:
usdamount=Decimal(m.ticker(altcoinmap[fromcur],convert="USD")[0]["price_usd"])*amount
#print(usdamount)
return c.convert("USD",tocur,usdamount)
elif fromcur in curlist and tocur not in curlist and tocur in altcoinmap:
#print(str(amount)+" "+fromcur)
usdamount=c.convert(fromcur,"USD",amount)
#print("USDAmount: "+str(usdamount))
factor=usdamount/Decimal(m.ticker(altcoinmap[tocur],convert="USD")[0]["price_usd"])
return factor#print("Factor: 1"+tocur+"="+str(m.ticker(altcoinmap[tocur],convert="USD")[0]["price_usd"])))
else:
return None
@commands('curr\s([0-9]+(\.[0-9][0-9]?[0-9]?[0-9]?[0-9]?[0-9]?[0-9]?[0-9]?)?\s)?([A-z]+)\sin\s([A-z]+)')
@example('.curr 20 EUR in USD')
def exchange(bot, trigger):
"""Prints the current exchange rate for the two given currencies"""
fromcur=trigger.group(4).upper()
tocur=trigger.group(5).upper()
if trigger.group(2)==None:
amount=Decimal(1.0)
else:
amount=Decimal(trigger.group(2))
if fromcur==tocur:
bot.say(str(amount)+" "+tocur)
elif fromcur=="SATOSHI" and tocur in curlist:
bot.say(str("%.2f "% (curconvert(fromcur,tocur,amount)+tocur)))
elif fromcur=="BTC" and tocur in curlist:
bot.say(str("%.2f "% (curconvert(fromcur,tocur,amount))+tocur))
elif fromcur in curlist and tocur=="SATOSHI":
bot.say(str("%.0f "% (curconvert(fromcur,tocur,amount))+tocur))
elif fromcur in curlist and tocur=="BTC":
bot.say(str("%.8f "% (curconvert(fromcur,tocur,amount))+tocur))
elif fromcur in curlist and tocur in curlist:
bot.say(str("%.2f " % (curconvert(fromcur,tocur,amount))+tocur))
elif fromcur not in curlist and fromcur in altcoinmap and tocur in curlist:
bot.say(str("%.2f "% (curconvert(fromcur,tocur,amount))+tocur))
elif fromcur in curlist and tocur not in curlist and tocur in altcoinmap:
bot.say(str("%.8f " % (curconvert(fromcur,tocur,amount))+tocur))
else:
bot.say("Due to an error, I currently cannot convert from "+fromcur+" to "+tocur)
@commands("curs")
@example(".curs")
def curs(bot,trigger):
"""Prints a list of available currencies in a private message"""
bot.reply("I am sending you the list of currencies in a private message!")
counter=0
line=""
for curr in allcoins:
if counter==5:
bot.msg(trigger.nick,line)
line=""
counter=0
else:
line+=allcoins[curr]+"("+curr+") "
counter+=1
if counter!=0:
bot.msg(trigger.nick,line)
@commands('currate\s+([A-z]+)')
@example('.currate currency')
def rate(bot,trigger):
cur=trigger.group(2)
bot.say(str(c.get_rates(cur)))