Compare commits
73 Commits
main
...
testing_ut
| Author | SHA1 | Date |
|---|---|---|
|
|
4f2fd73bb7 | |
|
|
40dca34fde | |
|
|
db77d7ab29 | |
|
|
8c274efd18 | |
|
|
0b123c9af9 | |
|
|
d17160519e | |
|
|
5bc7e4c9b6 | |
|
|
d35e1e5c67 | |
|
|
d4c10b2b0f | |
|
|
46285a601e | |
|
|
f9610c9e26 | |
|
|
9d5e405903 | |
|
|
e19a724037 | |
|
|
390a57c96d | |
|
|
69eac7bb15 | |
|
|
a45de0b710 | |
|
|
9df1988aa6 | |
|
|
f7caa75228 | |
|
|
e9613e46f6 | |
|
|
6637ca9e4f | |
|
|
7e139e6a8e | |
|
|
c2d9283db4 | |
|
|
28ba1392bb | |
|
|
f50202a6af | |
|
|
baff466ee0 | |
|
|
b01edcf65a | |
|
|
2545def7bb | |
|
|
1b74417688 | |
|
|
4d4f5d0af5 | |
|
|
7e82bf0729 | |
|
|
f1b4550483 | |
|
|
bdaf74a19a | |
|
|
b87ca76700 | |
|
|
94caa248e7 | |
|
|
da953b6b0c | |
|
|
fb8375f608 | |
|
|
d5faf4f59d | |
|
|
df5e72f7ae | |
|
|
bf33cb93b1 | |
|
|
d655e81290 | |
|
|
bc72e3d206 | |
|
|
35cb538a69 | |
|
|
8a768af5bb | |
|
|
8b0fac3b6c | |
|
|
36cc0cf750 | |
|
|
3ff0a86741 | |
|
|
705f0e86ac | |
|
|
2a24d1d50c | |
|
|
84ad34f51e | |
|
|
cbbf674737 | |
|
|
ec71dc2018 | |
|
|
17aebf44a9 | |
|
|
5f347c9f6a | |
|
|
cdb41e4881 | |
|
|
289b63bb2a | |
|
|
8f1e082c91 | |
|
|
b9321dbb49 | |
|
|
21d051b05f | |
|
|
3118d0f140 | |
|
|
4278d8e2f1 | |
|
|
b209512eb6 | |
|
|
8a9d21468a | |
|
|
75ddba09f7 | |
|
|
dae17bb043 | |
|
|
8bd0a182cf | |
|
|
04421e5ad2 | |
|
|
1e0c3da32d | |
|
|
5b87b3c2a6 | |
|
|
438e69e42c | |
|
|
ec6dd7cafc | |
|
|
f1436c93db | |
|
|
1061103f76 | |
|
|
3aea296caa |
|
|
@ -99,6 +99,8 @@ ENV/
|
|||
|
||||
# extra scripts dir
|
||||
/snippets
|
||||
# testing-utils aren't actively in use.
|
||||
/piker/_testing
|
||||
|
||||
# mypy
|
||||
.mypy_cache/
|
||||
|
|
|
|||
25
README.rst
25
README.rst
|
|
@ -93,38 +93,27 @@ bc why install with `python` when you can faster with `rust` ::
|
|||
# ^ astral's docs,
|
||||
# https://docs.astral.sh/uv/concepts/projects/sync/
|
||||
|
||||
include all GUIs (ex. for charting)::
|
||||
include all GUIs ::
|
||||
|
||||
uv sync --group uis
|
||||
uv sync --extra uis
|
||||
|
||||
AND with **all** our normal hacking tools::
|
||||
AND with all our hacking tools::
|
||||
|
||||
uv sync --dev
|
||||
uv sync --dev --extra uis
|
||||
|
||||
AND if you want to try WIP integrations::
|
||||
|
||||
uv sync --all-groups
|
||||
|
||||
Ensure you can run the root-daemon::
|
||||
|
||||
uv run pikerd [-l info --pdb]
|
||||
|
||||
|
||||
install on nix(os)
|
||||
******************
|
||||
hacky install on nixos
|
||||
**********************
|
||||
``NixOS`` is our core devs' distro of choice for which we offer
|
||||
a stringently defined development shell envoirment that can currently
|
||||
be applied in one of 2 ways::
|
||||
a stringently defined development shell envoirment that can be loaded with::
|
||||
|
||||
# ONLY if running on X11
|
||||
nix-shell default.nix
|
||||
|
||||
Or if you prefer flakes style and a modern DE::
|
||||
|
||||
# ONLY if also running on Wayland
|
||||
nix develop # for default bash
|
||||
nix develop -c uv run xonsh # for @goodboy's preferred sh B)
|
||||
|
||||
|
||||
start a chart
|
||||
*************
|
||||
|
|
|
|||
|
|
@ -1,5 +1,6 @@
|
|||
################
|
||||
# ---- CEXY ----
|
||||
|
||||
################
|
||||
[binance]
|
||||
accounts.paper = 'paper'
|
||||
|
||||
|
|
@ -12,41 +13,28 @@ accounts.spot = 'spot'
|
|||
spot.use_testnet = false
|
||||
spot.api_key = ''
|
||||
spot.api_secret = ''
|
||||
# ------ binance ------
|
||||
|
||||
|
||||
[deribit]
|
||||
# std assets
|
||||
key_id = ''
|
||||
key_secret = ''
|
||||
# options
|
||||
accounts.option = 'option'
|
||||
option.use_testnet = false
|
||||
option.key_id = ''
|
||||
option.key_secret = ''
|
||||
# aux logging from `cryptofeed`
|
||||
option.log.filename = 'cryptofeed.log'
|
||||
option.log.level = 'DEBUG'
|
||||
option.log.disabled = true
|
||||
# ------ deribit ------
|
||||
|
||||
|
||||
[kraken]
|
||||
key_descr = ''
|
||||
api_key = ''
|
||||
secret = ''
|
||||
# ------ kraken ------
|
||||
|
||||
|
||||
[kucoin]
|
||||
key_id = ''
|
||||
key_secret = ''
|
||||
key_passphrase = ''
|
||||
# ------ kucoin ------
|
||||
|
||||
|
||||
################
|
||||
# -- BROKERZ ---
|
||||
|
||||
################
|
||||
[questrade]
|
||||
refresh_token = ''
|
||||
access_token = ''
|
||||
|
|
@ -54,55 +42,44 @@ api_server = 'https://api06.iq.questrade.com/'
|
|||
expires_in = 1800
|
||||
token_type = 'Bearer'
|
||||
expires_at = 1616095326.355846
|
||||
# ------ questrade ------
|
||||
|
||||
|
||||
[ib]
|
||||
# define the (set of) host-port socketaddrs that
|
||||
# brokerd.ib will scan to connect to an API endpoint
|
||||
# (ib-gw or ib-tws listening instances)
|
||||
hosts = [
|
||||
'127.0.0.1',
|
||||
]
|
||||
# XXX: the order in which ports will be scanned
|
||||
# (by the `brokerd` daemon-actor)
|
||||
# is determined # by the line order here.
|
||||
# TODO: when we eventually spawn gateways in our
|
||||
# container, we can just dynamically allocate these
|
||||
# using IBC.
|
||||
ports = [
|
||||
4002, # gw
|
||||
7497, # tws
|
||||
]
|
||||
|
||||
# When API endpoints are being scanned durin startup, the order
|
||||
# of user-defined-account "names" (as defined below) here
|
||||
# determines which py-client connection is given priority to be
|
||||
# used for data-feed-requests by according to whichever client
|
||||
# connected to an API endpoing which reported the equivalent
|
||||
# account number for that name.
|
||||
# XXX: for a paper account the flex web query service
|
||||
# is not supported so you have to manually download
|
||||
# and XML report and put it in a location that can be
|
||||
# accessed by the ``brokerd.ib`` backend code for parsing.
|
||||
flex_token = ''
|
||||
flex_trades_query_id = '' # live account
|
||||
|
||||
# when clients are being scanned this determines
|
||||
# which clients are preferred to be used for data
|
||||
# feeds based on the order of account names, if
|
||||
# detected as active on an API client.
|
||||
prefer_data_account = [
|
||||
'paper',
|
||||
'margin',
|
||||
'ira',
|
||||
]
|
||||
|
||||
# For long-term trades txn (transaction) history
|
||||
# processing (i.e your txn ledger with IB) you can
|
||||
# (automatically for live accounts) query the FLEX
|
||||
# report system for past history.
|
||||
#
|
||||
# (For paper accounts the web query service
|
||||
# is not supported so you have to manually download
|
||||
# an XML report and put it in a location that can be
|
||||
# accessed by our `brokerd.ib` backend code for parsing).
|
||||
#
|
||||
flex_token = ''
|
||||
flex_trades_query_id = '' # live account
|
||||
|
||||
# define "aliases" (names) for each account number
|
||||
# such that the names can be reffed and logged throughout
|
||||
# `piker.accounting` subsys and more easily
|
||||
# referred to by the user.
|
||||
#
|
||||
# These keys will be the set exposed through the order-mode
|
||||
# account-selection UI so that numbers are never shown.
|
||||
[ib.accounts]
|
||||
paper = 'DU0000000' # <- literal account #
|
||||
margin = 'U0000000'
|
||||
ira = 'U0000000'
|
||||
# ------ ib ------
|
||||
# the order in which accounts will be selectable
|
||||
# in the order mode UI (if found via clients during
|
||||
# API-app scanning)when a new symbol is loaded.
|
||||
paper = 'XX0000000'
|
||||
margin = 'X0000000'
|
||||
ira = 'X0000000'
|
||||
|
|
|
|||
|
|
@ -1,9 +1,7 @@
|
|||
[network]
|
||||
pikerd = [
|
||||
'/ipv4/127.0.0.1/tcp/6116', # std localhost daemon-actor tree
|
||||
# '/uds/6116', # TODO std uds socket file
|
||||
]
|
||||
|
||||
tsdb.backend = 'marketstore'
|
||||
tsdb.host = 'localhost'
|
||||
tsdb.grpc_port = 5995
|
||||
|
||||
[ui]
|
||||
# set custom font + size which will scale entire UI
|
||||
|
|
|
|||
37
default.nix
37
default.nix
|
|
@ -11,12 +11,11 @@ let
|
|||
libxkbcommonStorePath = lib.getLib libxkbcommon;
|
||||
xcbutilcursorStorePath = lib.getLib xcb-util-cursor;
|
||||
|
||||
pypkgs = python313Packages;
|
||||
qtpyStorePath = lib.getLib pypkgs.qtpy;
|
||||
pyqt6StorePath = lib.getLib pypkgs.pyqt6;
|
||||
pyqt6SipStorePath = lib.getLib pypkgs.pyqt6-sip;
|
||||
rapidfuzzStorePath = lib.getLib pypkgs.rapidfuzz;
|
||||
qdarkstyleStorePath = lib.getLib pypkgs.qdarkstyle;
|
||||
qtpyStorePath = lib.getLib python312Packages.qtpy;
|
||||
pyqt6StorePath = lib.getLib python312Packages.pyqt6;
|
||||
pyqt6SipStorePath = lib.getLib python312Packages.pyqt6-sip;
|
||||
rapidfuzzStorePath = lib.getLib python312Packages.rapidfuzz;
|
||||
qdarkstyleStorePath = lib.getLib python312Packages.qdarkstyle;
|
||||
|
||||
xorgLibX11StorePath = lib.getLib xorg.libX11;
|
||||
xorgLibxcbStorePath = lib.getLib xorg.libxcb;
|
||||
|
|
@ -52,12 +51,12 @@ stdenv.mkDerivation {
|
|||
xorg.xcbutilrenderutil
|
||||
|
||||
# Python requirements.
|
||||
python313
|
||||
uv
|
||||
pypkgs.qdarkstyle
|
||||
pypkgs.rapidfuzz
|
||||
pypkgs.pyqt6
|
||||
pypkgs.qtpy
|
||||
python312Full
|
||||
python312Packages.uv
|
||||
python312Packages.qdarkstyle
|
||||
python312Packages.rapidfuzz
|
||||
python312Packages.pyqt6
|
||||
python312Packages.qtpy
|
||||
];
|
||||
src = null;
|
||||
shellHook = ''
|
||||
|
|
@ -114,11 +113,11 @@ stdenv.mkDerivation {
|
|||
|
||||
export LD_LIBRARY_PATH
|
||||
|
||||
RPDFUZZ_PATH="${rapidfuzzStorePath}/lib/python3.13/site-packages"
|
||||
QDRKSTYLE_PATH="${qdarkstyleStorePath}/lib/python3.13/site-packages"
|
||||
QTPY_PATH="${qtpyStorePath}/lib/python3.13/site-packages"
|
||||
PYQT6_PATH="${pyqt6StorePath}/lib/python3.13/site-packages"
|
||||
PYQT6_SIP_PATH="${pyqt6SipStorePath}/lib/python3.13/site-packages"
|
||||
RPDFUZZ_PATH="${rapidfuzzStorePath}/lib/python3.12/site-packages"
|
||||
QDRKSTYLE_PATH="${qdarkstyleStorePath}/lib/python3.12/site-packages"
|
||||
QTPY_PATH="${qtpyStorePath}/lib/python3.12/site-packages"
|
||||
PYQT6_PATH="${pyqt6StorePath}/lib/python3.12/site-packages"
|
||||
PYQT6_SIP_PATH="${pyqt6SipStorePath}/lib/python3.12/site-packages"
|
||||
|
||||
PATCH="$PATCH:$RPDFUZZ_PATH"
|
||||
PATCH="$PATCH:$QDRKSTYLE_PATH"
|
||||
|
|
@ -128,8 +127,8 @@ stdenv.mkDerivation {
|
|||
|
||||
export PATCH
|
||||
|
||||
# install all dev and extras
|
||||
uv sync --dev --all-extras
|
||||
# Install deps
|
||||
uv lock
|
||||
|
||||
'';
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,138 +1,30 @@
|
|||
running ``ib`` gateway in ``docker``
|
||||
------------------------------------
|
||||
We have a config based on a well maintained community
|
||||
image from `@gnzsnz`:
|
||||
We have a config based on the (now defunct)
|
||||
image from "waytrade":
|
||||
|
||||
https://github.com/gnzsnz/ib-gateway-docker
|
||||
https://github.com/waytrade/ib-gateway-docker
|
||||
|
||||
|
||||
To startup this image simply run the command::
|
||||
To startup this image with our custom settings
|
||||
simply run the command::
|
||||
|
||||
docker compose up
|
||||
|
||||
(For further usage^ see the official `docker-compose`_ docs)
|
||||
And you should have the following socket-available services:
|
||||
|
||||
- ``x11vnc1@127.0.0.1:3003``
|
||||
- ``ib-gw@127.0.0.1:4002``
|
||||
|
||||
And you should have the following socket-available services by
|
||||
default:
|
||||
You can attach to the container via a VNC client
|
||||
without password auth.
|
||||
|
||||
- ``x11vnc1 @ 127.0.0.1:5900``
|
||||
- ``ib-gw @ 127.0.0.1:4002``
|
||||
|
||||
You can now attach to the container via a VNC client with password-auth;
|
||||
here is an example using ``vncclient`` on ``linux``::
|
||||
|
||||
vncviewer localhost:5900
|
||||
|
||||
now enter the pw (password) you set via an (see second code blob)
|
||||
`.env file`_ or pw-file according to the `credentials section`_.
|
||||
|
||||
If you want to change away from their default config see the example
|
||||
`docker-compose.yml`-config issue and config-section of the readme,
|
||||
|
||||
- https://github.com/gnzsnz/ib-gateway-docker?tab=readme-ov-file#configuration
|
||||
- https://github.com/gnzsnz/ib-gateway-docker/discussions/103
|
||||
|
||||
.. _.env file: https://github.com/gnzsnz/ib-gateway-docker?tab=readme-ov-file#how-to-use-it
|
||||
.. _docker-compose: https://docs.docker.com/compose/
|
||||
.. _credentials section: https://github.com/gnzsnz/ib-gateway-docker?tab=readme-ov-file#credentials
|
||||
|
||||
|
||||
Connecting to the API from `piker`
|
||||
---------------------------------
|
||||
In order to expose the container's API endpoint to the
|
||||
`brokerd/datad/ib` actor, we need to add a section to the user's
|
||||
`brokers.toml` config (note the below is similar to the repo-shipped
|
||||
template file),
|
||||
|
||||
.. code:: toml
|
||||
|
||||
[ib]
|
||||
# define the (set of) host-port socketaddrs that
|
||||
# brokerd.ib will scan to connect to an API endpoint
|
||||
# (ib-gw or ib-tws listening instances)
|
||||
hosts = [
|
||||
'127.0.0.1',
|
||||
]
|
||||
ports = [
|
||||
4002, # gw
|
||||
7497, # tws
|
||||
]
|
||||
|
||||
# When API endpoints are being scanned durin startup, the order
|
||||
# of user-defined-account "names" (as defined below) here
|
||||
# determines which py-client connection is given priority to be
|
||||
# used for data-feed-requests by according to whichever client
|
||||
# connected to an API endpoing which reported the equivalent
|
||||
# account number for that name.
|
||||
prefer_data_account = [
|
||||
'paper',
|
||||
'margin',
|
||||
'ira',
|
||||
]
|
||||
|
||||
# define "aliases" (names) for each account number
|
||||
# such that the names can be reffed and logged throughout
|
||||
# `piker.accounting` subsys and more easily
|
||||
# referred to by the user.
|
||||
#
|
||||
# These keys will be the set exposed through the order-mode
|
||||
# account-selection UI so that numbers are never shown.
|
||||
[ib.accounts]
|
||||
paper = 'XX0000000'
|
||||
margin = 'X0000000'
|
||||
ira = 'X0000000'
|
||||
|
||||
|
||||
the broker daemon can also connect to the container's VNC server for
|
||||
added functionalies including,
|
||||
|
||||
- viewing the API endpoint program's GUI for manual interventions,
|
||||
- workarounds for historical data throttling using hotkey hacks,
|
||||
|
||||
Add a further section to `brokers.toml` which maps each API-ep's
|
||||
port to a table of VNC server connection info like,
|
||||
|
||||
.. code:: toml
|
||||
|
||||
[ib.vnc_addrs]
|
||||
4002 = {host = 'localhost', port = 5900, pw = 'doggy'}
|
||||
|
||||
The `pw = 'doggy'` here ^ should the same value as the particular
|
||||
container instances `.env` file setting (when it was run),
|
||||
|
||||
.. code:: ini
|
||||
|
||||
VNC_SERVER_PASSWORD='doggy'
|
||||
|
||||
|
||||
IF you also want to run ``TWS``
|
||||
-------------------------------
|
||||
You can also run it containerized,
|
||||
|
||||
https://github.com/gnzsnz/ib-gateway-docker?tab=readme-ov-file#using-tws
|
||||
|
||||
|
||||
SECURITY stuff (advanced, only if you're paranoid)
|
||||
--------------------------------------------------
|
||||
First and foremost if doing a "distributed" container setup where you
|
||||
run the ``ib-gw`` docker container and your connecting API client
|
||||
(likely ``ib_async`` from python) on **different hosts** be sure to
|
||||
read the `security considerations`_ section!
|
||||
|
||||
And for a further (somewhat paranoid) perspective from
|
||||
a long-time-ago serious devops eng..
|
||||
|
||||
Though "``ib``" claims they filter remote host connections outside
|
||||
``localhost`` (aka ``127.0.0.1`` on ipv4) it's prolly justified if
|
||||
you'd like to filter the socket at the *OS level* using a stateless
|
||||
firewall rule::
|
||||
SECURITY STUFF!?!?!
|
||||
-------------------
|
||||
Though "``ib``" claims they host filter connections outside
|
||||
localhost (aka ``127.0.0.1``) it's probably better if you filter
|
||||
the socket at the OS level using a stateless firewall rule::
|
||||
|
||||
ip rule add not unicast iif lo to 0.0.0.0/0 dport 4002
|
||||
|
||||
|
||||
We will soon have this either baked into our own custom derivative
|
||||
image (or patched into the current upstream one after further testin)
|
||||
but for now you'll have to do it urself, diggity dawg.
|
||||
|
||||
.. _security considerations: https://github.com/gnzsnz/ib-gateway-docker?tab=readme-ov-file#security-considerations
|
||||
We will soon have this baked into our own custom image but for
|
||||
now you'll have to do it urself dawgy.
|
||||
|
|
|
|||
|
|
@ -1,15 +1,10 @@
|
|||
# a community maintained IB API container!
|
||||
#
|
||||
# https://github.com/gnzsnz/ib-gateway-docker
|
||||
#
|
||||
# For piker we (currently) include some minor deviations
|
||||
# for some config files in the `volumes` section.
|
||||
#
|
||||
# See full configuration settings @
|
||||
# - https://github.com/gnzsnz/ib-gateway-docker?tab=readme-ov-file#configuration
|
||||
# - https://github.com/gnzsnz/ib-gateway-docker/discussions/103
|
||||
# rework from the original @
|
||||
# https://github.com/waytrade/ib-gateway-docker/blob/master/docker-compose.yml
|
||||
version: "3.5"
|
||||
|
||||
|
||||
services:
|
||||
|
||||
ib_gw_paper:
|
||||
|
||||
# apparently java is a mega cukc:
|
||||
|
|
@ -55,22 +50,16 @@ services:
|
|||
target: /root/scripts/run_x11_vnc.sh
|
||||
read_only: true
|
||||
|
||||
# NOTE: an alt method to fill these out is to
|
||||
# define an `.env` file in the same dir as
|
||||
# this compose file.
|
||||
# NOTE:to fill these out, define an `.env` file in the same dir as
|
||||
# this compose file which looks something like:
|
||||
# TWS_USERID='myuser'
|
||||
# TWS_PASSWORD='guest'
|
||||
environment:
|
||||
TWS_USERID: ${TWS_USERID}
|
||||
# TWS_USERID: 'myuser'
|
||||
TWS_PASSWORD: ${TWS_PASSWORD}
|
||||
# TWS_PASSWORD: 'guest'
|
||||
TRADING_MODE: ${TRADING_MODE}
|
||||
# TRADING_MODE: 'paper'
|
||||
VNC_SERVER_PASSWORD: ${VNC_SERVER_PASSWORD}
|
||||
# VNC_SERVER_PASSWORD: 'doggy'
|
||||
|
||||
# TODO, see if we can get this supported like it
|
||||
# was on the old `waytrade` image?
|
||||
# VNC_SERVER_PORT: '3003'
|
||||
TRADING_MODE: 'paper'
|
||||
VNC_SERVER_PASSWORD: 'doggy'
|
||||
VNC_SERVER_PORT: '3003'
|
||||
|
||||
# ports:
|
||||
# - target: 4002
|
||||
|
|
@ -87,9 +76,6 @@ services:
|
|||
# - "127.0.0.1:4002:4002"
|
||||
# - "127.0.0.1:5900:5900"
|
||||
|
||||
# TODO, a masked but working example of dual paper + live
|
||||
# ib-gw instances running in a single app run!
|
||||
#
|
||||
# ib_gw_live:
|
||||
# image: waytrade/ib-gateway:1012.2i
|
||||
# restart: no
|
||||
|
|
|
|||
|
|
@ -0,0 +1,338 @@
|
|||
#!/usr/bin/env python
|
||||
from decimal import (
|
||||
Decimal,
|
||||
)
|
||||
from pathlib import Path
|
||||
|
||||
import numpy as np
|
||||
# import polars as pl
|
||||
import trio
|
||||
import tractor
|
||||
from datetime import datetime
|
||||
# from pprint import pformat
|
||||
from piker.brokers.deribit.api import (
|
||||
get_client,
|
||||
maybe_open_oi_feed,
|
||||
)
|
||||
from piker.storage import open_storage_client, StorageClient
|
||||
from piker.log import get_logger
|
||||
import sys
|
||||
import pyqtgraph as pg
|
||||
from PyQt6 import QtCore
|
||||
from pyqtgraph import ScatterPlotItem, InfiniteLine
|
||||
from PyQt6.QtWidgets import QApplication
|
||||
from cryptofeed.symbols import Symbol
|
||||
|
||||
|
||||
log = get_logger(__name__)
|
||||
# XXX, use 2 newlines between top level LOC (even between these
|
||||
# imports and the next function line ;)
|
||||
|
||||
def check_if_complete(
|
||||
oi: dict[str, dict[str, Decimal | None]]
|
||||
) -> bool:
|
||||
return all(
|
||||
oi[strike]['C'] is not None
|
||||
and
|
||||
oi[strike]['P'] is not None for strike in oi
|
||||
)
|
||||
|
||||
|
||||
async def max_pain_daemon(
|
||||
) -> None:
|
||||
oi_by_strikes: dict[str, dict[str, Decimal | None]]
|
||||
instruments: list[Symbol] = []
|
||||
expiry_dates: list[str]
|
||||
expiry_date: str
|
||||
currency: str = 'btc'
|
||||
kind: str = 'option'
|
||||
|
||||
async with get_client(
|
||||
) as client:
|
||||
expiry_dates: list[str] = await client.get_expiration_dates(
|
||||
currency=currency,
|
||||
kind=kind
|
||||
)
|
||||
|
||||
log.info(
|
||||
f'Available expiries for {currency!r}-{kind}:\n'
|
||||
f'{expiry_dates}\n'
|
||||
)
|
||||
expiry_date: str = input(
|
||||
'Please enter a valid expiration date: '
|
||||
).upper()
|
||||
print('Starting little daemon...')
|
||||
|
||||
# maybe move this type annot down to the assignment line?
|
||||
oi_by_strikes: dict[str, dict[str, Decimal]]
|
||||
instruments = await client.get_instruments(
|
||||
expiry_date=expiry_date,
|
||||
)
|
||||
oi_by_strikes = client.get_strikes_dict(instruments)
|
||||
|
||||
|
||||
def get_total_intrinsic_values(
|
||||
oi_by_strikes: dict[str, dict[str, Decimal]]
|
||||
) -> dict[str, dict[str, Decimal]]:
|
||||
call_cash: Decimal = Decimal(0)
|
||||
put_cash: Decimal = Decimal(0)
|
||||
intrinsic_values: dict[str, dict[str, Decimal]] = {}
|
||||
closes: list = sorted(Decimal(close) for close in oi_by_strikes)
|
||||
|
||||
for strike, oi in oi_by_strikes.items():
|
||||
s = Decimal(strike)
|
||||
call_cash = sum(max(0, (s - c) * oi_by_strikes[str(c)]['C']) for c in closes)
|
||||
put_cash = sum(max(0, (c - s) * oi_by_strikes[str(c)]['P']) for c in closes)
|
||||
|
||||
intrinsic_values[strike] = {
|
||||
'C': call_cash,
|
||||
'P': put_cash,
|
||||
'total': call_cash + put_cash,
|
||||
}
|
||||
|
||||
return intrinsic_values
|
||||
|
||||
def get_intrinsic_value_and_max_pain(
|
||||
intrinsic_values: dict[str, dict[str, Decimal]]
|
||||
):
|
||||
# We meed to find the lowest value, so we start at
|
||||
# infinity to ensure that, and the max_pain must be
|
||||
# an amount greater than zero.
|
||||
total_intrinsic_value: Decimal = Decimal('Infinity')
|
||||
max_pain: Decimal = Decimal(0)
|
||||
|
||||
for strike, oi in oi_by_strikes.items():
|
||||
s = Decimal(strike)
|
||||
if intrinsic_values[strike]['total'] < total_intrinsic_value:
|
||||
total_intrinsic_value = intrinsic_values[strike]['total']
|
||||
max_pain = s
|
||||
|
||||
return total_intrinsic_value, max_pain
|
||||
|
||||
def plot_graph(
|
||||
oi_by_strikes: dict[str, dict[str, Decimal]],
|
||||
plot,
|
||||
):
|
||||
"""Update the bar graph with new open interest data."""
|
||||
plot.clear()
|
||||
|
||||
intrinsic_values = get_total_intrinsic_values(oi_by_strikes)
|
||||
|
||||
for strike_str in sorted(oi_by_strikes, key=lambda x: int(x)):
|
||||
strike = int(strike_str)
|
||||
calls_val = float(oi_by_strikes[strike_str]['C'])
|
||||
puts_val = float(oi_by_strikes[strike_str]['P'])
|
||||
|
||||
bar_c = pg.BarGraphItem(
|
||||
x=[strike - 100],
|
||||
height=[calls_val],
|
||||
width=200,
|
||||
pen='w',
|
||||
brush=(0, 0, 255, 150)
|
||||
)
|
||||
plot.addItem(bar_c)
|
||||
|
||||
bar_p = pg.BarGraphItem(
|
||||
x=[strike + 100],
|
||||
height=[puts_val],
|
||||
width=200,
|
||||
pen='w',
|
||||
brush=(255, 0, 0, 150)
|
||||
)
|
||||
plot.addItem(bar_p)
|
||||
|
||||
total_val = float(intrinsic_values[strike_str]['total']) / 100000
|
||||
|
||||
scatter_iv = ScatterPlotItem(
|
||||
x=[strike],
|
||||
y=[total_val],
|
||||
pen=pg.mkPen(color=(0, 255, 0), width=2),
|
||||
brush=pg.mkBrush(0, 255, 0, 150),
|
||||
size=3,
|
||||
symbol='o'
|
||||
)
|
||||
plot.addItem(scatter_iv)
|
||||
|
||||
_, max_pain = get_intrinsic_value_and_max_pain(intrinsic_values)
|
||||
|
||||
vertical_line = InfiniteLine(
|
||||
pos=max_pain,
|
||||
angle=90,
|
||||
pen=pg.mkPen(color='yellow', width=1, style=QtCore.Qt.PenStyle.DotLine),
|
||||
label=f'Max pain: {max_pain:,.0f}',
|
||||
labelOpts={
|
||||
'position': 0.85,
|
||||
'color': 'yellow',
|
||||
'movable': True
|
||||
}
|
||||
)
|
||||
plot.addItem(vertical_line)
|
||||
|
||||
def update_oi_by_strikes(msg: tuple):
|
||||
nonlocal oi_by_strikes
|
||||
if 'oi' == msg[0]:
|
||||
strike_price = msg[1]['strike_price']
|
||||
option_type = msg[1]['option_type']
|
||||
open_interest = msg[1]['open_interest']
|
||||
oi_by_strikes.setdefault(
|
||||
strike_price, {}
|
||||
).update(
|
||||
{option_type: open_interest}
|
||||
)
|
||||
|
||||
# Define the structured dtype
|
||||
dtype = np.dtype([
|
||||
('time', int),
|
||||
('oi', float),
|
||||
('oi_calc', float),
|
||||
])
|
||||
async def write_open_interest_on_file(msg: tuple, client: StorageClient):
|
||||
if 'oi' == msg[0]:
|
||||
nonlocal expiry_date
|
||||
timestamp = msg[1]['timestamp']
|
||||
strike_price = msg[1]["strike_price"]
|
||||
option_type = msg[1]['option_type'].lower()
|
||||
col_sym_key = f'btc-{expiry_date.lower()}-{strike_price}-{option_type}'
|
||||
|
||||
# Create the numpy array with sample data
|
||||
data = np.array([
|
||||
(
|
||||
int(timestamp),
|
||||
float(msg[1]['open_interest']),
|
||||
np.nan,
|
||||
),
|
||||
], dtype=dtype)
|
||||
|
||||
path: Path = await client.write_oi(
|
||||
col_sym_key,
|
||||
data,
|
||||
)
|
||||
# TODO, use std logging like this throughout for status
|
||||
# emissions on console!
|
||||
log.info(f'Wrote OI history to {path}')
|
||||
|
||||
def get_max_pain(
|
||||
oi_by_strikes: dict[str, dict[str, Decimal]]
|
||||
) -> dict[str, str | Decimal]:
|
||||
'''
|
||||
This method requires only the strike_prices and oi for call
|
||||
and puts, the closes list are the same as the strike_prices
|
||||
the idea is to sum all the calls and puts cash for each strike
|
||||
and the ITM strikes from that strike, the lowest value is what we
|
||||
are looking for the intrinsic value.
|
||||
|
||||
'''
|
||||
|
||||
nonlocal timestamp
|
||||
|
||||
intrinsic_values = get_total_intrinsic_values(oi_by_strikes)
|
||||
|
||||
total_intrinsic_value, max_pain = get_intrinsic_value_and_max_pain(intrinsic_values)
|
||||
|
||||
return {
|
||||
'timestamp': timestamp,
|
||||
'expiry_date': expiry_date,
|
||||
'total_intrinsic_value': total_intrinsic_value,
|
||||
'max_pain': max_pain,
|
||||
}
|
||||
|
||||
async with (
|
||||
open_storage_client() as (_, storage),
|
||||
|
||||
maybe_open_oi_feed(
|
||||
instruments,
|
||||
) as oi_feed,
|
||||
):
|
||||
# Initialize QApplication
|
||||
app = QApplication(sys.argv)
|
||||
|
||||
win = pg.GraphicsLayoutWidget(show=True)
|
||||
win.setWindowTitle('Calls (blue) vs Puts (red)')
|
||||
|
||||
plot = win.addPlot(title='OI by Strikes')
|
||||
plot.showGrid(x=True, y=True)
|
||||
print('Plot initialized...')
|
||||
|
||||
async for msg in oi_feed:
|
||||
|
||||
# In memory oi_by_strikes dict, all message are filtered here
|
||||
# and the dict is updated with the open interest data
|
||||
update_oi_by_strikes(msg)
|
||||
|
||||
# Write on file using storage client
|
||||
await write_open_interest_on_file(msg, storage)
|
||||
|
||||
# Max pain calcs, before start we must gather all the open interest for
|
||||
# all the strike prices and option types available for a expiration date
|
||||
if check_if_complete(oi_by_strikes):
|
||||
if 'oi' == msg[0]:
|
||||
# Here we must read for the filesystem all the latest open interest value for
|
||||
# each instrument for that specific expiration date, that means look up for the
|
||||
# last update got the instrument btc-{expity_date}-*oi1s.parquet (1s because is
|
||||
# hardcoded to something, sorry.)
|
||||
timestamp = msg[1]['timestamp']
|
||||
max_pain = get_max_pain(oi_by_strikes)
|
||||
# intrinsic_values = get_total_intrinsic_values(oi_by_strikes)
|
||||
|
||||
# graph here
|
||||
plot_graph(oi_by_strikes, plot)
|
||||
|
||||
# TODO, use a single multiline string with `()`
|
||||
# and drop the multiple `print()` calls (this
|
||||
# should be done elsewhere in this file as well!
|
||||
#
|
||||
# As per the docs,
|
||||
# https://docs.python.org/3/reference/lexical_analysis.html#string-literal-concatenation
|
||||
# you could instead do,
|
||||
# print(
|
||||
# '-----------------------------------------------\n'
|
||||
# f'timestamp: {datetime.fromtimestamp(max_pain['timestamp'])}\n'
|
||||
# )
|
||||
# WHY?
|
||||
# |_ less ctx-switches/calls to `print()`
|
||||
# |_ the `str` can then be modified / passed
|
||||
# around as a variable more easily if needed in
|
||||
# the future ;)
|
||||
#
|
||||
# ALSO, i believe there already is a stdlib
|
||||
# module to do "alignment" of text which you
|
||||
# could try for doing the right-side alignment,
|
||||
# https://docs.python.org/3/library/textwrap.html#textwrap.indent
|
||||
#
|
||||
print('-----------------------------------------------')
|
||||
print(f'timestamp: {datetime.fromtimestamp(max_pain['timestamp'])}')
|
||||
print(f'expiry_date: {max_pain['expiry_date']}')
|
||||
print(f'max_pain: {max_pain['max_pain']:,.0f}')
|
||||
print(f'total intrinsic value: {max_pain['total_intrinsic_value']:,.0f}')
|
||||
print('-----------------------------------------------')
|
||||
|
||||
# Process GUI events to keep the window responsive
|
||||
app.processEvents()
|
||||
|
||||
|
||||
async def main():
|
||||
|
||||
async with tractor.open_nursery(
|
||||
debug_mode=True,
|
||||
loglevel='info',
|
||||
) as an:
|
||||
from tractor import log
|
||||
log.get_console_log(level='info')
|
||||
|
||||
ptl: tractor.Portal = await an.start_actor(
|
||||
'max_pain_daemon',
|
||||
enable_modules=[__name__],
|
||||
infect_asyncio=True,
|
||||
# ^TODO, we can actually run this in the root-actor now
|
||||
# if needed as per 2nd "section" in,
|
||||
# https://pikers.dev/goodboy/tractor/pulls/2
|
||||
#
|
||||
# NOTE, will first require us porting to modern
|
||||
# `tractor:main` though ofc!
|
||||
|
||||
)
|
||||
await ptl.run(max_pain_daemon)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
trio.run(main)
|
||||
|
|
@ -0,0 +1,29 @@
|
|||
## Max Pain Calculation for Deribit Options
|
||||
|
||||
This feature, which calculates the max pain point for options traded
|
||||
on the Deribit exchange using cryptofeed library.
|
||||
|
||||
- Functions in the api module for fetching options data from Deribit.
|
||||
[commit](https://pikers.dev/pikers/piker/commit/da55856dd2876291f55a06eb0561438a912d8241)
|
||||
|
||||
- Compute the max pain point based on open interest data using
|
||||
deribit's api.
|
||||
[commit](https://pikers.dev/pikers/piker/commit/0d9d6e15ba0edeb662ec97f7599dd66af3046b94)
|
||||
|
||||
### How to test it?
|
||||
|
||||
**Before start:** in order to get this working with `uv`, you
|
||||
**must** use my [`tractor` fork](https://pikers.dev/ntorres/tractor/src/branch/aio_abandons)
|
||||
and this branch: `aio_abandons`, the reason is that I cherry-pick the
|
||||
`uv_migration` that guille made, for some reason that a didn't dive
|
||||
into, in my system y need tractor using `uv` too. quite hacky
|
||||
I guess.
|
||||
|
||||
1. `uv lock`
|
||||
|
||||
2. `uv run --no-dev python examples/max_pain.py`
|
||||
|
||||
3. A message should be display, enter one of the expiration date
|
||||
available.
|
||||
|
||||
4. The script should be up and running.
|
||||
127
flake.lock
127
flake.lock
|
|
@ -1,24 +1,135 @@
|
|||
{
|
||||
"nodes": {
|
||||
"nixpkgs": {
|
||||
"flake-utils": {
|
||||
"inputs": {
|
||||
"systems": "systems"
|
||||
},
|
||||
"locked": {
|
||||
"lastModified": 1765779637,
|
||||
"narHash": "sha256-KJ2wa/BLSrTqDjbfyNx70ov/HdgNBCBBSQP3BIzKnv4=",
|
||||
"owner": "nixos",
|
||||
"repo": "nixpkgs",
|
||||
"rev": "1306659b587dc277866c7b69eb97e5f07864d8c4",
|
||||
"lastModified": 1689068808,
|
||||
"narHash": "sha256-6ixXo3wt24N/melDWjq70UuHQLxGV8jZvooRanIHXw0=",
|
||||
"owner": "numtide",
|
||||
"repo": "flake-utils",
|
||||
"rev": "919d646de7be200f3bf08cb76ae1f09402b6f9b4",
|
||||
"type": "github"
|
||||
},
|
||||
"original": {
|
||||
"owner": "nixos",
|
||||
"owner": "numtide",
|
||||
"repo": "flake-utils",
|
||||
"type": "github"
|
||||
}
|
||||
},
|
||||
"flake-utils_2": {
|
||||
"inputs": {
|
||||
"systems": "systems_2"
|
||||
},
|
||||
"locked": {
|
||||
"lastModified": 1689068808,
|
||||
"narHash": "sha256-6ixXo3wt24N/melDWjq70UuHQLxGV8jZvooRanIHXw0=",
|
||||
"owner": "numtide",
|
||||
"repo": "flake-utils",
|
||||
"rev": "919d646de7be200f3bf08cb76ae1f09402b6f9b4",
|
||||
"type": "github"
|
||||
},
|
||||
"original": {
|
||||
"owner": "numtide",
|
||||
"repo": "flake-utils",
|
||||
"type": "github"
|
||||
}
|
||||
},
|
||||
"nix-github-actions": {
|
||||
"inputs": {
|
||||
"nixpkgs": [
|
||||
"poetry2nix",
|
||||
"nixpkgs"
|
||||
]
|
||||
},
|
||||
"locked": {
|
||||
"lastModified": 1688870561,
|
||||
"narHash": "sha256-4UYkifnPEw1nAzqqPOTL2MvWtm3sNGw1UTYTalkTcGY=",
|
||||
"owner": "nix-community",
|
||||
"repo": "nix-github-actions",
|
||||
"rev": "165b1650b753316aa7f1787f3005a8d2da0f5301",
|
||||
"type": "github"
|
||||
},
|
||||
"original": {
|
||||
"owner": "nix-community",
|
||||
"repo": "nix-github-actions",
|
||||
"type": "github"
|
||||
}
|
||||
},
|
||||
"nixpkgs": {
|
||||
"locked": {
|
||||
"lastModified": 1692174805,
|
||||
"narHash": "sha256-xmNPFDi/AUMIxwgOH/IVom55Dks34u1g7sFKKebxUm0=",
|
||||
"owner": "NixOS",
|
||||
"repo": "nixpkgs",
|
||||
"rev": "caac0eb6bdcad0b32cb2522e03e4002c8975c62e",
|
||||
"type": "github"
|
||||
},
|
||||
"original": {
|
||||
"owner": "NixOS",
|
||||
"ref": "nixos-unstable",
|
||||
"repo": "nixpkgs",
|
||||
"type": "github"
|
||||
}
|
||||
},
|
||||
"poetry2nix": {
|
||||
"inputs": {
|
||||
"flake-utils": "flake-utils_2",
|
||||
"nix-github-actions": "nix-github-actions",
|
||||
"nixpkgs": [
|
||||
"nixpkgs"
|
||||
]
|
||||
},
|
||||
"locked": {
|
||||
"lastModified": 1692048894,
|
||||
"narHash": "sha256-cDw03rso2V4CDc3Mll0cHN+ztzysAvdI8pJ7ybbz714=",
|
||||
"ref": "refs/heads/pyqt6",
|
||||
"rev": "b059ad4c3051f45d6c912e17747aae37a9ec1544",
|
||||
"revCount": 2276,
|
||||
"type": "git",
|
||||
"url": "file:///home/lord_fomo/repos/poetry2nix"
|
||||
},
|
||||
"original": {
|
||||
"type": "git",
|
||||
"url": "file:///home/lord_fomo/repos/poetry2nix"
|
||||
}
|
||||
},
|
||||
"root": {
|
||||
"inputs": {
|
||||
"nixpkgs": "nixpkgs"
|
||||
"flake-utils": "flake-utils",
|
||||
"nixpkgs": "nixpkgs",
|
||||
"poetry2nix": "poetry2nix"
|
||||
}
|
||||
},
|
||||
"systems": {
|
||||
"locked": {
|
||||
"lastModified": 1681028828,
|
||||
"narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=",
|
||||
"owner": "nix-systems",
|
||||
"repo": "default",
|
||||
"rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e",
|
||||
"type": "github"
|
||||
},
|
||||
"original": {
|
||||
"owner": "nix-systems",
|
||||
"repo": "default",
|
||||
"type": "github"
|
||||
}
|
||||
},
|
||||
"systems_2": {
|
||||
"locked": {
|
||||
"lastModified": 1681028828,
|
||||
"narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=",
|
||||
"owner": "nix-systems",
|
||||
"repo": "default",
|
||||
"rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e",
|
||||
"type": "github"
|
||||
},
|
||||
"original": {
|
||||
"owner": "nix-systems",
|
||||
"repo": "default",
|
||||
"type": "github"
|
||||
}
|
||||
}
|
||||
},
|
||||
|
|
|
|||
251
flake.nix
251
flake.nix
|
|
@ -1,103 +1,180 @@
|
|||
# An "impure" template thx to `pyproject.nix`,
|
||||
# https://pyproject-nix.github.io/pyproject.nix/templates.html#impure
|
||||
# https://github.com/pyproject-nix/pyproject.nix/blob/master/templates/impure/flake.nix
|
||||
{
|
||||
description = "An impure `piker` overlay using `uv` with Nix(OS)";
|
||||
# NOTE: to convert to a poetry2nix env like this here are the
|
||||
# steps:
|
||||
# - install poetry in your system nix config
|
||||
# - convert the repo to use poetry using `poetry init`:
|
||||
# https://python-poetry.org/docs/basic-usage/#initialising-a-pre-existing-project
|
||||
# - then manually ensuring all deps are converted over:
|
||||
# - add this file to the repo and commit it
|
||||
# -
|
||||
|
||||
inputs = {
|
||||
nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable";
|
||||
# GROKin tips:
|
||||
# - CLI eps are (ostensibly) added via an `entry_points.txt`:
|
||||
# - https://packaging.python.org/en/latest/specifications/entry-points/#file-format
|
||||
# - https://github.com/nix-community/poetry2nix/blob/master/editable.nix#L49
|
||||
{
|
||||
description = "piker: trading gear for hackers (pkged with poetry2nix)";
|
||||
|
||||
inputs.flake-utils.url = "github:numtide/flake-utils";
|
||||
inputs.nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
|
||||
|
||||
# see https://github.com/nix-community/poetry2nix/tree/master#api
|
||||
inputs.poetry2nix = {
|
||||
# url = "github:nix-community/poetry2nix";
|
||||
# url = "github:K900/poetry2nix/qt5-explicit-deps";
|
||||
url = "/home/lord_fomo/repos/poetry2nix";
|
||||
|
||||
inputs.nixpkgs.follows = "nixpkgs";
|
||||
};
|
||||
|
||||
outputs =
|
||||
{ nixpkgs, ... }:
|
||||
let
|
||||
inherit (nixpkgs) lib;
|
||||
forAllSystems = lib.genAttrs lib.systems.flakeExposed;
|
||||
in
|
||||
{
|
||||
devShells = forAllSystems (
|
||||
system:
|
||||
let
|
||||
pkgs = nixpkgs.legacyPackages.${system};
|
||||
outputs = {
|
||||
self,
|
||||
nixpkgs,
|
||||
flake-utils,
|
||||
poetry2nix,
|
||||
}:
|
||||
# TODO: build cross-OS and use the `${system}` var thingy..
|
||||
flake-utils.lib.eachDefaultSystem (system:
|
||||
let
|
||||
# use PWD as sources
|
||||
projectDir = ./.;
|
||||
pyproject = ./pyproject.toml;
|
||||
poetrylock = ./poetry.lock;
|
||||
|
||||
# do store-path extractions
|
||||
qt6baseStorePath = lib.getLib pkgs.qt6.qtbase;
|
||||
# ?TODO? can remove below since manual linking not needed?
|
||||
# qt6QtWaylandStorePath = lib.getLib pkgs.qt6.qtwayland;
|
||||
# TODO: port to 3.11 and support both versions?
|
||||
python = "python3.10";
|
||||
|
||||
# XXX NOTE XXX, for now we overlay specific pkgs via
|
||||
# a major-version-pinned-`cpython`
|
||||
cpython = "python313";
|
||||
pypkgs = pkgs."${cpython}Packages";
|
||||
in
|
||||
{
|
||||
default = pkgs.mkShell {
|
||||
# for more functions and examples.
|
||||
# inherit
|
||||
# (poetry2nix.legacyPackages.${system})
|
||||
# mkPoetryApplication;
|
||||
# pkgs = nixpkgs.legacyPackages.${system};
|
||||
|
||||
packages = with pkgs; [
|
||||
# XXX, ensure sh completions active!
|
||||
bashInteractive
|
||||
bash-completion
|
||||
pkgs = nixpkgs.legacyPackages.x86_64-linux;
|
||||
lib = pkgs.lib;
|
||||
p2npkgs = poetry2nix.legacyPackages.x86_64-linux;
|
||||
|
||||
# dev utils
|
||||
ruff
|
||||
pypkgs.ruff
|
||||
# define all pkg overrides per dep, see edgecases.md:
|
||||
# https://github.com/nix-community/poetry2nix/blob/master/docs/edgecases.md
|
||||
# TODO: add these into the json file:
|
||||
# https://github.com/nix-community/poetry2nix/blob/master/overrides/build-systems.json
|
||||
pypkgs-build-requirements = {
|
||||
asyncvnc = [ "setuptools" ];
|
||||
eventkit = [ "setuptools" ];
|
||||
ib-insync = [ "setuptools" "flake8" ];
|
||||
msgspec = [ "setuptools"];
|
||||
pdbp = [ "setuptools" ];
|
||||
pyqt6-sip = [ "setuptools" ];
|
||||
tabcompleter = [ "setuptools" ];
|
||||
tractor = [ "setuptools" ];
|
||||
tricycle = [ "setuptools" ];
|
||||
trio-typing = [ "setuptools" ];
|
||||
trio-util = [ "setuptools" ];
|
||||
xonsh = [ "setuptools" ];
|
||||
};
|
||||
|
||||
qt6.qtwayland
|
||||
qt6.qtbase
|
||||
# auto-generate override entries
|
||||
p2n-overrides = p2npkgs.defaultPoetryOverrides.extend (self: super:
|
||||
builtins.mapAttrs (package: build-requirements:
|
||||
(builtins.getAttr package super).overridePythonAttrs (old: {
|
||||
buildInputs = (
|
||||
old.buildInputs or [ ]
|
||||
) ++ (
|
||||
builtins.map (
|
||||
pkg: if builtins.isString pkg then builtins.getAttr pkg super else pkg
|
||||
) build-requirements
|
||||
);
|
||||
})
|
||||
) pypkgs-build-requirements
|
||||
);
|
||||
|
||||
uv
|
||||
python313 # ?TODO^ how to set from `cpython` above?
|
||||
pypkgs.pyqt6
|
||||
pypkgs.pyqt6-sip
|
||||
pypkgs.qtpy
|
||||
pypkgs.qdarkstyle
|
||||
pypkgs.rapidfuzz
|
||||
];
|
||||
# override some ahead-of-time compiled extensions
|
||||
# to be built with their wheels.
|
||||
ahot_overrides = p2n-overrides.extend(
|
||||
final: prev: {
|
||||
|
||||
shellHook = ''
|
||||
# unmask to debug **this** dev-shell-hook
|
||||
# set -e
|
||||
# llvmlite = prev.llvmlite.override {
|
||||
# preferWheel = false;
|
||||
# };
|
||||
|
||||
# set qt-base/plugin path(s)
|
||||
QTBASE_PATH="${qt6baseStorePath}/lib"
|
||||
QT_PLUGIN_PATH="${qt6baseStorePath}/lib/qt-6/plugins"
|
||||
QT_QPA_PLATFORM_PLUGIN_PATH="$QT_PLUGIN_PATH/platforms"
|
||||
# TODO: get this workin with p2n and nixpkgs..
|
||||
# pyqt6 = prev.pyqt6.override {
|
||||
# preferWheel = true;
|
||||
# };
|
||||
|
||||
# link in Qt cc lib paths from <nixpkgs>
|
||||
LD_LIBRARY_PATH="$LD_LIBRARY_PATH:$QTBASE_PATH"
|
||||
LD_LIBRARY_PATH="$LD_LIBRARY_PATH:$QT_PLUGIN_PATH"
|
||||
LD_LIBRARY_PATH="$LD_LIBRARY_PATH:$QT_QPA_PLATFORM_PLUGIN_PATH"
|
||||
# NOTE: this DOESN'T work atm but after a fix
|
||||
# to poetry2nix, it will and actually this line
|
||||
# won't be needed - thanks @k900:
|
||||
# https://github.com/nix-community/poetry2nix/pull/1257
|
||||
pyqt5 = prev.pyqt5.override {
|
||||
# withWebkit = false;
|
||||
preferWheel = true;
|
||||
};
|
||||
|
||||
# link-in c++ stdlib for various AOT-ext-pkgs (numpy, etc.)
|
||||
LD_LIBRARY_PATH="${pkgs.stdenv.cc.cc.lib}/lib:$LD_LIBRARY_PATH"
|
||||
# see PR from @k900:
|
||||
# https://github.com/nix-community/poetry2nix/pull/1257
|
||||
# pyqt5-qt5 = prev.pyqt5-qt5.override {
|
||||
# withWebkit = false;
|
||||
# preferWheel = true;
|
||||
# };
|
||||
|
||||
export LD_LIBRARY_PATH
|
||||
|
||||
# RUNTIME-SETTINGS
|
||||
#
|
||||
# ------ Qt ------
|
||||
# XXX, unmask to debug qt .so linking/loading deats
|
||||
# export QT_DEBUG_PLUGINS=1
|
||||
#
|
||||
# ALSO, for *modern linux* DEs,
|
||||
# - maybe set wayland-mode (TODO, parametrtize this!)
|
||||
# * a chosen wayland-mode shell-integration
|
||||
export QT_QPA_PLATFORM="wayland"
|
||||
export QT_WAYLAND_SHELL_INTEGRATION="xdg-shell"
|
||||
|
||||
# ------ uv ------
|
||||
# - always use the ./py313/ venv-subdir
|
||||
export UV_PROJECT_ENVIRONMENT="py313"
|
||||
# sync project-env with all extras
|
||||
uv sync --dev --all-extras --no-group lint
|
||||
|
||||
# ------ TIPS ------
|
||||
# NOTE, to launch the py-venv installed `xonsh` (like @goodboy)
|
||||
# run the `nix develop` cmd with,
|
||||
# >> nix develop -c uv run xonsh
|
||||
'';
|
||||
};
|
||||
}
|
||||
# TODO: patch in an override for polars to build
|
||||
# from src! See the details likely needed from
|
||||
# the cryptography entry:
|
||||
# https://github.com/nix-community/poetry2nix/blob/master/overrides/default.nix#L426-L435
|
||||
polars = prev.polars.override {
|
||||
preferWheel = true;
|
||||
};
|
||||
}
|
||||
);
|
||||
};
|
||||
|
||||
# WHY!? -> output-attrs that `nix develop` scans for:
|
||||
# https://nixos.org/manual/nix/stable/command-ref/new-cli/nix3-develop.html#flake-output-attributes
|
||||
in
|
||||
rec {
|
||||
packages = {
|
||||
# piker = poetry2nix.legacyPackages.x86_64-linux.mkPoetryEditablePackage {
|
||||
# editablePackageSources = { piker = ./piker; };
|
||||
|
||||
piker = p2npkgs.mkPoetryApplication {
|
||||
projectDir = projectDir;
|
||||
|
||||
# SEE ABOVE for auto-genned input set, override
|
||||
# buncha deps with extras.. like `setuptools` mostly.
|
||||
# TODO: maybe propose a patch to p2n to show that you
|
||||
# can even do this in the edgecases docs?
|
||||
overrides = ahot_overrides;
|
||||
|
||||
# XXX: won't work on llvmlite..
|
||||
# preferWheels = true;
|
||||
};
|
||||
};
|
||||
|
||||
# devShells.default = pkgs.mkShell {
|
||||
# projectDir = projectDir;
|
||||
# python = "python3.10";
|
||||
# overrides = ahot_overrides;
|
||||
# inputsFrom = [ self.packages.x86_64-linux.piker ];
|
||||
# packages = packages;
|
||||
# # packages = [ poetry2nix.packages.${system}.poetry ];
|
||||
# };
|
||||
|
||||
# TODO: grok the difference here..
|
||||
# - avoid re-cloning git repos on every develop entry..
|
||||
# - ideally allow hacking on the src code of some deps
|
||||
# (tractor, pyqtgraph, tomlkit, etc.) WITHOUT having to
|
||||
# re-install them every time a change is made.
|
||||
# - boot a usable xonsh inside the poetry virtualenv when
|
||||
# defined via a custom entry point?
|
||||
devShells.default = p2npkgs.mkPoetryEnv {
|
||||
# env = p2npkgs.mkPoetryEnv {
|
||||
projectDir = projectDir;
|
||||
python = pkgs.python310;
|
||||
overrides = ahot_overrides;
|
||||
editablePackageSources = packages;
|
||||
# piker = "./";
|
||||
# tractor = "../tractor/";
|
||||
# }; # wut?
|
||||
};
|
||||
}
|
||||
); # end of .outputs scope
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,81 @@
|
|||
# manual label testing cruft
|
||||
import trio
|
||||
import tractor
|
||||
|
||||
|
||||
async def test_bed(
|
||||
ohlcv,
|
||||
chart,
|
||||
lc,
|
||||
):
|
||||
from ._graphics._lines import order_line
|
||||
|
||||
sleep = 6
|
||||
|
||||
# from PyQt5.QtCore import QPointF
|
||||
vb = chart._vb
|
||||
# scene = vb.scene()
|
||||
|
||||
# raxis = chart.getAxis('right')
|
||||
# vb_right = vb.boundingRect().right()
|
||||
|
||||
last, i_end = ohlcv.array[-1][['close', 'index']]
|
||||
|
||||
line = order_line(
|
||||
chart,
|
||||
level=last,
|
||||
level_digits=2
|
||||
)
|
||||
# eps = line.getEndpoints()
|
||||
|
||||
# llabel = line._labels[1][1]
|
||||
|
||||
line.update_labels({'level': last})
|
||||
return
|
||||
|
||||
# rl = eps[1]
|
||||
# rlabel.setPos(rl)
|
||||
|
||||
# ti = pg.TextItem(text='Fuck you')
|
||||
# ti.setPos(pg.Point(i_end, last))
|
||||
# ti.setParentItem(line)
|
||||
# ti.setAnchor(pg.Point(1, 1))
|
||||
# vb.addItem(ti)
|
||||
# chart.plotItem.addItem(ti)
|
||||
|
||||
from ._label import Label
|
||||
|
||||
txt = Label(
|
||||
vb,
|
||||
fmt_str='fuck {it}',
|
||||
)
|
||||
txt.format(it='boy')
|
||||
txt.place_on_scene('left')
|
||||
txt.set_view_y(last)
|
||||
|
||||
# txt = QtGui.QGraphicsTextItem()
|
||||
# txt.setPlainText("FUCK YOU")
|
||||
# txt.setFont(_font.font)
|
||||
# txt.setDefaultTextColor(pg.mkColor(hcolor('bracket')))
|
||||
# # txt.setParentItem(vb)
|
||||
# w = txt.boundingRect().width()
|
||||
# scene.addItem(txt)
|
||||
|
||||
# txt.setParentItem(line)
|
||||
# d_coords = vb.mapFromView(QPointF(i_end, last))
|
||||
# txt.setPos(vb_right - w, d_coords.y())
|
||||
# txt.show()
|
||||
# txt.update()
|
||||
|
||||
# rlabel.setPos(vb_right - 2*w, d_coords.y())
|
||||
# rlabel.show()
|
||||
|
||||
i = 0
|
||||
while True:
|
||||
await trio.sleep(sleep)
|
||||
await tractor.breakpoint()
|
||||
txt.format(it=f'dog_{i}')
|
||||
# d_coords = vb.mapFromView(QPointF(i_end, last))
|
||||
# txt.setPos(vb_right - w, d_coords.y())
|
||||
# txt.setPlainText(f"FUCK YOU {i}")
|
||||
i += 1
|
||||
|
|
@ -33,6 +33,7 @@ from ._pos import (
|
|||
Account,
|
||||
load_account,
|
||||
load_account_from_ledger,
|
||||
open_pps,
|
||||
open_account,
|
||||
Position,
|
||||
)
|
||||
|
|
@ -67,6 +68,7 @@ __all__ = [
|
|||
'load_account_from_ledger',
|
||||
'mk_allocator',
|
||||
'open_account',
|
||||
'open_pps',
|
||||
'open_trade_ledger',
|
||||
'unpack_fqme',
|
||||
'DerivTypes',
|
||||
|
|
|
|||
|
|
@ -40,7 +40,7 @@ import tomli_w # for fast ledger writing
|
|||
|
||||
from piker.types import Struct
|
||||
from piker import config
|
||||
from piker.log import get_logger
|
||||
from ..log import get_logger
|
||||
from .calc import (
|
||||
iter_by_dt,
|
||||
)
|
||||
|
|
@ -239,9 +239,7 @@ class TransactionLedger(UserDict):
|
|||
|
||||
symcache: SymbologyCache = self._symcache
|
||||
towrite: dict[str, Any] = {}
|
||||
for tid, txdict in self.tx_sort(
|
||||
self.data.copy()
|
||||
):
|
||||
for tid, txdict in self.tx_sort(self.data.copy()):
|
||||
# write blank-str expiry for non-expiring assets
|
||||
if (
|
||||
'expiry' in txdict
|
||||
|
|
@ -379,7 +377,7 @@ def open_trade_ledger(
|
|||
account,
|
||||
dirpath=_fp,
|
||||
)
|
||||
cpy: dict = ledger_dict.copy()
|
||||
cpy = ledger_dict.copy()
|
||||
|
||||
# XXX NOTE: if not provided presume we are being called from
|
||||
# sync code and need to maybe run `trio` to generate..
|
||||
|
|
@ -408,13 +406,7 @@ def open_trade_ledger(
|
|||
account=account,
|
||||
mod=mod,
|
||||
symcache=symcache,
|
||||
|
||||
# NOTE: allow backends to provide custom ledger sorting
|
||||
tx_sort=getattr(
|
||||
mod,
|
||||
'tx_sort',
|
||||
tx_sort,
|
||||
),
|
||||
tx_sort=getattr(mod, 'tx_sort', tx_sort),
|
||||
)
|
||||
try:
|
||||
yield ledger
|
||||
|
|
|
|||
|
|
@ -305,8 +305,8 @@ class MktPair(Struct, frozen=True):
|
|||
# config right?
|
||||
# src_type: AssetTypeName
|
||||
|
||||
# for derivs, info describing contract, egs. strike price, call
|
||||
# or put, swap type, exercise model, etc.
|
||||
# for derivs, info describing contract, egs.
|
||||
# strike price, call or put, swap type, exercise model, etc.
|
||||
contract_info: list[str] | None = None
|
||||
|
||||
# TODO: rename to sectype since all of these can
|
||||
|
|
|
|||
|
|
@ -30,8 +30,7 @@ from types import ModuleType
|
|||
from typing import (
|
||||
Any,
|
||||
Iterator,
|
||||
Generator,
|
||||
TYPE_CHECKING,
|
||||
Generator
|
||||
)
|
||||
|
||||
import pendulum
|
||||
|
|
@ -60,10 +59,8 @@ from ..clearing._messages import (
|
|||
BrokerdPosition,
|
||||
)
|
||||
from piker.types import Struct
|
||||
from piker.log import get_logger
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from piker.data._symcache import SymbologyCache
|
||||
from piker.data._symcache import SymbologyCache
|
||||
from ..log import get_logger
|
||||
|
||||
log = get_logger(__name__)
|
||||
|
||||
|
|
@ -356,12 +353,13 @@ class Position(Struct):
|
|||
) -> bool:
|
||||
'''
|
||||
Update clearing table by calculating the rolling ppu and
|
||||
(accumulative) size in both the clears entry and local attrs
|
||||
state.
|
||||
(accumulative) size in both the clears entry and local
|
||||
attrs state.
|
||||
|
||||
Inserts are always done in datetime sorted order.
|
||||
|
||||
'''
|
||||
# added: bool = False
|
||||
tid: str = t.tid
|
||||
if tid in self._events:
|
||||
log.debug(
|
||||
|
|
@ -369,7 +367,7 @@ class Position(Struct):
|
|||
f'\n'
|
||||
f'{t}\n'
|
||||
)
|
||||
return False
|
||||
# return added
|
||||
|
||||
# TODO: apparently this IS possible with a dict but not
|
||||
# common and probably not that beneficial unless we're also
|
||||
|
|
@ -450,12 +448,6 @@ class Position(Struct):
|
|||
# def suggest_split(self) -> float:
|
||||
# ...
|
||||
|
||||
# ?TODO, for sending rendered state over the wire?
|
||||
# def summary(self) -> PositionSummary:
|
||||
# do minimal conversion to a subset of fields
|
||||
# currently defined in `.clearing._messages.BrokerdPosition`
|
||||
|
||||
|
||||
|
||||
class Account(Struct):
|
||||
'''
|
||||
|
|
@ -499,23 +491,12 @@ class Account(Struct):
|
|||
|
||||
def update_from_ledger(
|
||||
self,
|
||||
ledger: TransactionLedger|dict[str, Transaction],
|
||||
ledger: TransactionLedger | dict[str, Transaction],
|
||||
cost_scalar: float = 2,
|
||||
symcache: SymbologyCache|None = None,
|
||||
symcache: SymbologyCache | None = None,
|
||||
|
||||
_mktmap_table: dict[str, MktPair] | None = None,
|
||||
|
||||
only_require: list[str]|True = True,
|
||||
# ^list of fqmes that are "required" to be processed from
|
||||
# this ledger pass; we often don't care about others and
|
||||
# definitely shouldn't always error in such cases.
|
||||
# (eg. broker backend loaded that doesn't yet supsport the
|
||||
# symcache but also, inside the paper engine we don't ad-hoc
|
||||
# request `get_mkt_info()` for every symbol in the ledger,
|
||||
# only the one for which we're simulating against).
|
||||
# TODO, not sure if there's a better soln for this, ideally
|
||||
# all backends get symcache support afap i guess..
|
||||
|
||||
) -> dict[str, Position]:
|
||||
'''
|
||||
Update the internal `.pps[str, Position]` table from input
|
||||
|
|
@ -558,32 +539,11 @@ class Account(Struct):
|
|||
if _mktmap_table is None:
|
||||
raise
|
||||
|
||||
required: bool = (
|
||||
only_require is True
|
||||
or (
|
||||
only_require is not True
|
||||
and
|
||||
fqme in only_require
|
||||
)
|
||||
)
|
||||
# XXX: caller is allowed to provide a fallback
|
||||
# mktmap table for the case where a new position is
|
||||
# being added and the preloaded symcache didn't
|
||||
# have this entry prior (eg. with frickin IB..)
|
||||
if (
|
||||
not (mkt := _mktmap_table.get(fqme))
|
||||
and
|
||||
required
|
||||
):
|
||||
raise
|
||||
|
||||
elif not required:
|
||||
continue
|
||||
|
||||
else:
|
||||
# should be an entry retreived somewhere
|
||||
assert mkt
|
||||
|
||||
mkt = _mktmap_table[fqme]
|
||||
|
||||
if not (pos := pps.get(bs_mktid)):
|
||||
|
||||
|
|
@ -700,7 +660,7 @@ class Account(Struct):
|
|||
def write_config(self) -> None:
|
||||
'''
|
||||
Write the current account state to the user's account TOML file, normally
|
||||
something like `pps.toml`.
|
||||
something like ``pps.toml``.
|
||||
|
||||
'''
|
||||
# TODO: show diff output?
|
||||
|
|
@ -754,7 +714,7 @@ class Account(Struct):
|
|||
# XXX WTF: if we use a tomlkit.Integer here we get this
|
||||
# super weird --1 thing going on for cumsize!?1!
|
||||
# NOTE: the fix was to always float() the size value loaded
|
||||
# in open_account() below!
|
||||
# in open_pps() below!
|
||||
config.write(
|
||||
config=self.conf,
|
||||
path=self.conf_path,
|
||||
|
|
@ -938,6 +898,7 @@ def open_account(
|
|||
clears_table['dt'] = dt
|
||||
trans.append(Transaction(
|
||||
fqme=bs_mktid,
|
||||
# sym=mkt,
|
||||
bs_mktid=bs_mktid,
|
||||
tid=tid,
|
||||
# XXX: not sure why sometimes these are loaded as
|
||||
|
|
@ -960,22 +921,11 @@ def open_account(
|
|||
):
|
||||
expiry: pendulum.DateTime = pendulum.parse(expiry)
|
||||
|
||||
# !XXX, should never be duplicates over
|
||||
# a backend-(broker)-system's unique market-IDs!
|
||||
if pos := pp_objs.get(bs_mktid):
|
||||
if mkt != pos.mkt:
|
||||
log.warning(
|
||||
f'Duplicated position but diff `MktPair.fqme` ??\n'
|
||||
f'bs_mktid: {bs_mktid!r}\n'
|
||||
f'pos.mkt: {pos.mkt}\n'
|
||||
f'mkt: {mkt}\n'
|
||||
)
|
||||
else:
|
||||
pos = pp_objs[bs_mktid] = Position(
|
||||
mkt,
|
||||
split_ratio=split_ratio,
|
||||
bs_mktid=bs_mktid,
|
||||
)
|
||||
pp = pp_objs[bs_mktid] = Position(
|
||||
mkt,
|
||||
split_ratio=split_ratio,
|
||||
bs_mktid=bs_mktid,
|
||||
)
|
||||
|
||||
# XXX: super critical, we need to be sure to include
|
||||
# all pps.toml clears to avoid reusing clears that were
|
||||
|
|
@ -983,13 +933,8 @@ def open_account(
|
|||
# state, since today's records may have already been
|
||||
# processed!
|
||||
for t in trans:
|
||||
added: bool = pos.add_clear(t)
|
||||
if not added:
|
||||
log.warning(
|
||||
f'Txn already recorded in pp ??\n'
|
||||
f'\n'
|
||||
f'{t}\n'
|
||||
)
|
||||
pp.add_clear(t)
|
||||
|
||||
try:
|
||||
yield acnt
|
||||
finally:
|
||||
|
|
@ -997,6 +942,20 @@ def open_account(
|
|||
acnt.write_config()
|
||||
|
||||
|
||||
# TODO: drop the old name and THIS!
|
||||
@cm
|
||||
def open_pps(
|
||||
*args,
|
||||
**kwargs,
|
||||
) -> Generator[Account, None, None]:
|
||||
log.warning(
|
||||
'`open_pps()` is now deprecated!\n'
|
||||
'Please use `with open_account() as cnt:`'
|
||||
)
|
||||
with open_account(*args, **kwargs) as acnt:
|
||||
yield acnt
|
||||
|
||||
|
||||
def load_account_from_ledger(
|
||||
|
||||
brokername: str,
|
||||
|
|
|
|||
|
|
@ -32,7 +32,6 @@ from typing import (
|
|||
TYPE_CHECKING,
|
||||
)
|
||||
|
||||
from tractor.devx import maybe_open_crash_handler
|
||||
import polars as pl
|
||||
from pendulum import (
|
||||
DateTime,
|
||||
|
|
@ -268,6 +267,9 @@ def iter_by_dt(
|
|||
(v := tx.get(k))
|
||||
)
|
||||
):
|
||||
# TODO? remove yah?
|
||||
# v = tx[k] if isdict else tx.dt
|
||||
|
||||
# only call parser on the value if not None from
|
||||
# the `parsers` table above (when NOT using
|
||||
# `.get()`), otherwise pass through the value and
|
||||
|
|
@ -284,50 +286,26 @@ def iter_by_dt(
|
|||
return ret
|
||||
|
||||
else:
|
||||
log.debug(
|
||||
f'Parser-field not found in txn\n'
|
||||
f'\n'
|
||||
f'parser-field: {k!r}\n'
|
||||
f'txn: {tx!r}\n'
|
||||
f'\n'
|
||||
f'Trying next..\n'
|
||||
)
|
||||
continue
|
||||
|
||||
# XXX: we should never really get here bc it means some kinda
|
||||
# bad txn-record (field) data..
|
||||
#
|
||||
# -> set the `debug_mode = True` if you want to trace such
|
||||
# cases from REPL ;)
|
||||
# XXX: should never get here..
|
||||
else:
|
||||
# XXX: we should really never get here..
|
||||
# only if a ledger record has no expected sort(able)
|
||||
# field will we likely hit this.. like with ze IB.
|
||||
# if no sortable field just deliver epoch?
|
||||
log.warning(
|
||||
'No (time) sortable field for TXN:\n'
|
||||
f'{tx!r}\n'
|
||||
)
|
||||
report: str = (
|
||||
f'No supported time-field found in txn !?\n'
|
||||
f'\n'
|
||||
f'supported-time-fields: {parsers!r}\n'
|
||||
f'\n'
|
||||
f'txn: {tx!r}\n'
|
||||
)
|
||||
if debug:
|
||||
with maybe_open_crash_handler(
|
||||
pdb=debug,
|
||||
raise_on_exit=False,
|
||||
):
|
||||
raise ValueError(report)
|
||||
else:
|
||||
log.error(report)
|
||||
import tractor
|
||||
with tractor.devx.maybe_open_crash_handler():
|
||||
raise ValueError(
|
||||
f'Invalid txn time ??\n'
|
||||
f'txn-id: {k!r}\n'
|
||||
f'{k!r}: {v!r}\n'
|
||||
)
|
||||
# assert v is not None, f'No valid value for `{k}`!?'
|
||||
|
||||
if _invalid is not None:
|
||||
_invalid.append(tx)
|
||||
return from_timestamp(0.)
|
||||
|
||||
# breakpoint()
|
||||
|
||||
entry: tuple[str, dict]|Transaction
|
||||
invalid: list = []
|
||||
for entry in sorted(
|
||||
|
|
@ -341,6 +319,8 @@ def iter_by_dt(
|
|||
log.warning(
|
||||
f'Ignoring txn w invalid timestamp ??\n'
|
||||
f'{pformat(entry)}\n'
|
||||
# f'txn-id: {k!r}\n'
|
||||
# f'{k!r}: {v!r}\n'
|
||||
)
|
||||
continue
|
||||
|
||||
|
|
@ -406,7 +386,6 @@ def open_ledger_dfs(
|
|||
acctname: str,
|
||||
|
||||
ledger: TransactionLedger | None = None,
|
||||
debug_mode: bool = False,
|
||||
|
||||
**kwargs,
|
||||
|
||||
|
|
@ -421,10 +400,8 @@ def open_ledger_dfs(
|
|||
can update the ledger on exit.
|
||||
|
||||
'''
|
||||
with maybe_open_crash_handler(
|
||||
pdb=debug_mode,
|
||||
# raise_on_exit=False,
|
||||
):
|
||||
from piker.toolz import open_crash_handler
|
||||
with open_crash_handler():
|
||||
if not ledger:
|
||||
import time
|
||||
from ._ledger import open_trade_ledger
|
||||
|
|
@ -516,7 +493,7 @@ def ledger_to_dfs(
|
|||
|
||||
df = dfs[key] = ldf.with_columns([
|
||||
|
||||
pl.cum_sum('size').alias('cumsize'),
|
||||
pl.cumsum('size').alias('cumsize'),
|
||||
|
||||
# amount of source asset "sent" (via buy txns in
|
||||
# the market) to acquire the dst asset, PER txn.
|
||||
|
|
@ -531,7 +508,7 @@ def ledger_to_dfs(
|
|||
]).with_columns([
|
||||
|
||||
# rolling balance in src asset units
|
||||
(pl.col('dst_bot').cum_sum() * -1).alias('src_balance'),
|
||||
(pl.col('dst_bot').cumsum() * -1).alias('src_balance'),
|
||||
|
||||
# "position operation type" in terms of increasing the
|
||||
# amount in the dst asset (entering) or decreasing the
|
||||
|
|
@ -673,7 +650,7 @@ def ledger_to_dfs(
|
|||
# cost that was included in the least-recently
|
||||
# entered txn that is still part of the current CSi
|
||||
# set.
|
||||
# => we look up the cost-per-unit cum_sum and apply
|
||||
# => we look up the cost-per-unit cumsum and apply
|
||||
# if over the current txn size (by multiplication)
|
||||
# and then reverse that previusly applied cost on
|
||||
# the txn_cost for this record.
|
||||
|
|
|
|||
|
|
@ -300,8 +300,7 @@ def disect(
|
|||
assert not df.is_empty()
|
||||
|
||||
# muck around in pdbp REPL
|
||||
# tractor.devx.mk_pdb().set_trace()
|
||||
# breakpoint()
|
||||
breakpoint()
|
||||
|
||||
# TODO: we REALLY need a better console REPL for this
|
||||
# kinda thing..
|
||||
|
|
|
|||
|
|
@ -51,6 +51,7 @@ __brokers__: list[str] = [
|
|||
'ib',
|
||||
'kraken',
|
||||
'kucoin',
|
||||
'deribit',
|
||||
|
||||
# broken but used to work
|
||||
# 'questrade',
|
||||
|
|
@ -61,7 +62,6 @@ __brokers__: list[str] = [
|
|||
# wstrade
|
||||
# iex
|
||||
|
||||
# deribit
|
||||
# bitso
|
||||
]
|
||||
|
||||
|
|
@ -98,14 +98,13 @@ async def open_cached_client(
|
|||
If one has not been setup do it and cache it.
|
||||
|
||||
'''
|
||||
brokermod: ModuleType = get_brokermod(brokername)
|
||||
|
||||
# TODO: make abstract or `typing.Protocol`
|
||||
# client: Client
|
||||
brokermod = get_brokermod(brokername)
|
||||
async with maybe_open_context(
|
||||
acm_func=brokermod.get_client,
|
||||
kwargs=kwargs,
|
||||
|
||||
) as (cache_hit, client):
|
||||
|
||||
if cache_hit:
|
||||
log.runtime(f'Reusing existing {client}')
|
||||
|
||||
|
|
|
|||
|
|
@ -94,21 +94,18 @@ class L1(Struct):
|
|||
|
||||
|
||||
# validation type
|
||||
# https://developers.binance.com/docs/derivatives/usds-margined-futures/websocket-market-streams/Aggregate-Trade-Streams#response-example
|
||||
class AggTrade(Struct, frozen=True):
|
||||
e: str # Event type
|
||||
E: int # Event time
|
||||
s: str # Symbol
|
||||
a: int # Aggregate trade ID
|
||||
p: float # Price
|
||||
q: float # Quantity with all the market trades
|
||||
q: float # Quantity
|
||||
f: int # First trade ID
|
||||
l: int # noqa Last trade ID
|
||||
T: int # Trade time
|
||||
m: bool # Is the buyer the market maker?
|
||||
M: bool|None = None # Ignore
|
||||
nq: float|None = None # Normal quantity without the trades involving RPI orders
|
||||
# ^XXX https://developers.binance.com/docs/derivatives/change-log#2025-12-29
|
||||
M: bool | None = None # Ignore
|
||||
|
||||
|
||||
async def stream_messages(
|
||||
|
|
|
|||
|
|
@ -102,10 +102,7 @@ class Pair(Struct, frozen=True, kw_only=True):
|
|||
# https://developers.binance.com/docs/binance-spot-api-docs#2025-08-26
|
||||
# will become non-optional 2025-08-28?
|
||||
# https://developers.binance.com/docs/binance-spot-api-docs#future-changes
|
||||
pegInstructionsAllowed: bool = False
|
||||
|
||||
# https://developers.binance.com/docs/binance-spot-api-docs#2025-12-02
|
||||
opoAllowed: bool = False
|
||||
pegInstructionsAllowed: bool|None = None
|
||||
|
||||
filters: dict[
|
||||
str,
|
||||
|
|
@ -223,10 +220,7 @@ class FutesPair(Pair):
|
|||
assert pair == self.pair # sanity
|
||||
return f'{expiry}'
|
||||
|
||||
case (
|
||||
'PERPETUAL'
|
||||
| 'TRADIFI_PERPETUAL'
|
||||
):
|
||||
case 'PERPETUAL':
|
||||
return 'PERP'
|
||||
|
||||
case '':
|
||||
|
|
@ -255,10 +249,7 @@ class FutesPair(Pair):
|
|||
margin: str = self.marginAsset
|
||||
|
||||
match ctype:
|
||||
case (
|
||||
'PERPETUAL'
|
||||
| 'TRADIFI_PERPETUAL'
|
||||
):
|
||||
case 'PERPETUAL':
|
||||
return f'{margin}M'
|
||||
|
||||
case (
|
||||
|
|
|
|||
|
|
@ -471,15 +471,11 @@ def search(
|
|||
|
||||
'''
|
||||
# global opts
|
||||
brokermods: list[ModuleType] = list(config['brokermods'].values())
|
||||
|
||||
# TODO: this is coming from the `search --pdb` NOT from
|
||||
# the `piker --pdb` XD ..
|
||||
# -[ ] pull from the parent click ctx's values..dumdum
|
||||
# assert pdb
|
||||
brokermods = list(config['brokermods'].values())
|
||||
|
||||
# define tractor entrypoint
|
||||
async def main(func):
|
||||
|
||||
async with maybe_open_pikerd(
|
||||
loglevel=config['loglevel'],
|
||||
debug_mode=pdb,
|
||||
|
|
|
|||
|
|
@ -22,9 +22,7 @@ routines should be primitive data types where possible.
|
|||
"""
|
||||
import inspect
|
||||
from types import ModuleType
|
||||
from typing import (
|
||||
Any,
|
||||
)
|
||||
from typing import List, Dict, Any, Optional
|
||||
|
||||
import trio
|
||||
|
||||
|
|
@ -36,10 +34,8 @@ from ..accounting import MktPair
|
|||
|
||||
|
||||
async def api(brokername: str, methname: str, **kwargs) -> dict:
|
||||
'''
|
||||
Make (proxy through) a broker API call by name and return its result.
|
||||
|
||||
'''
|
||||
"""Make (proxy through) a broker API call by name and return its result.
|
||||
"""
|
||||
brokermod = get_brokermod(brokername)
|
||||
async with brokermod.get_client() as client:
|
||||
meth = getattr(client, methname, None)
|
||||
|
|
@ -66,14 +62,10 @@ async def api(brokername: str, methname: str, **kwargs) -> dict:
|
|||
|
||||
async def stocks_quote(
|
||||
brokermod: ModuleType,
|
||||
tickers: list[str]
|
||||
|
||||
) -> dict[str, dict[str, Any]]:
|
||||
'''
|
||||
Return a `dict` of snapshot quotes for the provided input
|
||||
`tickers`: a `list` of fqmes.
|
||||
|
||||
'''
|
||||
tickers: List[str]
|
||||
) -> Dict[str, Dict[str, Any]]:
|
||||
"""Return quotes dict for ``tickers``.
|
||||
"""
|
||||
async with brokermod.get_client() as client:
|
||||
return await client.quote(tickers)
|
||||
|
||||
|
|
@ -82,15 +74,13 @@ async def stocks_quote(
|
|||
async def option_chain(
|
||||
brokermod: ModuleType,
|
||||
symbol: str,
|
||||
date: str|None = None,
|
||||
) -> dict[str, dict[str, dict[str, Any]]]:
|
||||
'''
|
||||
Return option chain for ``symbol`` for ``date``.
|
||||
date: Optional[str] = None,
|
||||
) -> Dict[str, Dict[str, Dict[str, Any]]]:
|
||||
"""Return option chain for ``symbol`` for ``date``.
|
||||
|
||||
By default all expiries are returned. If ``date`` is provided
|
||||
then contract quotes for that single expiry are returned.
|
||||
|
||||
'''
|
||||
"""
|
||||
async with brokermod.get_client() as client:
|
||||
if date:
|
||||
id = int((await client.tickers2ids([symbol]))[symbol])
|
||||
|
|
@ -108,7 +98,7 @@ async def option_chain(
|
|||
# async def contracts(
|
||||
# brokermod: ModuleType,
|
||||
# symbol: str,
|
||||
# ) -> dict[str, dict[str, dict[str, Any]]]:
|
||||
# ) -> Dict[str, Dict[str, Dict[str, Any]]]:
|
||||
# """Return option contracts (all expiries) for ``symbol``.
|
||||
# """
|
||||
# async with brokermod.get_client() as client:
|
||||
|
|
@ -120,24 +110,15 @@ async def bars(
|
|||
brokermod: ModuleType,
|
||||
symbol: str,
|
||||
**kwargs,
|
||||
) -> dict[str, dict[str, dict[str, Any]]]:
|
||||
'''
|
||||
Return option contracts (all expiries) for ``symbol``.
|
||||
|
||||
'''
|
||||
) -> Dict[str, Dict[str, Dict[str, Any]]]:
|
||||
"""Return option contracts (all expiries) for ``symbol``.
|
||||
"""
|
||||
async with brokermod.get_client() as client:
|
||||
return await client.bars(symbol, **kwargs)
|
||||
|
||||
|
||||
async def search_w_brokerd(
|
||||
name: str,
|
||||
pattern: str,
|
||||
) -> dict:
|
||||
async def search_w_brokerd(name: str, pattern: str) -> dict:
|
||||
|
||||
# TODO: WHY NOT WORK!?!
|
||||
# when we `step` through the next block?
|
||||
# import tractor
|
||||
# await tractor.pause()
|
||||
async with open_cached_client(name) as client:
|
||||
|
||||
# TODO: support multiple asset type concurrent searches.
|
||||
|
|
@ -149,12 +130,12 @@ async def symbol_search(
|
|||
pattern: str,
|
||||
**kwargs,
|
||||
|
||||
) -> dict[str, dict[str, dict[str, Any]]]:
|
||||
) -> Dict[str, Dict[str, Dict[str, Any]]]:
|
||||
'''
|
||||
Return symbol info from broker.
|
||||
|
||||
'''
|
||||
results: list[str] = []
|
||||
results = []
|
||||
|
||||
async def search_backend(
|
||||
brokermod: ModuleType
|
||||
|
|
@ -162,13 +143,6 @@ async def symbol_search(
|
|||
|
||||
brokername: str = mod.name
|
||||
|
||||
# TODO: figure this the FUCK OUT
|
||||
# -> ok so obvi in the root actor any async task that's
|
||||
# spawned outside the main tractor-root-actor task needs to
|
||||
# call this..
|
||||
# await tractor.devx._debug.maybe_init_greenback()
|
||||
# tractor.pause_from_sync()
|
||||
|
||||
async with maybe_spawn_brokerd(
|
||||
mod.name,
|
||||
infect_asyncio=getattr(
|
||||
|
|
@ -188,6 +162,7 @@ async def symbol_search(
|
|||
))
|
||||
|
||||
async with trio.open_nursery() as n:
|
||||
|
||||
for mod in brokermods:
|
||||
n.start_soon(search_backend, mod.name)
|
||||
|
||||
|
|
@ -197,13 +172,11 @@ async def symbol_search(
|
|||
async def mkt_info(
|
||||
brokermod: ModuleType,
|
||||
fqme: str,
|
||||
|
||||
**kwargs,
|
||||
|
||||
) -> MktPair:
|
||||
'''
|
||||
Return the `piker.accounting.MktPair` info struct from a given
|
||||
backend broker tradable src/dst asset pair.
|
||||
Return MktPair info from broker including src and dst assets.
|
||||
|
||||
'''
|
||||
async with open_cached_client(brokermod.name) as client:
|
||||
|
|
|
|||
|
|
@ -25,6 +25,7 @@ from .api import (
|
|||
get_client,
|
||||
)
|
||||
from .feed import (
|
||||
get_mkt_info,
|
||||
open_history_client,
|
||||
open_symbol_search,
|
||||
stream_quotes,
|
||||
|
|
@ -34,15 +35,20 @@ from .feed import (
|
|||
# open_trade_dialog,
|
||||
# norm_trade_records,
|
||||
# )
|
||||
from .venues import (
|
||||
OptionPair,
|
||||
)
|
||||
|
||||
log = get_logger(__name__)
|
||||
|
||||
__all__ = [
|
||||
'get_client',
|
||||
# 'trades_dialogue',
|
||||
'get_mkt_info',
|
||||
'open_history_client',
|
||||
'open_symbol_search',
|
||||
'stream_quotes',
|
||||
'OptionPair',
|
||||
# 'norm_trade_records',
|
||||
]
|
||||
|
||||
|
|
|
|||
File diff suppressed because it is too large
Load Diff
|
|
@ -18,38 +18,59 @@
|
|||
Deribit backend.
|
||||
|
||||
'''
|
||||
from __future__ import annotations
|
||||
from contextlib import asynccontextmanager as acm
|
||||
from datetime import datetime
|
||||
from typing import Any, Optional, Callable
|
||||
from typing import (
|
||||
# Any,
|
||||
# Optional,
|
||||
Callable,
|
||||
)
|
||||
# from pprint import pformat
|
||||
import time
|
||||
|
||||
import cryptofeed
|
||||
import trio
|
||||
from trio_typing import TaskStatus
|
||||
import pendulum
|
||||
from rapidfuzz import process as fuzzy
|
||||
from pendulum import (
|
||||
from_timestamp,
|
||||
)
|
||||
import numpy as np
|
||||
import tractor
|
||||
|
||||
from piker.brokers import open_cached_client
|
||||
from piker.log import get_logger, get_console_log
|
||||
from piker.data import ShmArray
|
||||
from piker.brokers._util import (
|
||||
BrokerError,
|
||||
from piker.accounting import (
|
||||
Asset,
|
||||
MktPair,
|
||||
unpack_fqme,
|
||||
)
|
||||
from piker.brokers import (
|
||||
open_cached_client,
|
||||
NoData,
|
||||
DataUnavailable,
|
||||
)
|
||||
|
||||
from cryptofeed import FeedHandler
|
||||
from cryptofeed.defines import (
|
||||
DERIBIT, L1_BOOK, TRADES, OPTION, CALL, PUT
|
||||
from piker._cacheables import (
|
||||
async_lifo_cache,
|
||||
)
|
||||
from cryptofeed.symbols import Symbol
|
||||
from piker.log import (
|
||||
get_logger,
|
||||
mk_repr,
|
||||
)
|
||||
from piker.data.validate import FeedInit
|
||||
|
||||
|
||||
from .api import (
|
||||
Client, Trade,
|
||||
get_config,
|
||||
str_to_cb_sym, piker_sym_to_cb_sym, cb_sym_to_deribit_inst,
|
||||
Client,
|
||||
# get_config,
|
||||
piker_sym_to_cb_sym,
|
||||
cb_sym_to_deribit_inst,
|
||||
str_to_cb_sym,
|
||||
maybe_open_price_feed
|
||||
)
|
||||
from .venues import (
|
||||
Pair,
|
||||
OptionPair,
|
||||
Trade,
|
||||
)
|
||||
|
||||
_spawn_kwargs = {
|
||||
'infect_asyncio': True,
|
||||
|
|
@ -64,90 +85,215 @@ async def open_history_client(
|
|||
mkt: MktPair,
|
||||
) -> tuple[Callable, int]:
|
||||
|
||||
fnstrument: str = mkt.bs_fqme
|
||||
# TODO implement history getter for the new storage layer.
|
||||
async with open_cached_client('deribit') as client:
|
||||
|
||||
pair: OptionPair = client._pairs[mkt.dst.name]
|
||||
# XXX NOTE, the cuckers use ms !!!
|
||||
creation_time_s: int = pair.creation_timestamp/1000
|
||||
|
||||
async def get_ohlc(
|
||||
end_dt: Optional[datetime] = None,
|
||||
start_dt: Optional[datetime] = None,
|
||||
timeframe: float,
|
||||
end_dt: datetime | None = None,
|
||||
start_dt: datetime | None = None,
|
||||
|
||||
) -> tuple[
|
||||
np.ndarray,
|
||||
datetime, # start
|
||||
datetime, # end
|
||||
]:
|
||||
if timeframe != 60:
|
||||
raise DataUnavailable('Only 1m bars are supported')
|
||||
|
||||
array = await client.bars(
|
||||
instrument,
|
||||
array: np.ndarray = await client.bars(
|
||||
mkt,
|
||||
start_dt=start_dt,
|
||||
end_dt=end_dt,
|
||||
)
|
||||
if len(array) == 0:
|
||||
raise DataUnavailable
|
||||
if (
|
||||
end_dt is None
|
||||
):
|
||||
raise DataUnavailable(
|
||||
'No history seems to exist yet?\n\n'
|
||||
f'{mkt}'
|
||||
)
|
||||
elif (
|
||||
end_dt
|
||||
and
|
||||
end_dt.timestamp() < creation_time_s
|
||||
):
|
||||
# the contract can't have history
|
||||
# before it was created.
|
||||
pair_type_str: str = type(pair).__name__
|
||||
create_dt: datetime = from_timestamp(creation_time_s)
|
||||
raise DataUnavailable(
|
||||
f'No history prior to\n'
|
||||
f'`{pair_type_str}.creation_timestamp: int = '
|
||||
f'{pair.creation_timestamp}\n\n'
|
||||
f'------ deribit sux ------\n'
|
||||
f'WHICH IN "NORMAL PEOPLE WHO USE EPOCH TIME" form is,\n'
|
||||
f'creation_time_s: {creation_time_s}\n'
|
||||
f'create_dt: {create_dt}\n'
|
||||
)
|
||||
raise NoData(
|
||||
f'No frame for {start_dt} -> {end_dt}\n'
|
||||
)
|
||||
|
||||
start_dt = pendulum.from_timestamp(array[0]['time'])
|
||||
end_dt = pendulum.from_timestamp(array[-1]['time'])
|
||||
start_dt = from_timestamp(array[0]['time'])
|
||||
end_dt = from_timestamp(array[-1]['time'])
|
||||
|
||||
times = array['time']
|
||||
if not times.any():
|
||||
raise ValueError(
|
||||
'Bad frame with null-times?\n\n'
|
||||
f'{times}'
|
||||
)
|
||||
|
||||
if end_dt is None:
|
||||
inow: int = round(time.time())
|
||||
if (inow - times[-1]) > 60:
|
||||
await tractor.pause()
|
||||
|
||||
return array, start_dt, end_dt
|
||||
|
||||
yield get_ohlc, {'erlangs': 3, 'rate': 3}
|
||||
yield (
|
||||
get_ohlc,
|
||||
{ # backfill config
|
||||
'erlangs': 3,
|
||||
'rate': 3,
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
@async_lifo_cache()
|
||||
async def get_mkt_info(
|
||||
fqme: str,
|
||||
|
||||
) -> tuple[MktPair, Pair|OptionPair] | None:
|
||||
|
||||
# uppercase since kraken bs_mktid is always upper
|
||||
if 'deribit' not in fqme.lower():
|
||||
fqme += '.deribit'
|
||||
|
||||
mkt_mode: str = ''
|
||||
broker, mkt_ep, venue, expiry = unpack_fqme(fqme)
|
||||
|
||||
# NOTE: we always upper case all tokens to be consistent with
|
||||
# binance's symbology style for pairs, like `BTCUSDT`, but in
|
||||
# theory we could also just keep things lower case; as long as
|
||||
# we're consistent and the symcache matches whatever this func
|
||||
# returns, always!
|
||||
expiry: str = expiry.upper()
|
||||
venue: str = venue.upper()
|
||||
# venue_lower: str = venue.lower()
|
||||
|
||||
mkt_mode: str = 'option'
|
||||
|
||||
async with open_cached_client(
|
||||
'deribit',
|
||||
) as client:
|
||||
|
||||
assets: dict[str, Asset] = await client.get_assets()
|
||||
pair_str: str = mkt_ep.lower()
|
||||
|
||||
pair: Pair = await client.exch_info(
|
||||
sym=pair_str,
|
||||
)
|
||||
mkt_mode = pair.venue
|
||||
client.mkt_mode = mkt_mode
|
||||
|
||||
dst: Asset | None = assets.get(pair.bs_dst_asset)
|
||||
src: Asset | None = assets.get(pair.bs_src_asset)
|
||||
|
||||
mkt = MktPair(
|
||||
dst=dst,
|
||||
src=src,
|
||||
price_tick=pair.price_tick,
|
||||
size_tick=pair.size_tick,
|
||||
bs_mktid=pair.symbol,
|
||||
venue=mkt_mode,
|
||||
broker='deribit',
|
||||
_atype=mkt_mode,
|
||||
_fqme_without_src=True,
|
||||
|
||||
# expiry=pair.expiry,
|
||||
# XXX TODO, currently we don't use it since it's
|
||||
# already "described" in the `OptionPair.symbol: str`
|
||||
# and if we slap in the ISO repr it's kinda hideous..
|
||||
# -[ ] figure out the best either std
|
||||
)
|
||||
return mkt, pair
|
||||
|
||||
|
||||
async def stream_quotes(
|
||||
|
||||
send_chan: trio.abc.SendChannel,
|
||||
symbols: list[str],
|
||||
feed_is_live: trio.Event,
|
||||
loglevel: str = None,
|
||||
|
||||
# startup sync
|
||||
task_status: TaskStatus[tuple[dict, dict]] = trio.TASK_STATUS_IGNORED,
|
||||
|
||||
) -> None:
|
||||
# XXX: required to propagate ``tractor`` loglevel to piker logging
|
||||
get_console_log(loglevel or tractor.current_actor().loglevel)
|
||||
'''
|
||||
Open a live quote stream for the market set defined by `symbols`.
|
||||
|
||||
sym = symbols[0]
|
||||
Internally this starts a `cryptofeed.FeedHandler` inside an `asyncio`-side
|
||||
task and relays through L1 and `Trade` msgs here to our `trio.Task`.
|
||||
|
||||
'''
|
||||
sym = symbols[0].split('.')[0]
|
||||
init_msgs: list[FeedInit] = []
|
||||
|
||||
# multiline nested `dict` formatter (since rn quote-msgs are
|
||||
# just that).
|
||||
pfmt: Callable[[str], str] = mk_repr(
|
||||
# so we can see `deribit`'s delightfully mega-long bs fields..
|
||||
maxstring=100,
|
||||
)
|
||||
|
||||
async with (
|
||||
open_cached_client('deribit') as client,
|
||||
send_chan as send_chan
|
||||
):
|
||||
mkt: MktPair
|
||||
pair: Pair
|
||||
mkt, pair = await get_mkt_info(sym)
|
||||
|
||||
init_msgs = {
|
||||
# pass back token, and bool, signalling if we're the writer
|
||||
# and that history has been written
|
||||
sym: {
|
||||
'symbol_info': {
|
||||
'asset_type': 'option',
|
||||
'price_tick_size': 0.0005
|
||||
},
|
||||
'shm_write_opts': {'sum_tick_vml': False},
|
||||
'fqsn': sym,
|
||||
},
|
||||
}
|
||||
# build out init msgs according to latest spec
|
||||
init_msgs.append(
|
||||
FeedInit(
|
||||
mkt_info=mkt,
|
||||
)
|
||||
)
|
||||
# build `cryptofeed` feed-handle
|
||||
cf_sym: cryptofeed.Symbol = piker_sym_to_cb_sym(sym)
|
||||
|
||||
nsym = piker_sym_to_cb_sym(sym)
|
||||
from_cf: tractor.to_asyncio.LinkedTaskChannel
|
||||
async with maybe_open_price_feed(sym) as from_cf:
|
||||
|
||||
async with maybe_open_price_feed(sym) as stream:
|
||||
# load the "last trades" summary
|
||||
last_trades_res: cryptofeed.LastTradesResult = await client.last_trades(
|
||||
cb_sym_to_deribit_inst(cf_sym),
|
||||
count=1,
|
||||
)
|
||||
last_trades: list[Trade] = last_trades_res.trades
|
||||
|
||||
cache = await client.cache_symbols()
|
||||
# TODO, do we even need this or will the above always
|
||||
# work?
|
||||
# if not last_trades:
|
||||
# await tractor.pause()
|
||||
# async for typ, quote in from_cf:
|
||||
# if typ == 'trade':
|
||||
# last_trade = Trade(**(quote['data']))
|
||||
# break
|
||||
|
||||
last_trades = (await client.last_trades(
|
||||
cb_sym_to_deribit_inst(nsym), count=1)).trades
|
||||
# else:
|
||||
last_trade = Trade(
|
||||
**(last_trades[0])
|
||||
)
|
||||
|
||||
if len(last_trades) == 0:
|
||||
last_trade = None
|
||||
async for typ, quote in stream:
|
||||
if typ == 'trade':
|
||||
last_trade = Trade(**(quote['data']))
|
||||
break
|
||||
|
||||
else:
|
||||
last_trade = Trade(**(last_trades[0]))
|
||||
|
||||
first_quote = {
|
||||
first_quote: dict = {
|
||||
'symbol': sym,
|
||||
'last': last_trade.price,
|
||||
'brokerd_ts': last_trade.timestamp,
|
||||
|
|
@ -158,13 +304,84 @@ async def stream_quotes(
|
|||
'broker_ts': last_trade.timestamp
|
||||
}]
|
||||
}
|
||||
task_status.started((init_msgs, first_quote))
|
||||
task_status.started((
|
||||
init_msgs,
|
||||
first_quote,
|
||||
))
|
||||
|
||||
feed_is_live.set()
|
||||
|
||||
async for typ, quote in stream:
|
||||
topic = quote['symbol']
|
||||
await send_chan.send({topic: quote})
|
||||
# NOTE XXX, static for now!
|
||||
# => since this only handles ONE mkt feed at a time we
|
||||
# don't need a lookup table to map interleaved quotes
|
||||
# from multiple possible mkt-pairs
|
||||
topic: str = mkt.bs_fqme
|
||||
|
||||
# deliver until cancelled
|
||||
async for typ, ref in from_cf:
|
||||
match typ:
|
||||
case 'trade':
|
||||
trade: cryptofeed.types.Trade = ref
|
||||
|
||||
# TODO, re-impl this according to teh ideal
|
||||
# fqme for opts that we choose!!
|
||||
bs_fqme: str = cb_sym_to_deribit_inst(
|
||||
str_to_cb_sym(trade.symbol)
|
||||
).lower()
|
||||
|
||||
piker_quote: dict = {
|
||||
'symbol': bs_fqme,
|
||||
'last': trade.price,
|
||||
'broker_ts': time.time(),
|
||||
# ^TODO, name this `brokerd/datad_ts` and
|
||||
# use `time.time_ns()` ??
|
||||
'ticks': [{
|
||||
'type': 'trade',
|
||||
'price': float(trade.price),
|
||||
'size': float(trade.amount),
|
||||
'broker_ts': trade.timestamp,
|
||||
}],
|
||||
}
|
||||
log.info(
|
||||
f'deribit {typ!r} quote for {sym!r}\n\n'
|
||||
f'{trade}\n\n'
|
||||
f'{pfmt(piker_quote)}\n'
|
||||
)
|
||||
|
||||
case 'l1':
|
||||
book: cryptofeed.types.L1Book = ref
|
||||
|
||||
# TODO, so this is where we can possibly change things
|
||||
# and instead lever the `MktPair.bs_fqme: str` output?
|
||||
bs_fqme: str = cb_sym_to_deribit_inst(
|
||||
str_to_cb_sym(book.symbol)
|
||||
).lower()
|
||||
|
||||
piker_quote: dict = {
|
||||
'symbol': bs_fqme,
|
||||
'ticks': [
|
||||
|
||||
{'type': 'bid',
|
||||
'price': float(book.bid_price),
|
||||
'size': float(book.bid_size)},
|
||||
|
||||
{'type': 'bsize',
|
||||
'price': float(book.bid_price),
|
||||
'size': float(book.bid_size),},
|
||||
|
||||
{'type': 'ask',
|
||||
'price': float(book.ask_price),
|
||||
'size': float(book.ask_size),},
|
||||
|
||||
{'type': 'asize',
|
||||
'price': float(book.ask_price),
|
||||
'size': float(book.ask_size),}
|
||||
]
|
||||
}
|
||||
|
||||
await send_chan.send({
|
||||
topic: piker_quote,
|
||||
})
|
||||
|
||||
|
||||
@tractor.context
|
||||
|
|
@ -174,12 +391,21 @@ async def open_symbol_search(
|
|||
async with open_cached_client('deribit') as client:
|
||||
|
||||
# load all symbols locally for fast search
|
||||
cache = await client.cache_symbols()
|
||||
# cache = client._pairs
|
||||
await ctx.started()
|
||||
|
||||
async with ctx.open_stream() as stream:
|
||||
|
||||
pattern: str
|
||||
async for pattern in stream:
|
||||
# repack in dict form
|
||||
await stream.send(
|
||||
await client.search_symbols(pattern))
|
||||
|
||||
# NOTE: pattern fuzzy-matching is done within
|
||||
# the methd impl.
|
||||
pairs: dict[str, Pair] = await client.search_symbols(
|
||||
pattern,
|
||||
)
|
||||
# repack in fqme-keyed table
|
||||
byfqme: dict[str, Pair] = {}
|
||||
for pair in pairs.values():
|
||||
byfqme[pair.bs_fqme] = pair
|
||||
|
||||
await stream.send(byfqme)
|
||||
|
|
|
|||
|
|
@ -0,0 +1,196 @@
|
|||
# piker: trading gear for hackers
|
||||
# Copyright (C) Tyler Goodlet (in stewardship for pikers)
|
||||
|
||||
# This program is free software: you can redistribute it and/or modify
|
||||
# it under the terms of the GNU Affero General Public License as published by
|
||||
# the Free Software Foundation, either version 3 of the License, or
|
||||
# (at your option) any later version.
|
||||
|
||||
# This program is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU Affero General Public License for more details.
|
||||
|
||||
# You should have received a copy of the GNU Affero General Public License
|
||||
# along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
"""
|
||||
Per market data-type definitions and schemas types.
|
||||
|
||||
"""
|
||||
from __future__ import annotations
|
||||
import pendulum
|
||||
from typing import (
|
||||
Literal,
|
||||
Optional,
|
||||
)
|
||||
from decimal import Decimal
|
||||
|
||||
from piker.types import Struct
|
||||
|
||||
|
||||
# API endpoint paths by venue / sub-API
|
||||
_domain: str = 'deribit.com'
|
||||
_url = f'https://www.{_domain}'
|
||||
|
||||
# WEBsocketz
|
||||
_ws_url: str = f'wss://www.{_domain}/ws/api/v2'
|
||||
|
||||
# test nets
|
||||
_testnet_ws_url: str = f'wss://test.{_domain}/ws/api/v2'
|
||||
|
||||
MarketType = Literal[
|
||||
'option'
|
||||
]
|
||||
|
||||
|
||||
def get_api_eps(venue: MarketType) -> tuple[str, str]:
|
||||
'''
|
||||
Return API ep root paths per venue.
|
||||
|
||||
'''
|
||||
return {
|
||||
'option': (
|
||||
_ws_url,
|
||||
),
|
||||
}[venue]
|
||||
|
||||
|
||||
class Pair(Struct, frozen=True, kw_only=True):
|
||||
|
||||
symbol: str
|
||||
|
||||
# src
|
||||
quote_currency: str # 'BTC'
|
||||
|
||||
# dst
|
||||
base_currency: str # "BTC",
|
||||
|
||||
tick_size: float # 0.0001 # [{'above_price': 0.005, 'tick_size': 0.0005}]
|
||||
tick_size_steps: list[dict[str, float]]
|
||||
|
||||
@property
|
||||
def price_tick(self) -> Decimal:
|
||||
return Decimal(str(self.tick_size_steps[0]['above_price']))
|
||||
|
||||
@property
|
||||
def size_tick(self) -> Decimal:
|
||||
return Decimal(str(self.tick_size))
|
||||
|
||||
@property
|
||||
def bs_fqme(self) -> str:
|
||||
return f'{self.symbol}'
|
||||
|
||||
@property
|
||||
def bs_mktid(self) -> str:
|
||||
return f'{self.symbol}.{self.venue}'
|
||||
|
||||
|
||||
class OptionPair(Pair, frozen=True):
|
||||
|
||||
taker_commission: float # 0.0003
|
||||
strike: float # 5000.0
|
||||
settlement_period: str # 'day'
|
||||
settlement_currency: str # "BTC",
|
||||
rfq: bool # false
|
||||
price_index: str # 'btc_usd'
|
||||
option_type: str # 'call'
|
||||
min_trade_amount: float # 0.1
|
||||
maker_commission: float # 0.0003
|
||||
kind: str # 'option'
|
||||
is_active: bool # true
|
||||
instrument_type: str # 'reversed'
|
||||
instrument_name: str # 'BTC-1SEP24-55000-C'
|
||||
instrument_id: int # 364671
|
||||
expiration_timestamp: int # 1725177600000
|
||||
creation_timestamp: int # 1724918461000
|
||||
counter_currency: str # 'USD'
|
||||
contract_size: float # '1.0'
|
||||
block_trade_tick_size: float # '0.0001'
|
||||
block_trade_min_trade_amount: int # '25'
|
||||
block_trade_commission: float # '0.003'
|
||||
|
||||
# NOTE: see `.data._symcache.SymbologyCache.load()` for why
|
||||
ns_path: str = 'piker.brokers.deribit:OptionPair'
|
||||
|
||||
# TODO, impl this without the MM:SS part of
|
||||
# the `'THH:MM:SS..'` etc..
|
||||
@property
|
||||
def expiry(self) -> str:
|
||||
iso_date = pendulum.from_timestamp(
|
||||
self.expiration_timestamp / 1000
|
||||
).isoformat()
|
||||
return iso_date
|
||||
|
||||
@property
|
||||
def venue(self) -> str:
|
||||
return f'{self.instrument_type}_option'
|
||||
|
||||
@property
|
||||
def bs_fqme(self) -> str:
|
||||
return f'{self.symbol}'
|
||||
|
||||
@property
|
||||
def bs_src_asset(self) -> str:
|
||||
return f'{self.quote_currency}'
|
||||
|
||||
@property
|
||||
def bs_dst_asset(self) -> str:
|
||||
return f'{self.symbol}'
|
||||
|
||||
|
||||
PAIRTYPES: dict[MarketType, Pair] = {
|
||||
'option': OptionPair,
|
||||
}
|
||||
|
||||
|
||||
class JSONRPCResult(Struct):
|
||||
id: int
|
||||
usIn: int
|
||||
usOut: int
|
||||
usDiff: int
|
||||
testnet: bool
|
||||
jsonrpc: str = '2.0'
|
||||
error: Optional[dict] = None
|
||||
result: Optional[list[dict]] = None
|
||||
|
||||
|
||||
class JSONRPCChannel(Struct):
|
||||
method: str
|
||||
params: dict
|
||||
jsonrpc: str = '2.0'
|
||||
|
||||
|
||||
class KLinesResult(Struct):
|
||||
low: list[float]
|
||||
cost: list[float]
|
||||
high: list[float]
|
||||
open: list[float]
|
||||
close: list[float]
|
||||
ticks: list[int]
|
||||
status: str
|
||||
volume: list[float]
|
||||
|
||||
|
||||
class Trade(Struct):
|
||||
iv: float
|
||||
price: float
|
||||
amount: float
|
||||
trade_id: str
|
||||
contracts: float
|
||||
direction: str
|
||||
trade_seq: int
|
||||
timestamp: int
|
||||
mark_price: float
|
||||
index_price: float
|
||||
tick_direction: int
|
||||
instrument_name: str
|
||||
combo_id: Optional[str] = '',
|
||||
combo_trade_id: Optional[int] = 0,
|
||||
block_trade_id: Optional[str] = '',
|
||||
block_trade_leg_count: Optional[int] = 0,
|
||||
|
||||
|
||||
class LastTradesResult(Struct):
|
||||
trades: list[Trade]
|
||||
has_more: bool
|
||||
|
|
@ -38,6 +38,7 @@ from piker.brokers._util import get_logger
|
|||
|
||||
if TYPE_CHECKING:
|
||||
from .api import Client
|
||||
from ib_insync import IB
|
||||
import i3ipc
|
||||
|
||||
log = get_logger('piker.brokers.ib')
|
||||
|
|
@ -61,7 +62,7 @@ no_setup_msg:str = (
|
|||
|
||||
|
||||
def try_xdo_manual(
|
||||
client: Client,
|
||||
vnc_sockaddr: str,
|
||||
):
|
||||
'''
|
||||
Do the "manual" `xdo`-based screen switch + click
|
||||
|
|
@ -78,7 +79,6 @@ def try_xdo_manual(
|
|||
_reset_tech = 'i3ipc_xdotool'
|
||||
return True
|
||||
except OSError:
|
||||
vnc_sockaddr: str = client.conf.vnc_addrs
|
||||
log.exception(
|
||||
no_setup_msg.format(vnc_sockaddr=vnc_sockaddr)
|
||||
)
|
||||
|
|
@ -86,6 +86,7 @@ def try_xdo_manual(
|
|||
|
||||
|
||||
async def data_reset_hack(
|
||||
# vnc_host: str,
|
||||
client: Client,
|
||||
reset_type: Literal['data', 'connection'],
|
||||
|
||||
|
|
@ -117,24 +118,36 @@ async def data_reset_hack(
|
|||
that need to be wrangle.
|
||||
|
||||
'''
|
||||
ib_client: IB = client.ib
|
||||
|
||||
# look up any user defined vnc socket address mapped from
|
||||
# a particular API socket port.
|
||||
vnc_addrs: tuple[str]|None = client.conf.get('vnc_addrs')
|
||||
if not vnc_addrs:
|
||||
api_port: str = str(ib_client.client.port)
|
||||
vnc_host: str
|
||||
vnc_port: int
|
||||
vnc_sockaddr: tuple[str] | None = client.conf.get('vnc_addrs')
|
||||
|
||||
if not vnc_sockaddr:
|
||||
log.warning(
|
||||
no_setup_msg.format(vnc_sockaddr=client.conf)
|
||||
no_setup_msg.format(vnc_sockaddr=vnc_sockaddr)
|
||||
+
|
||||
'REQUIRES A `vnc_addrs: array` ENTRY'
|
||||
)
|
||||
|
||||
vnc_host, vnc_port = vnc_sockaddr.get(
|
||||
api_port,
|
||||
('localhost', 3003)
|
||||
)
|
||||
global _reset_tech
|
||||
|
||||
match _reset_tech:
|
||||
case 'vnc':
|
||||
try:
|
||||
await tractor.to_asyncio.run_task(
|
||||
partial(
|
||||
vnc_click_hack,
|
||||
client=client,
|
||||
host=vnc_host,
|
||||
port=vnc_port,
|
||||
)
|
||||
)
|
||||
except (
|
||||
|
|
@ -145,31 +158,29 @@ async def data_reset_hack(
|
|||
import i3ipc # noqa (since a deps dynamic check)
|
||||
except ModuleNotFoundError:
|
||||
log.warning(
|
||||
no_setup_msg.format(vnc_sockaddr=client.conf)
|
||||
no_setup_msg.format(vnc_sockaddr=vnc_sockaddr)
|
||||
)
|
||||
return False
|
||||
|
||||
# XXX, Xorg only workaround..
|
||||
# TODO? remove now that we have `pyvnc`?
|
||||
# if vnc_host not in {
|
||||
# 'localhost',
|
||||
# '127.0.0.1',
|
||||
# }:
|
||||
# focussed, matches = i3ipc_fin_wins_titled()
|
||||
# if not matches:
|
||||
# log.warning(
|
||||
# no_setup_msg.format(vnc_sockaddr=vnc_sockaddr)
|
||||
# )
|
||||
# return False
|
||||
# else:
|
||||
# try_xdo_manual(vnc_sockaddr)
|
||||
if vnc_host not in {
|
||||
'localhost',
|
||||
'127.0.0.1',
|
||||
}:
|
||||
focussed, matches = i3ipc_fin_wins_titled()
|
||||
if not matches:
|
||||
log.warning(
|
||||
no_setup_msg.format(vnc_sockaddr=vnc_sockaddr)
|
||||
)
|
||||
return False
|
||||
else:
|
||||
try_xdo_manual(vnc_sockaddr)
|
||||
|
||||
# localhost but no vnc-client or it borked..
|
||||
else:
|
||||
try_xdo_manual(client)
|
||||
try_xdo_manual(vnc_sockaddr)
|
||||
|
||||
case 'i3ipc_xdotool':
|
||||
try_xdo_manual(client)
|
||||
try_xdo_manual(vnc_sockaddr)
|
||||
# i3ipc_xdotool_manual_click_hack()
|
||||
|
||||
case _ as tech:
|
||||
|
|
@ -180,66 +191,21 @@ async def data_reset_hack(
|
|||
|
||||
|
||||
async def vnc_click_hack(
|
||||
client: Client,
|
||||
reset_type: str = 'data',
|
||||
pw: str|None = None,
|
||||
|
||||
host: str,
|
||||
port: int,
|
||||
reset_type: str = 'data'
|
||||
) -> None:
|
||||
'''
|
||||
Reset the data or network connection for the VNC attached
|
||||
ib-gateway using a (magic) keybinding combo.
|
||||
|
||||
A vnc-server password can be set either by an input `pw` param or
|
||||
set in the client's config with the latter loaded from the user's
|
||||
`brokers.toml` in a vnc-addrs-port-mapping section,
|
||||
|
||||
.. code:: toml
|
||||
|
||||
[ib.vnc_addrs]
|
||||
4002 = {host = 'localhost', port = 5900, pw = 'doggy'}
|
||||
ib gateway using magic combos.
|
||||
|
||||
'''
|
||||
api_port: str = str(client.ib.client.port)
|
||||
conf: dict = client.conf
|
||||
vnc_addrs: dict[int, tuple] = conf.get('vnc_addrs')
|
||||
if not vnc_addrs:
|
||||
return None
|
||||
|
||||
addr_entry: dict|tuple = vnc_addrs.get(
|
||||
api_port,
|
||||
('localhost', 5900) # a typical default
|
||||
)
|
||||
if pw is None:
|
||||
match addr_entry:
|
||||
case (
|
||||
host,
|
||||
port,
|
||||
):
|
||||
pass
|
||||
|
||||
case {
|
||||
'host': host,
|
||||
'port': port,
|
||||
'pw': pw
|
||||
}:
|
||||
pass
|
||||
|
||||
case _:
|
||||
raise ValueError(
|
||||
f'Invalid `ib.vnc_addrs` entry ?\n'
|
||||
f'{addr_entry!r}\n'
|
||||
)
|
||||
try:
|
||||
from pyvnc import (
|
||||
AsyncVNCClient,
|
||||
VNCConfig,
|
||||
Point,
|
||||
MOUSE_BUTTON_LEFT,
|
||||
)
|
||||
import asyncvnc
|
||||
except ModuleNotFoundError:
|
||||
log.warning(
|
||||
"In order to leverage `piker`'s built-in data reset hacks, install "
|
||||
"the `pyvnc` project: https://github.com/regulad/pyvnc.git"
|
||||
"the `asyncvnc` project: https://github.com/barneygale/asyncvnc"
|
||||
)
|
||||
return
|
||||
|
||||
|
|
@ -250,27 +216,24 @@ async def vnc_click_hack(
|
|||
'connection': 'r'
|
||||
}[reset_type]
|
||||
|
||||
with tractor.devx.open_crash_handler():
|
||||
client = await AsyncVNCClient.connect(
|
||||
VNCConfig(
|
||||
host=host,
|
||||
port=port,
|
||||
password=pw,
|
||||
)
|
||||
async with asyncvnc.connect(
|
||||
host,
|
||||
port=port,
|
||||
|
||||
# TODO: doesn't work?
|
||||
# see, https://github.com/barneygale/asyncvnc/issues/7
|
||||
password='doggy',
|
||||
|
||||
) as client:
|
||||
|
||||
# move to middle of screen
|
||||
# 640x1800
|
||||
client.mouse.move(
|
||||
x=500,
|
||||
y=500,
|
||||
)
|
||||
async with client:
|
||||
# move to middle of screen
|
||||
# 640x1800
|
||||
await client.move(
|
||||
Point(
|
||||
500,
|
||||
500,
|
||||
)
|
||||
)
|
||||
# ensure the ib-gw window is active
|
||||
await client.click(MOUSE_BUTTON_LEFT)
|
||||
# send the hotkeys combo B)
|
||||
await client.press('Ctrl', 'Alt', key) # keys are stacked
|
||||
client.mouse.click()
|
||||
client.keyboard.press('Ctrl', 'Alt', key) # keys are stacked
|
||||
|
||||
|
||||
def i3ipc_fin_wins_titled(
|
||||
|
|
|
|||
|
|
@ -97,6 +97,10 @@ from ._util import (
|
|||
get_logger,
|
||||
)
|
||||
|
||||
# ?TODO? this can now be removed since it was originally to extend
|
||||
# with a `bar_vwap` field that we removed from the default ohlcv
|
||||
# dtype since it's better calculated in an FSP func
|
||||
#
|
||||
_bar_load_dtype: list[tuple[str, type]] = [
|
||||
# NOTE XXX: only part that's diff
|
||||
# from our default fields where
|
||||
|
|
@ -944,7 +948,6 @@ class Client:
|
|||
)
|
||||
if tkr:
|
||||
break
|
||||
|
||||
except TimeoutError as err:
|
||||
timeouterr = err
|
||||
await asyncio.sleep(0.01)
|
||||
|
|
@ -953,9 +956,7 @@ class Client:
|
|||
else:
|
||||
if not warnset:
|
||||
log.warning(
|
||||
f'Quote req timed out..\n'
|
||||
f'Maybe the venue is closed?\n'
|
||||
f'\n'
|
||||
f'Quote req timed out..maybe venue is closed?\n'
|
||||
f'{asdict(contract)}'
|
||||
)
|
||||
warnset = True
|
||||
|
|
@ -967,11 +968,9 @@ class Client:
|
|||
)
|
||||
break
|
||||
else:
|
||||
if (
|
||||
timeouterr
|
||||
and
|
||||
raise_on_timeout
|
||||
):
|
||||
if timeouterr and raise_on_timeout:
|
||||
import pdbp
|
||||
pdbp.set_trace()
|
||||
raise timeouterr
|
||||
|
||||
if not warnset:
|
||||
|
|
@ -1368,7 +1367,9 @@ async def load_aio_clients(
|
|||
|
||||
|
||||
async def load_clients_for_trio(
|
||||
chan: tractor.to_asyncio.LinkedTaskChannel,
|
||||
from_trio: asyncio.Queue,
|
||||
to_trio: trio.abc.SendChannel,
|
||||
|
||||
) -> None:
|
||||
'''
|
||||
Pure async mngr proxy to ``load_aio_clients()``.
|
||||
|
|
@ -1381,7 +1382,8 @@ async def load_clients_for_trio(
|
|||
disconnect_on_exit=False,
|
||||
) as accts2clients:
|
||||
|
||||
chan.started_nowait(accts2clients)
|
||||
to_trio.send_nowait(accts2clients)
|
||||
|
||||
# TODO: maybe a sync event to wait on instead?
|
||||
await asyncio.sleep(float('inf'))
|
||||
|
||||
|
|
@ -1528,22 +1530,23 @@ class MethodProxy:
|
|||
|
||||
|
||||
async def open_aio_client_method_relay(
|
||||
chan: tractor.to_asyncio.LinkedTaskChannel,
|
||||
from_trio: asyncio.Queue,
|
||||
to_trio: trio.abc.SendChannel,
|
||||
client: Client,
|
||||
event_consumers: dict[str, trio.Event],
|
||||
|
||||
) -> None:
|
||||
|
||||
# sync with `open_client_proxy()` caller
|
||||
chan.started_nowait(client)
|
||||
to_trio.send_nowait(client)
|
||||
|
||||
# TODO: separate channel for error handling?
|
||||
client.inline_errors(chan)
|
||||
client.inline_errors(to_trio)
|
||||
|
||||
# relay all method requests to ``asyncio``-side client and deliver
|
||||
# back results
|
||||
while not chan._to_trio._closed: # <- TODO, better check like `._web_bs`?
|
||||
msg: tuple[str, dict]|dict|None = await chan.get()
|
||||
while not to_trio._closed:
|
||||
msg: tuple[str, dict]|dict|None = await from_trio.get()
|
||||
match msg:
|
||||
case None: # termination sentinel
|
||||
log.info('asyncio `Client` method-proxy SHUTDOWN!')
|
||||
|
|
@ -1556,7 +1559,7 @@ async def open_aio_client_method_relay(
|
|||
try:
|
||||
resp = await meth(**kwargs)
|
||||
# echo the msg back
|
||||
chan.send_nowait({'result': resp})
|
||||
to_trio.send_nowait({'result': resp})
|
||||
|
||||
except (
|
||||
RequestError,
|
||||
|
|
@ -1564,10 +1567,10 @@ async def open_aio_client_method_relay(
|
|||
# TODO: relay all errors to trio?
|
||||
# BaseException,
|
||||
) as err:
|
||||
chan.send_nowait({'exception': err})
|
||||
to_trio.send_nowait({'exception': err})
|
||||
|
||||
case {'error': content}:
|
||||
chan.send_nowait({'exception': content})
|
||||
to_trio.send_nowait({'exception': content})
|
||||
|
||||
case _:
|
||||
raise ValueError(f'Unhandled msg {msg}')
|
||||
|
|
|
|||
|
|
@ -117,11 +117,7 @@ def pack_position(
|
|||
symbol=fqme,
|
||||
currency=con.currency,
|
||||
size=float(pos.position),
|
||||
avg_price=(
|
||||
float(pos.avgCost)
|
||||
/
|
||||
float(con.multiplier or 1.0)
|
||||
),
|
||||
avg_price=float(pos.avgCost) / float(con.multiplier or 1.0),
|
||||
),
|
||||
)
|
||||
|
||||
|
|
@ -362,10 +358,6 @@ async def update_and_audit_pos_msg(
|
|||
size=ibpos.position,
|
||||
|
||||
avg_price=pikerpos.ppu,
|
||||
|
||||
# XXX ensures matching even if multiple venue-names
|
||||
# in `.bs_fqme`, likely from txn records..
|
||||
bs_mktid=mkt.bs_mktid,
|
||||
)
|
||||
|
||||
ibfmtmsg: str = pformat(ibpos._asdict())
|
||||
|
|
@ -434,8 +426,7 @@ async def aggr_open_orders(
|
|||
|
||||
) -> None:
|
||||
'''
|
||||
Collect all open orders from client and fill in `order_msgs:
|
||||
list`.
|
||||
Collect all open orders from client and fill in `order_msgs: list`.
|
||||
|
||||
'''
|
||||
trades: list[Trade] = client.ib.openTrades()
|
||||
|
|
@ -567,7 +558,7 @@ async def open_trade_dialog(
|
|||
ledgers: dict[str, TransactionLedger] = {}
|
||||
tables: dict[str, Account] = {}
|
||||
order_msgs: list[Status] = []
|
||||
conf: dict = get_config()
|
||||
conf = get_config()
|
||||
accounts_def_inv: bidict[str, str] = bidict(
|
||||
conf['accounts']
|
||||
).inverse
|
||||
|
|
|
|||
|
|
@ -214,9 +214,7 @@ async def open_history_client(
|
|||
|
||||
# could be trying to retreive bars over weekend
|
||||
if out is None:
|
||||
log.error(
|
||||
f"No bars starting at {end_dt!r} !?!?"
|
||||
)
|
||||
log.error(f"Can't grab bars starting at {end_dt}!?!?")
|
||||
if (
|
||||
end_dt
|
||||
and head_dt
|
||||
|
|
@ -613,7 +611,7 @@ async def get_bars(
|
|||
data_cs.cancel()
|
||||
|
||||
# spawn new data reset task
|
||||
data_cs, reset_done = await tn.start(
|
||||
data_cs, reset_done = await nurse.start(
|
||||
partial(
|
||||
wait_on_data_reset,
|
||||
proxy,
|
||||
|
|
@ -635,12 +633,12 @@ async def get_bars(
|
|||
unset_resetter: bool = False
|
||||
async with (
|
||||
tractor.trionics.collapse_eg(),
|
||||
trio.open_nursery() as tn
|
||||
trio.open_nursery() as nurse
|
||||
):
|
||||
|
||||
# start history request that we allow
|
||||
# to run indefinitely until a result is acquired
|
||||
tn.start_soon(query)
|
||||
nurse.start_soon(query)
|
||||
|
||||
# start history reset loop which waits up to the timeout
|
||||
# for a result before triggering a data feed reset.
|
||||
|
|
@ -660,7 +658,7 @@ async def get_bars(
|
|||
unset_resetter: bool = True
|
||||
|
||||
# spawn new data reset task
|
||||
data_cs, reset_done = await tn.start(
|
||||
data_cs, reset_done = await nurse.start(
|
||||
partial(
|
||||
wait_on_data_reset,
|
||||
proxy,
|
||||
|
|
@ -686,6 +684,8 @@ async def get_bars(
|
|||
_quote_streams: dict[str, trio.abc.ReceiveStream] = {}
|
||||
|
||||
|
||||
# TODO! update to the new style sig with,
|
||||
# `chan: to_asyncio.LinkedTaskChannel,`
|
||||
async def _setup_quote_stream(
|
||||
chan: tractor.to_asyncio.LinkedTaskChannel,
|
||||
symbol: str,
|
||||
|
|
@ -896,10 +896,7 @@ async def open_aio_quote_stream(
|
|||
symbol: str,
|
||||
contract: Contract|None = None,
|
||||
|
||||
) -> (
|
||||
trio.abc.Channel| # iface
|
||||
tractor.to_asyncio.LinkedTaskChannel # actually
|
||||
):
|
||||
) -> trio.abc.ReceiveStream:
|
||||
'''
|
||||
Open a real-time `Ticker` quote stream from an `asyncio.Task`
|
||||
spawned via `tractor.to_asyncio.open_channel_from()`, deliver the
|
||||
|
|
@ -922,7 +919,6 @@ async def open_aio_quote_stream(
|
|||
yield from_aio
|
||||
return
|
||||
|
||||
from_aio: tractor.to_asyncio.LinkedTaskChannel
|
||||
async with tractor.to_asyncio.open_channel_from(
|
||||
_setup_quote_stream,
|
||||
symbol=symbol,
|
||||
|
|
@ -1083,8 +1079,7 @@ async def stream_quotes(
|
|||
con: Contract = details.contract
|
||||
first_ticker: Ticker|None = None
|
||||
|
||||
timeout: float = 1.6
|
||||
with trio.move_on_after(timeout) as quote_cs:
|
||||
with trio.move_on_after(1.6) as quote_cs:
|
||||
first_ticker: Ticker = await proxy.get_quote(
|
||||
contract=con,
|
||||
raise_on_timeout=False,
|
||||
|
|
@ -1093,9 +1088,7 @@ async def stream_quotes(
|
|||
# XXX should never happen with this ep right?
|
||||
# but if so then, more then likely mkt is closed?
|
||||
if quote_cs.cancelled_caught:
|
||||
log.warning(
|
||||
f'First quote req timed out after {timeout!r}s'
|
||||
)
|
||||
await tractor.pause()
|
||||
|
||||
if first_ticker:
|
||||
first_quote: dict = normalize(first_ticker)
|
||||
|
|
@ -1168,7 +1161,6 @@ async def stream_quotes(
|
|||
)
|
||||
cs: trio.CancelScope|None = None
|
||||
startup: bool = True
|
||||
iter_quotes: trio.abc.Channel
|
||||
while (
|
||||
startup
|
||||
or
|
||||
|
|
@ -1177,11 +1169,11 @@ async def stream_quotes(
|
|||
with trio.CancelScope() as cs:
|
||||
async with (
|
||||
tractor.trionics.collapse_eg(),
|
||||
trio.open_nursery() as tn,
|
||||
trio.open_nursery() as nurse,
|
||||
open_aio_quote_stream(
|
||||
symbol=sym,
|
||||
contract=con,
|
||||
) as iter_quotes,
|
||||
) as stream,
|
||||
):
|
||||
# ?TODO? can we rm this - particularly for `ib_async`?
|
||||
# ugh, clear ticks since we've consumed them
|
||||
|
|
@ -1210,9 +1202,9 @@ async def stream_quotes(
|
|||
await rt_ev.wait()
|
||||
cs.cancel() # cancel called should now be set
|
||||
|
||||
tn.start_soon(reset_on_feed)
|
||||
nurse.start_soon(reset_on_feed)
|
||||
|
||||
async with aclosing(iter_quotes):
|
||||
async with aclosing(stream):
|
||||
# if syminfo.get('no_vlm', False):
|
||||
if not init_msg.shm_write_opts['has_vlm']:
|
||||
|
||||
|
|
@ -1227,21 +1219,19 @@ async def stream_quotes(
|
|||
# wait for real volume on feed (trading might be
|
||||
# closed)
|
||||
while True:
|
||||
ticker = await iter_quotes.receive()
|
||||
ticker = await stream.receive()
|
||||
|
||||
# for a real volume contract we rait for
|
||||
# the first "real" trade to take place
|
||||
if (
|
||||
# not calc_price
|
||||
# and not ticker.rtTime
|
||||
False
|
||||
# not ticker.rtTime
|
||||
not ticker.rtTime
|
||||
):
|
||||
# spin consuming tickers until we
|
||||
# get a real market datum
|
||||
log.debug(f"New unsent ticker: {ticker}")
|
||||
continue
|
||||
|
||||
else:
|
||||
log.debug("Received first volume tick")
|
||||
# ugh, clear ticks since we've
|
||||
|
|
@ -1257,18 +1247,13 @@ async def stream_quotes(
|
|||
log.debug(f"First ticker received {quote}")
|
||||
|
||||
# tell data-layer spawner-caller that live
|
||||
# quotes are now active desptie not having
|
||||
# necessarily received a first vlm/clearing
|
||||
# tick.
|
||||
ticker = await iter_quotes.receive()
|
||||
# quotes are now streaming.
|
||||
feed_is_live.set()
|
||||
fqme: str = quote['fqme']
|
||||
await send_chan.send({fqme: quote})
|
||||
|
||||
# last = time.time()
|
||||
async for ticker in iter_quotes:
|
||||
async for ticker in stream:
|
||||
quote = normalize(ticker)
|
||||
fqme: str = quote['fqme']
|
||||
fqme = quote['fqme']
|
||||
log.debug(
|
||||
f'Sending quote\n'
|
||||
f'{quote}'
|
||||
|
|
|
|||
|
|
@ -549,7 +549,7 @@ async def open_trade_dialog(
|
|||
# to be reloaded.
|
||||
balances: dict[str, float] = await client.get_balances()
|
||||
|
||||
await verify_balances(
|
||||
verify_balances(
|
||||
acnt,
|
||||
src_fiat,
|
||||
balances,
|
||||
|
|
|
|||
|
|
@ -37,12 +37,6 @@ import tractor
|
|||
from async_generator import asynccontextmanager
|
||||
import numpy as np
|
||||
import wrapt
|
||||
|
||||
# TODO, port to `httpx`/`trio-websocket` whenver i get back to
|
||||
# writing a proper ws-api streamer for this backend (since the data
|
||||
# feeds are free now) as per GH feat-req:
|
||||
# https://github.com/pikers/piker/issues/509
|
||||
#
|
||||
import asks
|
||||
|
||||
from ..calc import humanize, percent_change
|
||||
|
|
|
|||
|
|
@ -171,6 +171,7 @@ class OrderClient(Struct):
|
|||
|
||||
|
||||
async def relay_orders_from_sync_code(
|
||||
|
||||
client: OrderClient,
|
||||
symbol_key: str,
|
||||
to_ems_stream: tractor.MsgStream,
|
||||
|
|
@ -244,11 +245,6 @@ async def open_ems(
|
|||
|
||||
async with maybe_open_emsd(
|
||||
broker,
|
||||
# XXX NOTE, LOL so this determines the daemon `emsd` loglevel
|
||||
# then FYI.. that's kinda wrong no?
|
||||
# -[ ] shouldn't it be set by `pikerd -l` or no?
|
||||
# -[ ] would make a lot more sense to have a subsys ctl for
|
||||
# levels.. like `-l emsd.info` or something?
|
||||
loglevel=loglevel,
|
||||
) as portal:
|
||||
|
||||
|
|
|
|||
|
|
@ -388,7 +388,6 @@ async def open_brokerd_dialog(
|
|||
for ep_name in [
|
||||
'open_trade_dialog', # probably final name?
|
||||
'trades_dialogue', # legacy
|
||||
# ^!TODO, rm this since all backends ported no ?!?
|
||||
]:
|
||||
trades_endpoint = getattr(
|
||||
brokermod,
|
||||
|
|
@ -655,11 +654,7 @@ class Router(Struct):
|
|||
flume = feed.flumes[fqme]
|
||||
first_quote: dict = flume.first_quote
|
||||
book: DarkBook = self.get_dark_book(broker)
|
||||
|
||||
if not (last := first_quote.get('last')):
|
||||
last: float = flume.rt_shm.array[-1]['close']
|
||||
|
||||
book.lasts[fqme]: float = float(last)
|
||||
book.lasts[fqme]: float = float(first_quote['last'])
|
||||
|
||||
async with self.maybe_open_brokerd_dialog(
|
||||
brokermod=brokermod,
|
||||
|
|
@ -722,7 +717,7 @@ class Router(Struct):
|
|||
subs = self.subscribers[sub_key]
|
||||
|
||||
sent_some: bool = False
|
||||
for client_stream in subs.copy():
|
||||
for client_stream in subs:
|
||||
try:
|
||||
await client_stream.send(msg)
|
||||
sent_some = True
|
||||
|
|
@ -1018,28 +1013,14 @@ async def translate_and_relay_brokerd_events(
|
|||
status_msg.brokerd_msg = msg
|
||||
status_msg.src = msg.broker_details['name']
|
||||
|
||||
if not status_msg.req:
|
||||
# likely some order change state?
|
||||
await tractor.pause()
|
||||
else:
|
||||
await router.client_broadcast(
|
||||
status_msg.req.symbol,
|
||||
status_msg,
|
||||
)
|
||||
await router.client_broadcast(
|
||||
status_msg.req.symbol,
|
||||
status_msg,
|
||||
)
|
||||
|
||||
if status == 'closed':
|
||||
log.info(
|
||||
f'Execution is complete!\n'
|
||||
f'oid: {oid!r}\n'
|
||||
)
|
||||
status_msg = book._active.pop(oid, None)
|
||||
if status_msg is None:
|
||||
log.warning(
|
||||
f'Order was already cleared from book ??\n'
|
||||
f'oid: {oid!r}\n'
|
||||
f'\n'
|
||||
f'Maybe the order cancelled before submitted ??\n'
|
||||
)
|
||||
log.info(f'Execution for {oid} is complete!')
|
||||
status_msg = book._active.pop(oid)
|
||||
|
||||
elif status == 'canceled':
|
||||
log.cancel(f'Cancellation for {oid} is complete!')
|
||||
|
|
@ -1563,18 +1544,19 @@ async def maybe_open_trade_relays(
|
|||
|
||||
@tractor.context
|
||||
async def _emsd_main(
|
||||
ctx: tractor.Context, # becomes `ems_ctx` below
|
||||
ctx: tractor.Context,
|
||||
fqme: str,
|
||||
exec_mode: str, # ('paper', 'live')
|
||||
loglevel: str|None = None,
|
||||
|
||||
) -> tuple[ # `ctx.started()` value!
|
||||
dict[ # positions
|
||||
tuple[str, str], # brokername, acctid
|
||||
) -> tuple[
|
||||
dict[
|
||||
# brokername, acctid
|
||||
tuple[str, str],
|
||||
list[BrokerdPosition],
|
||||
],
|
||||
list[str], # accounts
|
||||
dict[str, Status], # dialogs
|
||||
list[str],
|
||||
dict[str, Status],
|
||||
]:
|
||||
'''
|
||||
EMS (sub)actor entrypoint providing the execution management
|
||||
|
|
|
|||
|
|
@ -301,9 +301,6 @@ class BrokerdError(Struct):
|
|||
|
||||
# TODO: yeah, so we REALLY need to completely deprecate
|
||||
# this and use the `.accounting.Position` msg-type instead..
|
||||
# -[ ] an alternative might be to add a `Position.summary() ->
|
||||
# `PositionSummary`-msg that we generate since `Position` has a lot
|
||||
# of fields by default we likely don't want to send over the wire?
|
||||
class BrokerdPosition(Struct):
|
||||
'''
|
||||
Position update event from brokerd.
|
||||
|
|
@ -316,4 +313,3 @@ class BrokerdPosition(Struct):
|
|||
avg_price: float
|
||||
currency: str = ''
|
||||
name: str = 'position'
|
||||
bs_mktid: str|int|None = None
|
||||
|
|
|
|||
|
|
@ -297,8 +297,6 @@ class PaperBoi(Struct):
|
|||
|
||||
# transmit pp msg to ems
|
||||
pp: Position = self.acnt.pps[bs_mktid]
|
||||
# TODO, this will break if `require_only=True` was passed to
|
||||
# `.update_from_ledger()`
|
||||
|
||||
pp_msg = BrokerdPosition(
|
||||
broker=self.broker,
|
||||
|
|
@ -655,7 +653,6 @@ async def open_trade_dialog(
|
|||
# in) use manually constructed table from calling
|
||||
# the `.get_mkt_info()` provider EP above.
|
||||
_mktmap_table=mkt_by_fqme,
|
||||
only_require=list(mkt_by_fqme),
|
||||
)
|
||||
|
||||
pp_msgs: list[BrokerdPosition] = []
|
||||
|
|
|
|||
|
|
@ -30,7 +30,6 @@ subsys: str = 'piker.clearing'
|
|||
|
||||
log = get_logger(subsys)
|
||||
|
||||
# TODO, oof doesn't this ignore the `loglevel` then???
|
||||
get_console_log = partial(
|
||||
get_console_log,
|
||||
name=subsys,
|
||||
|
|
|
|||
|
|
@ -183,8 +183,8 @@ def pikerd(
|
|||
registry_addrs=regaddrs,
|
||||
loglevel=loglevel,
|
||||
debug_mode=pdb,
|
||||
# enable_transports=['uds'],
|
||||
enable_transports=['tcp'],
|
||||
enable_transports=['uds'],
|
||||
# enable_transports=['tcp'],
|
||||
) as service_mngr,
|
||||
):
|
||||
assert service_mngr
|
||||
|
|
|
|||
|
|
@ -41,13 +41,10 @@ from .log import get_logger
|
|||
log = get_logger('broker-config')
|
||||
|
||||
|
||||
# XXX NOTE: taken from `click`
|
||||
# |_https://github.com/pallets/click/blob/main/src/click/utils.py#L449
|
||||
#
|
||||
# (since apparently they have some super weirdness with SIGINT and
|
||||
# sudo.. no clue we're probably going to slowly just modify it to our
|
||||
# own version over time..)
|
||||
#
|
||||
# XXX NOTE: taken from ``click`` since apparently they have some
|
||||
# super weirdness with sigint and sudo..no clue
|
||||
# we're probably going to slowly just modify it to our own version over
|
||||
# time..
|
||||
def get_app_dir(
|
||||
app_name: str,
|
||||
roaming: bool = True,
|
||||
|
|
@ -264,7 +261,7 @@ def load(
|
|||
MutableMapping,
|
||||
] = tomllib.loads,
|
||||
|
||||
touch_if_dne: bool = True,
|
||||
touch_if_dne: bool = False,
|
||||
|
||||
**tomlkws,
|
||||
|
||||
|
|
@ -273,7 +270,7 @@ def load(
|
|||
Load config file by name.
|
||||
|
||||
If desired config is not in the top level piker-user config path then
|
||||
pass the `path: Path` explicitly.
|
||||
pass the ``path: Path`` explicitly.
|
||||
|
||||
'''
|
||||
# create the $HOME/.config/piker dir if dne
|
||||
|
|
@ -288,8 +285,7 @@ def load(
|
|||
|
||||
if (
|
||||
not path.is_file()
|
||||
and
|
||||
touch_if_dne
|
||||
and touch_if_dne
|
||||
):
|
||||
# only do a template if no path provided,
|
||||
# just touch an empty file with same name.
|
||||
|
|
|
|||
|
|
@ -95,12 +95,6 @@ class Sampler:
|
|||
# history loading.
|
||||
incr_task_cs: trio.CancelScope | None = None
|
||||
|
||||
bcast_errors: tuple[Exception] = (
|
||||
trio.BrokenResourceError,
|
||||
trio.ClosedResourceError,
|
||||
trio.EndOfChannel,
|
||||
)
|
||||
|
||||
# holds all the ``tractor.Context`` remote subscriptions for
|
||||
# a particular sample period increment event: all subscribers are
|
||||
# notified on a step.
|
||||
|
|
@ -264,15 +258,14 @@ class Sampler:
|
|||
subs: set
|
||||
last_ts, subs = pair
|
||||
|
||||
# NOTE, for debugging pub-sub issues
|
||||
# task = trio.lowlevel.current_task()
|
||||
# log.debug(
|
||||
# f'AlL-SUBS@{period_s!r}: {self.subscribers}\n'
|
||||
# f'PAIR: {pair}\n'
|
||||
# f'TASK: {task}: {id(task)}\n'
|
||||
# f'broadcasting {period_s} -> {last_ts}\n'
|
||||
# f'consumers: {subs}'
|
||||
# )
|
||||
task = trio.lowlevel.current_task()
|
||||
log.debug(
|
||||
f'SUBS {self.subscribers}\n'
|
||||
f'PAIR {pair}\n'
|
||||
f'TASK: {task}: {id(task)}\n'
|
||||
f'broadcasting {period_s} -> {last_ts}\n'
|
||||
# f'consumers: {subs}'
|
||||
)
|
||||
borked: set[MsgStream] = set()
|
||||
sent: set[MsgStream] = set()
|
||||
while True:
|
||||
|
|
@ -289,11 +282,13 @@ class Sampler:
|
|||
await stream.send(msg)
|
||||
sent.add(stream)
|
||||
|
||||
except self.bcast_errors as err:
|
||||
except (
|
||||
trio.BrokenResourceError,
|
||||
trio.ClosedResourceError,
|
||||
trio.EndOfChannel,
|
||||
):
|
||||
log.error(
|
||||
f'Connection dropped for IPC ctx\n'
|
||||
f'{stream._ctx}\n\n'
|
||||
f'Due to {type(err)}'
|
||||
f'{stream._ctx.chan.uid} dropped connection'
|
||||
)
|
||||
borked.add(stream)
|
||||
else:
|
||||
|
|
@ -400,8 +395,7 @@ async def register_with_sampler(
|
|||
finally:
|
||||
if (
|
||||
sub_for_broadcasts
|
||||
and
|
||||
subs
|
||||
and subs
|
||||
):
|
||||
try:
|
||||
subs.remove(stream)
|
||||
|
|
@ -568,7 +562,8 @@ async def open_sample_stream(
|
|||
|
||||
|
||||
async def sample_and_broadcast(
|
||||
bus: _FeedsBus,
|
||||
|
||||
bus: _FeedsBus, # noqa
|
||||
rt_shm: ShmArray,
|
||||
hist_shm: ShmArray,
|
||||
quote_stream: trio.abc.ReceiveChannel,
|
||||
|
|
@ -588,33 +583,11 @@ async def sample_and_broadcast(
|
|||
|
||||
overruns = Counter()
|
||||
|
||||
# NOTE, only used for debugging live-data-feed issues, though
|
||||
# this should be resolved more correctly in the future using the
|
||||
# new typed-msgspec feats of `tractor`!
|
||||
#
|
||||
# XXX, a multiline nested `dict` formatter (since rn quote-msgs
|
||||
# are just that).
|
||||
# pfmt: Callable[[str], str] = mk_repr()
|
||||
|
||||
# iterate stream delivered by broker
|
||||
async for quotes in quote_stream:
|
||||
# print(quotes)
|
||||
|
||||
# XXX WARNING XXX only enable for debugging bc ow can cost
|
||||
# ALOT of perf with HF-feedz!!!
|
||||
#
|
||||
# log.info(
|
||||
# 'Rx live quotes:\n'
|
||||
# f'{pfmt(quotes)}'
|
||||
# )
|
||||
|
||||
# TODO,
|
||||
# -[ ] `numba` or `cython`-nize this loop possibly?
|
||||
# |_alternatively could we do it in rust somehow by upacking
|
||||
# arrow msgs instead of using `msgspec`?
|
||||
# -[ ] use `msgspec.Struct` support in new typed-msging from
|
||||
# `tractor` to ensure only allowed msgs are transmitted?
|
||||
#
|
||||
# TODO: ``numba`` this!
|
||||
for broker_symbol, quote in quotes.items():
|
||||
# TODO: in theory you can send the IPC msg *before* writing
|
||||
# to the sharedmem array to decrease latency, however, that
|
||||
|
|
@ -687,21 +660,6 @@ async def sample_and_broadcast(
|
|||
sub_key: str = broker_symbol.lower()
|
||||
subs: set[Sub] = bus.get_subs(sub_key)
|
||||
|
||||
# TODO, figure out how to make this useful whilst
|
||||
# incoporating feed "pausing" ..
|
||||
#
|
||||
# if not subs:
|
||||
# all_bs_fqmes: list[str] = list(
|
||||
# bus._subscribers.keys()
|
||||
# )
|
||||
# log.warning(
|
||||
# f'No subscribers for {brokername!r} live-quote ??\n'
|
||||
# f'broker_symbol: {broker_symbol}\n\n'
|
||||
|
||||
# f'Maybe the backend-sys symbol does not match one of,\n'
|
||||
# f'{pfmt(all_bs_fqmes)}\n'
|
||||
# )
|
||||
|
||||
# NOTE: by default the broker backend doesn't append
|
||||
# it's own "name" into the fqme schema (but maybe it
|
||||
# should?) so we have to manually generate the correct
|
||||
|
|
@ -771,14 +729,18 @@ async def sample_and_broadcast(
|
|||
if lags > 10:
|
||||
await tractor.pause()
|
||||
|
||||
except Sampler.bcast_errors as ipc_err:
|
||||
except (
|
||||
trio.BrokenResourceError,
|
||||
trio.ClosedResourceError,
|
||||
trio.EndOfChannel,
|
||||
):
|
||||
ctx: Context = ipc._ctx
|
||||
chan: Channel = ctx.chan
|
||||
if ctx:
|
||||
log.warning(
|
||||
f'Dropped `brokerd`-feed for {broker_symbol!r} due to,\n'
|
||||
f'x>) {ctx.cid}@{chan.uid}'
|
||||
f'|_{ipc_err!r}\n\n'
|
||||
'Dropped `brokerd`-quotes-feed connection:\n'
|
||||
f'{broker_symbol}:'
|
||||
f'{ctx.cid}@{chan.uid}'
|
||||
)
|
||||
if sub.throttle_rate:
|
||||
assert ipc._closed
|
||||
|
|
@ -795,11 +757,12 @@ async def sample_and_broadcast(
|
|||
|
||||
|
||||
async def uniform_rate_send(
|
||||
|
||||
rate: float,
|
||||
quote_stream: trio.abc.ReceiveChannel,
|
||||
stream: MsgStream,
|
||||
|
||||
task_status: TaskStatus[None] = trio.TASK_STATUS_IGNORED,
|
||||
task_status: TaskStatus = trio.TASK_STATUS_IGNORED,
|
||||
|
||||
) -> None:
|
||||
'''
|
||||
|
|
@ -817,16 +780,13 @@ async def uniform_rate_send(
|
|||
https://gist.github.com/njsmith/7ea44ec07e901cb78ebe1dd8dd846cb9
|
||||
|
||||
'''
|
||||
# ?TODO? dynamically compute the **actual** approx overhead latency per cycle
|
||||
# instead of this magic # bidinezz?
|
||||
throttle_period: float = 1/rate - 0.000616
|
||||
left_to_sleep: float = throttle_period
|
||||
# TODO: compute the approx overhead latency per cycle
|
||||
left_to_sleep = throttle_period = 1/rate - 0.000616
|
||||
|
||||
# send cycle state
|
||||
first_quote: dict|None
|
||||
first_quote = last_quote = None
|
||||
last_send: float = time.time()
|
||||
diff: float = 0
|
||||
last_send = time.time()
|
||||
diff = 0
|
||||
|
||||
task_status.started()
|
||||
ticks_by_type: dict[
|
||||
|
|
@ -837,28 +797,22 @@ async def uniform_rate_send(
|
|||
clear_types = _tick_groups['clears']
|
||||
|
||||
while True:
|
||||
|
||||
# compute the remaining time to sleep for this throttled cycle
|
||||
left_to_sleep: float = throttle_period - diff
|
||||
left_to_sleep = throttle_period - diff
|
||||
|
||||
if left_to_sleep > 0:
|
||||
cs: trio.CancelScope
|
||||
with trio.move_on_after(left_to_sleep) as cs:
|
||||
sym: str
|
||||
last_quote: dict
|
||||
try:
|
||||
sym, last_quote = await quote_stream.receive()
|
||||
except trio.EndOfChannel:
|
||||
log.exception(
|
||||
f'Live stream for feed for ended?\n'
|
||||
f'<=c\n'
|
||||
f' |_[{stream!r}\n'
|
||||
)
|
||||
log.exception(f"feed for {stream} ended?")
|
||||
break
|
||||
|
||||
diff: float = time.time() - last_send
|
||||
diff = time.time() - last_send
|
||||
|
||||
if not first_quote:
|
||||
first_quote: float = last_quote
|
||||
first_quote = last_quote
|
||||
# first_quote['tbt'] = ticks_by_type
|
||||
|
||||
if (throttle_period - diff) > 0:
|
||||
|
|
@ -919,12 +873,11 @@ async def uniform_rate_send(
|
|||
# TODO: now if only we could sync this to the display
|
||||
# rate timing exactly lul
|
||||
try:
|
||||
await stream.send({
|
||||
sym: first_quote
|
||||
})
|
||||
await stream.send({sym: first_quote})
|
||||
except tractor.RemoteActorError as rme:
|
||||
if rme.type is not tractor._exceptions.StreamOverrun:
|
||||
raise
|
||||
|
||||
ctx = stream._ctx
|
||||
chan = ctx.chan
|
||||
log.warning(
|
||||
|
|
@ -932,28 +885,20 @@ async def uniform_rate_send(
|
|||
f'{sym}:{ctx.cid}@{chan.uid}'
|
||||
)
|
||||
|
||||
# NOTE: any of these can be raised by `tractor`'s IPC
|
||||
# transport-layer and we want to be highly resilient
|
||||
# to consumers which crash or lose network connection.
|
||||
# I.e. we **DO NOT** want to crash and propagate up to
|
||||
# ``pikerd`` these kinds of errors!
|
||||
except (
|
||||
# NOTE: any of these can be raised by ``tractor``'s IPC
|
||||
# transport-layer and we want to be highly resilient
|
||||
# to consumers which crash or lose network connection.
|
||||
# I.e. we **DO NOT** want to crash and propagate up to
|
||||
# ``pikerd`` these kinds of errors!
|
||||
trio.ClosedResourceError,
|
||||
trio.BrokenResourceError,
|
||||
ConnectionResetError,
|
||||
) + Sampler.bcast_errors as ipc_err:
|
||||
match ipc_err:
|
||||
case trio.EndOfChannel():
|
||||
log.info(
|
||||
f'{stream} terminated by peer,\n'
|
||||
f'{ipc_err!r}'
|
||||
)
|
||||
case _:
|
||||
# if the feed consumer goes down then drop
|
||||
# out of this rate limiter
|
||||
log.warning(
|
||||
f'{stream} closed due to,\n'
|
||||
f'{ipc_err!r}'
|
||||
)
|
||||
|
||||
trio.EndOfChannel,
|
||||
):
|
||||
# if the feed consumer goes down then drop
|
||||
# out of this rate limiter
|
||||
log.warning(f'{stream} closed')
|
||||
await stream.aclose()
|
||||
return
|
||||
|
||||
|
|
|
|||
|
|
@ -31,7 +31,6 @@ from pathlib import Path
|
|||
from pprint import pformat
|
||||
from typing import (
|
||||
Any,
|
||||
Callable,
|
||||
Sequence,
|
||||
Hashable,
|
||||
TYPE_CHECKING,
|
||||
|
|
@ -57,7 +56,7 @@ from piker.brokers import (
|
|||
)
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from piker.accounting import (
|
||||
from ..accounting import (
|
||||
Asset,
|
||||
MktPair,
|
||||
)
|
||||
|
|
@ -91,18 +90,6 @@ class SymbologyCache(Struct):
|
|||
# provided by the backend pkg.
|
||||
mktmaps: dict[str, MktPair] = field(default_factory=dict)
|
||||
|
||||
def pformat(self) -> str:
|
||||
return (
|
||||
f'<{type(self).__name__}(\n'
|
||||
f' .mod: {self.mod!r}\n'
|
||||
f' .assets: {len(self.assets)!r}\n'
|
||||
f' .pairs: {len(self.pairs)!r}\n'
|
||||
f' .mktmaps: {len(self.mktmaps)!r}\n'
|
||||
f')>'
|
||||
)
|
||||
|
||||
__repr__ = pformat
|
||||
|
||||
def write_config(self) -> None:
|
||||
|
||||
# put the backend's pair-struct type ref at the top
|
||||
|
|
@ -162,68 +149,57 @@ class SymbologyCache(Struct):
|
|||
'Implement `Client.get_assets()`!'
|
||||
)
|
||||
|
||||
get_mkt_pairs: Callable|None = getattr(
|
||||
client,
|
||||
'get_mkt_pairs',
|
||||
None,
|
||||
)
|
||||
if not get_mkt_pairs:
|
||||
if get_mkt_pairs := getattr(client, 'get_mkt_pairs', None):
|
||||
|
||||
pairs: dict[str, Struct] = await get_mkt_pairs()
|
||||
for bs_fqme, pair in pairs.items():
|
||||
|
||||
# NOTE: every backend defined pair should
|
||||
# declare it's ns path for roundtrip
|
||||
# serialization lookup.
|
||||
if not getattr(pair, 'ns_path', None):
|
||||
raise TypeError(
|
||||
f'Pair-struct for {self.mod.name} MUST define a '
|
||||
'`.ns_path: str`!\n'
|
||||
f'{pair}'
|
||||
)
|
||||
|
||||
entry = await self.mod.get_mkt_info(pair.bs_fqme)
|
||||
if not entry:
|
||||
continue
|
||||
|
||||
mkt: MktPair
|
||||
pair: Struct
|
||||
mkt, _pair = entry
|
||||
assert _pair is pair, (
|
||||
f'`{self.mod.name}` backend probably has a '
|
||||
'keying-symmetry problem between the pair-`Struct` '
|
||||
'returned from `Client.get_mkt_pairs()`and the '
|
||||
'module level endpoint: `.get_mkt_info()`\n\n'
|
||||
"Here's the struct diff:\n"
|
||||
f'{_pair - pair}'
|
||||
)
|
||||
# NOTE XXX: this means backends MUST implement
|
||||
# a `Struct.bs_mktid: str` field to provide
|
||||
# a native-keyed map to their own symbol
|
||||
# set(s).
|
||||
self.pairs[pair.bs_mktid] = pair
|
||||
|
||||
# NOTE: `MktPair`s are keyed here using piker's
|
||||
# internal FQME schema so that search,
|
||||
# accounting and feed init can be accomplished
|
||||
# a sane, uniform, normalized basis.
|
||||
self.mktmaps[mkt.fqme] = mkt
|
||||
|
||||
self.pair_ns_path: str = tractor.msg.NamespacePath.from_ref(
|
||||
pair,
|
||||
)
|
||||
|
||||
else:
|
||||
log.warning(
|
||||
'No symbology cache `Pair` support for `{provider}`..\n'
|
||||
'Implement `Client.get_mkt_pairs()`!'
|
||||
)
|
||||
return self
|
||||
|
||||
pairs: dict[str, Struct] = await get_mkt_pairs()
|
||||
if not pairs:
|
||||
log.warning(
|
||||
'No pairs from intial {provider!r} sym-cache request?\n\n'
|
||||
'`Client.get_mkt_pairs()` -> {pairs!r} ?'
|
||||
)
|
||||
return self
|
||||
|
||||
for bs_fqme, pair in pairs.items():
|
||||
if not getattr(pair, 'ns_path', None):
|
||||
# XXX: every backend defined pair must declare
|
||||
# a `.ns_path: tractor.NamespacePath` to enable
|
||||
# roundtrip serialization lookup from a local
|
||||
# cache file.
|
||||
raise TypeError(
|
||||
f'Pair-struct for {self.mod.name} MUST define a '
|
||||
'`.ns_path: str`!\n\n'
|
||||
f'{pair!r}'
|
||||
)
|
||||
|
||||
entry = await self.mod.get_mkt_info(pair.bs_fqme)
|
||||
if not entry:
|
||||
continue
|
||||
|
||||
mkt: MktPair
|
||||
pair: Struct
|
||||
mkt, _pair = entry
|
||||
assert _pair is pair, (
|
||||
f'`{self.mod.name}` backend probably has a '
|
||||
'keying-symmetry problem between the pair-`Struct` '
|
||||
'returned from `Client.get_mkt_pairs()`and the '
|
||||
'module level endpoint: `.get_mkt_info()`\n\n'
|
||||
"Here's the struct diff:\n"
|
||||
f'{_pair - pair}'
|
||||
)
|
||||
# NOTE XXX: this means backends MUST implement
|
||||
# a `Struct.bs_mktid: str` field to provide
|
||||
# a native-keyed map to their own symbol
|
||||
# set(s).
|
||||
self.pairs[pair.bs_mktid] = pair
|
||||
|
||||
# NOTE: `MktPair`s are keyed here using piker's
|
||||
# internal FQME schema so that search,
|
||||
# accounting and feed init can be accomplished
|
||||
# a sane, uniform, normalized basis.
|
||||
self.mktmaps[mkt.fqme] = mkt
|
||||
|
||||
self.pair_ns_path: str = tractor.msg.NamespacePath.from_ref(
|
||||
pair,
|
||||
)
|
||||
|
||||
return self
|
||||
|
||||
|
|
|
|||
|
|
@ -357,9 +357,7 @@ async def allocate_persistent_feed(
|
|||
|
||||
# yield back control to starting nursery once we receive either
|
||||
# some history or a real-time quote.
|
||||
log.info(
|
||||
f'loading OHLCV history: {fqme!r}\n'
|
||||
)
|
||||
log.info(f'loading OHLCV history: {fqme}')
|
||||
await some_data_ready.wait()
|
||||
|
||||
flume = Flume(
|
||||
|
|
@ -796,6 +794,7 @@ async def install_brokerd_search(
|
|||
|
||||
@acm
|
||||
async def maybe_open_feed(
|
||||
|
||||
fqmes: list[str],
|
||||
loglevel: str | None = None,
|
||||
|
||||
|
|
@ -849,12 +848,13 @@ async def maybe_open_feed(
|
|||
|
||||
@acm
|
||||
async def open_feed(
|
||||
|
||||
fqmes: list[str],
|
||||
|
||||
loglevel: str|None = None,
|
||||
loglevel: str | None = None,
|
||||
allow_overruns: bool = True,
|
||||
start_stream: bool = True,
|
||||
tick_throttle: float|None = None, # Hz
|
||||
tick_throttle: float | None = None, # Hz
|
||||
|
||||
allow_remote_ctl_ui: bool = False,
|
||||
|
||||
|
|
|
|||
|
|
@ -36,10 +36,10 @@ from ._sharedmem import (
|
|||
ShmArray,
|
||||
_Token,
|
||||
)
|
||||
from piker.accounting import MktPair
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from piker.data.feed import Feed
|
||||
from ..accounting import MktPair
|
||||
from .feed import Feed
|
||||
|
||||
|
||||
class Flume(Struct):
|
||||
|
|
@ -82,7 +82,7 @@ class Flume(Struct):
|
|||
|
||||
# TODO: do we need this really if we can pull the `Portal` from
|
||||
# ``tractor``'s internals?
|
||||
feed: Feed|None = None
|
||||
feed: Feed | None = None
|
||||
|
||||
@property
|
||||
def rt_shm(self) -> ShmArray:
|
||||
|
|
|
|||
|
|
@ -113,9 +113,9 @@ def validate_backend(
|
|||
)
|
||||
if ep is None:
|
||||
log.warning(
|
||||
f'Provider backend {mod.name!r} is missing '
|
||||
f'{daemon_name!r} support?\n'
|
||||
f'|_module endpoint-func missing: {name!r}\n'
|
||||
f'Provider backend {mod.name} is missing '
|
||||
f'{daemon_name} support :(\n'
|
||||
f'The following endpoint is missing: {name}'
|
||||
)
|
||||
|
||||
inits: list[
|
||||
|
|
|
|||
|
|
@ -18,8 +18,8 @@
|
|||
Log like a forester!
|
||||
"""
|
||||
import logging
|
||||
import json
|
||||
import reprlib
|
||||
import json
|
||||
from typing import (
|
||||
Callable,
|
||||
)
|
||||
|
|
@ -90,8 +90,6 @@ def colorize_json(
|
|||
)
|
||||
|
||||
|
||||
# TODO, eventually defer to the version in `modden` once
|
||||
# it becomes a dep!
|
||||
def mk_repr(
|
||||
**repr_kws,
|
||||
) -> Callable[[str], str]:
|
||||
|
|
|
|||
|
|
@ -138,6 +138,16 @@ class StorageClient(
|
|||
) -> None:
|
||||
...
|
||||
|
||||
async def write_oi(
|
||||
self,
|
||||
fqme: str,
|
||||
oi: np.ndarray,
|
||||
append_and_duplicate: bool = True,
|
||||
limit: int = int(800e3),
|
||||
|
||||
) -> None:
|
||||
...
|
||||
|
||||
|
||||
class TimeseriesNotFound(Exception):
|
||||
'''
|
||||
|
|
|
|||
|
|
@ -111,6 +111,24 @@ def mk_ohlcv_shm_keyed_filepath(
|
|||
return path
|
||||
|
||||
|
||||
def mk_oi_shm_keyed_filepath(
|
||||
fqme: str,
|
||||
period: float | int,
|
||||
datadir: Path,
|
||||
|
||||
) -> Path:
|
||||
|
||||
if period < 1.:
|
||||
raise ValueError('Sample period should be >= 1.!?')
|
||||
|
||||
path: Path = (
|
||||
datadir
|
||||
/
|
||||
f'{fqme}.oi{int(period)}s.parquet'
|
||||
)
|
||||
return path
|
||||
|
||||
|
||||
def unpack_fqme_from_parquet_filepath(path: Path) -> str:
|
||||
|
||||
filename: str = str(path.name)
|
||||
|
|
@ -172,7 +190,11 @@ class NativeStorageClient:
|
|||
|
||||
key: str = path.name.rstrip('.parquet')
|
||||
fqme, _, descr = key.rpartition('.')
|
||||
prefix, _, suffix = descr.partition('ohlcv')
|
||||
if 'ohlcv' in descr:
|
||||
prefix, _, suffix = descr.partition('ohlcv')
|
||||
elif 'oi' in descr:
|
||||
prefix, _, suffix = descr.partition('oi')
|
||||
|
||||
period: int = int(suffix.strip('s'))
|
||||
|
||||
# cache description data
|
||||
|
|
@ -369,6 +391,61 @@ class NativeStorageClient:
|
|||
timeframe,
|
||||
)
|
||||
|
||||
def _write_oi(
|
||||
self,
|
||||
fqme: str,
|
||||
oi: np.ndarray,
|
||||
|
||||
) -> Path:
|
||||
'''
|
||||
Sync version of the public interface meth, since we don't
|
||||
currently actually need or support an async impl.
|
||||
|
||||
'''
|
||||
path: Path = mk_oi_shm_keyed_filepath(
|
||||
fqme=fqme,
|
||||
period=1,
|
||||
datadir=self._datadir,
|
||||
)
|
||||
if isinstance(oi, np.ndarray):
|
||||
new_df: pl.DataFrame = tsp.np2pl(oi)
|
||||
else:
|
||||
new_df = oi
|
||||
|
||||
if path.exists():
|
||||
old_df = pl.read_parquet(path)
|
||||
df = pl.concat([old_df, new_df])
|
||||
else:
|
||||
df = new_df
|
||||
|
||||
start = time.time()
|
||||
df.write_parquet(path)
|
||||
delay: float = round(
|
||||
time.time() - start,
|
||||
ndigits=6,
|
||||
)
|
||||
log.info(
|
||||
f'parquet write took {delay} secs\n'
|
||||
f'file path: {path}'
|
||||
)
|
||||
return path
|
||||
|
||||
async def write_oi(
|
||||
self,
|
||||
fqme: str,
|
||||
oi: np.ndarray,
|
||||
|
||||
) -> Path:
|
||||
'''
|
||||
Write input oi time series for fqme and sampling period
|
||||
to (local) disk.
|
||||
|
||||
'''
|
||||
return self._write_oi(
|
||||
fqme,
|
||||
oi,
|
||||
)
|
||||
|
||||
async def delete_ts(
|
||||
self,
|
||||
key: str,
|
||||
|
|
|
|||
|
|
@ -15,8 +15,8 @@
|
|||
# along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
'''
|
||||
Remote control tasks for sending annotations (and maybe more cmds) to
|
||||
a chart from some other actor.
|
||||
Remote control tasks for sending annotations (and maybe more cmds)
|
||||
to a chart from some other actor.
|
||||
|
||||
'''
|
||||
from __future__ import annotations
|
||||
|
|
@ -32,7 +32,6 @@ from typing import (
|
|||
)
|
||||
|
||||
import tractor
|
||||
import trio
|
||||
from tractor import trionics
|
||||
from tractor import (
|
||||
Portal,
|
||||
|
|
@ -317,9 +316,7 @@ class AnnotCtl(Struct):
|
|||
)
|
||||
yield aid
|
||||
finally:
|
||||
# async ipc send op
|
||||
with trio.CancelScope(shield=True):
|
||||
await self.remove(aid)
|
||||
await self.remove(aid)
|
||||
|
||||
async def redraw(
|
||||
self,
|
||||
|
|
|
|||
|
|
@ -555,13 +555,14 @@ class OrderMode:
|
|||
|
||||
def on_fill(
|
||||
self,
|
||||
|
||||
uuid: str,
|
||||
price: float,
|
||||
time_s: float,
|
||||
|
||||
pointing: str | None = None,
|
||||
|
||||
) -> bool:
|
||||
) -> None:
|
||||
'''
|
||||
Fill msg handler.
|
||||
|
||||
|
|
@ -574,83 +575,60 @@ class OrderMode:
|
|||
- update fill bar size
|
||||
|
||||
'''
|
||||
# XXX WARNING XXX
|
||||
# if a `Status(resp='error')` arrives *before* this
|
||||
# fill-status, the `.dialogs` entry may have already been
|
||||
# popped and thus the below will skipped.
|
||||
#
|
||||
# NOTE, to avoid this confusing scenario ensure that any
|
||||
# errors delivered thru from the broker-backend are not just
|
||||
# "noisy reporting" (like is very common from IB..) and are
|
||||
# instead ONLY errors-causing-order-dialog-cancellation!
|
||||
if not (dialog := self.dialogs.get(uuid)):
|
||||
log.warning(
|
||||
f'Order was already cleared from `.dialogs` ??\n'
|
||||
f'uuid: {uuid!r}\n'
|
||||
)
|
||||
return False
|
||||
|
||||
dialog = self.dialogs[uuid]
|
||||
lines = dialog.lines
|
||||
chart = self.chart
|
||||
|
||||
if not lines:
|
||||
log.warn("No line(s) for order {uuid}!?")
|
||||
return False
|
||||
|
||||
# update line state(s)
|
||||
#
|
||||
# ?XXX this fails on certain types of races?
|
||||
# XXX: seems to fail on certain types of races?
|
||||
# assert len(lines) == 2
|
||||
flume: Flume = self.feed.flumes[chart.linked.mkt.fqme]
|
||||
_, _, ratio = flume.get_ds_info()
|
||||
if lines:
|
||||
flume: Flume = self.feed.flumes[chart.linked.mkt.fqme]
|
||||
_, _, ratio = flume.get_ds_info()
|
||||
|
||||
for chart, shm in [
|
||||
(self.chart, flume.rt_shm),
|
||||
(self.hist_chart, flume.hist_shm),
|
||||
]:
|
||||
viz = chart.get_viz(chart.name)
|
||||
index_field = viz.index_field
|
||||
arr = shm.array
|
||||
for chart, shm in [
|
||||
(self.chart, flume.rt_shm),
|
||||
(self.hist_chart, flume.hist_shm),
|
||||
]:
|
||||
viz = chart.get_viz(chart.name)
|
||||
index_field = viz.index_field
|
||||
arr = shm.array
|
||||
|
||||
# TODO: borked for int index based..
|
||||
index = flume.get_index(time_s, arr)
|
||||
# TODO: borked for int index based..
|
||||
index = flume.get_index(time_s, arr)
|
||||
|
||||
# get absolute index for arrow placement
|
||||
arrow_index = arr[index_field][index]
|
||||
# get absolute index for arrow placement
|
||||
arrow_index = arr[index_field][index]
|
||||
|
||||
self.arrows.add(
|
||||
chart.plotItem,
|
||||
uuid,
|
||||
arrow_index,
|
||||
price,
|
||||
pointing=pointing,
|
||||
color=lines[0].color
|
||||
)
|
||||
self.arrows.add(
|
||||
chart.plotItem,
|
||||
uuid,
|
||||
arrow_index,
|
||||
price,
|
||||
pointing=pointing,
|
||||
color=lines[0].color
|
||||
)
|
||||
else:
|
||||
log.warn("No line(s) for order {uuid}!?")
|
||||
|
||||
def on_cancel(
|
||||
self,
|
||||
uuid: str,
|
||||
uuid: str
|
||||
|
||||
) -> bool:
|
||||
) -> None:
|
||||
|
||||
msg: Order|None = self.client._sent_orders.pop(uuid, None)
|
||||
if msg is None:
|
||||
msg: Order = self.client._sent_orders.pop(uuid, None)
|
||||
|
||||
if msg is not None:
|
||||
self.lines.remove_line(uuid=uuid)
|
||||
self.chart.linked.cursor.show_xhair()
|
||||
|
||||
dialog = self.dialogs.pop(uuid, None)
|
||||
if dialog:
|
||||
dialog.last_status_close()
|
||||
else:
|
||||
log.warning(
|
||||
f'Received cancel for unsubmitted order {pformat(msg)}'
|
||||
)
|
||||
return False
|
||||
|
||||
# remove GUI line, show cursor.
|
||||
self.lines.remove_line(uuid=uuid)
|
||||
self.chart.linked.cursor.show_xhair()
|
||||
|
||||
# remove msg dialog (history)
|
||||
dialog: Dialog|None = self.dialogs.pop(uuid, None)
|
||||
if dialog:
|
||||
dialog.last_status_close()
|
||||
|
||||
return True
|
||||
|
||||
|
||||
def cancel_orders_under_cursor(self) -> list[str]:
|
||||
return self.cancel_orders(
|
||||
|
|
@ -1079,23 +1057,13 @@ async def process_trade_msg(
|
|||
if name in (
|
||||
'position',
|
||||
):
|
||||
mkt: MktPair = mode.chart.linked.mkt
|
||||
sym: MktPair = mode.chart.linked.mkt
|
||||
pp_msg_symbol = msg['symbol'].lower()
|
||||
pp_msg_bsmktid = msg['bs_mktid']
|
||||
fqme = mkt.fqme
|
||||
broker = mkt.broker
|
||||
fqme = sym.fqme
|
||||
broker = sym.broker
|
||||
if (
|
||||
# match on any backed-specific(-unique)-ID first!
|
||||
(
|
||||
pp_msg_bsmktid
|
||||
and
|
||||
mkt.bs_mktid == pp_msg_bsmktid
|
||||
)
|
||||
or
|
||||
# OW try against what's provided as an FQME..
|
||||
pp_msg_symbol == fqme
|
||||
or
|
||||
pp_msg_symbol == fqme.removesuffix(f'.{broker}')
|
||||
or pp_msg_symbol == fqme.removesuffix(f'.{broker}')
|
||||
):
|
||||
log.info(
|
||||
f'Loading position for `{fqme}`:\n'
|
||||
|
|
@ -1118,7 +1086,7 @@ async def process_trade_msg(
|
|||
return
|
||||
|
||||
msg = Status(**msg)
|
||||
# resp: str = msg.resp
|
||||
resp = msg.resp
|
||||
oid = msg.oid
|
||||
dialog: Dialog = mode.dialogs.get(oid)
|
||||
|
||||
|
|
@ -1182,33 +1150,20 @@ async def process_trade_msg(
|
|||
mode.on_submit(oid)
|
||||
|
||||
case Status(resp='error'):
|
||||
# TODO: parse into broker-side msg, or should we
|
||||
# expect it to just be **that** msg verbatim (since
|
||||
# we'd presumably have only 1 `Error` msg-struct)
|
||||
broker_msg: dict = msg.brokerd_msg
|
||||
|
||||
# XXX NOTE, this presumes the rxed "error" is
|
||||
# order-dialog-cancel-causing, THUS backends much ONLY
|
||||
# relay errors of this "severity"!!
|
||||
log.error(
|
||||
f'Order errored ??\n'
|
||||
f'oid: {oid!r}\n'
|
||||
f'\n'
|
||||
f'{pformat(broker_msg)}\n'
|
||||
f'\n'
|
||||
f'=> CANCELLING ORDER DIALOG <=\n'
|
||||
|
||||
# from tractor.devx.pformat import ppfmt
|
||||
# !TODO LOL, wtf the msg is causing
|
||||
# a recursion bug!
|
||||
# -[ ] get this shit on msgspec stat!
|
||||
# f'{ppfmt(broker_msg)}'
|
||||
)
|
||||
# do all the things for a cancel:
|
||||
# - drop order-msg dialog from client table
|
||||
# - delete level line from view
|
||||
mode.on_cancel(oid)
|
||||
|
||||
# TODO: parse into broker-side msg, or should we
|
||||
# expect it to just be **that** msg verbatim (since
|
||||
# we'd presumably have only 1 `Error` msg-struct)
|
||||
broker_msg: dict = msg.brokerd_msg
|
||||
log.error(
|
||||
f'Order {oid}->{resp} with:\n{pformat(broker_msg)}'
|
||||
)
|
||||
|
||||
case Status(resp='canceled'):
|
||||
# delete level line from view
|
||||
mode.on_cancel(oid)
|
||||
|
|
@ -1223,10 +1178,10 @@ async def process_trade_msg(
|
|||
# TODO: UX for a "pending" clear/live order
|
||||
log.info(f'Dark order triggered for {fmtmsg}')
|
||||
|
||||
# TODO: do the struct-msg version, blah blah..
|
||||
# req=Order(exec_mode='live', action='alert') as req,
|
||||
case Status(
|
||||
resp='triggered',
|
||||
# TODO: do the struct-msg version, blah blah..
|
||||
# req=Order(exec_mode='live', action='alert') as req,
|
||||
req={
|
||||
'exec_mode': 'live',
|
||||
'action': 'alert',
|
||||
|
|
|
|||
150
pyproject.toml
150
pyproject.toml
|
|
@ -63,138 +63,90 @@ dependencies = [
|
|||
"trio-util >=0.7.0, <0.8.0",
|
||||
"trio-websocket >=0.10.3, <0.11.0",
|
||||
"typer >=0.9.0, <1.0.0",
|
||||
"rapidfuzz >=3.5.2, <4.0.0",
|
||||
"pdbp >=1.5.0, <2.0.0",
|
||||
"trio >=0.27",
|
||||
"pendulum",
|
||||
"pendulum >=3.0.0, <4.0.0",
|
||||
"httpx >=0.27.0, <0.28.0",
|
||||
"cryptofeed >=2.4.0, <3.0.0",
|
||||
"pyarrow>=18.0.0",
|
||||
"websockets ==12.0",
|
||||
"msgspec>=0.19.0,<0.20",
|
||||
"tractor",
|
||||
"asyncvnc",
|
||||
"tomlkit",
|
||||
"trio-typing>=0.10.0",
|
||||
"numba>=0.61.0",
|
||||
"pyvnc",
|
||||
]
|
||||
# ------ dependencies ------
|
||||
# NOTE, by default we ship only a "headless" deps set bc
|
||||
# the `uis` group is not listed in the optional set.
|
||||
|
||||
# [optional-dependencies]
|
||||
# uis = []
|
||||
# ?TODO? really we should be able to mv this `uis` group
|
||||
# to be under [optional-dependencies] and then include
|
||||
# it in the dev deps?
|
||||
# https://docs.astral.sh/uv/concepts/projects/dependencies/#optional-dependencies
|
||||
# -> uis should be included in pubbed pkgs.
|
||||
# [ ] uv seems to have no way to do this though?
|
||||
[project.optional-dependencies]
|
||||
uis = [
|
||||
# https://docs.astral.sh/uv/concepts/projects/dependencies/#optional-dependencies
|
||||
# TODO: make sure the levenshtein shit compiles on nix..
|
||||
# rapidfuzz = {extras = ["speedup"], version = "^0.18.0"}
|
||||
"rapidfuzz >=3.2.0, <4.0.0",
|
||||
"qdarkstyle >=3.0.2, <4.0.0",
|
||||
"pyqt6 >=6.7.0, <7.0.0",
|
||||
"pyqtgraph",
|
||||
|
||||
# TODO? move to a `uv.toml`?
|
||||
[tool.uv]
|
||||
# https://docs.astral.sh/uv/reference/settings/#python-preference
|
||||
python-preference = 'system'
|
||||
# https://docs.astral.sh/uv/reference/settings/#python-downloads
|
||||
python-downloads = 'manual'
|
||||
# https://docs.astral.sh/uv/concepts/projects/dependencies/#default-groups
|
||||
default-groups = [
|
||||
'uis',
|
||||
# for consideration,
|
||||
# - 'visidata'
|
||||
|
||||
# TODO: add an `--only daemon` group for running non-ui / pikerd
|
||||
# service tree in distributed mode B)
|
||||
# https://docs.astral.sh/uv/concepts/projects/dependencies/#optional-dependencies
|
||||
]
|
||||
# ------ tool.uv ------
|
||||
|
||||
[dependency-groups]
|
||||
uis = [
|
||||
"pyqtgraph",
|
||||
"qdarkstyle >=3.0.2, <4.0.0",
|
||||
"pyqt6 >=6.7.0, <7.0.0",
|
||||
|
||||
# fuzzy search
|
||||
"rapidfuzz >=3.2.0, <4.0.0",
|
||||
]
|
||||
|
||||
# dev deps enabled by `uv --dev`
|
||||
# https://docs.astral.sh/uv/concepts/projects/dependencies/#development-dependencies
|
||||
# TODO: a toolset that makes debugging a `pikerd` service (tree) easy
|
||||
# to hack on directly using more or less the local env:
|
||||
# - xonsh + xxh
|
||||
# - rsyscall + pdbp
|
||||
# - actor runtime control console like BEAM/OTP
|
||||
#
|
||||
# console ehancements and eventually remote debugging extras/helpers.
|
||||
# use `uv --dev` to enable
|
||||
dev = [
|
||||
# https://docs.astral.sh/uv/concepts/projects/dependencies/#development-dependencies
|
||||
"cython >=3.0.0, <4.0.0",
|
||||
|
||||
# nested deps-groups
|
||||
# https://docs.astral.sh/uv/concepts/projects/dependencies/#nesting-groups
|
||||
{include-group = 'uis'},
|
||||
{include-group = 'repl'},
|
||||
{include-group = 'testing'},
|
||||
{include-group = 'de'},
|
||||
]
|
||||
repl = [
|
||||
# `tractor`'s debugger
|
||||
"pdbp >=1.8.2, <2.0.0",
|
||||
"greenback >=1.1.1, <2.0.0",
|
||||
|
||||
# @goodboy's preferred console toolz
|
||||
"xonsh",
|
||||
"pytest >=6.0.0, <7.0.0",
|
||||
"elasticsearch >=8.9.0, <9.0.0",
|
||||
'xonsh',
|
||||
"prompt-toolkit ==3.0.40",
|
||||
"cython >=3.0.0, <4.0.0",
|
||||
"greenback >=1.1.1, <2.0.0",
|
||||
"ruff>=0.9.6",
|
||||
"pyperclip>=1.9.0",
|
||||
|
||||
# ?TODO, new stuff to consider..
|
||||
# "visidata" # console numerics
|
||||
# "xxh" # for remote `xonsh`-ing
|
||||
# "rsyscall" # (eventual) optional `tractor` backend
|
||||
# - an actor-runtime-ctl console like BEAM/OTP
|
||||
"i3ipc>=2.2.1",
|
||||
]
|
||||
testing = [
|
||||
"pytest",
|
||||
]
|
||||
de = [ # (linux) specific DEs
|
||||
"i3ipc>=2.2.1",
|
||||
]
|
||||
lint = [
|
||||
# XXX, with flake.nix needs to be from nixpkgs
|
||||
"ruff>=0.9.6"
|
||||
# ; os_name != 'nixos' and platform_system != 'NixOS'",
|
||||
# ?TODO? since ^ markers won't work, use a deps-flags to toggle for
|
||||
# now.
|
||||
]
|
||||
dbs = [
|
||||
"elasticsearch >=8.9.0, <9.0.0",
|
||||
]
|
||||
# ------ dependency-groups ------
|
||||
|
||||
|
||||
[tool.pytest.ini_options]
|
||||
# https://docs.pytest.org/en/stable/reference/reference.html#configuration-options
|
||||
testpaths = [
|
||||
"tests",
|
||||
]
|
||||
# https://docs.pytest.org/en/stable/reference/reference.html#confval-console_output_style
|
||||
console_output_style = 'progress'
|
||||
|
||||
# https://docs.pytest.org/en/stable/how-to/plugins.html#disabling-plugins-from-autoloading
|
||||
# https://docs.pytest.org/en/stable/how-to/plugins.html#deactivating-unregistering-a-plugin-by-name
|
||||
addopts = '-p no:xonsh'
|
||||
# ------ tool.pytest ------
|
||||
|
||||
|
||||
[project.scripts]
|
||||
piker = "piker.cli:cli"
|
||||
pikerd = "piker.cli:pikerd"
|
||||
ledger = "piker.accounting.cli:ledger"
|
||||
# ------ project.scripts ------
|
||||
|
||||
[tool.hatch.build.targets.sdist]
|
||||
include = ["piker"]
|
||||
|
||||
[tool.hatch.build.targets.wheel]
|
||||
include = ["piker"]
|
||||
# ------ tool.hatch ------
|
||||
|
||||
|
||||
# TODO? move to a `uv.toml`?
|
||||
[tool.uv]
|
||||
python-preference = 'system'
|
||||
python-downloads = 'manual'
|
||||
|
||||
|
||||
[tool.uv.sources]
|
||||
pyqtgraph = { git = "https://github.com/pikers/pyqtgraph.git" }
|
||||
asyncvnc = { git = "https://github.com/pikers/asyncvnc.git", branch = "main" }
|
||||
tomlkit = { git = "https://github.com/pikers/tomlkit.git", branch ="piker_pin" }
|
||||
pyvnc = { git = "https://github.com/regulad/pyvnc.git" }
|
||||
|
||||
# XXX since, we're like, always hacking new shite all-the-time. Bp
|
||||
tractor = { git = "https://github.com/goodboy/tractor.git", branch ="piker_pin" }
|
||||
# tractor = { git = "https://pikers.dev/goodboy/tractor", branch = "piker_pin" }
|
||||
# tractor = { git = "https://pikers.dev/goodboy/tractor", branch = "main" }
|
||||
# ------ goodboy ------
|
||||
# hackin dev-envs, usually there's something new he's hackin in..
|
||||
# tractor = { path = "../tractor", editable = true }
|
||||
# TODO, long term we should be synced to upstream `main` branch!
|
||||
# tractor = { git = "https://github.com/goodboy/tractor.git", branch ="piker_pin" }
|
||||
tractor = { git = "https://pikers.dev/goodboy/tractor", branch = "piker_pin" }
|
||||
|
||||
# goodboy's dev-env
|
||||
# XXX for @goodboy's hackin dev env, usually there's something new in
|
||||
# the runtime being seriously tested here Bp
|
||||
# tractor = { path = "../tractor/", editable = true }
|
||||
# xonsh = { path = "../xonsh", editable = true }
|
||||
|
|
|
|||
|
|
@ -15,12 +15,6 @@ from piker.service import (
|
|||
from piker.log import get_console_log
|
||||
|
||||
|
||||
# include `tractor`'s built-in fixtures!
|
||||
pytest_plugins: tuple[str] = (
|
||||
"tractor._testing.pytest",
|
||||
)
|
||||
|
||||
|
||||
def pytest_addoption(parser):
|
||||
parser.addoption("--ll", action="store", dest='loglevel',
|
||||
default=None, help="logging level to set when testing")
|
||||
|
|
|
|||
|
|
@ -12,14 +12,12 @@ from piker import config
|
|||
from piker.accounting import (
|
||||
Account,
|
||||
calc,
|
||||
open_account,
|
||||
load_account,
|
||||
load_account_from_ledger,
|
||||
open_trade_ledger,
|
||||
Position,
|
||||
TransactionLedger,
|
||||
open_trade_ledger,
|
||||
load_account,
|
||||
load_account_from_ledger,
|
||||
)
|
||||
import tractor
|
||||
|
||||
|
||||
def test_root_conf_networking_section(
|
||||
|
|
@ -55,17 +53,12 @@ def test_account_file_default_empty(
|
|||
)
|
||||
def test_paper_ledger_position_calcs(
|
||||
fq_acnt: tuple[str, str],
|
||||
debug_mode: bool,
|
||||
):
|
||||
broker: str
|
||||
acnt_name: str
|
||||
broker, acnt_name = fq_acnt
|
||||
|
||||
accounts_path: Path = (
|
||||
config.repodir()
|
||||
/ 'tests'
|
||||
/ '_inputs' # tests-local-subdir
|
||||
)
|
||||
accounts_path: Path = config.repodir() / 'tests' / '_inputs'
|
||||
|
||||
ldr: TransactionLedger
|
||||
with (
|
||||
|
|
@ -84,7 +77,6 @@ def test_paper_ledger_position_calcs(
|
|||
ledger=ldr,
|
||||
|
||||
_fp=accounts_path,
|
||||
debug_mode=debug_mode,
|
||||
|
||||
) as (dfs, ledger),
|
||||
|
||||
|
|
@ -110,87 +102,3 @@ def test_paper_ledger_position_calcs(
|
|||
df = dfs[xrp]
|
||||
assert df['cumsize'][-1] == 0
|
||||
assert pos.cumsize == 0
|
||||
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
'fq_acnt',
|
||||
[
|
||||
('ib', 'algopaper'),
|
||||
],
|
||||
)
|
||||
def test_ib_account_with_duplicated_mktids(
|
||||
fq_acnt: tuple[str, str],
|
||||
debug_mode: bool,
|
||||
):
|
||||
# ?TODO, once we start symcache-incremental-update-support?
|
||||
# from piker.data import (
|
||||
# open_symcache,
|
||||
# )
|
||||
#
|
||||
# async def main():
|
||||
# async with (
|
||||
# # TODO: do this as part of `open_account()`!?
|
||||
# open_symcache(
|
||||
# 'ib',
|
||||
# only_from_memcache=True,
|
||||
# ) as symcache,
|
||||
# ):
|
||||
|
||||
|
||||
from piker.brokers.ib.ledger import (
|
||||
tx_sort,
|
||||
|
||||
# ?TODO, once we want to pull lowlevel txns and process them?
|
||||
# norm_trade_records,
|
||||
# update_ledger_from_api_trades,
|
||||
)
|
||||
|
||||
broker: str
|
||||
acnt_id: str = 'algopaper'
|
||||
broker, acnt_id = fq_acnt
|
||||
accounts_def = config.load_accounts([broker])
|
||||
assert accounts_def[f'{broker}.{acnt_id}']
|
||||
|
||||
ledger: TransactionLedger
|
||||
acnt: Account
|
||||
with (
|
||||
tractor.devx.maybe_open_crash_handler(pdb=debug_mode),
|
||||
|
||||
open_trade_ledger(
|
||||
'ib',
|
||||
acnt_id,
|
||||
tx_sort=tx_sort,
|
||||
|
||||
# TODO, eventually incrementally updated for IB..
|
||||
# symcache=symcache,
|
||||
symcache=None,
|
||||
allow_from_sync_code=True,
|
||||
|
||||
) as ledger,
|
||||
|
||||
open_account(
|
||||
'ib',
|
||||
acnt_id,
|
||||
write_on_exit=True,
|
||||
) as acnt,
|
||||
):
|
||||
# per input params
|
||||
symcache = ledger.symcache
|
||||
assert not (
|
||||
symcache.pairs
|
||||
or
|
||||
symcache.pairs
|
||||
or
|
||||
symcache.mktmaps
|
||||
)
|
||||
# re-compute all positions that have changed state.
|
||||
# TODO: likely we should change the API to return the
|
||||
# position updates from `.update_from_ledger()`?
|
||||
active, closed = acnt.dump_active()
|
||||
|
||||
# breakpoint()
|
||||
|
||||
# TODO, (see above imports as well) incremental update from
|
||||
# (updated) ledger?
|
||||
# -[ ] pull some code from `.ib.broker` content.
|
||||
|
|
|
|||
|
|
@ -42,7 +42,7 @@ from piker.accounting import (
|
|||
unpack_fqme,
|
||||
)
|
||||
from piker.accounting import (
|
||||
open_account,
|
||||
open_pps,
|
||||
Position,
|
||||
)
|
||||
|
||||
|
|
@ -136,7 +136,7 @@ def load_and_check_pos(
|
|||
|
||||
) -> None:
|
||||
|
||||
with open_account(ppmsg.broker, ppmsg.account) as table:
|
||||
with open_pps(ppmsg.broker, ppmsg.account) as table:
|
||||
|
||||
if ppmsg.size == 0:
|
||||
assert ppmsg.symbol not in table.pps
|
||||
|
|
|
|||
Loading…
Reference in New Issue