Exemplo n.º 1
0
def test_new_trade_event(trading_mate):
    trade_dict = {
        "id": "new_trade",
        "date": "01/01/2020 00:00",
        "action": "DEPOSIT",
        "quantity": 1000,
        "symbol": "MSFT",
        "price": 1000,
        "fee": 10,
        "stamp_duty": 0.5,
        "notes": "some notes",
    }
    for pf in trading_mate.get_portfolios():
        trade = Trade.from_dict(trade_dict)
        trading_mate.new_trade_event(trade, pf.get_id())
    # Verify the trade has been added
    for pf in trading_mate.get_portfolios():
        assert pf.get_trade_history()[-1].id == "new_trade"
    # Verify invalid trade is rejected
    invalid_trade = {
        "id": "mock",
        "date": "01/01/2020 00:00",
        "action": "BUY",
        "quantity": 10000,
        "symbol": "MSFT",
        "price": 100000,
        "fee": 10,
        "stamp_duty": 0.5,
        "notes": "some notes",
    }
    for pf in trading_mate.get_portfolios():
        trade = Trade.from_dict(invalid_trade)
        with pytest.raises(RuntimeError):
            trading_mate.new_trade_event(trade, pf.get_id())
Exemplo n.º 2
0
def test_add_trade_invalid_past_date(portfolio):
    # Invalid buy due to too high cost
    item = {
        "id": "0",
        "date": "01/01/2018 00:00",
        "action": "BUY",
        "quantity": 1000,
        "symbol": "MOCK",
        "price": 1000.0,
        "fee": 1.0,
        "stamp_duty": 1.0,
        "notes": "mock",
    }
    with pytest.raises(RuntimeError):
        portfolio.add_trade(Trade.from_dict(item))
    # Invalid sell
    item = {
        "id": "0",
        "date": "02/01/2018 00:00",
        "action": "SELL",
        "quantity": 1990,
        "symbol": "MOCK13",
        "price": 1.0,
        "fee": 1.0,
        "stamp_duty": 1.0,
        "notes": "mock",
    }
    with pytest.raises(RuntimeError):
        portfolio.add_trade(Trade.from_dict(item))
    # Invalid withdraw
    item = {
        "id": "0",
        "date": "03/01/2018 00:00",
        "action": "WITHDRAW",
        "quantity": 20000,
        "symbol": "MOCK13",
        "price": 1.0,
        "fee": 1.0,
        "stamp_duty": 1.0,
        "notes": "mock",
    }
    with pytest.raises(RuntimeError):
        assert not portfolio.add_trade(Trade.from_dict(item))
    # Invalid fee
    item = {
        "id": "0",
        "date": "04/01/2018 00:00",
        "action": "FEE",
        "quantity": 20000,
        "symbol": "MOCK13",
        "price": 1.0,
        "fee": 1.0,
        "stamp_duty": 1.0,
        "notes": "mock",
    }
    with pytest.raises(RuntimeError):
        assert not portfolio.add_trade(Trade.from_dict(item))
Exemplo n.º 3
0
def test_has_unsaved_changes(portfolio):
    assert portfolio.has_unsaved_changes() is False
    item = {
        "id": "0",
        "date": "01/01/0001 00:00",
        "action": "DEPOSIT",
        "quantity": 1000,
        "symbol": "",
        "price": 0,
        "fee": 0,
        "stamp_duty": 0,
        "notes": "mock",
    }
    portfolio.add_trade(Trade.from_dict(item))
    assert portfolio.has_unsaved_changes() is True
    portfolio.save_portfolio("/tmp/TradingMate_test_portfolio.json")
    assert portfolio.has_unsaved_changes() is False
Exemplo n.º 4
0
def test_add_trade(dbh):
    """
    Test it adds the trade to the in memory list
    """
    prev_len = len(dbh.trading_history)
    item = {
        "id": "0",
        "date": "01/01/0001 00:00",
        "action": "BUY",
        "quantity": 1,
        "symbol": "MOCK",
        "price": 1.0,
        "fee": 1.0,
        "stamp_duty": 1.0,
        "notes": "hello",
    }
    trade = Trade.from_dict(item)
    dbh.add_trade(trade)
    assert len(dbh.trading_history) == prev_len + 1
Exemplo n.º 5
0
def test_add_trade_past_date(portfolio):
    # NOTE The dates in the mock items below needs to be sequencial
    # Valid buy
    item = {
        "id": "0",
        "date": "01/09/2018 00:00",
        "action": "BUY",
        "quantity": 1,
        "symbol": "MOCK",
        "price": 1.0,
        "fee": 0.0,
        "stamp_duty": 0.0,
        "notes": "mock",
    }
    portfolio.add_trade(Trade.from_dict(item))
    assert portfolio.get_holding_quantity("MOCK") == 1
    # Valid sell
    item = {
        "id": "0",
        "date": "02/09/2018 00:00",
        "action": "SELL",
        "quantity": 1,
        "symbol": "MOCK",
        "price": 1.0,
        "fee": 0.0,
        "stamp_duty": 0.0,
        "notes": "mock",
    }
    portfolio.add_trade(Trade.from_dict(item))
    assert portfolio.get_holding_quantity("MOCK") == 0
    # Valid deposit
    item = {
        "id": "0",
        "date": "03/09/2018 00:00",
        "action": "DEPOSIT",
        "quantity": 1000,
        "symbol": "",
        "price": 0,
        "fee": 0,
        "stamp_duty": 0,
        "notes": "mock",
    }
    portfolio.add_trade(Trade.from_dict(item))
    assert portfolio.get_cash_available() == PF_CASH_AVAILABLE + 1000
    # Valid withdraw
    item = {
        "id": "0",
        "date": "04/09/2018 00:00",
        "action": "WITHDRAW",
        "quantity": 1000,
        "symbol": "",
        "price": 0.0,
        "fee": 0.0,
        "stamp_duty": 0.0,
        "notes": "mock",
    }
    portfolio.add_trade(Trade.from_dict(item))
    assert portfolio.get_cash_available() == PF_CASH_AVAILABLE
    # Withdraws should not affect cash deposited
    assert portfolio.get_cash_deposited() == PF_CASH_DEPOSITED
    # Valid dividend
    item = {
        "id": "0",
        "date": "05/09/2018 00:00",
        "action": "DIVIDEND",
        "quantity": 1,
        "symbol": "MOCK13",
        "price": 0.0,
        "fee": 0.0,
        "stamp_duty": 0.0,
        "notes": "mock",
    }
    portfolio.add_trade(Trade.from_dict(item))
    assert portfolio.get_cash_available() == PF_CASH_AVAILABLE + 1
    # Dividends should not affect cash deposited
    assert portfolio.get_cash_deposited() == PF_CASH_DEPOSITED
    # Valid fee
    item = {
        "id": "0",
        "date": "06/09/2018 00:00",
        "action": "FEE",
        "quantity": 1,
        "symbol": "",
        "price": 0.0,
        "fee": 0.0,
        "stamp_duty": 0.0,
        "notes": "mock",
    }
    portfolio.add_trade(Trade.from_dict(item))
    assert portfolio.get_cash_available() == PF_CASH_AVAILABLE