Skip to content

Commit

Permalink
Fix for JalAccount.open_trades_list() method. FIFO test case was upda…
Browse files Browse the repository at this point in the history
…ted.
  • Loading branch information
titov-vv committed Feb 8, 2024
1 parent c95907b commit 8e21b32
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 6 deletions.
2 changes: 1 addition & 1 deletion jal/db/account.py
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ def open_trades_list(self, asset, timestamp=None) -> list:
trades = []
query = self._exec("WITH open_trades_numbered AS "
"(SELECT timestamp, op_type, operation_id, price, remaining_qty, "
"ROW_NUMBER() OVER (PARTITION BY op_type, operation_id ORDER BY timestamp DESC, op_type DESC) AS row_no "
"ROW_NUMBER() OVER (PARTITION BY op_type, operation_id ORDER BY timestamp DESC, id DESC) AS row_no "
"FROM trades_opened WHERE account_id=:account AND asset_id=:asset AND timestamp<=:timestamp) "
"SELECT op_type, operation_id, price, remaining_qty "
"FROM open_trades_numbered WHERE row_no=1 AND remaining_qty!=:zero "
Expand Down
22 changes: 17 additions & 5 deletions tests/test_ledger.py
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,7 @@ def test_fifo(prepare_db_fifo):
('N', 'N WITH STOCK DIVIDEND'),
('O', 'O SHARE'),
('P', 'P SHARE'),
('V', 'V SHARE')
]
create_stocks(test_assets, currency_id=2)

Expand Down Expand Up @@ -305,7 +306,14 @@ def test_fifo(prepare_db_fifo):
(1608714000, 1608757200, 18, 1.0, 2700.0, 0.0),
(1608717600, 1608757200, 18, -1.0, 3000.0, 0.0),
(1608721200, 1608757200, 18, -1.0, 2000.0, 0.0),
(1608724800, 1608757200, 18, 2.0, 2500.0, 0.0)
(1608724800, 1608757200, 18, 2.0, 2500.0, 0.0),
(d2t(210405), d2t(210406), 19, +5.0, 100, 0.0),
(d2t(210405), d2t(210406), 19, +12.0, 100, 0.0),
(d2t(210405), d2t(210406), 19, +8.0, 100, 0.0),
(d2t(210409), d2t(210410), 19, -20.0, 200, 0.0),
(d2t(210409), d2t(210410), 19, -5.0, 200, 0.0),
(d2t(210501), d2t(210502), 19, +10.0, 200, 0.0),
(d2t(210510), d2t(210511), 19, -10.0, 100, 0.0),
]
create_trades(1, test_trades)

Expand All @@ -315,9 +323,9 @@ def test_fifo(prepare_db_fifo):

trades = JalAccount(1).closed_trades_list()
# totals
assert len(trades) == 41
assert len([x for x in trades if x.open_operation().type() == LedgerTransaction.Trade and x.close_operation().type() == LedgerTransaction.Trade]) == 29
assert len([x for x in trades if x.open_operation().type() != LedgerTransaction.CorporateAction or x.close_operation().type() != LedgerTransaction.CorporateAction]) == 37
assert len(trades) == 46
assert len([x for x in trades if x.open_operation().type() == LedgerTransaction.Trade and x.close_operation().type() == LedgerTransaction.Trade]) == 34
assert len([x for x in trades if x.open_operation().type() != LedgerTransaction.CorporateAction or x.close_operation().type() != LedgerTransaction.CorporateAction]) == 42
assert len([x for x in trades if x.open_operation().type() == LedgerTransaction.CorporateAction and x.close_operation().type() == LedgerTransaction.CorporateAction]) == 4

# Check single deal
Expand Down Expand Up @@ -356,6 +364,10 @@ def test_fifo(prepare_db_fifo):
assert sum([x.profit() for x in trades]) == Decimal('3500')
assert sum([x.fee() for x in trades]) == Decimal('0')

trades = [x for x in JalAccount(1).closed_trades_list() if x.asset().id() == 19]
assert len(trades) == 5
assert [x.qty() for x in trades if x.asset().id() == 19] == [Decimal('5'), Decimal('12'), Decimal('3'), Decimal('5'), Decimal('10')]

# Symbol change
trades = [x for x in JalAccount(1).closed_trades_list() if x.asset().id() == 10]
assert len(trades) == 1
Expand Down Expand Up @@ -404,7 +416,7 @@ def test_fifo(prepare_db_fifo):
amounts = LedgerAmounts("amount_acc")
values = LedgerAmounts("value_acc")
assert amounts[BookAccount.Money, 1, 1] == Decimal('0')
assert amounts[BookAccount.Money, 1, 2] == Decimal('16700')
assert amounts[BookAccount.Money, 1, 2] == Decimal('18200')
for asset_id in range(4, 18):
assert values[BookAccount.Assets, 1, asset_id] == Decimal('0')

Expand Down

0 comments on commit 8e21b32

Please sign in to comment.