Exemplo n.º 1
0
def test_group_quotes_cherry_pick_invalid():
    group = Group()
    group.add_ticker("gme")
    with pytest.raises(ItemNotValidException):
        group.get_quotes(cherrypicks=["somethingDumb"])
    with pytest.raises(ItemNotValidException):
        group.get_quotes(cherrypicks=["somethingDumb"], exclude=True)
Exemplo n.º 2
0
def test_group_add_remove_ticker():
    group = Group([gme])
    assert len(group.tickers) == 1
    group.add_ticker("bb")
    assert len(group.tickers) == 2
    group.remove_ticker("gme")
    assert len(group.tickers) == 1
    with pytest.raises(TickerNotInGroupException):
        group.remove_ticker("ff")
Exemplo n.º 3
0
def handle_positions(
    pos_group: Group,
    pos_dict: dict,
    currency_wallet: CurrencyWallet,
    format_type: Union[discord.Embed, List],
):
    live_info = pos_group.get_quotes(
        cherrypicks=["symbol", "regularMarketPrice", "currency"]
    )

    for info in live_info:
        symbol = info.get("symbol")
        live = info.get("regularMarketPrice")
        amount = pos_dict.get(symbol).get("amount")
        book_value = pos_dict.get(symbol).get("book_value")
        average = pos_dict.get(symbol).get("average")
        currency = info.get("currency")
        live_total = live * amount
        pl, pl_percent = calculate_pl(live=live_total, book_value=book_value)
        pl = two_decimal(pl)
        pl_percent = two_decimal(pl_percent)
        currency_wallet.add_currency(
            currency=currency, book_value=book_value, live=live_total
        )
        if live > average:
            symbol = "+" + symbol
            pl = "+" + pl
            pl_percent = f"+{pl_percent}"
        elif average > live:
            symbol = "-" + symbol
        else:
            pass
        if isinstance(format_type, discord.Embed):
            format_type.add_field(
                name=f"**{symbol}**",
                value=f"> Amount: x {amount}\n"
                f"> Average Price: {two_decimal(average)}\n"
                f"> Live Price: {two_decimal(live)}\n"
                f"> Book Value: {two_decimal(book_value)}\n"
                f"> Current Total: {two_decimal(live_total)}\n"
                f"> P/L (%): {pl} ({pl_percent}%)\n"
                f"> Currency: {currency}",
            )
        else:
            format_type.append(
                [
                    symbol,
                    f"x {amount}",
                    two_decimal(average),
                    two_decimal(live),
                    two_decimal(book_value),
                    two_decimal(live_total),
                    f"{pl} ({pl_percent}%)",
                    currency,
                ]
            )
Exemplo n.º 4
0
def test_group_quotes_cherry_pick():
    group = Group()
    group.add_ticker("gme")
    data = group.get_quotes(cherrypicks=["shortName"])
    assert len(data.pop().keys()) == 1
    data = group.get_quotes(cherrypicks=["shortName", "longName"])
    assert len(data.pop().keys()) == 2
    data = group.get_quotes(cherrypicks=["shortName", "longName"],
                            exclude=True)
    assert len(data.pop().keys()) != 2
Exemplo n.º 5
0
def get_portfolio(user_id: str, username: str, mobile: bool):
    session = Session()
    try:
        user = get_user_or_create(session=session, user_id=user_id, username=username)
        user_id = user[0].id
        positions = session.query(db.Positions).filter_by(user_id=user_id).all()
        if not positions:
            raise NoPositionsException
        pf_list = (
            list()
            if not mobile
            else discord.Embed(
                title=f"{username}'s Portfolio", colour=discord.Colour.green()
            )
        )
        currency_wallet = CurrencyWallet()
        pos_group = Group()
        pos_dict = dict()

        for item in positions:
            symbol = (
                session.query(db.Symbols)
                .filter_by(symbol_id=item.symbol_id)
                .one()
                .symbol
            )
            pos_group.add_ticker(symbol)
            pos_dict[symbol] = dict(
                book_value=item.total_price,
                average=item.average_price,
                amount=item.amount,
            )

        handle_positions(
            pos_group=pos_group,
            pos_dict=pos_dict,
            currency_wallet=currency_wallet,
            format_type=pf_list,
        )
        if mobile:
            portfolio_table = pf_list
        else:
            headers = [
                "Symbol",
                "Amount",
                "Average Price",
                "Live Price",
                "Book Value",
                "Current Total",
                "P/L (%)",
                "Currency",
            ]
            pf_len = len(pf_list)
            if pf_len > 10:
                chunking = pf_len // 10
                if pf_len % 10:
                    chunking += 1
            else:
                chunking = 1
            portfolio_table = []
            for _ in range(chunking):
                chunk = pf_list[:10]
                portfolio_table.append(
                    tabulate(
                        chunk,
                        headers=headers,
                        disable_numparse=True,
                    )
                )
                del pf_list[:10]

        wallet_summary = currency_wallet.summary()
        if mobile:
            summary = discord.Embed(
                title=f"{username}'s Portfolio Summary", colour=discord.Colour.green()
            )
            summary_handler(wallet_summary, summary)
        else:
            summary = []
            summary_handler(wallet_summary, summary)
            summary = tabulate(
                summary,
                headers=["Currency", "Book Value", "Current Total", "P/L(%)"],
                stralign="left",
                disable_numparse=True,
            )
        return portfolio_table, summary
    finally:
        session.close()
Exemplo n.º 6
0
def test_group_quotes_invalid_ticker():
    with pytest.raises(DataRequestException):
        group = Group([gme, dumb])
        group.get_quotes()
Exemplo n.º 7
0
def test_group_quotes():
    group = Group([gme, bb])
    assert len(group.get_quotes()) == 2
Exemplo n.º 8
0
def test_group():
    group = Group()
    assert group
    group = Group([gme, bb])
    assert group