Tick
Tick Class
Market value at a given time representation.
A Tick
represents the market value of a financial instrument
at a given time.
Attributes:
Name | Type | Description |
---|---|---|
bid |
float |
bid value |
ask |
float |
ask value |
datetime |
arrow.Arrow |
datetime of open
(see |
meta |
Dict[str, Any] |
Dictionary free of use
(see |
ask: float
property
writable
Return the current instance ask.
Returns:
Type | Description |
---|---|
float |
tick ask value. |
bid: float
property
writable
Return the current instance bid.
Returns:
Type | Description |
---|---|
float |
tick bid value. |
datetime: Arrow
inherited
property
writable
Object datetime.
Returns:
Type | Description |
---|---|
Arrow |
object datetime |
datetime_utc: Arrow
inherited
property
readonly
Object datetime as UTC.
Returns:
Type | Description |
---|---|
Arrow |
datetime as UTC, this method is useful when datetime is zoned. |
spread: float
property
readonly
Return the difference between bid and ask.
Example
from datetime import datetime, timezone
from estrade import Tick
def test_value():
tick = Tick(
datetime.utcnow().replace(tzinfo=timezone.utc),
bid=99,
ask=101,
)
assert tick.spread == 2
Returns:
Type | Description |
---|---|
float |
spread value |
value: float
property
readonly
Return value in the middle between bid and ask.
Example
from datetime import datetime, timezone
from estrade import Tick
def test_value():
tick = Tick(
datetime.utcnow().replace(tzinfo=timezone.utc),
bid=49,
ask=50,
)
assert tick.value == 49.5
Returns:
Type | Description |
---|---|
float |
median value between bid and ask |
__init__(self, datetime, bid, ask, meta=None)
special
Instantiate a new tick object.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
datetime |
Union[datetime.datetime, arrow.arrow.Arrow] |
timezoned datetime |
required |
bid |
Union[float, int] |
tick bid value |
required |
ask |
Union[float, int] |
tick ask value |
required |
meta |
Optional[Dict[Any, Any]] |
free dictionary where you can store anything you need |
None |
Note
The Tick instance can be created with either a python datetime or an
Arrow
datetime.
from datetime import datetime, timezone
import arrow
from estrade import Tick
def test_python_datetime():
now = datetime.utcnow().replace(tzinfo=timezone.utc)
tick = Tick(
datetime=now,
bid=49,
ask=50,
)
assert tick.datetime == arrow.get(now)
def test_arrow_datetime():
now = arrow.utcnow()
tick = Tick(
datetime=now,
bid=49,
ask=50,
)
assert tick.datetime == now
Source code in estrade/tick.py
def __init__(
self,
datetime: Union[pydatetime, arrow.Arrow],
bid: Union[float, int],
ask: Union[float, int],
meta: Optional[Dict[Any, Any]] = None,
) -> None:
"""
Instantiate a new tick object.
Arguments:
datetime: timezoned datetime
bid: tick bid value
ask: tick ask value
meta: free dictionary where you can store anything you need
!!! note
The Tick instance can be created with either a python datetime or an
[`Arrow`](https://arrow.readthedocs.io/) datetime.
```python
--8<-- "tests/doc/reference/tick/test_datetime.py"
```
"""
TimedMixin.__init__(self, datetime)
self.ask = ask
self.bid = bid
if self.bid > self.ask:
raise TickException(f"Tick bid {bid} cannot be superior to ask {ask}")
MetaMixin.__init__(self, meta)
# add tick to epic
logger.debug(f"New tick : {self}")
__str__(self)
special
Return current instance as string.
Example
import arrow
from estrade import Tick
def test_convert_to_string():
now = arrow.utcnow()
tick = Tick(datetime=now, bid=999, ask=1001)
assert (
str(tick) == f"{now.strftime('%Y-%m-%d %H:%M:%S')} : 1000.0 "
f"(bid: 999.0, ask: 1001.0, spread: 2.0)"
)
Returns:
Type | Description |
---|---|
str |
String representation of tick (mainly used for logging) |
Source code in estrade/tick.py
def __str__(self) -> str:
"""
Return current instance as string.
!!! example
```python
--8<-- "tests/doc/reference/tick/test_str.py"
```
Returns:
String representation of tick (mainly used for logging)
"""
return (
f"{self.datetime.strftime('%Y-%m-%d %H:%M:%S')} : {self.value} "
f"(bid: {self.bid}, ask: {self.ask}, spread: {self.spread})"
)
asdict(self, datetime_to_str=False)
Convert Tick to dictionary (mainly used for reporting).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
datetime_to_str |
bool |
convert the tick datetime to string? |
False |
Returns:
Type | Description |
---|---|
Dict[str, Any] |
tick as dictionary containing |
Example
import arrow
from freezegun import freeze_time
from estrade import Tick
@freeze_time("2020-01-02 15:30:12")
def test_asdict():
now = arrow.utcnow()
tick = Tick(
now,
bid=49,
ask=50,
)
assert tick.asdict() == {
"ask": 50.0,
"bid": 49.0,
"datetime": now,
"spread": 1.0,
"value": 49.5,
}
assert tick.asdict(datetime_to_str=True) == {
"ask": 50.0,
"bid": 49.0,
"datetime": now.strftime("%Y-%m-%d %H:%M:%S"),
"spread": 1.0,
"value": 49.5,
}
Source code in estrade/tick.py
def asdict(self, datetime_to_str: bool = False) -> Dict[str, Any]:
"""
Convert Tick to dictionary (mainly used for reporting).
Arguments:
datetime_to_str: convert the tick datetime to string?
Returns:
tick as dictionary containing
!!! example
```python
--8<-- "tests/doc/reference/tick/test_asdict.py"
```
"""
return {
"datetime": self.datetime.strftime("%Y-%m-%d %H:%M:%S")
if datetime_to_str
else self.datetime,
"bid": self.bid,
"ask": self.ask,
"spread": self.spread,
"value": self.value,
}
check_value(v)
staticmethod
Check if bid/ask values are in either float or int.
Returns:
Type | Description |
---|---|
bool |
Is the bid/ask value of the correct type. |
Source code in estrade/tick.py
@staticmethod
def check_value(v: Any) -> bool:
"""
Check if bid/ask values are in either float or int.
Returns:
Is the bid/ask value of the correct type.
"""
if not isinstance(v, (int, float)):
return False
return True