Skip to content

Commit

Permalink
info updated to be json serialisable
Browse files Browse the repository at this point in the history
simulated info added
.json added to order
  • Loading branch information
liampauling committed Dec 21, 2020
1 parent ab25ef0 commit 2c74c21
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 10 deletions.
8 changes: 8 additions & 0 deletions flumine/backtest/simulated.py
Original file line number Diff line number Diff line change
Expand Up @@ -336,5 +336,13 @@ def status(self) -> str:
else:
return "EXECUTABLE"

@property
def info(self) -> dict:
return {
"profit": self.profit,
"piq": self._piq,
"matched": self.matched,
}

def __bool__(self):
return config.simulated or self.order.trade.client.paper_trade
6 changes: 6 additions & 0 deletions flumine/order/order.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import json
import uuid
import logging
import datetime
Expand Down Expand Up @@ -258,6 +259,7 @@ def info(self) -> dict:
"id": self.id,
"customer_order_ref": self.customer_order_ref,
"bet_id": self.bet_id,
"publish_time": str(self.publish_time) if self.publish_time else None,
"trade": self.trade.info,
"order_type": self.order_type.info,
"info": {
Expand All @@ -271,8 +273,12 @@ def info(self) -> dict:
},
"status": self.status.value if self.status else None,
"status_log": ", ".join([s.value for s in self.status_log]),
"simulated": self.simulated.info,
}

def json(self) -> str:
return json.dumps(self.info)

def __repr__(self):
return "Order {0}: {1}".format(
self.bet_id, self.status.value if self.status else None
Expand Down
6 changes: 3 additions & 3 deletions flumine/order/trade.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,9 +158,9 @@ def notes_str(self) -> str:
@property
def info(self) -> dict:
return {
"id": self.id,
"strategy": self.strategy,
"status": self.status,
"id": str(self.id),
"strategy": str(self.strategy),
"status": self.status.value if self.status else None,
"orders": [o.id for o in self.orders],
"notes": self.notes_str,
"market_notes": self.market_notes,
Expand Down
21 changes: 17 additions & 4 deletions tests/test_order.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@
class BaseOrderTest(unittest.TestCase):
def setUp(self) -> None:
mock_client = mock.Mock(paper_trade=False)
self.mock_trade = mock.Mock(client=mock_client)
self.mock_order_type = mock.Mock()
self.mock_trade = mock.Mock(
client=mock_client, market_id="1.1", selection_id=123, info={}
)
self.mock_order_type = mock.Mock(info={})
self.order = BaseOrder(self.mock_trade, "BACK", self.mock_order_type, 1)

def test_init(self):
Expand Down Expand Up @@ -253,9 +255,11 @@ def test_customer_order_ref(self):
class BetfairOrderTest(unittest.TestCase):
def setUp(self) -> None:
mock_client = mock.Mock(paper_trade=False)
self.mock_trade = mock.Mock(client=mock_client)
self.mock_trade = mock.Mock(
client=mock_client, market_id="1.1", selection_id=123, info={}
)
self.mock_status = mock.Mock()
self.mock_order_type = mock.Mock()
self.mock_order_type = mock.Mock(info={}, size=2.0, liability=2.0)
self.order = BetfairOrder(self.mock_trade, "BACK", self.mock_order_type)

def test_init(self):
Expand Down Expand Up @@ -480,6 +484,7 @@ def test_info(self):
"id": self.order.id,
"market_id": self.mock_trade.market_id,
"selection_id": self.mock_trade.selection_id,
"publish_time": None,
"status": None,
"status_log": "Pending, Execution complete",
"trade": self.mock_trade.info,
Expand All @@ -494,9 +499,17 @@ def test_info(self):
"average_price_matched": self.order.average_price_matched,
},
"customer_order_ref": self.order.customer_order_ref,
"simulated": {
"profit": 0.0,
"piq": 0.0,
"matched": [],
},
},
)

def test_json(self):
self.assertTrue(isinstance(self.order.json(), str))

def test_set_invalid_sep(self):
with self.assertRaises(ValueError):
self.order.sep = "@"
Expand Down
10 changes: 10 additions & 0 deletions tests/test_simulated.py
Original file line number Diff line number Diff line change
Expand Up @@ -551,6 +551,16 @@ def test_status(self):
self.mock_order.status.value = "PENDING"
self.assertEqual(self.simulated.status, "EXECUTABLE")

def test_info(self):
self.assertEqual(
self.simulated.info,
{
"profit": 0,
"piq": 0,
"matched": [],
},
)

def test_bool(self):
self.assertFalse(self.simulated)
from flumine import config
Expand Down
6 changes: 3 additions & 3 deletions tests/test_trade.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,10 +157,10 @@ def test_info(self):
self.assertEqual(
self.trade.info,
{
"id": self.trade.id,
"id": str(self.trade.id),
"orders": [],
"status": TradeStatus.LIVE,
"strategy": self.mock_strategy,
"status": "Live",
"strategy": str(self.mock_strategy),
"notes": "123",
"market_notes": None,
},
Expand Down

0 comments on commit 2c74c21

Please sign in to comment.