Example #1
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('..')
Example #2
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
    }