<commit_before>from collections import defaultdict
from collections import namedtuple
from stock_analysis.logic import order_history
from stock_analysis.logic import price_history
from stock_analysis import constants


PortfolioStockDetails = namedtuple(
    'PortfolioStockDetails',
    ['ticker', 'price', 'gain1dp', 'gain1dv', 'gainp', 'gainv', 'portfoliop', 'value']
)

# Get 1d for comparisons (then 1wk, 1mo, 3mo, 1yr)


class PortfolioCommands(object):

    order_logic = order_history.OrderHistoryLogic()
    price_logic = price_history.PriceHistoryLogic()

    def get_portfolio_details(self, user_id):
        (end_date, start_date) = self.price_logic.get_dates_last_two_sessions()
        ticker_to_num_shares = self.order_logic.get_portfolio_shares_owned_on_date(user_id, end_date)
        tickers = sorted(ticker_to_num_shares.keys())

        dates = [start_date, end_date]
        price_info = self.price_logic.get_ticker_price_history_map(tickers, dates)
        purchase_value, _ = self.order_logic.get_ticker_total_purchased_sold(user_id)

        port_value = sum(price_info[ticker][end_date] * ticker_to_num_shares[ticker] for ticker in tickers)

        return [
            PortfolioStockDetails(
                ticker=ticker,
                price=price_info[ticker][end_date],
                gain1dp=100 * (price_info[ticker][end_date] - price_info[ticker][start_date]) /
                    price_info[ticker][start_date],
                gain1dv=ticker_to_num_shares[ticker] * (price_info[ticker][end_date] - price_info[ticker][start_date]),
                gainp=100 * (price_info[ticker][end_date] * ticker_to_num_shares[ticker] -
                    purchase_value[ticker]) / purchase_value[ticker],
                gainv=ticker_to_num_shares[ticker] * price_info[ticker][end_date] -
                purchase_value[ticker],
                portfoliop=100 * (ticker_to_num_shares[ticker] * price_info[ticker][end_date]) / port_value,
                value=ticker_to_num_shares[ticker] * price_info[ticker][end_date]
            )
            for ticker in tickers
            # TODO - 2 things - figure out problem with IJR and handle general case better
            if ticker != 'IJR'
        ]

    def get_benchmark_comparison_to_order_prices(self, user_id):
        """Each purchase order should be compared against what would've happened if a benchmark was bought instead"""
        # get every order (assume only buys for now - think about how to handle sold)
        orders = self.order_logic.get_orders_for_user(user_id)
        tickers = set([o.ticker for o in orders if o.order_type == order_history.BUY_ORDER_TYPE])
        tickers = list(tickers)
        order_tickers = list(tickers)
        tickers.extend(constants.BENCHMARK_TICKERS)
        dates = set([o.date for o in orders if o.order_type == order_history.BUY_ORDER_TYPE])
        historical_dates = self.price_logic.get_benchmark_history_dates()
        dates.update(historical_dates)
        ticker_date_price_map = self.price_logic.get_ticker_price_history_map(tickers, dates)

        curr_date = historical_dates[0]

        # compute the purchase date vs a decision not to purchase {each bench ticker}
        total_assets_purchased = sum(o.price for o in orders if o.order_type == order_history.BUY_ORDER_TYPE)
        order_comps = defaultdict(list)  # {ticker: [(order, gain, weight)]
        for order in orders:
            order_gain = 100 * (ticker_date_price_map[order.ticker][curr_date] - order.price) / order.price
            weight = order.price / total_assets_purchased
            gain_info = {}
            for comp_ticker in constants.BENCHMARK_TICKERS:
                comp_curr_price = ticker_date_price_map[comp_ticker][curr_date]
                comp_start_price = ticker_date_price_map[comp_ticker][order.date]
                comp_gain = 100 * (comp_curr_price - comp_start_price) / comp_start_price<commit_msg>No problem with IJR just bad data<commit_after>from collections import defaultdict
from collections import namedtuple
from stock_analysis.logic import order_history
from stock_analysis.logic import price_history
from stock_analysis import constants


PortfolioStockDetails = namedtuple(
    'PortfolioStockDetails',
    ['ticker', 'price', 'gain1dp', 'gain1dv', 'gainp', 'gainv', 'portfoliop', 'value']
)

# Get 1d for comparisons (then 1wk, 1mo, 3mo, 1yr)


class PortfolioCommands(object):

    order_logic = order_history.OrderHistoryLogic()
    price_logic = price_history.PriceHistoryLogic()

    def get_portfolio_details(self, user_id):
        (end_date, start_date) = self.price_logic.get_dates_last_two_sessions()
        ticker_to_num_shares = self.order_logic.get_portfolio_shares_owned_on_date(user_id, end_date)
        tickers = sorted(ticker_to_num_shares.keys())

        dates = [start_date, end_date]
        price_info = self.price_logic.get_ticker_price_history_map(tickers, dates)
        purchase_value, _ = self.order_logic.get_ticker_total_purchased_sold(user_id)

        port_value = sum(price_info[ticker][end_date] * ticker_to_num_shares[ticker] for ticker in tickers)

        return [
            PortfolioStockDetails(
                ticker=ticker,
                price=price_info[ticker][end_date],
                gain1dp=100 * (price_info[ticker][end_date] - price_info[ticker][start_date]) /
                    price_info[ticker][start_date],
                gain1dv=ticker_to_num_shares[ticker] * (price_info[ticker][end_date] - price_info[ticker][start_date]),
                gainp=100 * (price_info[ticker][end_date] * ticker_to_num_shares[ticker] -
                    purchase_value[ticker]) / purchase_value[ticker],
                gainv=ticker_to_num_shares[ticker] * price_info[ticker][end_date] -
                purchase_value[ticker],
                portfoliop=100 * (ticker_to_num_shares[ticker] * price_info[ticker][end_date]) / port_value,
                value=ticker_to_num_shares[ticker] * price_info[ticker][end_date]
            )
            for ticker in tickers
        ]

    def get_benchmark_comparison_to_order_prices(self, user_id):
        """Each purchase order should be compared against what would've happened if a benchmark was bought instead"""
        # get every order (assume only buys for now - think about how to handle sold)
        orders = self.order_logic.get_orders_for_user(user_id)
        tickers = set([o.ticker for o in orders if o.order_type == order_history.BUY_ORDER_TYPE])
        tickers = list(tickers)
        order_tickers = list(tickers)
        tickers.extend(constants.BENCHMARK_TICKERS)
        dates = set([o.date for o in orders if o.order_type == order_history.BUY_ORDER_TYPE])
        historical_dates = self.price_logic.get_benchmark_history_dates()
        dates.update(historical_dates)
        ticker_date_price_map = self.price_logic.get_ticker_price_history_map(tickers, dates)

        curr_date = historical_dates[0]

        # compute the purchase date vs a decision not to purchase {each bench ticker}
        total_assets_purchased = sum(o.price for o in orders if o.order_type == order_history.BUY_ORDER_TYPE)
        order_comps = defaultdict(list)  # {ticker: [(order, gain, weight)]
        for order in orders:
            order_gain = 100 * (ticker_date_price_map[order.ticker][curr_date] - order.price) / order.price
            weight = order.price / total_assets_purchased
            gain_info = {}
            for comp_ticker in constants.BENCHMARK_TICKERS:
                comp_curr_price = ticker_date_price_map[comp_ticker][curr_date]
                comp_start_price = ticker_date_price_map[comp_ticker][order.date]
                comp_gain = 100 * (comp_curr_price - comp_start_price) / comp_start_price
{'fraction_non_alphanumeric': 0.08577674897119342, 'fraction_numerical': 0.006558641975308642, 'mean_word_length': 3.256704980842912, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5280713', 'n_tokens_mistral': 2547, 'n_tokens_neox': 2430, 'n_words': 463}