I am using a 2d array and am trying to search through it and update it like a database, I am going to try to put this on a website so I'm just using a 2d array for now. However when I try using a for loop to search through the index of the 2d array it comes up with the error.
This is the whole code. I am talking about specifically the sell function, I want it to go through the values in the 2d array to look at the 0th index where the ticker will be if it matches then I want the 1st index (quantity) of that iteration to be minused by the quantity input by user. How do I get this to work?
`import requests
ticker = input("ticker?")
quantity = int(input("quantity?"))
portfolio = [1000,[ticker,"quatity","datebought"]]
def getshareprice (ticker):
url = f"https://api.twelvedata.com/price?symbol={ticker}&apikey={TDKEY}"
r = requests.get(url)
data = r.json()
price = data.get('price')
return float(price)
def buy(ticker,quantity):
allocated_funds = float(getshareprice(ticker)*quantity)
if allocated_funds<int(portfolio[0]):
portfolio[0] -= allocated_funds
portfolio.append([ticker,quantity,"datebought"])
print ("you have bought",quantity,"shares of:",ticker,"for",allocated_funds,",in cash you have",portfolio[0])
else:
purchasable = int(portfolio[0])/getshareprice(ticker)
print("not enough funds to allocate transaction")
def sell(ticker,quantity):
for i in portfolio:
if i[0] == ticker:
i[1] -= quantity
else:
print("not enough shares to allocate transaction")`
Another way I was thinking of doing this was to just add another array into the 2d array saying how many shares the user wanted to sell, however this comes with the same problem of searching through the 2d array trying to find the correct ticker. It needs to search through to see how many shares they have as the user should not be able to sell more shares than they own. This way I could try to implement a portfolio history as well.
Consider a design like this. "portfolio" becomes the database of your holdings, with a special ticker called "cash" that has your cash.
import requests
ticker = input("ticker?")
quantity = int(input("quantity?"))
portfolio = {
'cash': [1000, None]
}
def getshareprice (ticker):
url = f"https://api.twelvedata.com/price?symbol={ticker}&apikey={TDKEY}"
r = requests.get(url)
data = r.json()
price = data.get('price')
return float(price)
def buy(ticker,quantity):
allocated_funds = float(getshareprice(ticker)*quantity)
if allocated_funds < portfolio['cash'][0]:
portfolio['cash'][0] -= allocated_funds
if ticker not in portfolio:
portfolio[ticker] = [0, None]
portfolio[ticker][0] += quantity
print ("you have bought",quantity,"shares of:",ticker,"for",allocated_funds,",in cash you have",portfolio['cash'][0])
else:
purchasable = int(portfolio['cash'][0])/getshareprice(ticker)
print("not enough funds to allocate transaction")
def sell(ticker,quantity):
if ticker not in portfolio:
print("You don't own that ticker")
elif portfolio[ticker][0] > quantity:
print("not enough shares to allocate transaction")
else:
portfolio[ticker][0] -= quantity
buy('MSFT',10)
sell('INTL',9)
sell('MSFT',5)
buy(ticker,quantity)