def get_stock_history(ticker, period, interval): """ Input example: {"stocks":["GOOG","MSFT"], "period":"1y", "interval":"1wk"} """ ticker = Ticker(ticker) history = ticker.history(period=period, interval=interval) dates = history.index.get_level_values(1).tolist() close = history['close'].values.tolist() open = history['open'].values.tolist() high = history['high'].values.tolist() low = history['low'].values.tolist() result = {'dates': dates, "close": close, "open": open, "high": high, "low": low} return jsonify(result)
def get_raw_data_shares_outstanding(self): if self.tickers is None: self.tickers = Ticker(self.ticker_list, asynchronous=True) yf_data_key_stats = self.tickers.key_stats bad_ticker_stats = [ k for k, v in yf_data_key_stats.items() if isinstance(v, str) ] [yf_data_key_stats.pop(key) for key in bad_ticker_stats] if len(yf_data_key_stats.keys()) > 0: sp500_yf_df = pd.DataFrame.from_dict(yf_data_key_stats).T self.shares_outstanding_df = sp500_yf_df[['sharesOutstanding']] self.shares_outstanding_df.index.name = 'symbol' else: logger.info(f'No Shares Outstanding for list of symbols.')
def load_data(codes, start=str(LAST_WEEK), end=TOMORROW, verbose=True): ''' Takes list of shares and returns data from the start date ''' daily = pd.DataFrame({'date': []}) codes = tqdm(codes) if verbose else codes # Load data for code in codes: df = Ticker(f'{code}.AX').history(start=start, end=end).reset_index()[['date', 'close']] df.columns = ['date', code] daily = pd.merge(daily, df, on='date', how='outer') daily['date'] = pd.to_datetime(daily.date) daily = daily.sort_values('date').ffill().set_index('date') return daily
def __init__(self, symbol: Optional[str] = None) -> None: self.symbol = symbol self.ticker = Ticker(self.symbol) if symbol: try: self.check_symbol(symbol) except SymbolNotFoundException: if not str(symbol).endswith(".SA"): self.symbol = symbol + ".SA" self.ticker = Ticker(self.symbol) self.check_symbol(self.symbol) else: raise
def train(msg): comm, ticket = msg.text.split() ticker = Ticker(ticket, asynchronous=True) df = ticker.history(period='5d', interval='1m') if df[ticket] == 'No data found, symbol may be delisted': bot.send_message(msg.chat.id, 'Не нашел такой :(') else: bot.send_message(msg.chat.id, 'Нужно немного подождать') net.train(ticket) bot.send_message( msg.chat.id, f'Модель по {ticket} обучена и будет доступна в течение часа.') price = net.magic(ticket) bot.send_message( msg.chat.id, f'Предполагаемая цена закрытия следующей свечи: {price}')
def main(): """Run this to run the application""" st.sidebar.subheader("YahooQuery") symbols = st.sidebar.text_input( "Enter symbol or list of symbols (comma separated)", value="aapl" ) symbols = [x.strip() for x in symbols.split(",")] tickers = Ticker(symbols) page = st.sidebar.selectbox( "Choose a page", ["Homepage", "Base", "Base - Multiple", "Options", "Historical Pricing"] ) st.markdown("# Welcome to [YahooQuery](https://github.com/dpguthrie/yahooquery)") if page == "Homepage": homepage_view(tickers, symbols) elif page == "Base": base_view(tickers, symbols) elif page == "Base - Multiple": base_multiple_view(tickers, symbols) elif page == "Options": options_view(tickers, symbols) else: history_view(tickers, symbols)
def train(self, train_size=.8): from API.Help import import_sklearn_module, pct_change from yahooquery import Ticker print('training data for {}---------------------------'.format( str(self.ticker))) # retrieving data print('retrieving data') period = self.portfolio.get_max_period() interval = self.portfolio.get_trading_frequency() ticker_data = Ticker(str(self.ticker)).history(period=period, interval=interval) ticker_data_normalized = pct_change(ticker_data).dropna() y_label = ticker_data_normalized[ 'close'] > 0 # stock went up during that period # splitting data into train/test set print('splitting data in test/train') rows, _ = ticker_data_normalized.size() training_size_num = rows * train_size ticker_data_train = ticker_data_normalized[:training_size_num] ticker_data_test = ticker_data_normalized[training_size_num + 1:] y_label_train = y_label[:training_size_num] y_label_test = y_label[training_size_num + 1:] # training and testing model print('training model') model = import_sklearn_module(self.sklearn_model) model.fit(ticker_data_train, y_label_train) self.accuracy = model.score(ticker_data_test, y_label_test) print('model accuracy {}'.format(self.accuracy)) self.save() print()
async def stock_profile(symbol: str = Query(..., regex='^\S+$')): symbol = symbol.upper() yf = Ticker(symbol) quotes = yf.quotes asset_profile = yf.asset_profile if 'No fundamentals data found' in asset_profile[symbol]: raise HTTPException(status_code=404, detail=f"Ticker {symbol} not found.") quotes = quotes[symbol] asset_profile = asset_profile[symbol] data = { 'ticker': symbol, 'name': quotes.get('longName'), 'country': asset_profile.get('country'), 'industry': asset_profile.get('industry'), 'sector': asset_profile.get('sector'), 'fullTimeEmployees': asset_profile.get('fullTimeEmployees'), 'summary': asset_profile.get('longBusinessSummary'), 'website': asset_profile.get('website'), 'market': quotes.get('market'), 'exchange': quotes.get('fullExchangeName'), 'currency': quotes.get('currency'), 'marketCap': quotes.get('marketCap'), 'sharesOutstanding': quotes.get('sharesOutstanding') } return data
def tickerEarningsYield(stockList, finList): assetList = [[] for _ in finList] for stock in stockList: print(stock) stockInfo = Ticker(stock) try: if (int( str(stockInfo.price).split('\'regularMarketTime\': \'') [1].split('-')[0]) >= 2020): #eps = float(latestEarnings)/float(sharePrice) #NEW CALC: EBIT/(Enterprise Value) sumDetail = stockInfo.summary_detail[stock] finData = stockInfo.financial_data[stock] for index, string in enumerate(finList): infoBool = 0 if (string == "earningsYield" and ("forwardPE" in sumDetail)): assetList[index].append(1 / sumDetail["forwardPE"] * 100) infoBool += 1 elif (string == "returnOnAssets" and ("returnOnAssets" in finData)): assetList[index].append(finData["returnOnAssets"]) infoBool += 1 elif (string == "returnOnEquity" and ("returnOnEquity" in finData)): assetList[index].append(finData["returnOnEquity"]) infoBool += 1 elif (infoBool == 0): assetList[index].append(-100000000) else: assetList[index].append(-100000000) except: print("stock does not exist: " + stock) return assetList
async def stock_profile(symbol: str = Query(..., regex="^\S+$")): symbol = symbol.upper() yf = Ticker(symbol) quotes = yf.quotes asset_profile = yf.asset_profile if "No fundamentals data found" in asset_profile[symbol]: raise HTTPException(status_code=404, detail=f"Ticker {symbol} not found.") quotes = quotes[symbol] asset_profile = asset_profile[symbol] data = { "ticker": symbol, "name": quotes.get("longName"), "country": asset_profile.get("country"), "industry": asset_profile.get("industry"), "sector": asset_profile.get("sector"), "fullTimeEmployees": asset_profile.get("fullTimeEmployees"), "summary": asset_profile.get("longBusinessSummary"), "website": asset_profile.get("website"), "market": quotes.get("market"), "exchange": quotes.get("fullExchangeName"), "currency": quotes.get("currency"), "marketCap": quotes.get("marketCap"), "sharesOutstanding": quotes.get("sharesOutstanding"), } return data
def update_closing_price(self): from yahooquery import Ticker ticker = Ticker(self.symbol) self.previous_closing_price = ticker.summary_detail[ self.symbol]['previousClose'] self.save()
def get_current_price(self, ticker: str): """ Get the current price data for a given ticker symbol """ tickerObj = Ticker(ticker) pdata = tickerObj.price[ticker] # If the price is a string, it means we couldn't get the data. # yahooquery does not throw an error when that happens if (isinstance(pdata, str)): print("No data for %s. Fix me!" % ticker) return None priceData = { 'ticker': ticker, 'name': pdata['shortName'], 'price': pdata['regularMarketPrice'] } if (pdata['quoteType'] == "EQUITY"): priceData.update({ 'dayhigh': pdata['regularMarketDayHigh'], 'daylow': pdata['regularMarketDayLow'], 'dayvolume': pdata['regularMarketVolume'], 'dayopen': pdata['regularMarketOpen'], 'currency': pdata['currency'] }) return priceData
def gerar_valor_acao(codigo_acao): ticker_acao = Ticker(codigo_acao) resultado = ticker_acao.history(period="7d", interval="5m") resultado.reset_index(inplace=True) index = random.randrange(0, resultado.shape[0], 1) resultado = resultado.iloc[[index]] retorno = { 'codigo': codigo_acao, 'horario': datetime.now(pytz.timezone('AMERICA/Sao_Paulo')).isoformat(), 'valor': resultado['close'].values[0] } return json.dumps(retorno), 200
def get_calendar_list(bench_code="CSI300") -> List[pd.Timestamp]: """get SH/SZ history calendar list Parameters ---------- bench_code: str value from ["CSI300", "CSI500", "ALL", "US_ALL"] Returns ------- history calendar list """ logger.info(f"get calendar list: {bench_code}......") def _get_calendar(url): _value_list = requests.get(url).json()["data"]["klines"] return sorted(map(lambda x: pd.Timestamp(x.split(",")[0]), _value_list)) calendar = _CALENDAR_MAP.get(bench_code, None) if calendar is None: if bench_code.startswith("US_") or bench_code.startswith("IN_"): df = Ticker(CALENDAR_BENCH_URL_MAP[bench_code]).history( interval="1d", period="max") calendar = df.index.get_level_values(level="date").map( pd.Timestamp).unique().tolist() else: if bench_code.upper() == "ALL": @deco_retry def _get_calendar(month): _cal = [] try: resp = requests.get( SZSE_CALENDAR_URL.format( month=month, random=random.random)).json() for _r in resp["data"]: if int(_r["jybz"]): _cal.append(pd.Timestamp(_r["jyrq"])) except Exception as e: raise ValueError(f"{month}-->{e}") return _cal month_range = pd.date_range(start="2000-01", end=pd.Timestamp.now() + pd.Timedelta(days=31), freq="M") calendar = [] for _m in month_range: cal = _get_calendar(_m.strftime("%Y-%m")) if cal: calendar += cal calendar = list( filter(lambda x: x <= pd.Timestamp.now(), calendar)) else: calendar = _get_calendar(CALENDAR_BENCH_URL_MAP[bench_code]) _CALENDAR_MAP[bench_code] = calendar logger.info(f"end of get calendar list: {bench_code}.") return calendar
def set_df_hist(self, t0, t1, method='yahoo', interval='1d'): t0=pd.to_datetime(t0) t1=pd.to_datetime(t1) self._t0 = t0 self._t1 = t1 if self._ticker in ['XAU=', 'CLc1', 'LCOc1', 'GBP=', 'XLK', '.IXTTR', '.IXE', '.RUT']: self._df_hist = self.read_from_local(self._ticker) else: # uses yahooquery.Ticker to read yahoo finance data # indexed from least recent to most self._df_hist = Ticker(self._ticker).history(start=self._t0, end=self._t1, interval=interval)['adjclose'] # set column name to be ticker self._df_hist.columns = [self._ticker] if interval=='1d': self._df_hist.index = self._df_hist.index.normalize() # remove time portion of datetime for daily frequency
def getTickerInfo(results_tbl): filtered_tbl = [] for entry in results_tbl: ticker = Ticker(entry[0]) if ticker is not None: valid = False for measure in summary_measures.keys(): result = get_nested(ticker.summary_detail, entry[0], measure) if result is not None: entry[1].append(result) if result != 0: valid = True else: entry[1].append(0) for measure in financial_measures.keys(): result = get_nested(ticker.financial_data, entry[0], measure) if result is not None: entry[1].append(result) if result != 0: valid = True else: entry[1].append(0) if valid: filtered_tbl.append(entry) return filtered_tbl
def get_calendar_list(bench_code="CSI300") -> list: """get SH/SZ history calendar list Parameters ---------- bench_code: str value from ["CSI300", "CSI500", "ALL", "US_ALL"] Returns ------- history calendar list """ logger.info(f"get calendar list: {bench_code}......") def _get_calendar(url): _value_list = requests.get(url).json()["data"]["klines"] return sorted(map(lambda x: pd.Timestamp(x.split(",")[0]), _value_list)) calendar = _CALENDAR_MAP.get(bench_code, None) if calendar is None: if bench_code.startswith("US_"): df = Ticker(CALENDAR_BENCH_URL_MAP[bench_code]).history(interval="1d", period="max") calendar = df.index.get_level_values(level="date").map(pd.Timestamp).unique().tolist() else: calendar = _get_calendar(CALENDAR_BENCH_URL_MAP[bench_code]) _CALENDAR_MAP[bench_code] = calendar logger.info(f"end of get calendar list: {bench_code}.") return calendar
def __init__(self, c_name: str, t_name: str): super().__init__(c_name.replace(' ', '')) self.__ticker = t_name self.__class = 'Mutual' # self.__y_query = Ticker(t_name) self._setInfo()
def __init__(self, c_name: str, t_name: str, q_type: str): super().__init__(c_name.replace(' ', '').replace('/', '-'), q_type) self.__class = 'Regular' self.__ticker = t_name self.__y_query = Ticker(t_name) print(type(self.__y_query.fund_sector_weightings)) print(self.__y_query.fund_sector_weightings)
def load_comp(self): ''' Loads units, price, sell_price, buy_date, sell_date and code as registered in the data csv ''' # First, work out when the portfolio starts data = pd.read_csv('./comps/' + self.filename + '.csv').dropna(how='all') data['date'] = pd.to_datetime(data.date, dayfirst=True).dt.date data['price'] = data.price.astype(float) min_date = data.date.min() self.dates = Ticker('NDQ.AX').history( start=min_date, end=TOMORROW).reset_index()['date'] # Calculate the net units owned of each share, and split data into past and current shares share_data = data[~data.code.isin(['CASH_FLOW', 'DIVIDEND'])] share_data['units'] = share_data.units.astype(int) share_data['fee'] = share_data.fee.astype(float) shares = [] cash_flows = [] dividends = [] for code in share_data.code.unique(): shares.append( Share(share_data[share_data.code == code], self.dates)) cash_flow_data = data[data.code == 'CASH_FLOW'] dividends_data = data[data.code == 'DIVIDEND'] for _, row in cash_flow_data.iterrows(): cash_flows.append(CashFlow(row)) for _, row in dividends_data.iterrows(): dividends.append(Dividend(row)) return shares, cash_flows, dividends
def get_debt(ticker): try: return round( float(Ticker(ticker).balance_sheet('q')['TotalDebt'].iloc[-1]) / 1000000, 2) except: return None
def get_cash(ticker): try: return round( Ticker(ticker).balance_sheet('q') ['CashCashEquivalentsAndShortTermInvestments'].iloc[-1] / 1000000, 2) except: return None
def __init__(self): if len(sys.argv) > 2: self.args = [] self.method = sys.argv[1] x = range(2, len(sys.argv)) for i in x: self.args.append(sys.argv[i]) self.ticker = Ticker(self.args)
def run(self, dataset: AnyStr, symbol: AnyStr, **kwargs) -> DataFrame: stock = Ticker(symbol) source_df = self.fetch(dataset, stock, **kwargs) if not source_df.empty: parsed_df = self.parse(source_df, dataset=dataset, symbol=symbol) return parsed_df else: return source_df
def get_live_stock_price(self, ticker): try: tick = Ticker(ticker) price = tick.price[ticker]["regularMarketPrice"] except: print("Failed to query stock price from Yahoo") raise return float(price)
def check_symbol_history(self, symbol, start: datetime, end: datetime): logging.info("checking history for: %s", symbol) symbol_data = self.historical_data[self.historical_data.symbol == symbol] if len(symbol_data) == 0 or symbol_data['date'].min() > start or symbol_data['date'].max() < end: logging.info("check failed, downloading history for: %s", symbol) data = Ticker(symbol).history(start=start).reset_index(drop=False) data['date'] = pd.to_datetime(data['date']) self.historical_data = pd.concat([self.historical_data, data], ignore_index=True).drop_duplicates()
def get_papel_values(papel_alias): ticker = Ticker(papel_alias) sum = ticker.summary_detail if sum[papel_alias] != "No fundamentals data found for any of the summaryTypes=summaryDetail": end = datetime.now().strftime('%Y-%m-%d') start = (datetime.now() - timedelta(days=180)).strftime('%Y-%m-%d') ticker_hist = ticker.history(start=start, end=end) if isinstance(ticker_hist, pd.DataFrame): ticker_hist.sort_values(by='date', ascending=False, inplace=True) else: pass else: ticker_hist = 0 return ticker_hist
def max_sharpe(ticker, period='1y', interval='1d'): x = Ticker(ticker, retry=20, status_forcelist=[404, 429, 500, 502, 503, 504]) data = x.history(period=period, interval=interval) df = pd.DataFrame() df[ticker] = data['close'] mu = expected_returns.mean_historical_return(df) s = risk_models.sample_cov(df) ef = EfficientFrontier(mu, s) weights = ef.max_sharpe() cleaned_weights = ef.clean_weights() x = ef.portfolio_performance(verbose=False) if x is None: return 0 else: return round(2.5 * x[2] / 15, 3) # sharpe adjusted weight
def _collector(self, stock_list): error_symbol = [] with ThreadPoolExecutor(max_workers=self.max_workers) as worker: futures = {} p_bar = tqdm(total=len(stock_list)) for symbols in [ stock_list[i:i + self.max_workers] for i in range(0, len(stock_list), self.max_workers) ]: self._sleep() resp = Ticker( symbols, asynchronous=self._asynchronous, max_workers=self.max_workers).history(period="max") if isinstance(resp, dict): for symbol, df in resp.items(): if isinstance(df, pd.DataFrame): self._temp_save_small_data(self, df) futures[worker.submit( self.save_stock, symbol, df.reset_index().rename( columns={"index": "date"}))] = symbol else: error_symbol.append(symbol) else: for symbol, df in resp.reset_index().groupby("symbol"): self._temp_save_small_data(self, df) futures[worker.submit(self.save_stock, symbol, df)] = symbol p_bar.update(self.max_workers) p_bar.close() with tqdm(total=len(futures.values())) as p_bar: for future in as_completed(futures): try: future.result() except Exception as e: logger.error(e) error_symbol.append(futures[future]) p_bar.update() print(error_symbol) logger.info(f"error symbol nums: {len(error_symbol)}") logger.info(f"current get symbol nums: {len(stock_list)}") error_symbol.extend(self._mini_symbol_map.keys()) return error_symbol
def __init__(self, c_name: str, t_name: str, q_type: str): super().__init__(c_name.replace(' ', ''), q_type) self.__ticker = t_name self.__class = 'Etf' #self.__quote_type = q_type # self.__y_query = Ticker(t_name) self._setInfo()