def test_quotes(self): quote = r.get_quotes(self.single_stock, info=None) assert (len(quote) == 1) quote = quote[0] assert (quote['symbol'] == self.single_stock) assert ('ask_price' in quote) assert ('ask_size' in quote) assert ('bid_price' in quote) assert ('bid_size' in quote) assert ('last_trade_price' in quote) assert ('last_extended_hours_trade_price' in quote) assert ('previous_close' in quote) assert ('adjusted_previous_close' in quote) assert ('previous_close_date' in quote) assert ('symbol' in quote) assert ('trading_halted' in quote) assert ('has_traded' in quote) assert ('last_trade_price_source' in quote) assert ('updated_at' in quote) assert ('instrument' in quote) # more_quotes = r.get_quotes(self.list_stocks, info=None) assert (len(more_quotes) == len(self.list_stocks)) # fake_quotes = r.get_quotes(self.fake_stocks, info=None) assert (len(fake_quotes) == 1) assert (fake_quotes[0] == None)
def tracker(): f = open("ticks.txt", "r") d = open("shares.txt", "r") share = 0 total = 0 price = 0 share = d.readline() time.sleep(5) while share != '': currentTick = f.readline() price = str(r.get_quotes(currentTick, info='bid_price')) price = price.replace("['", "") price = price.replace("']", "") total = total + float(share) * float(price) share = d.readline() share = share.replace("\n", "") share = share.replace(" ", "") print(total) f.close() d.close()
def buyer(): f = open("prices.txt", "w") f.close() d = open("shares.txt", "w") d.close() x=0 totalcash = 1000.00 price = 0.0 z = open("ticks.txt", "r") tick = z.readline() while tick != '': currentbuy = str(r.get_quotes(tick, info='ask_price')) f = open("prices.txt", "a") currentbuy = currentbuy.replace("['", "") currentbuy = currentbuy.replace("']", "") f.write(currentbuy + "\n") tick = z.readline() x=x+1 z.close() f.close() f = open("prices.txt", "r") d = open("shares.txt", "a") price = (f.readline()) price = price.replace("\n", "") price = price.replace(' ', "") price = (price) coun = x while coun > 0: share = ((totalcash / x) / float(price)) print(share) price = (f.readline()) d.write(str(share) + "\n") price = price.replace("\n", "") price = price.replace(' ', "") price = (price) coun = coun - 1 d.close() f.close()
password = '' #!!! login = r.login(username, password) # Query your positions positions = r.get_open_stock_positions() # Get Ticker symbols tickers = [r.get_symbol_by_url(item["instrument"]) for item in positions] # Get your quantities quantities = [float(item["quantity"]) for item in positions] # Query previous close price for each stock ticker prevClose = r.get_quotes(tickers, "previous_close") # Query last trading price for each stock ticker lastPrice = r.get_quotes(tickers, "last_trade_price") # Calculate the profit per share profitPerShare = [ float(lastPrice[i]) - float(prevClose[i]) for i in range(len(tickers)) ] # Calculate the percent change for each stock ticker percentChange = [ 100.0 * profitPerShare[i] / float(prevClose[i]) for i in range(len(tickers)) ]
def collect_stock_info(myStocks): # This is created to hold all of the created tuples for my stocks collected_stock_info = [] # This is creating a tuple for one stock CurStock = namedtuple("CurStock", [ "symbol", "sector", "price", "average_buy_price", "quantity", "amount_spent", "closing_price", "previous_close_date", "fifty_day_avg", "twohundred_day_avg" ]) # This will iterate through all of my stocks, grab the needed data and create a tuple for each stock avgCost = rb.account.get_open_stock_positions(info="average_buy_price") quantity = rb.account.get_open_stock_positions(info="quantity") index = 0 for stock in myStocks: # Grabbing the neccessary data curSymbol = rb.get_quotes(stock, info="symbol")[0] curStockSector = rb.stocks.get_fundamentals(stock, info="sector")[0] curStockPrice = float(rb.stocks.get_latest_price(stock)[0]) curStockAvgCost = float(avgCost[index]) curStockQuantity = float(quantity[index]) curStockAmountSpent = (curStockAvgCost * curStockQuantity) curPreviousClose = float( rb.get_quotes(stock, info="previous_close")[0]) curPreviousCloseDate = rb.get_quotes(stock, info="previous_close_date")[0] curStock50DayAvg = "none" curStock200DayAvg = "none" index = index + 1 # This is getting the 50 Day and 200 Day moving Avg # If there is an error it will try to use a different website to get the needed info try: headers = { 'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.77 Safari/537.36' } html = "https://financhill.com/stock-price-chart/" + curSymbol + "-technical-analysis" html_text = requests.get(html, headers=headers) soup = BeautifulSoup(html_text.text, "lxml") prevTd = soup.find("td", string="50-day Simple Moving Average:") curStock50DayAvg = float(prevTd.find_next_sibling("td").text) prevTd = soup.find("td", string="200-day Simple Moving Average:") curStock200DayAvg = float(prevTd.find_next_sibling("td").text) except: print( "Could not find using financhill website. Trying stockanalysis website" ) headers = { 'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.77 Safari/537.36' } html = "https://stockanalysis.com/stocks/" + curSymbol + "/statistics/" html_text = requests.get(html, headers=headers) soup = BeautifulSoup(html_text.text, "lxml") prevTd = soup.find("span", string="50-Day Moving Average") curStock50DayAvg = float(prevTd.find_next("td").text) prevTd = soup.find("td", string="200-Day Moving Average") curStock200DayAvg = float(prevTd.find_next("td").text) curStock = CurStock(symbol=curSymbol, sector=curStockSector, price=curStockPrice, average_buy_price=curStockAvgCost, quantity=curStockQuantity, amount_spent=curStockAmountSpent, closing_price=curPreviousClose, previous_close_date=curPreviousCloseDate, fifty_day_avg=curStock50DayAvg, twohundred_day_avg=curStock200DayAvg) # Add created stock tuple to the collection of all stock tuples collected_stock_info.append(curStock) return collected_stock_info
def getticks(): res = requests.get( 'https://www.tradingview.com/markets/stocks-usa/market-movers-gainers/') soup = BeautifulSoup(res.text, "html.parser") all_td_tags = [] # Set all_h1_tags to all h1 tags of the soup for element in soup.select('td'): all_td_tags.append(element.text) # Create seventh_p_text and set it to 7th p element text of the page x=0 tick = "" f = open("ticks.txt", "w") f.close() f = open("ticks.txt", "a") while (x < 1100): #1100 for entire list s = soup.select('td')[x].text s = s.replace("\n", "|") s = s.replace("\t", "|") if x%11 == 0: ress = '' for i in range(0, len(s)): if i>3 and i<10: ress = ress + s[i] s=ress s = s.replace("|", "") tick = s percent = soup.select('td')[x + 2].text percent = percent.replace("%", "") percent = float(percent) if "/" not in s: if "." not in s: if soup.select('td')[x + 4].text == "Buy" or soup.select('td')[x + 4].text == "Strong Buy": if percent < 30.00: f.write(tick + "\n") s = s.replace("|", "") if x%11 == 2: s = s.replace("%", "") s = float(s) if x%11 == 0 or x % 11 == 2 or x % 11 == 4: print(s) if x%11 == 5: print() x = x+1 f.close() current = r.get_quotes(tick, info='ask_price') print(current) cash = str(r.load_phoenix_account(info='account_buying_power')) print(cash) print(current)