示例#1
0
def portfolio(request, portfolio_pk):

    portfolio = Portfolio.objects.get(pk=portfolio_pk)
    check_ownership(request, portfolio)
    stock_not_found = False

    quote_name = request.POST.get('quote', None)
    if quote_name:
        quote_name = quote_name.upper()
        yf = YahooFinancials(quote_name)
        exchange = yf.get_stock_exchange()
        try:
            stock = Stock.objects.get(name=quote_name)
        except Stock.DoesNotExist:
            stock = Stock.objects.create(
                name=quote_name,
                stock_exchange=yf.get_stock_exchange(),
                market_cap=convert_number(yf.get_market_cap()),
                last_price=convert_number(yf.get_current_price())
            )
            PortfolioStock.objects.get_or_create(stock=stock,
                portfolio=portfolio)

    stocks = PortfolioStock.objects.filter(portfolio=portfolio).order_by("-stock__value_score")

    operations = Operation.objects.filter(portfolio_stock__portfolio=portfolio)[0:10]

    total_value, total_spent, profit = portfolio.balance_sheet()
    if total_spent != 0:
        profit_purcent = round(profit / total_spent * 100, 2)
    else:
        profit_purcent = 0

    total_cash_value = total_value + portfolio.cash

    c = {
        'quote_name': quote_name,
        'stock_not_found': stock_not_found,
        'portfolio':portfolio,
        'stocks': stocks,
        'operations':operations,
        'total_value':total_value,
        'total_spent':total_spent,
        'profit':profit,
        'profit_purcent':profit_purcent,
        'total_cash_value':total_cash_value,
    }
    c.update(csrf(request))

    return render_to_response('portfolio.html', c)
示例#2
0
def update_shares(request, portfolio_pk):

    portfolio = Portfolio.objects.get(pk=portfolio_pk)
    check_ownership(request, portfolio)
    pstocks = PortfolioStock.objects.filter(portfolio=portfolio)
    for pstock in pstocks:
        stock = pstock.stock
        yf = YahooFinancials(quote_name)
        stock.last_price = convert_number(yf.get_current_price())
        stock.price_sales_ratio = convert_number(yf.get_price_to_sales())
        stock.dividend_yield = convert_number(yf.get_dividend_yield())

        stock.save()
    return http.HttpResponseRedirect('..')
def main():
    yahoo_stock = YahooFinancials(NASDAQ_COMPOSITE_SYMBOL)
    market_notes = is_market_in_a_buy_situation(yahoo_stock)
    if not market_notes:
        print('BUY')
    else:
        print('DO NOT BUY due to the following reasons:')
        for note in market_notes:
            print(f'* {note}')
示例#4
0
def details(request, slug):
    stocks = Stock.objects.filter(slug=slug).first()
    name = stocks.stock_name
    date = stocks.stock_date
    quant = stocks.stock_quant
    p_price = stocks.stock_cb
    slug = slug
    yahoo_financials = YahooFinancials(name)
    gain_loss = round(yahoo_financials.get_current_percent_change() * 100, 3)
    return render(
        request, 'details.html', {
            'slug': slug,
            'name': name,
            'date': date,
            'quant': quant,
            'p_price': p_price,
            'gain_loss': gain_loss
        })
示例#5
0
def retrieve_stock_info(errored_tickers, tickers, angels):

    # stock object we will return
    curr_stock = None
    # call to library to get object
    yf_object = YahooFinancials(tickers)
    today = get_today()
    lastMonth = today - timedelta(days=28)
    start_date = lastMonth.strftime("%Y-%m-%d")
    end_date = today.strftime("%Y-%m-%d")

    try:
        # print("getting historical prices")
        historical_price_data = yf_object.get_historical_price_data(
            start_date, end_date, "daily")
    except Exception:
        print("error from getting data from library")
        return None

    rows = []
    # print("getting individual stock stats")
    for symbol in tickers:
        curr_stock = Stock(symbol, historical_price_data[symbol])
        if curr_stock.errored == True:
            errored_tickers.append(symbol)
            curr_row = [symbol, "ERRORED", curr_stock.error_message]
            rows.append(curr_row)
            continue
        # add it to our list of angels
        elif curr_stock.angel_status == True:
            angels.append(curr_stock)
        curr_row = [
            curr_stock.ticker,
            curr_stock.month_price,
            curr_stock.month_date,
            curr_stock.week_price,
            curr_stock.week_date,
            curr_stock.current_price,
            curr_stock.angel_status,
        ]
        rows.append(curr_row)
    #
    # print("got all stock info")
    return rows
示例#6
0
 def __init__(self, stock, from_date, to_date, ma_short, ma_long):
     self.stock = stock
     self.from_date = from_date
     self.to_date = to_date
     self.ma_short = ma_short
     self.ma_long = ma_long
     # assign prices
     prices = YahooFinancials(stock).get_historical_price_data(
         from_date, to_date, 'daily')[stock]["prices"]
     self.price_history = PriceHistory(prices, ma_short, ma_long)
示例#7
0
 def _DownloadDividendsCSVFromYahooFinance(self, symbols: List[Text]) -> List[Optional[Text]]:
   csv_data = []
   today = datetime.datetime.now().strftime("%Y-%m-%d")
   all_assets = YahooFinancials(symbols)
   all_dividends = all_assets.get_daily_dividend_data(
       start_date=config.DEFAULT_MIN_DATE, end_date=today)
   for symbol in symbols:
     dividends = all_dividends[symbol]
     if dividends is None:
       csv_data.append(None)
       continue
     lines = ['Date,Amount']  # Header
     for e in dividends:
       if e['amount'] is None:
         continue
       lines.append(f'{e["formatted_date"]},{e["amount"]}')
     lines.append('')  # This will add a last line break.
     csv_data.append("\n".join(lines))
   return csv_data
示例#8
0
def multistocks_yahoofinancials(stocks, start, end, period_number,
                                stock_value):
    periods = ["daily", "weekly", "monthly"]
    stock_value = ['high', 'adjclose']
    prices = pd.DataFrame()
    #end_date=(datetime.date.today()).strftime('%Y-%m-%d')
    #start_date=(datetime.date.today()-datetime.timedelta(1825)).strftime('%Y-%m-%d')
    for ticker in stocks:
        #create a object
        stock_name = YahooFinancials(ticker)
        data = stock_name.get_historical_price_data(start, end,
                                                    periods[period_number])
        value = data[ticker]["prices"]
        value_period = periods[period_number]
        #print(value_period)
        temp = pd.DataFrame(value)[["formatted_date", "high"]]
        temp.set_index("formatted_date", inplace=True)
        temp.dropna(inplace=True)
        prices[ticker] = temp["high"]
    return prices
示例#9
0
def stock_statements(tickers, frequency, statement_type):
    #    data_list = []
    #    for ticker in tickers:
    yahoo_financials = YahooFinancials(ticker)
    ass = yahoo_financials.get_financial_stmts(frequency,
                                               statement_type,
                                               reformat=True)
    if statement_type is 'cash' and frequency is 'quarterly':
        data = ass['cashflowStatementHistoryQuarterly']
    if statement_type is 'cash' and frequency is 'annual':
        data = ass['cashflowStatementHistory']
    if statement_type is 'income' and frequency is 'quarterly':
        data = ass['incomeStatementHistoryQuarterly']
    if statement_type is 'income' and frequency is 'annual':
        data = ass['incomeStatementHistory']
    if statement_type is 'balance' and frequency is 'quarterly':
        data = ass['balanceSheetHistoryQuarterly']
    if statement_type is 'balance' and frequency is 'annual':
        data = ass['balanceSheetHistory']
    return data
示例#10
0
def get_stock_price(startDate, endDate, periodtype, listStock):
    dfListStocks = pd.DataFrame(columns=['Date'])
    for s in listStock:
        dictStockPrice = YahooFinancials(s).get_historical_price_data(
            startDate, endDate, periodtype)
        dfStockPrice = pd.DataFrame.from_dict(
            dictStockPrice[s]["prices"])[['formatted_date', 'adjclose']]
        dfStockPrice.columns = ['Date', s]
        dfListStocks = dfListStocks.merge(dfStockPrice, how='right', on='Date')
    dfListStocks = dfListStocks.set_index('Date')
    return dfListStocks
def get_yahoo_finance_historical_price_data():

    map_of_all_index_date_to_price = {}

    nyse_map = get_map_of_date_to_price(
        YahooFinancials(nyse_composite_index_symbol),
        nyse_composite_index_symbol)
    dow_map = get_map_of_date_to_price(YahooFinancials(dow_index_symbol),
                                       dow_index_symbol)
    nasdaq_map = get_map_of_date_to_price(
        YahooFinancials(nasdaq_composite_symbol), nasdaq_composite_symbol)
    sp500_map = get_map_of_date_to_price(YahooFinancials(sp500_index_symbol),
                                         sp500_index_symbol)

    map_of_all_index_date_to_price[nyse_composite_index_symbol] = nyse_map
    map_of_all_index_date_to_price[dow_index_symbol] = dow_map
    map_of_all_index_date_to_price[nasdaq_composite_symbol] = nasdaq_map
    map_of_all_index_date_to_price[sp500_index_symbol] = sp500_map

    return map_of_all_index_date_to_price
示例#12
0
def update_true_values():
    client = pymongo.MongoClient("mongodb+srv://kmhatre:" + PASSWORD +
                                 "@crypto.j5hw0.mongodb.net/" + 'predictions' +
                                 "?retryWrites=true&w=majority")
    db = client['predictions']
    col = db['predictions']
    pred = {}
    for crypto in CRYPTO_LIST.keys():
        yf = YahooFinancials(CRYPTO_LIST[crypto])
        today = str(
            (dt.datetime.now(timezone('EST')) - dt.timedelta(days=1)))[:10]
        yesterday = str(
            (dt.datetime.now(timezone('EST')) - dt.timedelta(days=1)))[:10]
        prices = yf.get_historical_price_data(yesterday, today, 'daily')
        price = prices[CRYPTO_LIST[crypto]]['prices'][0]['open']
        pred[crypto + '_true'] = str(price)
    query = {'date': today}
    newvalues = {"$set": pred}
    col.update_one(query, newvalues)
    client.close()
示例#13
0
def get_map_of_date_to_price(symbol, start_date, end_date, period,
                             indexing_flag):
    yahoo_stock = YahooFinancials(symbol)
    json = (yahoo_stock.get_historical_price_data(start_date, end_date,
                                                  period))
    map_of_prices = {}

    x = 0
    for row in json[symbol]["prices"]:

        price_today = row['close']
        date = row['formatted_date']

        if indexing_flag == True:
            map_of_prices[x] = [date, price_today]

        else:
            map_of_prices[date] = price_today
        x = x + 1
    return map_of_prices
示例#14
0
def retrieve_stock_data(ticker, start, end):
    """
    Retrieves daily stock open, close, and adjusted close data.
    """
    json = YahooFinancials(ticker).get_historical_price_data(start, end, "daily")
    print(f"Got {ticker} information")
    df = pandas.DataFrame(columns=["open", "close", "adjclose"])
    for row in json[ticker]["prices"]:
        date = datetime.fromisoformat(row["formatted_date"])
        df.loc[date] = [row["open"], row["close"], row["adjclose"]]
    df.index.name = "date"
    return df
示例#15
0
    def _get_ohlcv(self, params):
        yf = YahooFinancials([params['symbol']])
        hist_price = yf.get_historical_price_data(
            start_date=params['start_date'].strftime("%Y-%m-%d"),
            end_date=params['end_date'].strftime("%Y-%m-%d"),
            time_interval=params['frequency'],
        )[params['symbol']]['prices']

        df = pd.DataFrame.from_dict(hist_price)
        df = df[params['req_data_fields']]
        df.columns = params['data_fields']
        df.set_index('DATE', drop=True, inplace=True)
        df.index = pd.to_datetime(df.index)

        for column in df.columns:
            try:
                df[column] = pd.to_numeric(df[column])
            except:
                print(column)

        return self._adjust_ohlcv(df) if params['adjusted'] == True else df
示例#16
0
    def get_general_info(self):
        general_info_dict = dict()
        yfinance_info = yfinance.Ticker(self.ticker).info
        general_info_dict["company"] = yfinance_info["shortName"]
        general_info_dict["currency"] = yfinance_info["currency"]
        general_info_dict["country"] = yfinance_info["country"]
        general_info_dict["exchange"] = YahooFinancials(
            self.ticker).get_stock_exchange()
        general_info_dict["sector"] = yfinance_info["sector"]
        general_info_dict["industry"] = yfinance_info["industry"]

        return general_info_dict
示例#17
0
def extraction(output_data, WATCHLIST):

    # Run Yahoo API with 1 indexes at a time.
    # can run multiple as well
    n = 1

    for category in WATCHLIST:
        category_name = category.pop(0)
        output_data.append(category_name)

        while len(category) > 0:
            index_to_search = category[0:n]
            category = category[n:]

            process_from_yahoo = YahooFinancials(index_to_search)
            result_data = process_from_yahoo.get_stock_price_data()

            extract(output_data, result_data)

        # append an empty column to use that to add a row in excel
        output_data.append(" ")
示例#18
0
    def get_data(cls, self):
        """Get data from yahoo finance."""
        treasuries = YahooFinancials(self.tickers)
        tdata = treasuries.get_current_price()

        # Add current timestamp
        tdata['time'] = datetime.datetime.now()
        # Append data to existing data frame
        df = self.df.append(tdata, ignore_index=True)
        """
        # Remove time from columns for data conversion
        try:
            self.cols.remove('time')
        except ValueError:
            pass
        # Convert cols to float 16s
        df[self.cols] = df[self.cols].astype(np.float16)
        """
        df.reset_index(inplace=True, drop=True)

        return df
示例#19
0
文件: fx.py 项目: MoeZaza/ewah
    def ewah_execute(self, context):
        data_from = self.data_from or context["dag"].start_date
        data_until = self.data_until or datetime_utcnow_with_tz()

        format_str = "%Y-%m-%d"
        currency_str = "{0}{1}=X".format(*self.currency_pair)
        data = YahooFinancials([currency_str]).get_historical_price_data(
            data_from.strftime(format_str),
            data_until.strftime(format_str),
            self.frequency,
        )
        self.upload_data(data[currency_str]["prices"])
示例#20
0
 def post(self) -> asset_returns:
     args = request.json
     print('Processing price request for', args)
     try:
         ticker = args['ticker'].upper()
         start_date = dateparser.parse(
             args['start_date']).strftime('%Y-%m-%d')
         end_date = dateparser.parse(args['end_date']).strftime('%Y-%m-%d')
         yf = YahooFinancials(ticker)
         prices = yf.get_historical_price_data(start_date, end_date,
                                               args['interval'])
         ret_prices = [{
             'date':
             datetime.utcfromtimestamp(price['date']).strftime('%Y-%m-%d'),
             'close':
             price['close']
         } for price in prices[ticker]['prices']
                       if price['close'] is not None]
         return {'prices': ret_prices, 'ticker': ticker}
     except Exception as e:
         return {'message': 'Unable to retrive prices'}, 500
示例#21
0
def calc_earnings_yield(tickers):
    """
    calc earnings to yield ratio
    :param tickers:
    :return:
    """
    print('Fetching Earnings to Yield Data from Yahoo for 100 Tickers...')
    total_data = {}
    for index, ticker in enumerate(tickers):
        if index % 1 == 0:
            print('Fetching Earnings to Yield Data for %d out of %d' %
                  (index, len(tickers)))
        try:
            yahoo_financials = YahooFinancials(ticker)
            total_data[ticker] = 1 / yahoo_financials.get_pe_ratio()
        except KeyError:
            print('Could not calc. PE for %s' % ticker)
            total_data[ticker] = None
            continue

    return total_data
示例#22
0
def retrieve_yahoo_data(stock, start, end, method=1):
    """
  Function used to retrieve Yahoo Finance stock data
  """
    if method == 1:
        stock = yf.Ticker(stock)
        hist = stock.history(start=start, end=end, auto_adjust=False)
        prices = []
        for index, day in hist.iterrows():
            day_info = (index, day['Open'], day['High'], day['Low'],
                        day['Close'], day['Volume'], day['Adj Close'])
            prices.append(day_info)
        return prices

    if method == 2:
        stock_2 = YahooFinancials(stock)
        stock_info = stock_2.get_historical_price_data(
            start, end.strftime('%Y-%m-%d'), 'daily')
        prices = []
        prices_ = stock_info[stock]['prices']
        for p in prices_:
            day_info = (pd.Timestamp(p['formatted_date']), round(p['open'], 2),
                        round(p['high'], 2), round(p['low'], 2),
                        round(p['close'],
                              2), p['volume'], round(p['adjclose'], 2))
            prices.append(day_info)
        return prices

    if method == 3:
        yf.pdr_override()
        data = pdr.get_data_yahoo([stock], start=start, end=end)
        prices = []
        for index, day in data.iterrows():
            day_info = (index, round(day['Open'],
                                     2), round(day['High'],
                                               2), round(day['Low'], 2),
                        round(day['Close'],
                              2), day['Volume'], round(day['Adj Close'], 2))
            prices.append(day_info)
        return prices
示例#23
0
def update_tdata_history(ticker, colab):
    for line in reversed(list(open(c.ROOT_DIR_Google + "Tdata_out.csv"))):
        last = line
        break
    token = last.split(",")
    min_date = token[0]
    # print(min_date)
    time = datetime.datetime.now()
    time = datetime.datetime.strftime(time, "%H:%M:%S")
    if time < "16:00:00":
        max_date = (datetime.datetime.today() -
                    timedelta(days=1)).strftime('%Y-%m-%d')
    else:
        max_date = datetime.datetime.today().strftime('%Y-%m-%d')
    # print(max_date)
    if (max_date > min_date):
        yahoo_financials = YahooFinancials(ticker)
        historical_stock_prices = yahoo_financials.get_historical_price_data(
            min_date, max_date, 'daily')
        nya_data = historical_stock_prices[ticker]
        price_data = nya_data['prices']
        if colab:
            csv_out = open(c.ROOT_DIR_Google + 'Tdata_out.csv',
                           mode='a')  # opens csv file
        else:
            csv_out = open('Technical_data/Tdata_out.csv',
                           mode='a')  # opens csv file
        writer = csv.writer(csv_out)  # create the csv writer object
        for line in price_data:
            # writes a row and gets the fields from the json object
            writer.writerow([
                line.get('formatted_date'),
                line.get('high'),
                line.get('low'),
                line.get('open'),
                line.get('close'),
                line.get('volume'),
                line.get('adjclose')
            ])
        csv_out.close()
示例#24
0
def calculate():
    url = 'http://www.google.com/'
    timeout = 2
    try:
        _ = requests.get(url, timeout=timeout)
        symbol = (request.form['ticketSymbol'])
        try:
            data = YahooFinancials(symbol)
            date = datetime.datetime.now(tzlocal())
            timezone = str(time.asctime(time.localtime(time.time())))
            strftime = date.strftime("%Z")
            financials = data.get_stock_quote_type_data()
            name = str(financials.get(symbol).get('longName'))
            sym = financials.get(symbol).get('symbol')
            current = str(data.get_current_price())
            currentchange = str(round(data.get_current_change(), 2))
            percentchange = str(
                round(data.get_current_percent_change() * 100, 2)) + '%'
            return render_template('output.html',
                                   symbol=symbol,
                                   data=data,
                                   timezone=timezone,
                                   strftime=strftime,
                                   financials=financials,
                                   name=name,
                                   sym=sym,
                                   current=current,
                                   currentchange=currentchange,
                                   percentchange=percentchange)
        except:
            return 'INVALID SYMBOL'
    except:
        return 'NO INTERNET CONNECTION'
示例#25
0
def daily_data(tickers, start_date, end_date):
    freq = 'daily'

    # Construct yahoo financials objects for data extraction
    financial = {}
    daily = {}
    fails = []

    for ticker in tickers:
        try:
            financial[ticker] = YahooFinancials(ticker)
            tick = financial[ticker].get_historical_price_data(
                start_date, end_date, freq)[ticker]['prices']
            tick = pd.DataFrame(
                clean_stock_data(tick))[['formatted_date', 'open', 'close']]
            tick = tick.rename(columns={'formatted_date': 'date', 'close': f'{ticker}_close', \
                'open': f'{ticker}_open'})
            daily[ticker] = tick.set_index('date')
        except:
            print(ticker)
            fails.append(ticker)

    # Join into one daily master dataset
    daily_master = pd.DataFrame()
    delta_master = pd.DataFrame()

    tickers = [tick for tick in tickers if tick not in fails]

    for ticker in tickers:
        daily_master = daily_master.merge(daily[ticker],
                                          how='outer',
                                          left_index=True,
                                          right_index=True)
        delta_master[f'{ticker}_delta'] = daily_master[
            f'{ticker}_close'] - daily_master[f'{ticker}_open']
        delta_master[f'{ticker}_open'] = daily_master[f'{ticker}_open']

    #Reset Index so that we can create a lag
    daily_master = delta_master.reset_index()

    #Set time period to base predictions
    # i.e. t = 7 will use week before to make prediction for next day.
    t = 2

    #Create new features which have open, close, delta, and volume of stock for last t days
    for ticker in tickers:
        for k in range(1, t + 1):
            daily_master[f'{ticker}_delta_{k}'] = daily_master[
                f'{ticker}_delta'].shift(k)

    #Remove NaN from dataset after creating lag and return
    return daily_master.dropna()
示例#26
0
 async def crypto(self, ctx, coin, currency):
     bot.command_used(ctx, "crypto")
     data = YahooFinancials(coin + "-" + currency)
     if str(data) != "None" and str(data) != "":
         await ctx.send(
             f"**{coin.upper()} - {currency.upper()}**\nCurrent Value: `{str(data.get_current_price())}`\nDaily Low/High: `"
             +
             f"{str(data.get_daily_low())} `/` {str(data.get_daily_high())}`\nYearly Low/High: `{str(data.get_yearly_low())} `/` {str(data.get_yearly_high())}`"
         )
     else:
         await ctx.send(
             "Invalid Listing or currency, listing and currency must be abbreviated e.g. ETH and GBP",
             delete_after=7)
示例#27
0
    def load_data(self):
        '''
        idYahoo: Indicated of company from yahoo finance
        startHistorical: Start date of the history in format 'yyyy-MM-dd'
        endHistorical: End date of the history in format 'yyyy-MM-dd'
        
        return: dataframe with data history
        '''
        def __create_new_vars(df):

            # Get difference between high and low of each day and get difference between open and close of each day
            df['range_hl'] = df['high'] - df['low']
            df['range_oc'] = df['open'] - df['close']
            # Add a column 'order_day' to indicate the order of the rows by date
            df['order_day'] = [x for x in list(range(len(df)))]

            return df

        trainingModel_ml.__createDirectory(self)
        yahoo_financials = YahooFinancials(self.idYahoo)

        data = yahoo_financials.get_historical_price_data(
            start_date=self.startHistorical,
            end_date=self.endHistorical,
            time_interval='daily')

        df = pd.DataFrame(data[self.idYahoo]['prices'])
        df['Date'] = df.formatted_date
        df = df.drop(['date', 'formatted_date'], axis=1)
        # convert Date column to datetime
        df['Date'] = pd.to_datetime(df['Date'], format='%Y-%m-%d')
        df['month'] = df['Date'].dt.month
        df = df.drop_duplicates()

        # sort by datetime
        df.sort_values(by='Date', inplace=True, ascending=True)
        df = __create_new_vars(df)

        self.df = df
示例#28
0
def create_retrieve_thread(tickers, metric, file_name, data_dict, batch_no):
    start_loop = time.time()
    print(
        f"Batch/thread {batch_no + 1}: Tickers to be retrieved are: {tickers}")
    yahoo_financials = YahooFinancials(tickers)

    if metric == "balance":
        print(f"Retrieving annual balance sheets from Yahoo Finance...")
        financial_statement = yahoo_financials.get_financial_stmts(
            'annual', 'balance')['balanceSheetHistory']

    elif metric == "income":
        print(
            f"Retrieving annual income statement history from Yahoo Finance..."
        )
        financial_statement = yahoo_financials.get_financial_stmts(
            'annual', 'income')['incomeStatementHistory']

    elif metric == "cap":
        print(f"Retrieving market cap information from Yahoo Finance...")
        financial_statement = yahoo_financials.get_market_cap()

    else:
        print("Metric entered is not recognized.")
        financial_statement = {}

    dict_lock.acquire()
    data_dict.update(financial_statement)

    end_loop = time.time()

    print(f"Saving batch {batch_no + 1} to JSON file...")
    json.dump(data_dict, open(file_name + '.json', 'w'))
    dict_lock.release()

    print(
        f"Time elapsed for batch {batch_no + 1}: {end_loop - start_loop}, metric: {metric}"
    )
    print()
示例#29
0
 def retrieve_stock_data(ticker, start, end):
     json = YahooFinancials([ticker]).get_historical_price_data(
         start, end, "daily")
     df = pd.DataFrame(columns=["open", "close", "adjclose", "volume"])
     for row in json[ticker]["prices"]:
         date = datetime.date.fromisoformat(row["formatted_date"])
         df.loc[date] = [
             row["open"], row["close"], row["adjclose"], row["volume"]
         ]
     df.index.name = "Date"
     print("Ação " + ticker + " enviado ao dataframe - " +
           str(datetime.datetime.now().strftime('%M:%S.%f')[:-4]))
     return df
def yahoo2pandas(tickers,
                 from_date="1900-01-01",
                 to_date="2100-12-31",
                 frequency="daily",
                 allow_null=False):
    """Download raw stocks data from yahoo and return a pandas dataframe.
    allow_null parameter allow you to fetch data even by dates where some tickers don't exist.
    """
    results = {}
    yahoo_financials = YahooFinancials(tickers)
    historical_stock_prices = yahoo_financials.get_historical_price_data(
        from_date, to_date, frequency)

    for ticker in historical_stock_prices:
        df = pd.DataFrame(historical_stock_prices[ticker]["prices"])
        df["formatted_date"] = (df["formatted_date"]).astype("datetime64[ns]")
        df = df.set_index("formatted_date")
        results[ticker] = df["close"].loc[~df["close"].index.duplicated(
            keep="first")]

    return pd.DataFrame.from_dict(results).dropna(
    ) if not allow_null else pd.DataFrame.from_dict(results)
示例#31
0
def download_yahoo(ticker: str, base_dir: str = default_data_dir):
    try:
        yf = YahooFinancials(ticker)
        data = yf.get_historical_price_data(dt_to_str(start_date),
                                            dt_to_str(end_date), 'daily')
    except Exception as err:
        print(f'Unable to read data for {ticker}: {err}')
        return pd.DataFrame({})

    if data.get(ticker) is None or data[ticker].get('prices') is None or \
            data[ticker].get('timeZone') is None or len(data[ticker]['prices']) == 0:
        print(f'Yahoo: no data for {ticker}')
        return pd.DataFrame({})

    prices = {}
    for rec in sorted(data[ticker]['prices'], key=lambda r: r['date']):
        date = datetime.strptime(rec['formatted_date'], '%Y-%m-%d')
        dic_with_prices(prices, ticker, date, rec['open'], rec['high'],
                        rec['low'], rec['close'], rec['volume'])

    if 'dividends' in data[ticker]['eventsData']:
        for date, rec in sorted(
                data[ticker]['eventsData']['dividends'].items(),
                key=lambda r: r[0]):
            date = datetime.strptime(date, '%Y-%m-%d')
            dic_with_div(prices, ticker, date, rec['amount'])

    if 'splits' in data[ticker]['eventsData']:
        for date, rec in sorted(data[ticker]['eventsData']['splits'].items(),
                                key=lambda r: r[0]):
            date = datetime.strptime(date, '%Y-%m-%d')
            print(f"{ticker} has split {rec['splitRatio']} for {date}")

    frame = pd.DataFrame.from_dict(
        prices,
        orient='index',
        columns=['Open', 'High', 'Low', 'Close', 'Volume', 'Dividend'])
    save_csv(base_dir, ticker, frame, 'yahoo')
def get_data_from_yahoo(reload_sp500=False):
    if reload_sp500:
        tickers = save_sp500_tickers()
    else:
        with open("sp500tickers.pickle", "rb") as f:
            tickers = pickle.load(f)
    if not os.path.exists('stock_dfs'):
        os.makedirs('stock_dfs')

    start = str(dt.datetime(2010, 1, 1).date())
    end = str(dt.datetime.now().date())
    time_period = 'daily'
    errors = []

    for ticker in tickers:
        try:
            if not os.path.exists('stock_dfs/{}.csv'.format(ticker)):
                TKR = YahooFinancials(ticker)
                fin_dict = TKR.get_historical_price_data(
                    start, end, time_period)
                df = pd.DataFrame(fin_dict[ticker]['prices'])
                df.reset_index(inplace=True)
                df.set_index("date", inplace=True)
                df.to_csv('stock_dfs/{}.csv'.format(ticker))
            #else:
            #print('Already have {}'.format(ticker))

        except:
            print('No data available for {} ticker'.format(ticker))
            errors.append(ticker)
            pass
    print(errors)
    for e in errors:
        tickers.remove(e)

    print(tickers)
    with open("sp500tickers.pickle", "wb") as f:
        pickle.dump(tickers, f)
示例#33
0
def analyze_stock(pstock):

    days = 1000
    now = datetime.datetime.now()
    year_before = now - datetime.timedelta(days=days)

    stock = pstock.stock
    yf = YahooFinancials(stock.name)
    stock_analysis = StockAnalysis(yf)

    # update important fields
    stock.last_price = convert_number(yf.get_current_price())
    stock.price_sales_ratio = convert_number(yf.get_price_to_sales())
    stock.dividend_yield = convert_number(yf.get_annual_avg_div_yield())

    # historical_prices = yf.get_historical_price_data(stock.name,
    #     year_before.strftime('%Y%m%d'), now.strftime('%Y%m%d'), 'daily')

    #[['Date', 'Open', 'High', 'Low', 'Close', 'Volume', 'Adj Clos']
    # history = []
    # historical_prices = historical_prices[1:]
    # has_history = True
    # for p in historical_prices:
    #     # error index out of range here
    #     if(len(p) == 1):
    #         has_history = False
    #         break
    #     close_v = float(p[4])
    #     date = datetime.datetime.strptime(p[0],'%Y-%m-%d')
    #     history.append({"date":date, "close_value":close_v})

    # price_trends = []
    # volatilities = [
    #     {'days':100},
    #     {'days':300},
    #     {'days':600},
    # ]
    # if has_history:
    #     # today first
    #     history = sorted(history, key=lambda p: p['date'])
    #     assert history[1]['date'] > history[0]['date']

    #     for v in volatilities:
    #         if len(history) < v['days']:
    #             v['days'] = len(history)

    #         v['volatility'] = round(calculate_historical_volatility(history[-v['days']:]), 2)
    #         v['start_date'] = history[-v['days']:][0]['date']

    #     stock_analysis.volatility = volatilities[1]['volatility']
    #     stock.volatility = stock_analysis.volatility

    #     start = 0
    #     interval = int(len(history) / 5.0)
    #     while len(history) > (start + interval) and interval > 0:
    #         trend = calculate_historical_price_trend(history[start:start+interval])
    #         price_trends.append(trend)
    #         start = start + interval
    #         #if len(history) < (start + interval):
    #         #    interval = len(history) - start - 1

    #     trend = calculate_historical_price_trend(history)
    #     price_trends.append(trend)
    #     stock_analysis.trend = trend

    stock.value_score = stock_analysis.value_score_analysis()
    stock.save()

    return {
        #'price_trends':price_trends,
        'stock':stock,
        #'volatilities':volatilities,
        'stock_analysis':stock_analysis,
        'days':days,
        #'history': history
    }