-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbanky.py
248 lines (210 loc) Β· 8.54 KB
/
banky.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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
import json
from rich import print
import yfinance as yf
from datetime import date
from datetime import timedelta
from rich.table import Table
import numpy as np
import pandas as pd
import plotly.graph_objs as go
class balance():
def __init__(self):
self.dicty = {}
self.total = 0
self.openned = 0
def wallet(self,amount = 0):
balance.reader(self)
self.dicty = self.openned
if "money" not in self.dicty:
self.dicty["money"] = 0
else:
self.dicty["money"] += amount
j = json.dumps(self.dicty)
with open("Saved Money.json", "w") as f:
f.write(j)
balance.reader(self)
def reader(self):
self.openned = json.load(open("Saved Money.json"))
def readMoney(self):
print(self.dicty)
class Trade(balance):
def __init__(self):
super(balance,self).__init__
self.portfolio = {}
self.day = 2
self.trader_reply = ""
self.day_viewer = 2
self.loop = 0
def Saved(self,stock):
j = json.dumps(stock)
with open("Saved Info.json", "w") as f:
f.write(j)
f.close()
def Saved_Reader(self,type = "yes"):
if type == "no":
self.portfolio= json.load(open("Saved Info.json"))
else:
self.portfolio= json.load(open("Saved Info.json"))
print("[dark_sea_green2]Your portfolio:[/dark_sea_green2]")
print(self.portfolio)
Trade.reader(self)
print("[dark_sea_green2]Your Total Balance: [/dark_sea_green2]",self.openned)
def graph_company(self,company):
data = yf.download(tickers=company, interval="1d",period="1mo")
fig = go.Figure()
fig.add_trace(go.Candlestick(x=data.index,
open = data["Open"],
high = data["High"],
low = data["Low"],
close = data["Close"], name = "market data"))
fig.update_layout(
title = f"{company} Share Prices",
yaxis_title = "Stock Price ")
fig.update_xaxes(
rangeslider_visible = True,
rangeselector = dict(
buttons=list([
dict(count=5, label="5days",step="day",stepmode="backward"),
dict(count=1, label="months",step="year",stepmode="backward"),
dict(step="all")
])
)
)
fig.show()
def infoOptions(self):
print(
"""
[dark_slate_gray2]0 : View more days[/dark_slate_gray2]
[chartreuse1]1 : View Graph [chartreuse1]
[chartreuse4]2 : Look at different stocks[/chartreuse4]
[chartreuse2]3 : Exit [/chartreuse1]
"""
)
def info(self, company = None):
today = date.today()
yesterday = today - timedelta(days = self.day_viewer)
data = yf.download(tickers=company, interval="1d",start=yesterday)
tickerdata = yf.Ticker(company)
tickerinfo = tickerdata.info
current_price = tickerinfo["currentPrice"]
print(data)
print(f"[green]The current Price: {current_price}[/green]")
Trade.infoOptions(self)
self.trader_reply = input("")
if self.trader_reply == "0":
self.day_viewer += 2
Trade.info(self,company)
elif self.trader_reply == "1":
Trade.graph_company(self,company)
Trade.info(self,company)
Trade.infoOptions(self)
self.trader_reply = input("")
elif self.trader_reply == "2":
print("[thistle1]What stock would you like to look at?[/thistle1]")
company = input("")
Trade.info(self,company)
elif self.trader_reply == "3":
Trade.Options(self)
else:
print("[red] Invalid Option [/red]")
Trade.infoOptions(self)
self.trader_reply = input("")
def Options(self):
print(
"""
[green]1 : Access portfolio[/green]
[blue]2 : Buy Stocks[/blue]
[purple]3 : Sell Stocks[/purple]
[yellow]4 : View Stocks[/yellow]
[red]5 : Stop Trading[/red]
"""
)
answer = input("")
if answer == "1":
Trade.Saved_Reader(self)
Trade.Options(self)
elif answer == "2":
Trade.buy(self)
elif answer == "3":
Trade.sell(self)
elif answer == "4":
print("[thistle1]What stock would you like to look at?[/thistle1]")
company = input("")
Trade.info(self,company)
elif answer == "5":
exit()
else:
print("[red] Invalid Option [/red]")
Trade.Options(self)
def buy(self):
self.portfolio = json.load(open("Saved Info.json"))
which = input("Name of stock?\n")
try:
balance.reader(self)
tickerdata = yf.Ticker(which)
tickerinfo = tickerdata.info
shower = (tickerinfo["currentPrice"])
print("[blue]How many shares do you want to buy[/blue]")
amount = input("")
total = (tickerinfo["currentPrice"]) * int(amount)
print(total)
if int(self.openned["money"] > total):
print("[light_green]Current price right now:[/light_green]",(tickerinfo["currentPrice"]))
print("[light_green]You have:[/light_green]",self.openned["money"])
print("\n[cyan1]Are you sure[/cyan1]")
print("1:[green]YES[/green] 2:[red]NO[/red]")
answer = input("")
if answer == "1":
balance.wallet(self,-total)
if which.upper() not in self.portfolio:
self.portfolio[which.upper()] = int(amount)
print(which.upper())
Trade.Saved(self,self.portfolio)
Trade.Options(self)
print(f"[red bold]-{shower}[/red bold]")
print("[light_green]You now have have:[/light_green]",self.openned["money"])
else:
self.portfolio[which.upper()] += int(amount)
Trade.Saved(self,self.portfolio)
print(f"[red bold]-{total}[/red bold]")
print("[light_green]You now have have:[/light_green]",self.openned["money"])
Trade.Options(self)
elif answer == "2":
Trade.Options(self)
else:
print("[red]You dont have enough right now\nYou have[/red]",self.openned["money"],"Dollars[red]\nBuying parts will be coming soon[/red]")
Trade.Options(self)
except:
print("[red bold]That is not a valid share[/red bold]")
Trade.buy(self)
def sell(self):
balance.reader(self)
print("[green]This is your current portfolio[/green]\n")
Trade.Saved_Reader(self,"yes")
print("[bright_magenta]What do you want to sell[/bright_magenta]")
print("[red]0 : to quit[/red]")
try:
answer = input("")
tickerdata = yf.Ticker(answer)
tickerinfo = tickerdata.info
shower = (tickerinfo["currentPrice"])
print("Each share is currently:", shower)
print("[bright_magenta bold]How many shares do you want to sell [/bright_magenta bold]")
amount = input("")
if int(amount) < self.portfolio[answer]:
total = shower * int(amount)
print("[green bold]You will gain: [/green bold]",total)
print("1:[green]YES[/green] 2:[red]NO[/red]")
confirm = input("")
if confirm == "1":
self.portfolio[answer] -= int(amount)
Trade.Saved(self,self.portfolio)
balance.wallet(self,int(total))
Trade.Options(self)
elif confirm == "0":
Trade.Options(self)
except:
print("[red bold]Invalid[/red bold]")
Trade.Options(self)
Trades = Trade()
Trades.Options()