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
|
# extra scripts dir
|
||||||
/snippets
|
/snippets
|
||||||
|
# testing-utils aren't actively in use.
|
||||||
|
/piker/_testing
|
||||||
|
|
||||||
# mypy
|
# mypy
|
||||||
.mypy_cache/
|
.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,
|
# ^ astral's docs,
|
||||||
# https://docs.astral.sh/uv/concepts/projects/sync/
|
# 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::
|
Ensure you can run the root-daemon::
|
||||||
|
|
||||||
uv run pikerd [-l info --pdb]
|
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
|
``NixOS`` is our core devs' distro of choice for which we offer
|
||||||
a stringently defined development shell envoirment that can currently
|
a stringently defined development shell envoirment that can be loaded with::
|
||||||
be applied in one of 2 ways::
|
|
||||||
|
|
||||||
# ONLY if running on X11
|
|
||||||
nix-shell default.nix
|
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
|
start a chart
|
||||||
*************
|
*************
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,6 @@
|
||||||
|
################
|
||||||
# ---- CEXY ----
|
# ---- CEXY ----
|
||||||
|
################
|
||||||
[binance]
|
[binance]
|
||||||
accounts.paper = 'paper'
|
accounts.paper = 'paper'
|
||||||
|
|
||||||
|
|
@ -12,41 +13,28 @@ accounts.spot = 'spot'
|
||||||
spot.use_testnet = false
|
spot.use_testnet = false
|
||||||
spot.api_key = ''
|
spot.api_key = ''
|
||||||
spot.api_secret = ''
|
spot.api_secret = ''
|
||||||
# ------ binance ------
|
|
||||||
|
|
||||||
|
|
||||||
[deribit]
|
[deribit]
|
||||||
# std assets
|
|
||||||
key_id = ''
|
key_id = ''
|
||||||
key_secret = ''
|
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]
|
[kraken]
|
||||||
key_descr = ''
|
key_descr = ''
|
||||||
api_key = ''
|
api_key = ''
|
||||||
secret = ''
|
secret = ''
|
||||||
# ------ kraken ------
|
|
||||||
|
|
||||||
|
|
||||||
[kucoin]
|
[kucoin]
|
||||||
key_id = ''
|
key_id = ''
|
||||||
key_secret = ''
|
key_secret = ''
|
||||||
key_passphrase = ''
|
key_passphrase = ''
|
||||||
# ------ kucoin ------
|
|
||||||
|
|
||||||
|
|
||||||
|
################
|
||||||
# -- BROKERZ ---
|
# -- BROKERZ ---
|
||||||
|
################
|
||||||
[questrade]
|
[questrade]
|
||||||
refresh_token = ''
|
refresh_token = ''
|
||||||
access_token = ''
|
access_token = ''
|
||||||
|
|
@ -54,55 +42,44 @@ api_server = 'https://api06.iq.questrade.com/'
|
||||||
expires_in = 1800
|
expires_in = 1800
|
||||||
token_type = 'Bearer'
|
token_type = 'Bearer'
|
||||||
expires_at = 1616095326.355846
|
expires_at = 1616095326.355846
|
||||||
# ------ questrade ------
|
|
||||||
|
|
||||||
|
|
||||||
[ib]
|
[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 = [
|
hosts = [
|
||||||
'127.0.0.1',
|
'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 = [
|
ports = [
|
||||||
4002, # gw
|
4002, # gw
|
||||||
7497, # tws
|
7497, # tws
|
||||||
]
|
]
|
||||||
|
|
||||||
# When API endpoints are being scanned durin startup, the order
|
# XXX: for a paper account the flex web query service
|
||||||
# of user-defined-account "names" (as defined below) here
|
# is not supported so you have to manually download
|
||||||
# determines which py-client connection is given priority to be
|
# and XML report and put it in a location that can be
|
||||||
# used for data-feed-requests by according to whichever client
|
# accessed by the ``brokerd.ib`` backend code for parsing.
|
||||||
# connected to an API endpoing which reported the equivalent
|
flex_token = ''
|
||||||
# account number for that name.
|
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 = [
|
prefer_data_account = [
|
||||||
'paper',
|
'paper',
|
||||||
'margin',
|
'margin',
|
||||||
'ira',
|
'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]
|
[ib.accounts]
|
||||||
paper = 'DU0000000' # <- literal account #
|
# the order in which accounts will be selectable
|
||||||
margin = 'U0000000'
|
# in the order mode UI (if found via clients during
|
||||||
ira = 'U0000000'
|
# API-app scanning)when a new symbol is loaded.
|
||||||
# ------ ib ------
|
paper = 'XX0000000'
|
||||||
|
margin = 'X0000000'
|
||||||
|
ira = 'X0000000'
|
||||||
|
|
|
||||||
|
|
@ -1,9 +1,7 @@
|
||||||
[network]
|
[network]
|
||||||
pikerd = [
|
tsdb.backend = 'marketstore'
|
||||||
'/ipv4/127.0.0.1/tcp/6116', # std localhost daemon-actor tree
|
tsdb.host = 'localhost'
|
||||||
# '/uds/6116', # TODO std uds socket file
|
tsdb.grpc_port = 5995
|
||||||
]
|
|
||||||
|
|
||||||
|
|
||||||
[ui]
|
[ui]
|
||||||
# set custom font + size which will scale entire 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;
|
libxkbcommonStorePath = lib.getLib libxkbcommon;
|
||||||
xcbutilcursorStorePath = lib.getLib xcb-util-cursor;
|
xcbutilcursorStorePath = lib.getLib xcb-util-cursor;
|
||||||
|
|
||||||
pypkgs = python313Packages;
|
qtpyStorePath = lib.getLib python312Packages.qtpy;
|
||||||
qtpyStorePath = lib.getLib pypkgs.qtpy;
|
pyqt6StorePath = lib.getLib python312Packages.pyqt6;
|
||||||
pyqt6StorePath = lib.getLib pypkgs.pyqt6;
|
pyqt6SipStorePath = lib.getLib python312Packages.pyqt6-sip;
|
||||||
pyqt6SipStorePath = lib.getLib pypkgs.pyqt6-sip;
|
rapidfuzzStorePath = lib.getLib python312Packages.rapidfuzz;
|
||||||
rapidfuzzStorePath = lib.getLib pypkgs.rapidfuzz;
|
qdarkstyleStorePath = lib.getLib python312Packages.qdarkstyle;
|
||||||
qdarkstyleStorePath = lib.getLib pypkgs.qdarkstyle;
|
|
||||||
|
|
||||||
xorgLibX11StorePath = lib.getLib xorg.libX11;
|
xorgLibX11StorePath = lib.getLib xorg.libX11;
|
||||||
xorgLibxcbStorePath = lib.getLib xorg.libxcb;
|
xorgLibxcbStorePath = lib.getLib xorg.libxcb;
|
||||||
|
|
@ -52,12 +51,12 @@ stdenv.mkDerivation {
|
||||||
xorg.xcbutilrenderutil
|
xorg.xcbutilrenderutil
|
||||||
|
|
||||||
# Python requirements.
|
# Python requirements.
|
||||||
python313
|
python312Full
|
||||||
uv
|
python312Packages.uv
|
||||||
pypkgs.qdarkstyle
|
python312Packages.qdarkstyle
|
||||||
pypkgs.rapidfuzz
|
python312Packages.rapidfuzz
|
||||||
pypkgs.pyqt6
|
python312Packages.pyqt6
|
||||||
pypkgs.qtpy
|
python312Packages.qtpy
|
||||||
];
|
];
|
||||||
src = null;
|
src = null;
|
||||||
shellHook = ''
|
shellHook = ''
|
||||||
|
|
@ -114,11 +113,11 @@ stdenv.mkDerivation {
|
||||||
|
|
||||||
export LD_LIBRARY_PATH
|
export LD_LIBRARY_PATH
|
||||||
|
|
||||||
RPDFUZZ_PATH="${rapidfuzzStorePath}/lib/python3.13/site-packages"
|
RPDFUZZ_PATH="${rapidfuzzStorePath}/lib/python3.12/site-packages"
|
||||||
QDRKSTYLE_PATH="${qdarkstyleStorePath}/lib/python3.13/site-packages"
|
QDRKSTYLE_PATH="${qdarkstyleStorePath}/lib/python3.12/site-packages"
|
||||||
QTPY_PATH="${qtpyStorePath}/lib/python3.13/site-packages"
|
QTPY_PATH="${qtpyStorePath}/lib/python3.12/site-packages"
|
||||||
PYQT6_PATH="${pyqt6StorePath}/lib/python3.13/site-packages"
|
PYQT6_PATH="${pyqt6StorePath}/lib/python3.12/site-packages"
|
||||||
PYQT6_SIP_PATH="${pyqt6SipStorePath}/lib/python3.13/site-packages"
|
PYQT6_SIP_PATH="${pyqt6SipStorePath}/lib/python3.12/site-packages"
|
||||||
|
|
||||||
PATCH="$PATCH:$RPDFUZZ_PATH"
|
PATCH="$PATCH:$RPDFUZZ_PATH"
|
||||||
PATCH="$PATCH:$QDRKSTYLE_PATH"
|
PATCH="$PATCH:$QDRKSTYLE_PATH"
|
||||||
|
|
@ -128,8 +127,8 @@ stdenv.mkDerivation {
|
||||||
|
|
||||||
export PATCH
|
export PATCH
|
||||||
|
|
||||||
# install all dev and extras
|
# Install deps
|
||||||
uv sync --dev --all-extras
|
uv lock
|
||||||
|
|
||||||
'';
|
'';
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,138 +1,30 @@
|
||||||
running ``ib`` gateway in ``docker``
|
running ``ib`` gateway in ``docker``
|
||||||
------------------------------------
|
------------------------------------
|
||||||
We have a config based on a well maintained community
|
We have a config based on the (now defunct)
|
||||||
image from `@gnzsnz`:
|
image from "waytrade":
|
||||||
|
|
||||||
https://github.com/gnzsnz/ib-gateway-docker
|
https://github.com/waytrade/ib-gateway-docker
|
||||||
|
|
||||||
|
To startup this image with our custom settings
|
||||||
To startup this image simply run the command::
|
simply run the command::
|
||||||
|
|
||||||
docker compose up
|
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
|
You can attach to the container via a VNC client
|
||||||
default:
|
without password auth.
|
||||||
|
|
||||||
- ``x11vnc1 @ 127.0.0.1:5900``
|
SECURITY STUFF!?!?!
|
||||||
- ``ib-gw @ 127.0.0.1:4002``
|
-------------------
|
||||||
|
Though "``ib``" claims they host filter connections outside
|
||||||
You can now attach to the container via a VNC client with password-auth;
|
localhost (aka ``127.0.0.1``) it's probably better if you filter
|
||||||
here is an example using ``vncclient`` on ``linux``::
|
the socket at the OS level using a stateless firewall rule::
|
||||||
|
|
||||||
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::
|
|
||||||
|
|
||||||
ip rule add not unicast iif lo to 0.0.0.0/0 dport 4002
|
ip rule add not unicast iif lo to 0.0.0.0/0 dport 4002
|
||||||
|
|
||||||
|
We will soon have this baked into our own custom image but for
|
||||||
We will soon have this either baked into our own custom derivative
|
now you'll have to do it urself dawgy.
|
||||||
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
|
|
||||||
|
|
|
||||||
|
|
@ -1,15 +1,10 @@
|
||||||
# a community maintained IB API container!
|
# rework from the original @
|
||||||
#
|
# https://github.com/waytrade/ib-gateway-docker/blob/master/docker-compose.yml
|
||||||
# https://github.com/gnzsnz/ib-gateway-docker
|
version: "3.5"
|
||||||
#
|
|
||||||
# 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
|
|
||||||
|
|
||||||
services:
|
services:
|
||||||
|
|
||||||
ib_gw_paper:
|
ib_gw_paper:
|
||||||
|
|
||||||
# apparently java is a mega cukc:
|
# apparently java is a mega cukc:
|
||||||
|
|
@ -55,22 +50,16 @@ services:
|
||||||
target: /root/scripts/run_x11_vnc.sh
|
target: /root/scripts/run_x11_vnc.sh
|
||||||
read_only: true
|
read_only: true
|
||||||
|
|
||||||
# NOTE: an alt method to fill these out is to
|
# NOTE:to fill these out, define an `.env` file in the same dir as
|
||||||
# define an `.env` file in the same dir as
|
# this compose file which looks something like:
|
||||||
# this compose file.
|
# TWS_USERID='myuser'
|
||||||
|
# TWS_PASSWORD='guest'
|
||||||
environment:
|
environment:
|
||||||
TWS_USERID: ${TWS_USERID}
|
TWS_USERID: ${TWS_USERID}
|
||||||
# TWS_USERID: 'myuser'
|
|
||||||
TWS_PASSWORD: ${TWS_PASSWORD}
|
TWS_PASSWORD: ${TWS_PASSWORD}
|
||||||
# TWS_PASSWORD: 'guest'
|
TRADING_MODE: 'paper'
|
||||||
TRADING_MODE: ${TRADING_MODE}
|
VNC_SERVER_PASSWORD: 'doggy'
|
||||||
# TRADING_MODE: 'paper'
|
VNC_SERVER_PORT: '3003'
|
||||||
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'
|
|
||||||
|
|
||||||
# ports:
|
# ports:
|
||||||
# - target: 4002
|
# - target: 4002
|
||||||
|
|
@ -87,9 +76,6 @@ services:
|
||||||
# - "127.0.0.1:4002:4002"
|
# - "127.0.0.1:4002:4002"
|
||||||
# - "127.0.0.1:5900:5900"
|
# - "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:
|
# ib_gw_live:
|
||||||
# image: waytrade/ib-gateway:1012.2i
|
# image: waytrade/ib-gateway:1012.2i
|
||||||
# restart: no
|
# 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": {
|
"nodes": {
|
||||||
"nixpkgs": {
|
"flake-utils": {
|
||||||
|
"inputs": {
|
||||||
|
"systems": "systems"
|
||||||
|
},
|
||||||
"locked": {
|
"locked": {
|
||||||
"lastModified": 1765779637,
|
"lastModified": 1689068808,
|
||||||
"narHash": "sha256-KJ2wa/BLSrTqDjbfyNx70ov/HdgNBCBBSQP3BIzKnv4=",
|
"narHash": "sha256-6ixXo3wt24N/melDWjq70UuHQLxGV8jZvooRanIHXw0=",
|
||||||
"owner": "nixos",
|
"owner": "numtide",
|
||||||
"repo": "nixpkgs",
|
"repo": "flake-utils",
|
||||||
"rev": "1306659b587dc277866c7b69eb97e5f07864d8c4",
|
"rev": "919d646de7be200f3bf08cb76ae1f09402b6f9b4",
|
||||||
"type": "github"
|
"type": "github"
|
||||||
},
|
},
|
||||||
"original": {
|
"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",
|
"ref": "nixos-unstable",
|
||||||
"repo": "nixpkgs",
|
"repo": "nixpkgs",
|
||||||
"type": "github"
|
"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": {
|
"root": {
|
||||||
"inputs": {
|
"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`,
|
# NOTE: to convert to a poetry2nix env like this here are the
|
||||||
# https://pyproject-nix.github.io/pyproject.nix/templates.html#impure
|
# steps:
|
||||||
# https://github.com/pyproject-nix/pyproject.nix/blob/master/templates/impure/flake.nix
|
# - install poetry in your system nix config
|
||||||
{
|
# - convert the repo to use poetry using `poetry init`:
|
||||||
description = "An impure `piker` overlay using `uv` with Nix(OS)";
|
# 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 = {
|
# GROKin tips:
|
||||||
nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable";
|
# - 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 =
|
outputs = {
|
||||||
{ nixpkgs, ... }:
|
self,
|
||||||
let
|
nixpkgs,
|
||||||
inherit (nixpkgs) lib;
|
flake-utils,
|
||||||
forAllSystems = lib.genAttrs lib.systems.flakeExposed;
|
poetry2nix,
|
||||||
in
|
}:
|
||||||
{
|
# TODO: build cross-OS and use the `${system}` var thingy..
|
||||||
devShells = forAllSystems (
|
flake-utils.lib.eachDefaultSystem (system:
|
||||||
system:
|
let
|
||||||
let
|
# use PWD as sources
|
||||||
pkgs = nixpkgs.legacyPackages.${system};
|
projectDir = ./.;
|
||||||
|
pyproject = ./pyproject.toml;
|
||||||
|
poetrylock = ./poetry.lock;
|
||||||
|
|
||||||
# do store-path extractions
|
# TODO: port to 3.11 and support both versions?
|
||||||
qt6baseStorePath = lib.getLib pkgs.qt6.qtbase;
|
python = "python3.10";
|
||||||
# ?TODO? can remove below since manual linking not needed?
|
|
||||||
# qt6QtWaylandStorePath = lib.getLib pkgs.qt6.qtwayland;
|
|
||||||
|
|
||||||
# XXX NOTE XXX, for now we overlay specific pkgs via
|
# for more functions and examples.
|
||||||
# a major-version-pinned-`cpython`
|
# inherit
|
||||||
cpython = "python313";
|
# (poetry2nix.legacyPackages.${system})
|
||||||
pypkgs = pkgs."${cpython}Packages";
|
# mkPoetryApplication;
|
||||||
in
|
# pkgs = nixpkgs.legacyPackages.${system};
|
||||||
{
|
|
||||||
default = pkgs.mkShell {
|
|
||||||
|
|
||||||
packages = with pkgs; [
|
pkgs = nixpkgs.legacyPackages.x86_64-linux;
|
||||||
# XXX, ensure sh completions active!
|
lib = pkgs.lib;
|
||||||
bashInteractive
|
p2npkgs = poetry2nix.legacyPackages.x86_64-linux;
|
||||||
bash-completion
|
|
||||||
|
|
||||||
# dev utils
|
# define all pkg overrides per dep, see edgecases.md:
|
||||||
ruff
|
# https://github.com/nix-community/poetry2nix/blob/master/docs/edgecases.md
|
||||||
pypkgs.ruff
|
# 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
|
# auto-generate override entries
|
||||||
qt6.qtbase
|
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
|
# override some ahead-of-time compiled extensions
|
||||||
python313 # ?TODO^ how to set from `cpython` above?
|
# to be built with their wheels.
|
||||||
pypkgs.pyqt6
|
ahot_overrides = p2n-overrides.extend(
|
||||||
pypkgs.pyqt6-sip
|
final: prev: {
|
||||||
pypkgs.qtpy
|
|
||||||
pypkgs.qdarkstyle
|
|
||||||
pypkgs.rapidfuzz
|
|
||||||
];
|
|
||||||
|
|
||||||
shellHook = ''
|
# llvmlite = prev.llvmlite.override {
|
||||||
# unmask to debug **this** dev-shell-hook
|
# preferWheel = false;
|
||||||
# set -e
|
# };
|
||||||
|
|
||||||
# set qt-base/plugin path(s)
|
# TODO: get this workin with p2n and nixpkgs..
|
||||||
QTBASE_PATH="${qt6baseStorePath}/lib"
|
# pyqt6 = prev.pyqt6.override {
|
||||||
QT_PLUGIN_PATH="${qt6baseStorePath}/lib/qt-6/plugins"
|
# preferWheel = true;
|
||||||
QT_QPA_PLATFORM_PLUGIN_PATH="$QT_PLUGIN_PATH/platforms"
|
# };
|
||||||
|
|
||||||
# link in Qt cc lib paths from <nixpkgs>
|
# NOTE: this DOESN'T work atm but after a fix
|
||||||
LD_LIBRARY_PATH="$LD_LIBRARY_PATH:$QTBASE_PATH"
|
# to poetry2nix, it will and actually this line
|
||||||
LD_LIBRARY_PATH="$LD_LIBRARY_PATH:$QT_PLUGIN_PATH"
|
# won't be needed - thanks @k900:
|
||||||
LD_LIBRARY_PATH="$LD_LIBRARY_PATH:$QT_QPA_PLATFORM_PLUGIN_PATH"
|
# 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.)
|
# see PR from @k900:
|
||||||
LD_LIBRARY_PATH="${pkgs.stdenv.cc.cc.lib}/lib:$LD_LIBRARY_PATH"
|
# https://github.com/nix-community/poetry2nix/pull/1257
|
||||||
|
# pyqt5-qt5 = prev.pyqt5-qt5.override {
|
||||||
|
# withWebkit = false;
|
||||||
|
# preferWheel = true;
|
||||||
|
# };
|
||||||
|
|
||||||
export LD_LIBRARY_PATH
|
# TODO: patch in an override for polars to build
|
||||||
|
# from src! See the details likely needed from
|
||||||
# RUNTIME-SETTINGS
|
# the cryptography entry:
|
||||||
#
|
# https://github.com/nix-community/poetry2nix/blob/master/overrides/default.nix#L426-L435
|
||||||
# ------ Qt ------
|
polars = prev.polars.override {
|
||||||
# XXX, unmask to debug qt .so linking/loading deats
|
preferWheel = true;
|
||||||
# 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
|
|
||||||
'';
|
|
||||||
};
|
|
||||||
}
|
|
||||||
);
|
);
|
||||||
};
|
|
||||||
|
# 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,
|
Account,
|
||||||
load_account,
|
load_account,
|
||||||
load_account_from_ledger,
|
load_account_from_ledger,
|
||||||
|
open_pps,
|
||||||
open_account,
|
open_account,
|
||||||
Position,
|
Position,
|
||||||
)
|
)
|
||||||
|
|
@ -67,6 +68,7 @@ __all__ = [
|
||||||
'load_account_from_ledger',
|
'load_account_from_ledger',
|
||||||
'mk_allocator',
|
'mk_allocator',
|
||||||
'open_account',
|
'open_account',
|
||||||
|
'open_pps',
|
||||||
'open_trade_ledger',
|
'open_trade_ledger',
|
||||||
'unpack_fqme',
|
'unpack_fqme',
|
||||||
'DerivTypes',
|
'DerivTypes',
|
||||||
|
|
|
||||||
|
|
@ -40,7 +40,7 @@ import tomli_w # for fast ledger writing
|
||||||
|
|
||||||
from piker.types import Struct
|
from piker.types import Struct
|
||||||
from piker import config
|
from piker import config
|
||||||
from piker.log import get_logger
|
from ..log import get_logger
|
||||||
from .calc import (
|
from .calc import (
|
||||||
iter_by_dt,
|
iter_by_dt,
|
||||||
)
|
)
|
||||||
|
|
@ -239,9 +239,7 @@ class TransactionLedger(UserDict):
|
||||||
|
|
||||||
symcache: SymbologyCache = self._symcache
|
symcache: SymbologyCache = self._symcache
|
||||||
towrite: dict[str, Any] = {}
|
towrite: dict[str, Any] = {}
|
||||||
for tid, txdict in self.tx_sort(
|
for tid, txdict in self.tx_sort(self.data.copy()):
|
||||||
self.data.copy()
|
|
||||||
):
|
|
||||||
# write blank-str expiry for non-expiring assets
|
# write blank-str expiry for non-expiring assets
|
||||||
if (
|
if (
|
||||||
'expiry' in txdict
|
'expiry' in txdict
|
||||||
|
|
@ -379,7 +377,7 @@ def open_trade_ledger(
|
||||||
account,
|
account,
|
||||||
dirpath=_fp,
|
dirpath=_fp,
|
||||||
)
|
)
|
||||||
cpy: dict = ledger_dict.copy()
|
cpy = ledger_dict.copy()
|
||||||
|
|
||||||
# XXX NOTE: if not provided presume we are being called from
|
# XXX NOTE: if not provided presume we are being called from
|
||||||
# sync code and need to maybe run `trio` to generate..
|
# sync code and need to maybe run `trio` to generate..
|
||||||
|
|
@ -408,13 +406,7 @@ def open_trade_ledger(
|
||||||
account=account,
|
account=account,
|
||||||
mod=mod,
|
mod=mod,
|
||||||
symcache=symcache,
|
symcache=symcache,
|
||||||
|
tx_sort=getattr(mod, 'tx_sort', tx_sort),
|
||||||
# NOTE: allow backends to provide custom ledger sorting
|
|
||||||
tx_sort=getattr(
|
|
||||||
mod,
|
|
||||||
'tx_sort',
|
|
||||||
tx_sort,
|
|
||||||
),
|
|
||||||
)
|
)
|
||||||
try:
|
try:
|
||||||
yield ledger
|
yield ledger
|
||||||
|
|
|
||||||
|
|
@ -305,8 +305,8 @@ class MktPair(Struct, frozen=True):
|
||||||
# config right?
|
# config right?
|
||||||
# src_type: AssetTypeName
|
# src_type: AssetTypeName
|
||||||
|
|
||||||
# for derivs, info describing contract, egs. strike price, call
|
# for derivs, info describing contract, egs.
|
||||||
# or put, swap type, exercise model, etc.
|
# strike price, call or put, swap type, exercise model, etc.
|
||||||
contract_info: list[str] | None = None
|
contract_info: list[str] | None = None
|
||||||
|
|
||||||
# TODO: rename to sectype since all of these can
|
# TODO: rename to sectype since all of these can
|
||||||
|
|
|
||||||
|
|
@ -30,8 +30,7 @@ from types import ModuleType
|
||||||
from typing import (
|
from typing import (
|
||||||
Any,
|
Any,
|
||||||
Iterator,
|
Iterator,
|
||||||
Generator,
|
Generator
|
||||||
TYPE_CHECKING,
|
|
||||||
)
|
)
|
||||||
|
|
||||||
import pendulum
|
import pendulum
|
||||||
|
|
@ -60,10 +59,8 @@ from ..clearing._messages import (
|
||||||
BrokerdPosition,
|
BrokerdPosition,
|
||||||
)
|
)
|
||||||
from piker.types import Struct
|
from piker.types import Struct
|
||||||
from piker.log import get_logger
|
from piker.data._symcache import SymbologyCache
|
||||||
|
from ..log import get_logger
|
||||||
if TYPE_CHECKING:
|
|
||||||
from piker.data._symcache import SymbologyCache
|
|
||||||
|
|
||||||
log = get_logger(__name__)
|
log = get_logger(__name__)
|
||||||
|
|
||||||
|
|
@ -356,12 +353,13 @@ class Position(Struct):
|
||||||
) -> bool:
|
) -> bool:
|
||||||
'''
|
'''
|
||||||
Update clearing table by calculating the rolling ppu and
|
Update clearing table by calculating the rolling ppu and
|
||||||
(accumulative) size in both the clears entry and local attrs
|
(accumulative) size in both the clears entry and local
|
||||||
state.
|
attrs state.
|
||||||
|
|
||||||
Inserts are always done in datetime sorted order.
|
Inserts are always done in datetime sorted order.
|
||||||
|
|
||||||
'''
|
'''
|
||||||
|
# added: bool = False
|
||||||
tid: str = t.tid
|
tid: str = t.tid
|
||||||
if tid in self._events:
|
if tid in self._events:
|
||||||
log.debug(
|
log.debug(
|
||||||
|
|
@ -369,7 +367,7 @@ class Position(Struct):
|
||||||
f'\n'
|
f'\n'
|
||||||
f'{t}\n'
|
f'{t}\n'
|
||||||
)
|
)
|
||||||
return False
|
# return added
|
||||||
|
|
||||||
# TODO: apparently this IS possible with a dict but not
|
# TODO: apparently this IS possible with a dict but not
|
||||||
# common and probably not that beneficial unless we're also
|
# common and probably not that beneficial unless we're also
|
||||||
|
|
@ -450,12 +448,6 @@ class Position(Struct):
|
||||||
# def suggest_split(self) -> float:
|
# 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):
|
class Account(Struct):
|
||||||
'''
|
'''
|
||||||
|
|
@ -499,23 +491,12 @@ class Account(Struct):
|
||||||
|
|
||||||
def update_from_ledger(
|
def update_from_ledger(
|
||||||
self,
|
self,
|
||||||
ledger: TransactionLedger|dict[str, Transaction],
|
ledger: TransactionLedger | dict[str, Transaction],
|
||||||
cost_scalar: float = 2,
|
cost_scalar: float = 2,
|
||||||
symcache: SymbologyCache|None = None,
|
symcache: SymbologyCache | None = None,
|
||||||
|
|
||||||
_mktmap_table: dict[str, MktPair] | 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]:
|
) -> dict[str, Position]:
|
||||||
'''
|
'''
|
||||||
Update the internal `.pps[str, Position]` table from input
|
Update the internal `.pps[str, Position]` table from input
|
||||||
|
|
@ -558,32 +539,11 @@ class Account(Struct):
|
||||||
if _mktmap_table is None:
|
if _mktmap_table is None:
|
||||||
raise
|
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
|
# XXX: caller is allowed to provide a fallback
|
||||||
# mktmap table for the case where a new position is
|
# mktmap table for the case where a new position is
|
||||||
# being added and the preloaded symcache didn't
|
# being added and the preloaded symcache didn't
|
||||||
# have this entry prior (eg. with frickin IB..)
|
# have this entry prior (eg. with frickin IB..)
|
||||||
if (
|
mkt = _mktmap_table[fqme]
|
||||||
not (mkt := _mktmap_table.get(fqme))
|
|
||||||
and
|
|
||||||
required
|
|
||||||
):
|
|
||||||
raise
|
|
||||||
|
|
||||||
elif not required:
|
|
||||||
continue
|
|
||||||
|
|
||||||
else:
|
|
||||||
# should be an entry retreived somewhere
|
|
||||||
assert mkt
|
|
||||||
|
|
||||||
|
|
||||||
if not (pos := pps.get(bs_mktid)):
|
if not (pos := pps.get(bs_mktid)):
|
||||||
|
|
||||||
|
|
@ -700,7 +660,7 @@ class Account(Struct):
|
||||||
def write_config(self) -> None:
|
def write_config(self) -> None:
|
||||||
'''
|
'''
|
||||||
Write the current account state to the user's account TOML file, normally
|
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?
|
# TODO: show diff output?
|
||||||
|
|
@ -754,7 +714,7 @@ class Account(Struct):
|
||||||
# XXX WTF: if we use a tomlkit.Integer here we get this
|
# XXX WTF: if we use a tomlkit.Integer here we get this
|
||||||
# super weird --1 thing going on for cumsize!?1!
|
# super weird --1 thing going on for cumsize!?1!
|
||||||
# NOTE: the fix was to always float() the size value loaded
|
# NOTE: the fix was to always float() the size value loaded
|
||||||
# in open_account() below!
|
# in open_pps() below!
|
||||||
config.write(
|
config.write(
|
||||||
config=self.conf,
|
config=self.conf,
|
||||||
path=self.conf_path,
|
path=self.conf_path,
|
||||||
|
|
@ -938,6 +898,7 @@ def open_account(
|
||||||
clears_table['dt'] = dt
|
clears_table['dt'] = dt
|
||||||
trans.append(Transaction(
|
trans.append(Transaction(
|
||||||
fqme=bs_mktid,
|
fqme=bs_mktid,
|
||||||
|
# sym=mkt,
|
||||||
bs_mktid=bs_mktid,
|
bs_mktid=bs_mktid,
|
||||||
tid=tid,
|
tid=tid,
|
||||||
# XXX: not sure why sometimes these are loaded as
|
# XXX: not sure why sometimes these are loaded as
|
||||||
|
|
@ -960,22 +921,11 @@ def open_account(
|
||||||
):
|
):
|
||||||
expiry: pendulum.DateTime = pendulum.parse(expiry)
|
expiry: pendulum.DateTime = pendulum.parse(expiry)
|
||||||
|
|
||||||
# !XXX, should never be duplicates over
|
pp = pp_objs[bs_mktid] = Position(
|
||||||
# a backend-(broker)-system's unique market-IDs!
|
mkt,
|
||||||
if pos := pp_objs.get(bs_mktid):
|
split_ratio=split_ratio,
|
||||||
if mkt != pos.mkt:
|
bs_mktid=bs_mktid,
|
||||||
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,
|
|
||||||
)
|
|
||||||
|
|
||||||
# XXX: super critical, we need to be sure to include
|
# XXX: super critical, we need to be sure to include
|
||||||
# all pps.toml clears to avoid reusing clears that were
|
# 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
|
# state, since today's records may have already been
|
||||||
# processed!
|
# processed!
|
||||||
for t in trans:
|
for t in trans:
|
||||||
added: bool = pos.add_clear(t)
|
pp.add_clear(t)
|
||||||
if not added:
|
|
||||||
log.warning(
|
|
||||||
f'Txn already recorded in pp ??\n'
|
|
||||||
f'\n'
|
|
||||||
f'{t}\n'
|
|
||||||
)
|
|
||||||
try:
|
try:
|
||||||
yield acnt
|
yield acnt
|
||||||
finally:
|
finally:
|
||||||
|
|
@ -997,6 +942,20 @@ def open_account(
|
||||||
acnt.write_config()
|
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(
|
def load_account_from_ledger(
|
||||||
|
|
||||||
brokername: str,
|
brokername: str,
|
||||||
|
|
|
||||||
|
|
@ -32,7 +32,6 @@ from typing import (
|
||||||
TYPE_CHECKING,
|
TYPE_CHECKING,
|
||||||
)
|
)
|
||||||
|
|
||||||
from tractor.devx import maybe_open_crash_handler
|
|
||||||
import polars as pl
|
import polars as pl
|
||||||
from pendulum import (
|
from pendulum import (
|
||||||
DateTime,
|
DateTime,
|
||||||
|
|
@ -268,6 +267,9 @@ def iter_by_dt(
|
||||||
(v := tx.get(k))
|
(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
|
# only call parser on the value if not None from
|
||||||
# the `parsers` table above (when NOT using
|
# the `parsers` table above (when NOT using
|
||||||
# `.get()`), otherwise pass through the value and
|
# `.get()`), otherwise pass through the value and
|
||||||
|
|
@ -284,50 +286,26 @@ def iter_by_dt(
|
||||||
return ret
|
return ret
|
||||||
|
|
||||||
else:
|
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
|
continue
|
||||||
|
|
||||||
# XXX: we should never really get here bc it means some kinda
|
# XXX: should never get here..
|
||||||
# bad txn-record (field) data..
|
|
||||||
#
|
|
||||||
# -> set the `debug_mode = True` if you want to trace such
|
|
||||||
# cases from REPL ;)
|
|
||||||
else:
|
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:
|
if debug:
|
||||||
with maybe_open_crash_handler(
|
import tractor
|
||||||
pdb=debug,
|
with tractor.devx.maybe_open_crash_handler():
|
||||||
raise_on_exit=False,
|
raise ValueError(
|
||||||
):
|
f'Invalid txn time ??\n'
|
||||||
raise ValueError(report)
|
f'txn-id: {k!r}\n'
|
||||||
else:
|
f'{k!r}: {v!r}\n'
|
||||||
log.error(report)
|
)
|
||||||
|
# assert v is not None, f'No valid value for `{k}`!?'
|
||||||
|
|
||||||
if _invalid is not None:
|
if _invalid is not None:
|
||||||
_invalid.append(tx)
|
_invalid.append(tx)
|
||||||
return from_timestamp(0.)
|
return from_timestamp(0.)
|
||||||
|
|
||||||
|
# breakpoint()
|
||||||
|
|
||||||
entry: tuple[str, dict]|Transaction
|
entry: tuple[str, dict]|Transaction
|
||||||
invalid: list = []
|
invalid: list = []
|
||||||
for entry in sorted(
|
for entry in sorted(
|
||||||
|
|
@ -341,6 +319,8 @@ def iter_by_dt(
|
||||||
log.warning(
|
log.warning(
|
||||||
f'Ignoring txn w invalid timestamp ??\n'
|
f'Ignoring txn w invalid timestamp ??\n'
|
||||||
f'{pformat(entry)}\n'
|
f'{pformat(entry)}\n'
|
||||||
|
# f'txn-id: {k!r}\n'
|
||||||
|
# f'{k!r}: {v!r}\n'
|
||||||
)
|
)
|
||||||
continue
|
continue
|
||||||
|
|
||||||
|
|
@ -406,7 +386,6 @@ def open_ledger_dfs(
|
||||||
acctname: str,
|
acctname: str,
|
||||||
|
|
||||||
ledger: TransactionLedger | None = None,
|
ledger: TransactionLedger | None = None,
|
||||||
debug_mode: bool = False,
|
|
||||||
|
|
||||||
**kwargs,
|
**kwargs,
|
||||||
|
|
||||||
|
|
@ -421,10 +400,8 @@ def open_ledger_dfs(
|
||||||
can update the ledger on exit.
|
can update the ledger on exit.
|
||||||
|
|
||||||
'''
|
'''
|
||||||
with maybe_open_crash_handler(
|
from piker.toolz import open_crash_handler
|
||||||
pdb=debug_mode,
|
with open_crash_handler():
|
||||||
# raise_on_exit=False,
|
|
||||||
):
|
|
||||||
if not ledger:
|
if not ledger:
|
||||||
import time
|
import time
|
||||||
from ._ledger import open_trade_ledger
|
from ._ledger import open_trade_ledger
|
||||||
|
|
@ -516,7 +493,7 @@ def ledger_to_dfs(
|
||||||
|
|
||||||
df = dfs[key] = ldf.with_columns([
|
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
|
# amount of source asset "sent" (via buy txns in
|
||||||
# the market) to acquire the dst asset, PER txn.
|
# the market) to acquire the dst asset, PER txn.
|
||||||
|
|
@ -531,7 +508,7 @@ def ledger_to_dfs(
|
||||||
]).with_columns([
|
]).with_columns([
|
||||||
|
|
||||||
# rolling balance in src asset units
|
# 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
|
# "position operation type" in terms of increasing the
|
||||||
# amount in the dst asset (entering) or decreasing 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
|
# cost that was included in the least-recently
|
||||||
# entered txn that is still part of the current CSi
|
# entered txn that is still part of the current CSi
|
||||||
# set.
|
# 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)
|
# if over the current txn size (by multiplication)
|
||||||
# and then reverse that previusly applied cost on
|
# and then reverse that previusly applied cost on
|
||||||
# the txn_cost for this record.
|
# the txn_cost for this record.
|
||||||
|
|
|
||||||
|
|
@ -300,8 +300,7 @@ def disect(
|
||||||
assert not df.is_empty()
|
assert not df.is_empty()
|
||||||
|
|
||||||
# muck around in pdbp REPL
|
# muck around in pdbp REPL
|
||||||
# tractor.devx.mk_pdb().set_trace()
|
breakpoint()
|
||||||
# breakpoint()
|
|
||||||
|
|
||||||
# TODO: we REALLY need a better console REPL for this
|
# TODO: we REALLY need a better console REPL for this
|
||||||
# kinda thing..
|
# kinda thing..
|
||||||
|
|
|
||||||
|
|
@ -51,6 +51,7 @@ __brokers__: list[str] = [
|
||||||
'ib',
|
'ib',
|
||||||
'kraken',
|
'kraken',
|
||||||
'kucoin',
|
'kucoin',
|
||||||
|
'deribit',
|
||||||
|
|
||||||
# broken but used to work
|
# broken but used to work
|
||||||
# 'questrade',
|
# 'questrade',
|
||||||
|
|
@ -61,7 +62,6 @@ __brokers__: list[str] = [
|
||||||
# wstrade
|
# wstrade
|
||||||
# iex
|
# iex
|
||||||
|
|
||||||
# deribit
|
|
||||||
# bitso
|
# bitso
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
@ -98,14 +98,13 @@ async def open_cached_client(
|
||||||
If one has not been setup do it and cache it.
|
If one has not been setup do it and cache it.
|
||||||
|
|
||||||
'''
|
'''
|
||||||
brokermod: ModuleType = get_brokermod(brokername)
|
brokermod = get_brokermod(brokername)
|
||||||
|
|
||||||
# TODO: make abstract or `typing.Protocol`
|
|
||||||
# client: Client
|
|
||||||
async with maybe_open_context(
|
async with maybe_open_context(
|
||||||
acm_func=brokermod.get_client,
|
acm_func=brokermod.get_client,
|
||||||
kwargs=kwargs,
|
kwargs=kwargs,
|
||||||
|
|
||||||
) as (cache_hit, client):
|
) as (cache_hit, client):
|
||||||
|
|
||||||
if cache_hit:
|
if cache_hit:
|
||||||
log.runtime(f'Reusing existing {client}')
|
log.runtime(f'Reusing existing {client}')
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -94,21 +94,18 @@ class L1(Struct):
|
||||||
|
|
||||||
|
|
||||||
# validation type
|
# validation type
|
||||||
# https://developers.binance.com/docs/derivatives/usds-margined-futures/websocket-market-streams/Aggregate-Trade-Streams#response-example
|
|
||||||
class AggTrade(Struct, frozen=True):
|
class AggTrade(Struct, frozen=True):
|
||||||
e: str # Event type
|
e: str # Event type
|
||||||
E: int # Event time
|
E: int # Event time
|
||||||
s: str # Symbol
|
s: str # Symbol
|
||||||
a: int # Aggregate trade ID
|
a: int # Aggregate trade ID
|
||||||
p: float # Price
|
p: float # Price
|
||||||
q: float # Quantity with all the market trades
|
q: float # Quantity
|
||||||
f: int # First trade ID
|
f: int # First trade ID
|
||||||
l: int # noqa Last trade ID
|
l: int # noqa Last trade ID
|
||||||
T: int # Trade time
|
T: int # Trade time
|
||||||
m: bool # Is the buyer the market maker?
|
m: bool # Is the buyer the market maker?
|
||||||
M: bool|None = None # Ignore
|
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
|
|
||||||
|
|
||||||
|
|
||||||
async def stream_messages(
|
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
|
# https://developers.binance.com/docs/binance-spot-api-docs#2025-08-26
|
||||||
# will become non-optional 2025-08-28?
|
# will become non-optional 2025-08-28?
|
||||||
# https://developers.binance.com/docs/binance-spot-api-docs#future-changes
|
# https://developers.binance.com/docs/binance-spot-api-docs#future-changes
|
||||||
pegInstructionsAllowed: bool = False
|
pegInstructionsAllowed: bool|None = None
|
||||||
|
|
||||||
# https://developers.binance.com/docs/binance-spot-api-docs#2025-12-02
|
|
||||||
opoAllowed: bool = False
|
|
||||||
|
|
||||||
filters: dict[
|
filters: dict[
|
||||||
str,
|
str,
|
||||||
|
|
@ -223,10 +220,7 @@ class FutesPair(Pair):
|
||||||
assert pair == self.pair # sanity
|
assert pair == self.pair # sanity
|
||||||
return f'{expiry}'
|
return f'{expiry}'
|
||||||
|
|
||||||
case (
|
case 'PERPETUAL':
|
||||||
'PERPETUAL'
|
|
||||||
| 'TRADIFI_PERPETUAL'
|
|
||||||
):
|
|
||||||
return 'PERP'
|
return 'PERP'
|
||||||
|
|
||||||
case '':
|
case '':
|
||||||
|
|
@ -255,10 +249,7 @@ class FutesPair(Pair):
|
||||||
margin: str = self.marginAsset
|
margin: str = self.marginAsset
|
||||||
|
|
||||||
match ctype:
|
match ctype:
|
||||||
case (
|
case 'PERPETUAL':
|
||||||
'PERPETUAL'
|
|
||||||
| 'TRADIFI_PERPETUAL'
|
|
||||||
):
|
|
||||||
return f'{margin}M'
|
return f'{margin}M'
|
||||||
|
|
||||||
case (
|
case (
|
||||||
|
|
|
||||||
|
|
@ -471,15 +471,11 @@ def search(
|
||||||
|
|
||||||
'''
|
'''
|
||||||
# global opts
|
# global opts
|
||||||
brokermods: list[ModuleType] = list(config['brokermods'].values())
|
brokermods = 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
|
|
||||||
|
|
||||||
# define tractor entrypoint
|
# define tractor entrypoint
|
||||||
async def main(func):
|
async def main(func):
|
||||||
|
|
||||||
async with maybe_open_pikerd(
|
async with maybe_open_pikerd(
|
||||||
loglevel=config['loglevel'],
|
loglevel=config['loglevel'],
|
||||||
debug_mode=pdb,
|
debug_mode=pdb,
|
||||||
|
|
|
||||||
|
|
@ -22,9 +22,7 @@ routines should be primitive data types where possible.
|
||||||
"""
|
"""
|
||||||
import inspect
|
import inspect
|
||||||
from types import ModuleType
|
from types import ModuleType
|
||||||
from typing import (
|
from typing import List, Dict, Any, Optional
|
||||||
Any,
|
|
||||||
)
|
|
||||||
|
|
||||||
import trio
|
import trio
|
||||||
|
|
||||||
|
|
@ -36,10 +34,8 @@ from ..accounting import MktPair
|
||||||
|
|
||||||
|
|
||||||
async def api(brokername: str, methname: str, **kwargs) -> dict:
|
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)
|
brokermod = get_brokermod(brokername)
|
||||||
async with brokermod.get_client() as client:
|
async with brokermod.get_client() as client:
|
||||||
meth = getattr(client, methname, None)
|
meth = getattr(client, methname, None)
|
||||||
|
|
@ -66,14 +62,10 @@ async def api(brokername: str, methname: str, **kwargs) -> dict:
|
||||||
|
|
||||||
async def stocks_quote(
|
async def stocks_quote(
|
||||||
brokermod: ModuleType,
|
brokermod: ModuleType,
|
||||||
tickers: list[str]
|
tickers: List[str]
|
||||||
|
) -> Dict[str, Dict[str, Any]]:
|
||||||
) -> dict[str, dict[str, Any]]:
|
"""Return quotes dict for ``tickers``.
|
||||||
'''
|
"""
|
||||||
Return a `dict` of snapshot quotes for the provided input
|
|
||||||
`tickers`: a `list` of fqmes.
|
|
||||||
|
|
||||||
'''
|
|
||||||
async with brokermod.get_client() as client:
|
async with brokermod.get_client() as client:
|
||||||
return await client.quote(tickers)
|
return await client.quote(tickers)
|
||||||
|
|
||||||
|
|
@ -82,15 +74,13 @@ async def stocks_quote(
|
||||||
async def option_chain(
|
async def option_chain(
|
||||||
brokermod: ModuleType,
|
brokermod: ModuleType,
|
||||||
symbol: str,
|
symbol: str,
|
||||||
date: str|None = None,
|
date: Optional[str] = None,
|
||||||
) -> dict[str, dict[str, dict[str, Any]]]:
|
) -> Dict[str, Dict[str, Dict[str, Any]]]:
|
||||||
'''
|
"""Return option chain for ``symbol`` for ``date``.
|
||||||
Return option chain for ``symbol`` for ``date``.
|
|
||||||
|
|
||||||
By default all expiries are returned. If ``date`` is provided
|
By default all expiries are returned. If ``date`` is provided
|
||||||
then contract quotes for that single expiry are returned.
|
then contract quotes for that single expiry are returned.
|
||||||
|
"""
|
||||||
'''
|
|
||||||
async with brokermod.get_client() as client:
|
async with brokermod.get_client() as client:
|
||||||
if date:
|
if date:
|
||||||
id = int((await client.tickers2ids([symbol]))[symbol])
|
id = int((await client.tickers2ids([symbol]))[symbol])
|
||||||
|
|
@ -108,7 +98,7 @@ async def option_chain(
|
||||||
# async def contracts(
|
# async def contracts(
|
||||||
# brokermod: ModuleType,
|
# brokermod: ModuleType,
|
||||||
# symbol: str,
|
# symbol: str,
|
||||||
# ) -> dict[str, dict[str, dict[str, Any]]]:
|
# ) -> Dict[str, Dict[str, Dict[str, Any]]]:
|
||||||
# """Return option contracts (all expiries) for ``symbol``.
|
# """Return option contracts (all expiries) for ``symbol``.
|
||||||
# """
|
# """
|
||||||
# async with brokermod.get_client() as client:
|
# async with brokermod.get_client() as client:
|
||||||
|
|
@ -120,24 +110,15 @@ async def bars(
|
||||||
brokermod: ModuleType,
|
brokermod: ModuleType,
|
||||||
symbol: str,
|
symbol: str,
|
||||||
**kwargs,
|
**kwargs,
|
||||||
) -> dict[str, dict[str, dict[str, Any]]]:
|
) -> Dict[str, Dict[str, Dict[str, Any]]]:
|
||||||
'''
|
"""Return option contracts (all expiries) for ``symbol``.
|
||||||
Return option contracts (all expiries) for ``symbol``.
|
"""
|
||||||
|
|
||||||
'''
|
|
||||||
async with brokermod.get_client() as client:
|
async with brokermod.get_client() as client:
|
||||||
return await client.bars(symbol, **kwargs)
|
return await client.bars(symbol, **kwargs)
|
||||||
|
|
||||||
|
|
||||||
async def search_w_brokerd(
|
async def search_w_brokerd(name: str, pattern: str) -> dict:
|
||||||
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:
|
async with open_cached_client(name) as client:
|
||||||
|
|
||||||
# TODO: support multiple asset type concurrent searches.
|
# TODO: support multiple asset type concurrent searches.
|
||||||
|
|
@ -149,12 +130,12 @@ async def symbol_search(
|
||||||
pattern: str,
|
pattern: str,
|
||||||
**kwargs,
|
**kwargs,
|
||||||
|
|
||||||
) -> dict[str, dict[str, dict[str, Any]]]:
|
) -> Dict[str, Dict[str, Dict[str, Any]]]:
|
||||||
'''
|
'''
|
||||||
Return symbol info from broker.
|
Return symbol info from broker.
|
||||||
|
|
||||||
'''
|
'''
|
||||||
results: list[str] = []
|
results = []
|
||||||
|
|
||||||
async def search_backend(
|
async def search_backend(
|
||||||
brokermod: ModuleType
|
brokermod: ModuleType
|
||||||
|
|
@ -162,13 +143,6 @@ async def symbol_search(
|
||||||
|
|
||||||
brokername: str = mod.name
|
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(
|
async with maybe_spawn_brokerd(
|
||||||
mod.name,
|
mod.name,
|
||||||
infect_asyncio=getattr(
|
infect_asyncio=getattr(
|
||||||
|
|
@ -188,6 +162,7 @@ async def symbol_search(
|
||||||
))
|
))
|
||||||
|
|
||||||
async with trio.open_nursery() as n:
|
async with trio.open_nursery() as n:
|
||||||
|
|
||||||
for mod in brokermods:
|
for mod in brokermods:
|
||||||
n.start_soon(search_backend, mod.name)
|
n.start_soon(search_backend, mod.name)
|
||||||
|
|
||||||
|
|
@ -197,13 +172,11 @@ async def symbol_search(
|
||||||
async def mkt_info(
|
async def mkt_info(
|
||||||
brokermod: ModuleType,
|
brokermod: ModuleType,
|
||||||
fqme: str,
|
fqme: str,
|
||||||
|
|
||||||
**kwargs,
|
**kwargs,
|
||||||
|
|
||||||
) -> MktPair:
|
) -> MktPair:
|
||||||
'''
|
'''
|
||||||
Return the `piker.accounting.MktPair` info struct from a given
|
Return MktPair info from broker including src and dst assets.
|
||||||
backend broker tradable src/dst asset pair.
|
|
||||||
|
|
||||||
'''
|
'''
|
||||||
async with open_cached_client(brokermod.name) as client:
|
async with open_cached_client(brokermod.name) as client:
|
||||||
|
|
|
||||||
|
|
@ -25,6 +25,7 @@ from .api import (
|
||||||
get_client,
|
get_client,
|
||||||
)
|
)
|
||||||
from .feed import (
|
from .feed import (
|
||||||
|
get_mkt_info,
|
||||||
open_history_client,
|
open_history_client,
|
||||||
open_symbol_search,
|
open_symbol_search,
|
||||||
stream_quotes,
|
stream_quotes,
|
||||||
|
|
@ -34,15 +35,20 @@ from .feed import (
|
||||||
# open_trade_dialog,
|
# open_trade_dialog,
|
||||||
# norm_trade_records,
|
# norm_trade_records,
|
||||||
# )
|
# )
|
||||||
|
from .venues import (
|
||||||
|
OptionPair,
|
||||||
|
)
|
||||||
|
|
||||||
log = get_logger(__name__)
|
log = get_logger(__name__)
|
||||||
|
|
||||||
__all__ = [
|
__all__ = [
|
||||||
'get_client',
|
'get_client',
|
||||||
# 'trades_dialogue',
|
# 'trades_dialogue',
|
||||||
|
'get_mkt_info',
|
||||||
'open_history_client',
|
'open_history_client',
|
||||||
'open_symbol_search',
|
'open_symbol_search',
|
||||||
'stream_quotes',
|
'stream_quotes',
|
||||||
|
'OptionPair',
|
||||||
# 'norm_trade_records',
|
# 'norm_trade_records',
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
|
||||||
File diff suppressed because it is too large
Load Diff
|
|
@ -18,38 +18,59 @@
|
||||||
Deribit backend.
|
Deribit backend.
|
||||||
|
|
||||||
'''
|
'''
|
||||||
|
from __future__ import annotations
|
||||||
from contextlib import asynccontextmanager as acm
|
from contextlib import asynccontextmanager as acm
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
from typing import Any, Optional, Callable
|
from typing import (
|
||||||
|
# Any,
|
||||||
|
# Optional,
|
||||||
|
Callable,
|
||||||
|
)
|
||||||
|
# from pprint import pformat
|
||||||
import time
|
import time
|
||||||
|
|
||||||
|
import cryptofeed
|
||||||
import trio
|
import trio
|
||||||
from trio_typing import TaskStatus
|
from trio_typing import TaskStatus
|
||||||
import pendulum
|
from pendulum import (
|
||||||
from rapidfuzz import process as fuzzy
|
from_timestamp,
|
||||||
|
)
|
||||||
import numpy as np
|
import numpy as np
|
||||||
import tractor
|
import tractor
|
||||||
|
|
||||||
from piker.brokers import open_cached_client
|
from piker.accounting import (
|
||||||
from piker.log import get_logger, get_console_log
|
Asset,
|
||||||
from piker.data import ShmArray
|
MktPair,
|
||||||
from piker.brokers._util import (
|
unpack_fqme,
|
||||||
BrokerError,
|
)
|
||||||
|
from piker.brokers import (
|
||||||
|
open_cached_client,
|
||||||
|
NoData,
|
||||||
DataUnavailable,
|
DataUnavailable,
|
||||||
)
|
)
|
||||||
|
from piker._cacheables import (
|
||||||
from cryptofeed import FeedHandler
|
async_lifo_cache,
|
||||||
from cryptofeed.defines import (
|
|
||||||
DERIBIT, L1_BOOK, TRADES, OPTION, CALL, PUT
|
|
||||||
)
|
)
|
||||||
from cryptofeed.symbols import Symbol
|
from piker.log import (
|
||||||
|
get_logger,
|
||||||
|
mk_repr,
|
||||||
|
)
|
||||||
|
from piker.data.validate import FeedInit
|
||||||
|
|
||||||
|
|
||||||
from .api import (
|
from .api import (
|
||||||
Client, Trade,
|
Client,
|
||||||
get_config,
|
# get_config,
|
||||||
str_to_cb_sym, piker_sym_to_cb_sym, cb_sym_to_deribit_inst,
|
piker_sym_to_cb_sym,
|
||||||
|
cb_sym_to_deribit_inst,
|
||||||
|
str_to_cb_sym,
|
||||||
maybe_open_price_feed
|
maybe_open_price_feed
|
||||||
)
|
)
|
||||||
|
from .venues import (
|
||||||
|
Pair,
|
||||||
|
OptionPair,
|
||||||
|
Trade,
|
||||||
|
)
|
||||||
|
|
||||||
_spawn_kwargs = {
|
_spawn_kwargs = {
|
||||||
'infect_asyncio': True,
|
'infect_asyncio': True,
|
||||||
|
|
@ -64,90 +85,215 @@ async def open_history_client(
|
||||||
mkt: MktPair,
|
mkt: MktPair,
|
||||||
) -> tuple[Callable, int]:
|
) -> tuple[Callable, int]:
|
||||||
|
|
||||||
fnstrument: str = mkt.bs_fqme
|
|
||||||
# TODO implement history getter for the new storage layer.
|
# TODO implement history getter for the new storage layer.
|
||||||
async with open_cached_client('deribit') as client:
|
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(
|
async def get_ohlc(
|
||||||
end_dt: Optional[datetime] = None,
|
timeframe: float,
|
||||||
start_dt: Optional[datetime] = None,
|
end_dt: datetime | None = None,
|
||||||
|
start_dt: datetime | None = None,
|
||||||
|
|
||||||
) -> tuple[
|
) -> tuple[
|
||||||
np.ndarray,
|
np.ndarray,
|
||||||
datetime, # start
|
datetime, # start
|
||||||
datetime, # end
|
datetime, # end
|
||||||
]:
|
]:
|
||||||
|
if timeframe != 60:
|
||||||
|
raise DataUnavailable('Only 1m bars are supported')
|
||||||
|
|
||||||
array = await client.bars(
|
array: np.ndarray = await client.bars(
|
||||||
instrument,
|
mkt,
|
||||||
start_dt=start_dt,
|
start_dt=start_dt,
|
||||||
end_dt=end_dt,
|
end_dt=end_dt,
|
||||||
)
|
)
|
||||||
if len(array) == 0:
|
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'])
|
start_dt = from_timestamp(array[0]['time'])
|
||||||
end_dt = pendulum.from_timestamp(array[-1]['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
|
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(
|
async def stream_quotes(
|
||||||
|
|
||||||
send_chan: trio.abc.SendChannel,
|
send_chan: trio.abc.SendChannel,
|
||||||
symbols: list[str],
|
symbols: list[str],
|
||||||
feed_is_live: trio.Event,
|
feed_is_live: trio.Event,
|
||||||
loglevel: str = None,
|
|
||||||
|
|
||||||
# startup sync
|
# startup sync
|
||||||
task_status: TaskStatus[tuple[dict, dict]] = trio.TASK_STATUS_IGNORED,
|
task_status: TaskStatus[tuple[dict, dict]] = trio.TASK_STATUS_IGNORED,
|
||||||
|
|
||||||
) -> None:
|
) -> 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 (
|
async with (
|
||||||
open_cached_client('deribit') as client,
|
open_cached_client('deribit') as client,
|
||||||
send_chan as send_chan
|
send_chan as send_chan
|
||||||
):
|
):
|
||||||
|
mkt: MktPair
|
||||||
|
pair: Pair
|
||||||
|
mkt, pair = await get_mkt_info(sym)
|
||||||
|
|
||||||
init_msgs = {
|
# build out init msgs according to latest spec
|
||||||
# pass back token, and bool, signalling if we're the writer
|
init_msgs.append(
|
||||||
# and that history has been written
|
FeedInit(
|
||||||
sym: {
|
mkt_info=mkt,
|
||||||
'symbol_info': {
|
)
|
||||||
'asset_type': 'option',
|
)
|
||||||
'price_tick_size': 0.0005
|
# build `cryptofeed` feed-handle
|
||||||
},
|
cf_sym: cryptofeed.Symbol = piker_sym_to_cb_sym(sym)
|
||||||
'shm_write_opts': {'sum_tick_vml': False},
|
|
||||||
'fqsn': 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(
|
# else:
|
||||||
cb_sym_to_deribit_inst(nsym), count=1)).trades
|
last_trade = Trade(
|
||||||
|
**(last_trades[0])
|
||||||
|
)
|
||||||
|
|
||||||
if len(last_trades) == 0:
|
first_quote: dict = {
|
||||||
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 = {
|
|
||||||
'symbol': sym,
|
'symbol': sym,
|
||||||
'last': last_trade.price,
|
'last': last_trade.price,
|
||||||
'brokerd_ts': last_trade.timestamp,
|
'brokerd_ts': last_trade.timestamp,
|
||||||
|
|
@ -158,13 +304,84 @@ async def stream_quotes(
|
||||||
'broker_ts': last_trade.timestamp
|
'broker_ts': last_trade.timestamp
|
||||||
}]
|
}]
|
||||||
}
|
}
|
||||||
task_status.started((init_msgs, first_quote))
|
task_status.started((
|
||||||
|
init_msgs,
|
||||||
|
first_quote,
|
||||||
|
))
|
||||||
|
|
||||||
feed_is_live.set()
|
feed_is_live.set()
|
||||||
|
|
||||||
async for typ, quote in stream:
|
# NOTE XXX, static for now!
|
||||||
topic = quote['symbol']
|
# => since this only handles ONE mkt feed at a time we
|
||||||
await send_chan.send({topic: quote})
|
# 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
|
@tractor.context
|
||||||
|
|
@ -174,12 +391,21 @@ async def open_symbol_search(
|
||||||
async with open_cached_client('deribit') as client:
|
async with open_cached_client('deribit') as client:
|
||||||
|
|
||||||
# load all symbols locally for fast search
|
# load all symbols locally for fast search
|
||||||
cache = await client.cache_symbols()
|
# cache = client._pairs
|
||||||
await ctx.started()
|
await ctx.started()
|
||||||
|
|
||||||
async with ctx.open_stream() as stream:
|
async with ctx.open_stream() as stream:
|
||||||
|
pattern: str
|
||||||
async for pattern in stream:
|
async for pattern in stream:
|
||||||
# repack in dict form
|
|
||||||
await stream.send(
|
# NOTE: pattern fuzzy-matching is done within
|
||||||
await client.search_symbols(pattern))
|
# 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:
|
if TYPE_CHECKING:
|
||||||
from .api import Client
|
from .api import Client
|
||||||
|
from ib_insync import IB
|
||||||
import i3ipc
|
import i3ipc
|
||||||
|
|
||||||
log = get_logger('piker.brokers.ib')
|
log = get_logger('piker.brokers.ib')
|
||||||
|
|
@ -61,7 +62,7 @@ no_setup_msg:str = (
|
||||||
|
|
||||||
|
|
||||||
def try_xdo_manual(
|
def try_xdo_manual(
|
||||||
client: Client,
|
vnc_sockaddr: str,
|
||||||
):
|
):
|
||||||
'''
|
'''
|
||||||
Do the "manual" `xdo`-based screen switch + click
|
Do the "manual" `xdo`-based screen switch + click
|
||||||
|
|
@ -78,7 +79,6 @@ def try_xdo_manual(
|
||||||
_reset_tech = 'i3ipc_xdotool'
|
_reset_tech = 'i3ipc_xdotool'
|
||||||
return True
|
return True
|
||||||
except OSError:
|
except OSError:
|
||||||
vnc_sockaddr: str = client.conf.vnc_addrs
|
|
||||||
log.exception(
|
log.exception(
|
||||||
no_setup_msg.format(vnc_sockaddr=vnc_sockaddr)
|
no_setup_msg.format(vnc_sockaddr=vnc_sockaddr)
|
||||||
)
|
)
|
||||||
|
|
@ -86,6 +86,7 @@ def try_xdo_manual(
|
||||||
|
|
||||||
|
|
||||||
async def data_reset_hack(
|
async def data_reset_hack(
|
||||||
|
# vnc_host: str,
|
||||||
client: Client,
|
client: Client,
|
||||||
reset_type: Literal['data', 'connection'],
|
reset_type: Literal['data', 'connection'],
|
||||||
|
|
||||||
|
|
@ -117,24 +118,36 @@ async def data_reset_hack(
|
||||||
that need to be wrangle.
|
that need to be wrangle.
|
||||||
|
|
||||||
'''
|
'''
|
||||||
|
ib_client: IB = client.ib
|
||||||
|
|
||||||
# look up any user defined vnc socket address mapped from
|
# look up any user defined vnc socket address mapped from
|
||||||
# a particular API socket port.
|
# a particular API socket port.
|
||||||
vnc_addrs: tuple[str]|None = client.conf.get('vnc_addrs')
|
api_port: str = str(ib_client.client.port)
|
||||||
if not vnc_addrs:
|
vnc_host: str
|
||||||
|
vnc_port: int
|
||||||
|
vnc_sockaddr: tuple[str] | None = client.conf.get('vnc_addrs')
|
||||||
|
|
||||||
|
if not vnc_sockaddr:
|
||||||
log.warning(
|
log.warning(
|
||||||
no_setup_msg.format(vnc_sockaddr=client.conf)
|
no_setup_msg.format(vnc_sockaddr=vnc_sockaddr)
|
||||||
+
|
+
|
||||||
'REQUIRES A `vnc_addrs: array` ENTRY'
|
'REQUIRES A `vnc_addrs: array` ENTRY'
|
||||||
)
|
)
|
||||||
|
|
||||||
|
vnc_host, vnc_port = vnc_sockaddr.get(
|
||||||
|
api_port,
|
||||||
|
('localhost', 3003)
|
||||||
|
)
|
||||||
global _reset_tech
|
global _reset_tech
|
||||||
|
|
||||||
match _reset_tech:
|
match _reset_tech:
|
||||||
case 'vnc':
|
case 'vnc':
|
||||||
try:
|
try:
|
||||||
await tractor.to_asyncio.run_task(
|
await tractor.to_asyncio.run_task(
|
||||||
partial(
|
partial(
|
||||||
vnc_click_hack,
|
vnc_click_hack,
|
||||||
client=client,
|
host=vnc_host,
|
||||||
|
port=vnc_port,
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
except (
|
except (
|
||||||
|
|
@ -145,31 +158,29 @@ async def data_reset_hack(
|
||||||
import i3ipc # noqa (since a deps dynamic check)
|
import i3ipc # noqa (since a deps dynamic check)
|
||||||
except ModuleNotFoundError:
|
except ModuleNotFoundError:
|
||||||
log.warning(
|
log.warning(
|
||||||
no_setup_msg.format(vnc_sockaddr=client.conf)
|
no_setup_msg.format(vnc_sockaddr=vnc_sockaddr)
|
||||||
)
|
)
|
||||||
return False
|
return False
|
||||||
|
|
||||||
# XXX, Xorg only workaround..
|
if vnc_host not in {
|
||||||
# TODO? remove now that we have `pyvnc`?
|
'localhost',
|
||||||
# if vnc_host not in {
|
'127.0.0.1',
|
||||||
# 'localhost',
|
}:
|
||||||
# '127.0.0.1',
|
focussed, matches = i3ipc_fin_wins_titled()
|
||||||
# }:
|
if not matches:
|
||||||
# focussed, matches = i3ipc_fin_wins_titled()
|
log.warning(
|
||||||
# if not matches:
|
no_setup_msg.format(vnc_sockaddr=vnc_sockaddr)
|
||||||
# log.warning(
|
)
|
||||||
# no_setup_msg.format(vnc_sockaddr=vnc_sockaddr)
|
return False
|
||||||
# )
|
else:
|
||||||
# return False
|
try_xdo_manual(vnc_sockaddr)
|
||||||
# else:
|
|
||||||
# try_xdo_manual(vnc_sockaddr)
|
|
||||||
|
|
||||||
# localhost but no vnc-client or it borked..
|
# localhost but no vnc-client or it borked..
|
||||||
else:
|
else:
|
||||||
try_xdo_manual(client)
|
try_xdo_manual(vnc_sockaddr)
|
||||||
|
|
||||||
case 'i3ipc_xdotool':
|
case 'i3ipc_xdotool':
|
||||||
try_xdo_manual(client)
|
try_xdo_manual(vnc_sockaddr)
|
||||||
# i3ipc_xdotool_manual_click_hack()
|
# i3ipc_xdotool_manual_click_hack()
|
||||||
|
|
||||||
case _ as tech:
|
case _ as tech:
|
||||||
|
|
@ -180,66 +191,21 @@ async def data_reset_hack(
|
||||||
|
|
||||||
|
|
||||||
async def vnc_click_hack(
|
async def vnc_click_hack(
|
||||||
client: Client,
|
host: str,
|
||||||
reset_type: str = 'data',
|
port: int,
|
||||||
pw: str|None = None,
|
reset_type: str = 'data'
|
||||||
|
|
||||||
) -> None:
|
) -> None:
|
||||||
'''
|
'''
|
||||||
Reset the data or network connection for the VNC attached
|
Reset the data or network connection for the VNC attached
|
||||||
ib-gateway using a (magic) keybinding combo.
|
ib gateway using magic combos.
|
||||||
|
|
||||||
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'}
|
|
||||||
|
|
||||||
'''
|
'''
|
||||||
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:
|
try:
|
||||||
from pyvnc import (
|
import asyncvnc
|
||||||
AsyncVNCClient,
|
|
||||||
VNCConfig,
|
|
||||||
Point,
|
|
||||||
MOUSE_BUTTON_LEFT,
|
|
||||||
)
|
|
||||||
except ModuleNotFoundError:
|
except ModuleNotFoundError:
|
||||||
log.warning(
|
log.warning(
|
||||||
"In order to leverage `piker`'s built-in data reset hacks, install "
|
"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
|
return
|
||||||
|
|
||||||
|
|
@ -250,27 +216,24 @@ async def vnc_click_hack(
|
||||||
'connection': 'r'
|
'connection': 'r'
|
||||||
}[reset_type]
|
}[reset_type]
|
||||||
|
|
||||||
with tractor.devx.open_crash_handler():
|
async with asyncvnc.connect(
|
||||||
client = await AsyncVNCClient.connect(
|
host,
|
||||||
VNCConfig(
|
port=port,
|
||||||
host=host,
|
|
||||||
port=port,
|
# TODO: doesn't work?
|
||||||
password=pw,
|
# 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:
|
client.mouse.click()
|
||||||
# move to middle of screen
|
client.keyboard.press('Ctrl', 'Alt', key) # keys are stacked
|
||||||
# 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
|
|
||||||
|
|
||||||
|
|
||||||
def i3ipc_fin_wins_titled(
|
def i3ipc_fin_wins_titled(
|
||||||
|
|
|
||||||
|
|
@ -97,6 +97,10 @@ from ._util import (
|
||||||
get_logger,
|
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]] = [
|
_bar_load_dtype: list[tuple[str, type]] = [
|
||||||
# NOTE XXX: only part that's diff
|
# NOTE XXX: only part that's diff
|
||||||
# from our default fields where
|
# from our default fields where
|
||||||
|
|
@ -944,7 +948,6 @@ class Client:
|
||||||
)
|
)
|
||||||
if tkr:
|
if tkr:
|
||||||
break
|
break
|
||||||
|
|
||||||
except TimeoutError as err:
|
except TimeoutError as err:
|
||||||
timeouterr = err
|
timeouterr = err
|
||||||
await asyncio.sleep(0.01)
|
await asyncio.sleep(0.01)
|
||||||
|
|
@ -953,9 +956,7 @@ class Client:
|
||||||
else:
|
else:
|
||||||
if not warnset:
|
if not warnset:
|
||||||
log.warning(
|
log.warning(
|
||||||
f'Quote req timed out..\n'
|
f'Quote req timed out..maybe venue is closed?\n'
|
||||||
f'Maybe the venue is closed?\n'
|
|
||||||
f'\n'
|
|
||||||
f'{asdict(contract)}'
|
f'{asdict(contract)}'
|
||||||
)
|
)
|
||||||
warnset = True
|
warnset = True
|
||||||
|
|
@ -967,11 +968,9 @@ class Client:
|
||||||
)
|
)
|
||||||
break
|
break
|
||||||
else:
|
else:
|
||||||
if (
|
if timeouterr and raise_on_timeout:
|
||||||
timeouterr
|
import pdbp
|
||||||
and
|
pdbp.set_trace()
|
||||||
raise_on_timeout
|
|
||||||
):
|
|
||||||
raise timeouterr
|
raise timeouterr
|
||||||
|
|
||||||
if not warnset:
|
if not warnset:
|
||||||
|
|
@ -1368,7 +1367,9 @@ async def load_aio_clients(
|
||||||
|
|
||||||
|
|
||||||
async def load_clients_for_trio(
|
async def load_clients_for_trio(
|
||||||
chan: tractor.to_asyncio.LinkedTaskChannel,
|
from_trio: asyncio.Queue,
|
||||||
|
to_trio: trio.abc.SendChannel,
|
||||||
|
|
||||||
) -> None:
|
) -> None:
|
||||||
'''
|
'''
|
||||||
Pure async mngr proxy to ``load_aio_clients()``.
|
Pure async mngr proxy to ``load_aio_clients()``.
|
||||||
|
|
@ -1381,7 +1382,8 @@ async def load_clients_for_trio(
|
||||||
disconnect_on_exit=False,
|
disconnect_on_exit=False,
|
||||||
) as accts2clients:
|
) as accts2clients:
|
||||||
|
|
||||||
chan.started_nowait(accts2clients)
|
to_trio.send_nowait(accts2clients)
|
||||||
|
|
||||||
# TODO: maybe a sync event to wait on instead?
|
# TODO: maybe a sync event to wait on instead?
|
||||||
await asyncio.sleep(float('inf'))
|
await asyncio.sleep(float('inf'))
|
||||||
|
|
||||||
|
|
@ -1528,22 +1530,23 @@ class MethodProxy:
|
||||||
|
|
||||||
|
|
||||||
async def open_aio_client_method_relay(
|
async def open_aio_client_method_relay(
|
||||||
chan: tractor.to_asyncio.LinkedTaskChannel,
|
from_trio: asyncio.Queue,
|
||||||
|
to_trio: trio.abc.SendChannel,
|
||||||
client: Client,
|
client: Client,
|
||||||
event_consumers: dict[str, trio.Event],
|
event_consumers: dict[str, trio.Event],
|
||||||
|
|
||||||
) -> None:
|
) -> None:
|
||||||
|
|
||||||
# sync with `open_client_proxy()` caller
|
# sync with `open_client_proxy()` caller
|
||||||
chan.started_nowait(client)
|
to_trio.send_nowait(client)
|
||||||
|
|
||||||
# TODO: separate channel for error handling?
|
# 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
|
# relay all method requests to ``asyncio``-side client and deliver
|
||||||
# back results
|
# back results
|
||||||
while not chan._to_trio._closed: # <- TODO, better check like `._web_bs`?
|
while not to_trio._closed:
|
||||||
msg: tuple[str, dict]|dict|None = await chan.get()
|
msg: tuple[str, dict]|dict|None = await from_trio.get()
|
||||||
match msg:
|
match msg:
|
||||||
case None: # termination sentinel
|
case None: # termination sentinel
|
||||||
log.info('asyncio `Client` method-proxy SHUTDOWN!')
|
log.info('asyncio `Client` method-proxy SHUTDOWN!')
|
||||||
|
|
@ -1556,7 +1559,7 @@ async def open_aio_client_method_relay(
|
||||||
try:
|
try:
|
||||||
resp = await meth(**kwargs)
|
resp = await meth(**kwargs)
|
||||||
# echo the msg back
|
# echo the msg back
|
||||||
chan.send_nowait({'result': resp})
|
to_trio.send_nowait({'result': resp})
|
||||||
|
|
||||||
except (
|
except (
|
||||||
RequestError,
|
RequestError,
|
||||||
|
|
@ -1564,10 +1567,10 @@ async def open_aio_client_method_relay(
|
||||||
# TODO: relay all errors to trio?
|
# TODO: relay all errors to trio?
|
||||||
# BaseException,
|
# BaseException,
|
||||||
) as err:
|
) as err:
|
||||||
chan.send_nowait({'exception': err})
|
to_trio.send_nowait({'exception': err})
|
||||||
|
|
||||||
case {'error': content}:
|
case {'error': content}:
|
||||||
chan.send_nowait({'exception': content})
|
to_trio.send_nowait({'exception': content})
|
||||||
|
|
||||||
case _:
|
case _:
|
||||||
raise ValueError(f'Unhandled msg {msg}')
|
raise ValueError(f'Unhandled msg {msg}')
|
||||||
|
|
|
||||||
|
|
@ -117,11 +117,7 @@ def pack_position(
|
||||||
symbol=fqme,
|
symbol=fqme,
|
||||||
currency=con.currency,
|
currency=con.currency,
|
||||||
size=float(pos.position),
|
size=float(pos.position),
|
||||||
avg_price=(
|
avg_price=float(pos.avgCost) / float(con.multiplier or 1.0),
|
||||||
float(pos.avgCost)
|
|
||||||
/
|
|
||||||
float(con.multiplier or 1.0)
|
|
||||||
),
|
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
@ -362,10 +358,6 @@ async def update_and_audit_pos_msg(
|
||||||
size=ibpos.position,
|
size=ibpos.position,
|
||||||
|
|
||||||
avg_price=pikerpos.ppu,
|
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())
|
ibfmtmsg: str = pformat(ibpos._asdict())
|
||||||
|
|
@ -434,8 +426,7 @@ async def aggr_open_orders(
|
||||||
|
|
||||||
) -> None:
|
) -> None:
|
||||||
'''
|
'''
|
||||||
Collect all open orders from client and fill in `order_msgs:
|
Collect all open orders from client and fill in `order_msgs: list`.
|
||||||
list`.
|
|
||||||
|
|
||||||
'''
|
'''
|
||||||
trades: list[Trade] = client.ib.openTrades()
|
trades: list[Trade] = client.ib.openTrades()
|
||||||
|
|
@ -567,7 +558,7 @@ async def open_trade_dialog(
|
||||||
ledgers: dict[str, TransactionLedger] = {}
|
ledgers: dict[str, TransactionLedger] = {}
|
||||||
tables: dict[str, Account] = {}
|
tables: dict[str, Account] = {}
|
||||||
order_msgs: list[Status] = []
|
order_msgs: list[Status] = []
|
||||||
conf: dict = get_config()
|
conf = get_config()
|
||||||
accounts_def_inv: bidict[str, str] = bidict(
|
accounts_def_inv: bidict[str, str] = bidict(
|
||||||
conf['accounts']
|
conf['accounts']
|
||||||
).inverse
|
).inverse
|
||||||
|
|
|
||||||
|
|
@ -214,9 +214,7 @@ async def open_history_client(
|
||||||
|
|
||||||
# could be trying to retreive bars over weekend
|
# could be trying to retreive bars over weekend
|
||||||
if out is None:
|
if out is None:
|
||||||
log.error(
|
log.error(f"Can't grab bars starting at {end_dt}!?!?")
|
||||||
f"No bars starting at {end_dt!r} !?!?"
|
|
||||||
)
|
|
||||||
if (
|
if (
|
||||||
end_dt
|
end_dt
|
||||||
and head_dt
|
and head_dt
|
||||||
|
|
@ -613,7 +611,7 @@ async def get_bars(
|
||||||
data_cs.cancel()
|
data_cs.cancel()
|
||||||
|
|
||||||
# spawn new data reset task
|
# spawn new data reset task
|
||||||
data_cs, reset_done = await tn.start(
|
data_cs, reset_done = await nurse.start(
|
||||||
partial(
|
partial(
|
||||||
wait_on_data_reset,
|
wait_on_data_reset,
|
||||||
proxy,
|
proxy,
|
||||||
|
|
@ -635,12 +633,12 @@ async def get_bars(
|
||||||
unset_resetter: bool = False
|
unset_resetter: bool = False
|
||||||
async with (
|
async with (
|
||||||
tractor.trionics.collapse_eg(),
|
tractor.trionics.collapse_eg(),
|
||||||
trio.open_nursery() as tn
|
trio.open_nursery() as nurse
|
||||||
):
|
):
|
||||||
|
|
||||||
# start history request that we allow
|
# start history request that we allow
|
||||||
# to run indefinitely until a result is acquired
|
# 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
|
# start history reset loop which waits up to the timeout
|
||||||
# for a result before triggering a data feed reset.
|
# for a result before triggering a data feed reset.
|
||||||
|
|
@ -660,7 +658,7 @@ async def get_bars(
|
||||||
unset_resetter: bool = True
|
unset_resetter: bool = True
|
||||||
|
|
||||||
# spawn new data reset task
|
# spawn new data reset task
|
||||||
data_cs, reset_done = await tn.start(
|
data_cs, reset_done = await nurse.start(
|
||||||
partial(
|
partial(
|
||||||
wait_on_data_reset,
|
wait_on_data_reset,
|
||||||
proxy,
|
proxy,
|
||||||
|
|
@ -686,6 +684,8 @@ async def get_bars(
|
||||||
_quote_streams: dict[str, trio.abc.ReceiveStream] = {}
|
_quote_streams: dict[str, trio.abc.ReceiveStream] = {}
|
||||||
|
|
||||||
|
|
||||||
|
# TODO! update to the new style sig with,
|
||||||
|
# `chan: to_asyncio.LinkedTaskChannel,`
|
||||||
async def _setup_quote_stream(
|
async def _setup_quote_stream(
|
||||||
chan: tractor.to_asyncio.LinkedTaskChannel,
|
chan: tractor.to_asyncio.LinkedTaskChannel,
|
||||||
symbol: str,
|
symbol: str,
|
||||||
|
|
@ -896,10 +896,7 @@ async def open_aio_quote_stream(
|
||||||
symbol: str,
|
symbol: str,
|
||||||
contract: Contract|None = None,
|
contract: Contract|None = None,
|
||||||
|
|
||||||
) -> (
|
) -> trio.abc.ReceiveStream:
|
||||||
trio.abc.Channel| # iface
|
|
||||||
tractor.to_asyncio.LinkedTaskChannel # actually
|
|
||||||
):
|
|
||||||
'''
|
'''
|
||||||
Open a real-time `Ticker` quote stream from an `asyncio.Task`
|
Open a real-time `Ticker` quote stream from an `asyncio.Task`
|
||||||
spawned via `tractor.to_asyncio.open_channel_from()`, deliver the
|
spawned via `tractor.to_asyncio.open_channel_from()`, deliver the
|
||||||
|
|
@ -922,7 +919,6 @@ async def open_aio_quote_stream(
|
||||||
yield from_aio
|
yield from_aio
|
||||||
return
|
return
|
||||||
|
|
||||||
from_aio: tractor.to_asyncio.LinkedTaskChannel
|
|
||||||
async with tractor.to_asyncio.open_channel_from(
|
async with tractor.to_asyncio.open_channel_from(
|
||||||
_setup_quote_stream,
|
_setup_quote_stream,
|
||||||
symbol=symbol,
|
symbol=symbol,
|
||||||
|
|
@ -1083,8 +1079,7 @@ async def stream_quotes(
|
||||||
con: Contract = details.contract
|
con: Contract = details.contract
|
||||||
first_ticker: Ticker|None = None
|
first_ticker: Ticker|None = None
|
||||||
|
|
||||||
timeout: float = 1.6
|
with trio.move_on_after(1.6) as quote_cs:
|
||||||
with trio.move_on_after(timeout) as quote_cs:
|
|
||||||
first_ticker: Ticker = await proxy.get_quote(
|
first_ticker: Ticker = await proxy.get_quote(
|
||||||
contract=con,
|
contract=con,
|
||||||
raise_on_timeout=False,
|
raise_on_timeout=False,
|
||||||
|
|
@ -1093,9 +1088,7 @@ async def stream_quotes(
|
||||||
# XXX should never happen with this ep right?
|
# XXX should never happen with this ep right?
|
||||||
# but if so then, more then likely mkt is closed?
|
# but if so then, more then likely mkt is closed?
|
||||||
if quote_cs.cancelled_caught:
|
if quote_cs.cancelled_caught:
|
||||||
log.warning(
|
await tractor.pause()
|
||||||
f'First quote req timed out after {timeout!r}s'
|
|
||||||
)
|
|
||||||
|
|
||||||
if first_ticker:
|
if first_ticker:
|
||||||
first_quote: dict = normalize(first_ticker)
|
first_quote: dict = normalize(first_ticker)
|
||||||
|
|
@ -1168,7 +1161,6 @@ async def stream_quotes(
|
||||||
)
|
)
|
||||||
cs: trio.CancelScope|None = None
|
cs: trio.CancelScope|None = None
|
||||||
startup: bool = True
|
startup: bool = True
|
||||||
iter_quotes: trio.abc.Channel
|
|
||||||
while (
|
while (
|
||||||
startup
|
startup
|
||||||
or
|
or
|
||||||
|
|
@ -1177,11 +1169,11 @@ async def stream_quotes(
|
||||||
with trio.CancelScope() as cs:
|
with trio.CancelScope() as cs:
|
||||||
async with (
|
async with (
|
||||||
tractor.trionics.collapse_eg(),
|
tractor.trionics.collapse_eg(),
|
||||||
trio.open_nursery() as tn,
|
trio.open_nursery() as nurse,
|
||||||
open_aio_quote_stream(
|
open_aio_quote_stream(
|
||||||
symbol=sym,
|
symbol=sym,
|
||||||
contract=con,
|
contract=con,
|
||||||
) as iter_quotes,
|
) as stream,
|
||||||
):
|
):
|
||||||
# ?TODO? can we rm this - particularly for `ib_async`?
|
# ?TODO? can we rm this - particularly for `ib_async`?
|
||||||
# ugh, clear ticks since we've consumed them
|
# ugh, clear ticks since we've consumed them
|
||||||
|
|
@ -1210,9 +1202,9 @@ async def stream_quotes(
|
||||||
await rt_ev.wait()
|
await rt_ev.wait()
|
||||||
cs.cancel() # cancel called should now be set
|
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 syminfo.get('no_vlm', False):
|
||||||
if not init_msg.shm_write_opts['has_vlm']:
|
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
|
# wait for real volume on feed (trading might be
|
||||||
# closed)
|
# closed)
|
||||||
while True:
|
while True:
|
||||||
ticker = await iter_quotes.receive()
|
ticker = await stream.receive()
|
||||||
|
|
||||||
# for a real volume contract we rait for
|
# for a real volume contract we rait for
|
||||||
# the first "real" trade to take place
|
# the first "real" trade to take place
|
||||||
if (
|
if (
|
||||||
# not calc_price
|
# not calc_price
|
||||||
# and not ticker.rtTime
|
# and not ticker.rtTime
|
||||||
False
|
not ticker.rtTime
|
||||||
# not ticker.rtTime
|
|
||||||
):
|
):
|
||||||
# spin consuming tickers until we
|
# spin consuming tickers until we
|
||||||
# get a real market datum
|
# get a real market datum
|
||||||
log.debug(f"New unsent ticker: {ticker}")
|
log.debug(f"New unsent ticker: {ticker}")
|
||||||
continue
|
continue
|
||||||
|
|
||||||
else:
|
else:
|
||||||
log.debug("Received first volume tick")
|
log.debug("Received first volume tick")
|
||||||
# ugh, clear ticks since we've
|
# ugh, clear ticks since we've
|
||||||
|
|
@ -1257,18 +1247,13 @@ async def stream_quotes(
|
||||||
log.debug(f"First ticker received {quote}")
|
log.debug(f"First ticker received {quote}")
|
||||||
|
|
||||||
# tell data-layer spawner-caller that live
|
# tell data-layer spawner-caller that live
|
||||||
# quotes are now active desptie not having
|
# quotes are now streaming.
|
||||||
# necessarily received a first vlm/clearing
|
|
||||||
# tick.
|
|
||||||
ticker = await iter_quotes.receive()
|
|
||||||
feed_is_live.set()
|
feed_is_live.set()
|
||||||
fqme: str = quote['fqme']
|
|
||||||
await send_chan.send({fqme: quote})
|
|
||||||
|
|
||||||
# last = time.time()
|
# last = time.time()
|
||||||
async for ticker in iter_quotes:
|
async for ticker in stream:
|
||||||
quote = normalize(ticker)
|
quote = normalize(ticker)
|
||||||
fqme: str = quote['fqme']
|
fqme = quote['fqme']
|
||||||
log.debug(
|
log.debug(
|
||||||
f'Sending quote\n'
|
f'Sending quote\n'
|
||||||
f'{quote}'
|
f'{quote}'
|
||||||
|
|
|
||||||
|
|
@ -549,7 +549,7 @@ async def open_trade_dialog(
|
||||||
# to be reloaded.
|
# to be reloaded.
|
||||||
balances: dict[str, float] = await client.get_balances()
|
balances: dict[str, float] = await client.get_balances()
|
||||||
|
|
||||||
await verify_balances(
|
verify_balances(
|
||||||
acnt,
|
acnt,
|
||||||
src_fiat,
|
src_fiat,
|
||||||
balances,
|
balances,
|
||||||
|
|
|
||||||
|
|
@ -37,12 +37,6 @@ import tractor
|
||||||
from async_generator import asynccontextmanager
|
from async_generator import asynccontextmanager
|
||||||
import numpy as np
|
import numpy as np
|
||||||
import wrapt
|
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
|
import asks
|
||||||
|
|
||||||
from ..calc import humanize, percent_change
|
from ..calc import humanize, percent_change
|
||||||
|
|
|
||||||
|
|
@ -171,6 +171,7 @@ class OrderClient(Struct):
|
||||||
|
|
||||||
|
|
||||||
async def relay_orders_from_sync_code(
|
async def relay_orders_from_sync_code(
|
||||||
|
|
||||||
client: OrderClient,
|
client: OrderClient,
|
||||||
symbol_key: str,
|
symbol_key: str,
|
||||||
to_ems_stream: tractor.MsgStream,
|
to_ems_stream: tractor.MsgStream,
|
||||||
|
|
@ -244,11 +245,6 @@ async def open_ems(
|
||||||
|
|
||||||
async with maybe_open_emsd(
|
async with maybe_open_emsd(
|
||||||
broker,
|
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,
|
loglevel=loglevel,
|
||||||
) as portal:
|
) as portal:
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -388,7 +388,6 @@ async def open_brokerd_dialog(
|
||||||
for ep_name in [
|
for ep_name in [
|
||||||
'open_trade_dialog', # probably final name?
|
'open_trade_dialog', # probably final name?
|
||||||
'trades_dialogue', # legacy
|
'trades_dialogue', # legacy
|
||||||
# ^!TODO, rm this since all backends ported no ?!?
|
|
||||||
]:
|
]:
|
||||||
trades_endpoint = getattr(
|
trades_endpoint = getattr(
|
||||||
brokermod,
|
brokermod,
|
||||||
|
|
@ -655,11 +654,7 @@ class Router(Struct):
|
||||||
flume = feed.flumes[fqme]
|
flume = feed.flumes[fqme]
|
||||||
first_quote: dict = flume.first_quote
|
first_quote: dict = flume.first_quote
|
||||||
book: DarkBook = self.get_dark_book(broker)
|
book: DarkBook = self.get_dark_book(broker)
|
||||||
|
book.lasts[fqme]: float = float(first_quote['last'])
|
||||||
if not (last := first_quote.get('last')):
|
|
||||||
last: float = flume.rt_shm.array[-1]['close']
|
|
||||||
|
|
||||||
book.lasts[fqme]: float = float(last)
|
|
||||||
|
|
||||||
async with self.maybe_open_brokerd_dialog(
|
async with self.maybe_open_brokerd_dialog(
|
||||||
brokermod=brokermod,
|
brokermod=brokermod,
|
||||||
|
|
@ -722,7 +717,7 @@ class Router(Struct):
|
||||||
subs = self.subscribers[sub_key]
|
subs = self.subscribers[sub_key]
|
||||||
|
|
||||||
sent_some: bool = False
|
sent_some: bool = False
|
||||||
for client_stream in subs.copy():
|
for client_stream in subs:
|
||||||
try:
|
try:
|
||||||
await client_stream.send(msg)
|
await client_stream.send(msg)
|
||||||
sent_some = True
|
sent_some = True
|
||||||
|
|
@ -1018,28 +1013,14 @@ async def translate_and_relay_brokerd_events(
|
||||||
status_msg.brokerd_msg = msg
|
status_msg.brokerd_msg = msg
|
||||||
status_msg.src = msg.broker_details['name']
|
status_msg.src = msg.broker_details['name']
|
||||||
|
|
||||||
if not status_msg.req:
|
await router.client_broadcast(
|
||||||
# likely some order change state?
|
status_msg.req.symbol,
|
||||||
await tractor.pause()
|
status_msg,
|
||||||
else:
|
)
|
||||||
await router.client_broadcast(
|
|
||||||
status_msg.req.symbol,
|
|
||||||
status_msg,
|
|
||||||
)
|
|
||||||
|
|
||||||
if status == 'closed':
|
if status == 'closed':
|
||||||
log.info(
|
log.info(f'Execution for {oid} is complete!')
|
||||||
f'Execution is complete!\n'
|
status_msg = book._active.pop(oid)
|
||||||
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'
|
|
||||||
)
|
|
||||||
|
|
||||||
elif status == 'canceled':
|
elif status == 'canceled':
|
||||||
log.cancel(f'Cancellation for {oid} is complete!')
|
log.cancel(f'Cancellation for {oid} is complete!')
|
||||||
|
|
@ -1563,18 +1544,19 @@ async def maybe_open_trade_relays(
|
||||||
|
|
||||||
@tractor.context
|
@tractor.context
|
||||||
async def _emsd_main(
|
async def _emsd_main(
|
||||||
ctx: tractor.Context, # becomes `ems_ctx` below
|
ctx: tractor.Context,
|
||||||
fqme: str,
|
fqme: str,
|
||||||
exec_mode: str, # ('paper', 'live')
|
exec_mode: str, # ('paper', 'live')
|
||||||
loglevel: str|None = None,
|
loglevel: str|None = None,
|
||||||
|
|
||||||
) -> tuple[ # `ctx.started()` value!
|
) -> tuple[
|
||||||
dict[ # positions
|
dict[
|
||||||
tuple[str, str], # brokername, acctid
|
# brokername, acctid
|
||||||
|
tuple[str, str],
|
||||||
list[BrokerdPosition],
|
list[BrokerdPosition],
|
||||||
],
|
],
|
||||||
list[str], # accounts
|
list[str],
|
||||||
dict[str, Status], # dialogs
|
dict[str, Status],
|
||||||
]:
|
]:
|
||||||
'''
|
'''
|
||||||
EMS (sub)actor entrypoint providing the execution management
|
EMS (sub)actor entrypoint providing the execution management
|
||||||
|
|
|
||||||
|
|
@ -301,9 +301,6 @@ class BrokerdError(Struct):
|
||||||
|
|
||||||
# TODO: yeah, so we REALLY need to completely deprecate
|
# TODO: yeah, so we REALLY need to completely deprecate
|
||||||
# this and use the `.accounting.Position` msg-type instead..
|
# 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):
|
class BrokerdPosition(Struct):
|
||||||
'''
|
'''
|
||||||
Position update event from brokerd.
|
Position update event from brokerd.
|
||||||
|
|
@ -316,4 +313,3 @@ class BrokerdPosition(Struct):
|
||||||
avg_price: float
|
avg_price: float
|
||||||
currency: str = ''
|
currency: str = ''
|
||||||
name: str = 'position'
|
name: str = 'position'
|
||||||
bs_mktid: str|int|None = None
|
|
||||||
|
|
|
||||||
|
|
@ -297,8 +297,6 @@ class PaperBoi(Struct):
|
||||||
|
|
||||||
# transmit pp msg to ems
|
# transmit pp msg to ems
|
||||||
pp: Position = self.acnt.pps[bs_mktid]
|
pp: Position = self.acnt.pps[bs_mktid]
|
||||||
# TODO, this will break if `require_only=True` was passed to
|
|
||||||
# `.update_from_ledger()`
|
|
||||||
|
|
||||||
pp_msg = BrokerdPosition(
|
pp_msg = BrokerdPosition(
|
||||||
broker=self.broker,
|
broker=self.broker,
|
||||||
|
|
@ -655,7 +653,6 @@ async def open_trade_dialog(
|
||||||
# in) use manually constructed table from calling
|
# in) use manually constructed table from calling
|
||||||
# the `.get_mkt_info()` provider EP above.
|
# the `.get_mkt_info()` provider EP above.
|
||||||
_mktmap_table=mkt_by_fqme,
|
_mktmap_table=mkt_by_fqme,
|
||||||
only_require=list(mkt_by_fqme),
|
|
||||||
)
|
)
|
||||||
|
|
||||||
pp_msgs: list[BrokerdPosition] = []
|
pp_msgs: list[BrokerdPosition] = []
|
||||||
|
|
|
||||||
|
|
@ -30,7 +30,6 @@ subsys: str = 'piker.clearing'
|
||||||
|
|
||||||
log = get_logger(subsys)
|
log = get_logger(subsys)
|
||||||
|
|
||||||
# TODO, oof doesn't this ignore the `loglevel` then???
|
|
||||||
get_console_log = partial(
|
get_console_log = partial(
|
||||||
get_console_log,
|
get_console_log,
|
||||||
name=subsys,
|
name=subsys,
|
||||||
|
|
|
||||||
|
|
@ -183,8 +183,8 @@ def pikerd(
|
||||||
registry_addrs=regaddrs,
|
registry_addrs=regaddrs,
|
||||||
loglevel=loglevel,
|
loglevel=loglevel,
|
||||||
debug_mode=pdb,
|
debug_mode=pdb,
|
||||||
# enable_transports=['uds'],
|
enable_transports=['uds'],
|
||||||
enable_transports=['tcp'],
|
# enable_transports=['tcp'],
|
||||||
) as service_mngr,
|
) as service_mngr,
|
||||||
):
|
):
|
||||||
assert service_mngr
|
assert service_mngr
|
||||||
|
|
|
||||||
|
|
@ -41,13 +41,10 @@ from .log import get_logger
|
||||||
log = get_logger('broker-config')
|
log = get_logger('broker-config')
|
||||||
|
|
||||||
|
|
||||||
# XXX NOTE: taken from `click`
|
# XXX NOTE: taken from ``click`` since apparently they have some
|
||||||
# |_https://github.com/pallets/click/blob/main/src/click/utils.py#L449
|
# super weirdness with sigint and sudo..no clue
|
||||||
#
|
# we're probably going to slowly just modify it to our own version over
|
||||||
# (since apparently they have some super weirdness with SIGINT and
|
# time..
|
||||||
# sudo.. no clue we're probably going to slowly just modify it to our
|
|
||||||
# own version over time..)
|
|
||||||
#
|
|
||||||
def get_app_dir(
|
def get_app_dir(
|
||||||
app_name: str,
|
app_name: str,
|
||||||
roaming: bool = True,
|
roaming: bool = True,
|
||||||
|
|
@ -264,7 +261,7 @@ def load(
|
||||||
MutableMapping,
|
MutableMapping,
|
||||||
] = tomllib.loads,
|
] = tomllib.loads,
|
||||||
|
|
||||||
touch_if_dne: bool = True,
|
touch_if_dne: bool = False,
|
||||||
|
|
||||||
**tomlkws,
|
**tomlkws,
|
||||||
|
|
||||||
|
|
@ -273,7 +270,7 @@ def load(
|
||||||
Load config file by name.
|
Load config file by name.
|
||||||
|
|
||||||
If desired config is not in the top level piker-user config path then
|
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
|
# create the $HOME/.config/piker dir if dne
|
||||||
|
|
@ -288,8 +285,7 @@ def load(
|
||||||
|
|
||||||
if (
|
if (
|
||||||
not path.is_file()
|
not path.is_file()
|
||||||
and
|
and touch_if_dne
|
||||||
touch_if_dne
|
|
||||||
):
|
):
|
||||||
# only do a template if no path provided,
|
# only do a template if no path provided,
|
||||||
# just touch an empty file with same name.
|
# just touch an empty file with same name.
|
||||||
|
|
|
||||||
|
|
@ -95,12 +95,6 @@ class Sampler:
|
||||||
# history loading.
|
# history loading.
|
||||||
incr_task_cs: trio.CancelScope | None = None
|
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
|
# holds all the ``tractor.Context`` remote subscriptions for
|
||||||
# a particular sample period increment event: all subscribers are
|
# a particular sample period increment event: all subscribers are
|
||||||
# notified on a step.
|
# notified on a step.
|
||||||
|
|
@ -264,15 +258,14 @@ class Sampler:
|
||||||
subs: set
|
subs: set
|
||||||
last_ts, subs = pair
|
last_ts, subs = pair
|
||||||
|
|
||||||
# NOTE, for debugging pub-sub issues
|
task = trio.lowlevel.current_task()
|
||||||
# task = trio.lowlevel.current_task()
|
log.debug(
|
||||||
# log.debug(
|
f'SUBS {self.subscribers}\n'
|
||||||
# f'AlL-SUBS@{period_s!r}: {self.subscribers}\n'
|
f'PAIR {pair}\n'
|
||||||
# f'PAIR: {pair}\n'
|
f'TASK: {task}: {id(task)}\n'
|
||||||
# f'TASK: {task}: {id(task)}\n'
|
f'broadcasting {period_s} -> {last_ts}\n'
|
||||||
# f'broadcasting {period_s} -> {last_ts}\n'
|
# f'consumers: {subs}'
|
||||||
# f'consumers: {subs}'
|
)
|
||||||
# )
|
|
||||||
borked: set[MsgStream] = set()
|
borked: set[MsgStream] = set()
|
||||||
sent: set[MsgStream] = set()
|
sent: set[MsgStream] = set()
|
||||||
while True:
|
while True:
|
||||||
|
|
@ -289,11 +282,13 @@ class Sampler:
|
||||||
await stream.send(msg)
|
await stream.send(msg)
|
||||||
sent.add(stream)
|
sent.add(stream)
|
||||||
|
|
||||||
except self.bcast_errors as err:
|
except (
|
||||||
|
trio.BrokenResourceError,
|
||||||
|
trio.ClosedResourceError,
|
||||||
|
trio.EndOfChannel,
|
||||||
|
):
|
||||||
log.error(
|
log.error(
|
||||||
f'Connection dropped for IPC ctx\n'
|
f'{stream._ctx.chan.uid} dropped connection'
|
||||||
f'{stream._ctx}\n\n'
|
|
||||||
f'Due to {type(err)}'
|
|
||||||
)
|
)
|
||||||
borked.add(stream)
|
borked.add(stream)
|
||||||
else:
|
else:
|
||||||
|
|
@ -400,8 +395,7 @@ async def register_with_sampler(
|
||||||
finally:
|
finally:
|
||||||
if (
|
if (
|
||||||
sub_for_broadcasts
|
sub_for_broadcasts
|
||||||
and
|
and subs
|
||||||
subs
|
|
||||||
):
|
):
|
||||||
try:
|
try:
|
||||||
subs.remove(stream)
|
subs.remove(stream)
|
||||||
|
|
@ -568,7 +562,8 @@ async def open_sample_stream(
|
||||||
|
|
||||||
|
|
||||||
async def sample_and_broadcast(
|
async def sample_and_broadcast(
|
||||||
bus: _FeedsBus,
|
|
||||||
|
bus: _FeedsBus, # noqa
|
||||||
rt_shm: ShmArray,
|
rt_shm: ShmArray,
|
||||||
hist_shm: ShmArray,
|
hist_shm: ShmArray,
|
||||||
quote_stream: trio.abc.ReceiveChannel,
|
quote_stream: trio.abc.ReceiveChannel,
|
||||||
|
|
@ -588,33 +583,11 @@ async def sample_and_broadcast(
|
||||||
|
|
||||||
overruns = Counter()
|
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
|
# iterate stream delivered by broker
|
||||||
async for quotes in quote_stream:
|
async for quotes in quote_stream:
|
||||||
# print(quotes)
|
# print(quotes)
|
||||||
|
|
||||||
# XXX WARNING XXX only enable for debugging bc ow can cost
|
# TODO: ``numba`` this!
|
||||||
# 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?
|
|
||||||
#
|
|
||||||
for broker_symbol, quote in quotes.items():
|
for broker_symbol, quote in quotes.items():
|
||||||
# TODO: in theory you can send the IPC msg *before* writing
|
# TODO: in theory you can send the IPC msg *before* writing
|
||||||
# to the sharedmem array to decrease latency, however, that
|
# to the sharedmem array to decrease latency, however, that
|
||||||
|
|
@ -687,21 +660,6 @@ async def sample_and_broadcast(
|
||||||
sub_key: str = broker_symbol.lower()
|
sub_key: str = broker_symbol.lower()
|
||||||
subs: set[Sub] = bus.get_subs(sub_key)
|
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
|
# NOTE: by default the broker backend doesn't append
|
||||||
# it's own "name" into the fqme schema (but maybe it
|
# it's own "name" into the fqme schema (but maybe it
|
||||||
# should?) so we have to manually generate the correct
|
# should?) so we have to manually generate the correct
|
||||||
|
|
@ -771,14 +729,18 @@ async def sample_and_broadcast(
|
||||||
if lags > 10:
|
if lags > 10:
|
||||||
await tractor.pause()
|
await tractor.pause()
|
||||||
|
|
||||||
except Sampler.bcast_errors as ipc_err:
|
except (
|
||||||
|
trio.BrokenResourceError,
|
||||||
|
trio.ClosedResourceError,
|
||||||
|
trio.EndOfChannel,
|
||||||
|
):
|
||||||
ctx: Context = ipc._ctx
|
ctx: Context = ipc._ctx
|
||||||
chan: Channel = ctx.chan
|
chan: Channel = ctx.chan
|
||||||
if ctx:
|
if ctx:
|
||||||
log.warning(
|
log.warning(
|
||||||
f'Dropped `brokerd`-feed for {broker_symbol!r} due to,\n'
|
'Dropped `brokerd`-quotes-feed connection:\n'
|
||||||
f'x>) {ctx.cid}@{chan.uid}'
|
f'{broker_symbol}:'
|
||||||
f'|_{ipc_err!r}\n\n'
|
f'{ctx.cid}@{chan.uid}'
|
||||||
)
|
)
|
||||||
if sub.throttle_rate:
|
if sub.throttle_rate:
|
||||||
assert ipc._closed
|
assert ipc._closed
|
||||||
|
|
@ -795,11 +757,12 @@ async def sample_and_broadcast(
|
||||||
|
|
||||||
|
|
||||||
async def uniform_rate_send(
|
async def uniform_rate_send(
|
||||||
|
|
||||||
rate: float,
|
rate: float,
|
||||||
quote_stream: trio.abc.ReceiveChannel,
|
quote_stream: trio.abc.ReceiveChannel,
|
||||||
stream: MsgStream,
|
stream: MsgStream,
|
||||||
|
|
||||||
task_status: TaskStatus[None] = trio.TASK_STATUS_IGNORED,
|
task_status: TaskStatus = trio.TASK_STATUS_IGNORED,
|
||||||
|
|
||||||
) -> None:
|
) -> None:
|
||||||
'''
|
'''
|
||||||
|
|
@ -817,16 +780,13 @@ async def uniform_rate_send(
|
||||||
https://gist.github.com/njsmith/7ea44ec07e901cb78ebe1dd8dd846cb9
|
https://gist.github.com/njsmith/7ea44ec07e901cb78ebe1dd8dd846cb9
|
||||||
|
|
||||||
'''
|
'''
|
||||||
# ?TODO? dynamically compute the **actual** approx overhead latency per cycle
|
# TODO: compute the approx overhead latency per cycle
|
||||||
# instead of this magic # bidinezz?
|
left_to_sleep = throttle_period = 1/rate - 0.000616
|
||||||
throttle_period: float = 1/rate - 0.000616
|
|
||||||
left_to_sleep: float = throttle_period
|
|
||||||
|
|
||||||
# send cycle state
|
# send cycle state
|
||||||
first_quote: dict|None
|
|
||||||
first_quote = last_quote = None
|
first_quote = last_quote = None
|
||||||
last_send: float = time.time()
|
last_send = time.time()
|
||||||
diff: float = 0
|
diff = 0
|
||||||
|
|
||||||
task_status.started()
|
task_status.started()
|
||||||
ticks_by_type: dict[
|
ticks_by_type: dict[
|
||||||
|
|
@ -837,28 +797,22 @@ async def uniform_rate_send(
|
||||||
clear_types = _tick_groups['clears']
|
clear_types = _tick_groups['clears']
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
|
|
||||||
# compute the remaining time to sleep for this throttled cycle
|
# 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:
|
if left_to_sleep > 0:
|
||||||
cs: trio.CancelScope
|
|
||||||
with trio.move_on_after(left_to_sleep) as cs:
|
with trio.move_on_after(left_to_sleep) as cs:
|
||||||
sym: str
|
|
||||||
last_quote: dict
|
|
||||||
try:
|
try:
|
||||||
sym, last_quote = await quote_stream.receive()
|
sym, last_quote = await quote_stream.receive()
|
||||||
except trio.EndOfChannel:
|
except trio.EndOfChannel:
|
||||||
log.exception(
|
log.exception(f"feed for {stream} ended?")
|
||||||
f'Live stream for feed for ended?\n'
|
|
||||||
f'<=c\n'
|
|
||||||
f' |_[{stream!r}\n'
|
|
||||||
)
|
|
||||||
break
|
break
|
||||||
|
|
||||||
diff: float = time.time() - last_send
|
diff = time.time() - last_send
|
||||||
|
|
||||||
if not first_quote:
|
if not first_quote:
|
||||||
first_quote: float = last_quote
|
first_quote = last_quote
|
||||||
# first_quote['tbt'] = ticks_by_type
|
# first_quote['tbt'] = ticks_by_type
|
||||||
|
|
||||||
if (throttle_period - diff) > 0:
|
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
|
# TODO: now if only we could sync this to the display
|
||||||
# rate timing exactly lul
|
# rate timing exactly lul
|
||||||
try:
|
try:
|
||||||
await stream.send({
|
await stream.send({sym: first_quote})
|
||||||
sym: first_quote
|
|
||||||
})
|
|
||||||
except tractor.RemoteActorError as rme:
|
except tractor.RemoteActorError as rme:
|
||||||
if rme.type is not tractor._exceptions.StreamOverrun:
|
if rme.type is not tractor._exceptions.StreamOverrun:
|
||||||
raise
|
raise
|
||||||
|
|
||||||
ctx = stream._ctx
|
ctx = stream._ctx
|
||||||
chan = ctx.chan
|
chan = ctx.chan
|
||||||
log.warning(
|
log.warning(
|
||||||
|
|
@ -932,28 +885,20 @@ async def uniform_rate_send(
|
||||||
f'{sym}:{ctx.cid}@{chan.uid}'
|
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 (
|
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,
|
ConnectionResetError,
|
||||||
) + Sampler.bcast_errors as ipc_err:
|
trio.EndOfChannel,
|
||||||
match ipc_err:
|
):
|
||||||
case trio.EndOfChannel():
|
# if the feed consumer goes down then drop
|
||||||
log.info(
|
# out of this rate limiter
|
||||||
f'{stream} terminated by peer,\n'
|
log.warning(f'{stream} closed')
|
||||||
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}'
|
|
||||||
)
|
|
||||||
|
|
||||||
await stream.aclose()
|
await stream.aclose()
|
||||||
return
|
return
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -31,7 +31,6 @@ from pathlib import Path
|
||||||
from pprint import pformat
|
from pprint import pformat
|
||||||
from typing import (
|
from typing import (
|
||||||
Any,
|
Any,
|
||||||
Callable,
|
|
||||||
Sequence,
|
Sequence,
|
||||||
Hashable,
|
Hashable,
|
||||||
TYPE_CHECKING,
|
TYPE_CHECKING,
|
||||||
|
|
@ -57,7 +56,7 @@ from piker.brokers import (
|
||||||
)
|
)
|
||||||
|
|
||||||
if TYPE_CHECKING:
|
if TYPE_CHECKING:
|
||||||
from piker.accounting import (
|
from ..accounting import (
|
||||||
Asset,
|
Asset,
|
||||||
MktPair,
|
MktPair,
|
||||||
)
|
)
|
||||||
|
|
@ -91,18 +90,6 @@ class SymbologyCache(Struct):
|
||||||
# provided by the backend pkg.
|
# provided by the backend pkg.
|
||||||
mktmaps: dict[str, MktPair] = field(default_factory=dict)
|
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:
|
def write_config(self) -> None:
|
||||||
|
|
||||||
# put the backend's pair-struct type ref at the top
|
# put the backend's pair-struct type ref at the top
|
||||||
|
|
@ -162,68 +149,57 @@ class SymbologyCache(Struct):
|
||||||
'Implement `Client.get_assets()`!'
|
'Implement `Client.get_assets()`!'
|
||||||
)
|
)
|
||||||
|
|
||||||
get_mkt_pairs: Callable|None = getattr(
|
if get_mkt_pairs := getattr(client, 'get_mkt_pairs', None):
|
||||||
client,
|
|
||||||
'get_mkt_pairs',
|
pairs: dict[str, Struct] = await get_mkt_pairs()
|
||||||
None,
|
for bs_fqme, pair in pairs.items():
|
||||||
)
|
|
||||||
if not get_mkt_pairs:
|
# 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(
|
log.warning(
|
||||||
'No symbology cache `Pair` support for `{provider}`..\n'
|
'No symbology cache `Pair` support for `{provider}`..\n'
|
||||||
'Implement `Client.get_mkt_pairs()`!'
|
'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
|
return self
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -357,9 +357,7 @@ async def allocate_persistent_feed(
|
||||||
|
|
||||||
# yield back control to starting nursery once we receive either
|
# yield back control to starting nursery once we receive either
|
||||||
# some history or a real-time quote.
|
# some history or a real-time quote.
|
||||||
log.info(
|
log.info(f'loading OHLCV history: {fqme}')
|
||||||
f'loading OHLCV history: {fqme!r}\n'
|
|
||||||
)
|
|
||||||
await some_data_ready.wait()
|
await some_data_ready.wait()
|
||||||
|
|
||||||
flume = Flume(
|
flume = Flume(
|
||||||
|
|
@ -796,6 +794,7 @@ async def install_brokerd_search(
|
||||||
|
|
||||||
@acm
|
@acm
|
||||||
async def maybe_open_feed(
|
async def maybe_open_feed(
|
||||||
|
|
||||||
fqmes: list[str],
|
fqmes: list[str],
|
||||||
loglevel: str | None = None,
|
loglevel: str | None = None,
|
||||||
|
|
||||||
|
|
@ -849,12 +848,13 @@ async def maybe_open_feed(
|
||||||
|
|
||||||
@acm
|
@acm
|
||||||
async def open_feed(
|
async def open_feed(
|
||||||
|
|
||||||
fqmes: list[str],
|
fqmes: list[str],
|
||||||
|
|
||||||
loglevel: str|None = None,
|
loglevel: str | None = None,
|
||||||
allow_overruns: bool = True,
|
allow_overruns: bool = True,
|
||||||
start_stream: bool = True,
|
start_stream: bool = True,
|
||||||
tick_throttle: float|None = None, # Hz
|
tick_throttle: float | None = None, # Hz
|
||||||
|
|
||||||
allow_remote_ctl_ui: bool = False,
|
allow_remote_ctl_ui: bool = False,
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -36,10 +36,10 @@ from ._sharedmem import (
|
||||||
ShmArray,
|
ShmArray,
|
||||||
_Token,
|
_Token,
|
||||||
)
|
)
|
||||||
from piker.accounting import MktPair
|
|
||||||
|
|
||||||
if TYPE_CHECKING:
|
if TYPE_CHECKING:
|
||||||
from piker.data.feed import Feed
|
from ..accounting import MktPair
|
||||||
|
from .feed import Feed
|
||||||
|
|
||||||
|
|
||||||
class Flume(Struct):
|
class Flume(Struct):
|
||||||
|
|
@ -82,7 +82,7 @@ class Flume(Struct):
|
||||||
|
|
||||||
# TODO: do we need this really if we can pull the `Portal` from
|
# TODO: do we need this really if we can pull the `Portal` from
|
||||||
# ``tractor``'s internals?
|
# ``tractor``'s internals?
|
||||||
feed: Feed|None = None
|
feed: Feed | None = None
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def rt_shm(self) -> ShmArray:
|
def rt_shm(self) -> ShmArray:
|
||||||
|
|
|
||||||
|
|
@ -113,9 +113,9 @@ def validate_backend(
|
||||||
)
|
)
|
||||||
if ep is None:
|
if ep is None:
|
||||||
log.warning(
|
log.warning(
|
||||||
f'Provider backend {mod.name!r} is missing '
|
f'Provider backend {mod.name} is missing '
|
||||||
f'{daemon_name!r} support?\n'
|
f'{daemon_name} support :(\n'
|
||||||
f'|_module endpoint-func missing: {name!r}\n'
|
f'The following endpoint is missing: {name}'
|
||||||
)
|
)
|
||||||
|
|
||||||
inits: list[
|
inits: list[
|
||||||
|
|
|
||||||
|
|
@ -18,8 +18,8 @@
|
||||||
Log like a forester!
|
Log like a forester!
|
||||||
"""
|
"""
|
||||||
import logging
|
import logging
|
||||||
import json
|
|
||||||
import reprlib
|
import reprlib
|
||||||
|
import json
|
||||||
from typing import (
|
from typing import (
|
||||||
Callable,
|
Callable,
|
||||||
)
|
)
|
||||||
|
|
@ -90,8 +90,6 @@ def colorize_json(
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
# TODO, eventually defer to the version in `modden` once
|
|
||||||
# it becomes a dep!
|
|
||||||
def mk_repr(
|
def mk_repr(
|
||||||
**repr_kws,
|
**repr_kws,
|
||||||
) -> Callable[[str], str]:
|
) -> Callable[[str], str]:
|
||||||
|
|
|
||||||
|
|
@ -138,6 +138,16 @@ class StorageClient(
|
||||||
) -> None:
|
) -> None:
|
||||||
...
|
...
|
||||||
|
|
||||||
|
async def write_oi(
|
||||||
|
self,
|
||||||
|
fqme: str,
|
||||||
|
oi: np.ndarray,
|
||||||
|
append_and_duplicate: bool = True,
|
||||||
|
limit: int = int(800e3),
|
||||||
|
|
||||||
|
) -> None:
|
||||||
|
...
|
||||||
|
|
||||||
|
|
||||||
class TimeseriesNotFound(Exception):
|
class TimeseriesNotFound(Exception):
|
||||||
'''
|
'''
|
||||||
|
|
|
||||||
|
|
@ -111,6 +111,24 @@ def mk_ohlcv_shm_keyed_filepath(
|
||||||
return path
|
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:
|
def unpack_fqme_from_parquet_filepath(path: Path) -> str:
|
||||||
|
|
||||||
filename: str = str(path.name)
|
filename: str = str(path.name)
|
||||||
|
|
@ -172,7 +190,11 @@ class NativeStorageClient:
|
||||||
|
|
||||||
key: str = path.name.rstrip('.parquet')
|
key: str = path.name.rstrip('.parquet')
|
||||||
fqme, _, descr = key.rpartition('.')
|
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'))
|
period: int = int(suffix.strip('s'))
|
||||||
|
|
||||||
# cache description data
|
# cache description data
|
||||||
|
|
@ -369,6 +391,61 @@ class NativeStorageClient:
|
||||||
timeframe,
|
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(
|
async def delete_ts(
|
||||||
self,
|
self,
|
||||||
key: str,
|
key: str,
|
||||||
|
|
|
||||||
|
|
@ -15,8 +15,8 @@
|
||||||
# along with this program. If not, see <https://www.gnu.org/licenses/>.
|
# along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
'''
|
'''
|
||||||
Remote control tasks for sending annotations (and maybe more cmds) to
|
Remote control tasks for sending annotations (and maybe more cmds)
|
||||||
a chart from some other actor.
|
to a chart from some other actor.
|
||||||
|
|
||||||
'''
|
'''
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
@ -32,7 +32,6 @@ from typing import (
|
||||||
)
|
)
|
||||||
|
|
||||||
import tractor
|
import tractor
|
||||||
import trio
|
|
||||||
from tractor import trionics
|
from tractor import trionics
|
||||||
from tractor import (
|
from tractor import (
|
||||||
Portal,
|
Portal,
|
||||||
|
|
@ -317,9 +316,7 @@ class AnnotCtl(Struct):
|
||||||
)
|
)
|
||||||
yield aid
|
yield aid
|
||||||
finally:
|
finally:
|
||||||
# async ipc send op
|
await self.remove(aid)
|
||||||
with trio.CancelScope(shield=True):
|
|
||||||
await self.remove(aid)
|
|
||||||
|
|
||||||
async def redraw(
|
async def redraw(
|
||||||
self,
|
self,
|
||||||
|
|
|
||||||
|
|
@ -555,13 +555,14 @@ class OrderMode:
|
||||||
|
|
||||||
def on_fill(
|
def on_fill(
|
||||||
self,
|
self,
|
||||||
|
|
||||||
uuid: str,
|
uuid: str,
|
||||||
price: float,
|
price: float,
|
||||||
time_s: float,
|
time_s: float,
|
||||||
|
|
||||||
pointing: str | None = None,
|
pointing: str | None = None,
|
||||||
|
|
||||||
) -> bool:
|
) -> None:
|
||||||
'''
|
'''
|
||||||
Fill msg handler.
|
Fill msg handler.
|
||||||
|
|
||||||
|
|
@ -574,83 +575,60 @@ class OrderMode:
|
||||||
- update fill bar size
|
- update fill bar size
|
||||||
|
|
||||||
'''
|
'''
|
||||||
# XXX WARNING XXX
|
dialog = self.dialogs[uuid]
|
||||||
# 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
|
|
||||||
|
|
||||||
lines = dialog.lines
|
lines = dialog.lines
|
||||||
chart = self.chart
|
chart = self.chart
|
||||||
|
|
||||||
if not lines:
|
# XXX: seems to fail on certain types of races?
|
||||||
log.warn("No line(s) for order {uuid}!?")
|
|
||||||
return False
|
|
||||||
|
|
||||||
# update line state(s)
|
|
||||||
#
|
|
||||||
# ?XXX this fails on certain types of races?
|
|
||||||
# assert len(lines) == 2
|
# assert len(lines) == 2
|
||||||
flume: Flume = self.feed.flumes[chart.linked.mkt.fqme]
|
if lines:
|
||||||
_, _, ratio = flume.get_ds_info()
|
flume: Flume = self.feed.flumes[chart.linked.mkt.fqme]
|
||||||
|
_, _, ratio = flume.get_ds_info()
|
||||||
|
|
||||||
for chart, shm in [
|
for chart, shm in [
|
||||||
(self.chart, flume.rt_shm),
|
(self.chart, flume.rt_shm),
|
||||||
(self.hist_chart, flume.hist_shm),
|
(self.hist_chart, flume.hist_shm),
|
||||||
]:
|
]:
|
||||||
viz = chart.get_viz(chart.name)
|
viz = chart.get_viz(chart.name)
|
||||||
index_field = viz.index_field
|
index_field = viz.index_field
|
||||||
arr = shm.array
|
arr = shm.array
|
||||||
|
|
||||||
# TODO: borked for int index based..
|
# TODO: borked for int index based..
|
||||||
index = flume.get_index(time_s, arr)
|
index = flume.get_index(time_s, arr)
|
||||||
|
|
||||||
# get absolute index for arrow placement
|
# get absolute index for arrow placement
|
||||||
arrow_index = arr[index_field][index]
|
arrow_index = arr[index_field][index]
|
||||||
|
|
||||||
self.arrows.add(
|
self.arrows.add(
|
||||||
chart.plotItem,
|
chart.plotItem,
|
||||||
uuid,
|
uuid,
|
||||||
arrow_index,
|
arrow_index,
|
||||||
price,
|
price,
|
||||||
pointing=pointing,
|
pointing=pointing,
|
||||||
color=lines[0].color
|
color=lines[0].color
|
||||||
)
|
)
|
||||||
|
else:
|
||||||
|
log.warn("No line(s) for order {uuid}!?")
|
||||||
|
|
||||||
def on_cancel(
|
def on_cancel(
|
||||||
self,
|
self,
|
||||||
uuid: str,
|
uuid: str
|
||||||
|
|
||||||
) -> bool:
|
) -> None:
|
||||||
|
|
||||||
msg: Order|None = self.client._sent_orders.pop(uuid, None)
|
msg: Order = self.client._sent_orders.pop(uuid, None)
|
||||||
if msg is 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(
|
log.warning(
|
||||||
f'Received cancel for unsubmitted order {pformat(msg)}'
|
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]:
|
def cancel_orders_under_cursor(self) -> list[str]:
|
||||||
return self.cancel_orders(
|
return self.cancel_orders(
|
||||||
|
|
@ -1079,23 +1057,13 @@ async def process_trade_msg(
|
||||||
if name in (
|
if name in (
|
||||||
'position',
|
'position',
|
||||||
):
|
):
|
||||||
mkt: MktPair = mode.chart.linked.mkt
|
sym: MktPair = mode.chart.linked.mkt
|
||||||
pp_msg_symbol = msg['symbol'].lower()
|
pp_msg_symbol = msg['symbol'].lower()
|
||||||
pp_msg_bsmktid = msg['bs_mktid']
|
fqme = sym.fqme
|
||||||
fqme = mkt.fqme
|
broker = sym.broker
|
||||||
broker = mkt.broker
|
|
||||||
if (
|
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
|
pp_msg_symbol == fqme
|
||||||
or
|
or pp_msg_symbol == fqme.removesuffix(f'.{broker}')
|
||||||
pp_msg_symbol == fqme.removesuffix(f'.{broker}')
|
|
||||||
):
|
):
|
||||||
log.info(
|
log.info(
|
||||||
f'Loading position for `{fqme}`:\n'
|
f'Loading position for `{fqme}`:\n'
|
||||||
|
|
@ -1118,7 +1086,7 @@ async def process_trade_msg(
|
||||||
return
|
return
|
||||||
|
|
||||||
msg = Status(**msg)
|
msg = Status(**msg)
|
||||||
# resp: str = msg.resp
|
resp = msg.resp
|
||||||
oid = msg.oid
|
oid = msg.oid
|
||||||
dialog: Dialog = mode.dialogs.get(oid)
|
dialog: Dialog = mode.dialogs.get(oid)
|
||||||
|
|
||||||
|
|
@ -1182,33 +1150,20 @@ async def process_trade_msg(
|
||||||
mode.on_submit(oid)
|
mode.on_submit(oid)
|
||||||
|
|
||||||
case Status(resp='error'):
|
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:
|
# do all the things for a cancel:
|
||||||
# - drop order-msg dialog from client table
|
# - drop order-msg dialog from client table
|
||||||
# - delete level line from view
|
# - delete level line from view
|
||||||
mode.on_cancel(oid)
|
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'):
|
case Status(resp='canceled'):
|
||||||
# delete level line from view
|
# delete level line from view
|
||||||
mode.on_cancel(oid)
|
mode.on_cancel(oid)
|
||||||
|
|
@ -1223,10 +1178,10 @@ async def process_trade_msg(
|
||||||
# TODO: UX for a "pending" clear/live order
|
# TODO: UX for a "pending" clear/live order
|
||||||
log.info(f'Dark order triggered for {fmtmsg}')
|
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(
|
case Status(
|
||||||
resp='triggered',
|
resp='triggered',
|
||||||
|
# TODO: do the struct-msg version, blah blah..
|
||||||
|
# req=Order(exec_mode='live', action='alert') as req,
|
||||||
req={
|
req={
|
||||||
'exec_mode': 'live',
|
'exec_mode': 'live',
|
||||||
'action': 'alert',
|
'action': 'alert',
|
||||||
|
|
|
||||||
150
pyproject.toml
150
pyproject.toml
|
|
@ -63,138 +63,90 @@ dependencies = [
|
||||||
"trio-util >=0.7.0, <0.8.0",
|
"trio-util >=0.7.0, <0.8.0",
|
||||||
"trio-websocket >=0.10.3, <0.11.0",
|
"trio-websocket >=0.10.3, <0.11.0",
|
||||||
"typer >=0.9.0, <1.0.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",
|
"trio >=0.27",
|
||||||
"pendulum",
|
"pendulum >=3.0.0, <4.0.0",
|
||||||
"httpx >=0.27.0, <0.28.0",
|
"httpx >=0.27.0, <0.28.0",
|
||||||
"cryptofeed >=2.4.0, <3.0.0",
|
"cryptofeed >=2.4.0, <3.0.0",
|
||||||
"pyarrow>=18.0.0",
|
"pyarrow>=18.0.0",
|
||||||
"websockets ==12.0",
|
"websockets ==12.0",
|
||||||
"msgspec>=0.19.0,<0.20",
|
"msgspec>=0.19.0,<0.20",
|
||||||
"tractor",
|
"tractor",
|
||||||
|
"asyncvnc",
|
||||||
"tomlkit",
|
"tomlkit",
|
||||||
"trio-typing>=0.10.0",
|
"trio-typing>=0.10.0",
|
||||||
"numba>=0.61.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]
|
[project.optional-dependencies]
|
||||||
# uis = []
|
uis = [
|
||||||
# ?TODO? really we should be able to mv this `uis` group
|
# https://docs.astral.sh/uv/concepts/projects/dependencies/#optional-dependencies
|
||||||
# to be under [optional-dependencies] and then include
|
# TODO: make sure the levenshtein shit compiles on nix..
|
||||||
# it in the dev deps?
|
# rapidfuzz = {extras = ["speedup"], version = "^0.18.0"}
|
||||||
# https://docs.astral.sh/uv/concepts/projects/dependencies/#optional-dependencies
|
"rapidfuzz >=3.2.0, <4.0.0",
|
||||||
# -> uis should be included in pubbed pkgs.
|
"qdarkstyle >=3.0.2, <4.0.0",
|
||||||
# [ ] uv seems to have no way to do this though?
|
"pyqt6 >=6.7.0, <7.0.0",
|
||||||
|
"pyqtgraph",
|
||||||
|
|
||||||
# TODO? move to a `uv.toml`?
|
# for consideration,
|
||||||
[tool.uv]
|
# - 'visidata'
|
||||||
# https://docs.astral.sh/uv/reference/settings/#python-preference
|
|
||||||
python-preference = 'system'
|
# TODO: add an `--only daemon` group for running non-ui / pikerd
|
||||||
# https://docs.astral.sh/uv/reference/settings/#python-downloads
|
# service tree in distributed mode B)
|
||||||
python-downloads = 'manual'
|
# https://docs.astral.sh/uv/concepts/projects/dependencies/#optional-dependencies
|
||||||
# https://docs.astral.sh/uv/concepts/projects/dependencies/#default-groups
|
|
||||||
default-groups = [
|
|
||||||
'uis',
|
|
||||||
]
|
]
|
||||||
# ------ tool.uv ------
|
|
||||||
|
|
||||||
[dependency-groups]
|
[dependency-groups]
|
||||||
uis = [
|
# TODO: a toolset that makes debugging a `pikerd` service (tree) easy
|
||||||
"pyqtgraph",
|
# to hack on directly using more or less the local env:
|
||||||
"qdarkstyle >=3.0.2, <4.0.0",
|
# - xonsh + xxh
|
||||||
"pyqt6 >=6.7.0, <7.0.0",
|
# - rsyscall + pdbp
|
||||||
|
# - actor runtime control console like BEAM/OTP
|
||||||
# fuzzy search
|
#
|
||||||
"rapidfuzz >=3.2.0, <4.0.0",
|
# console ehancements and eventually remote debugging extras/helpers.
|
||||||
]
|
# use `uv --dev` to enable
|
||||||
|
|
||||||
# dev deps enabled by `uv --dev`
|
|
||||||
# https://docs.astral.sh/uv/concepts/projects/dependencies/#development-dependencies
|
|
||||||
dev = [
|
dev = [
|
||||||
# https://docs.astral.sh/uv/concepts/projects/dependencies/#development-dependencies
|
"pytest >=6.0.0, <7.0.0",
|
||||||
"cython >=3.0.0, <4.0.0",
|
"elasticsearch >=8.9.0, <9.0.0",
|
||||||
|
'xonsh',
|
||||||
# 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",
|
|
||||||
"prompt-toolkit ==3.0.40",
|
"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",
|
"pyperclip>=1.9.0",
|
||||||
|
"i3ipc>=2.2.1",
|
||||||
# ?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
|
|
||||||
]
|
]
|
||||||
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]
|
[project.scripts]
|
||||||
piker = "piker.cli:cli"
|
piker = "piker.cli:cli"
|
||||||
pikerd = "piker.cli:pikerd"
|
pikerd = "piker.cli:pikerd"
|
||||||
ledger = "piker.accounting.cli:ledger"
|
ledger = "piker.accounting.cli:ledger"
|
||||||
# ------ project.scripts ------
|
|
||||||
|
|
||||||
[tool.hatch.build.targets.sdist]
|
[tool.hatch.build.targets.sdist]
|
||||||
include = ["piker"]
|
include = ["piker"]
|
||||||
|
|
||||||
[tool.hatch.build.targets.wheel]
|
[tool.hatch.build.targets.wheel]
|
||||||
include = ["piker"]
|
include = ["piker"]
|
||||||
# ------ tool.hatch ------
|
|
||||||
|
|
||||||
|
# TODO? move to a `uv.toml`?
|
||||||
|
[tool.uv]
|
||||||
|
python-preference = 'system'
|
||||||
|
python-downloads = 'manual'
|
||||||
|
|
||||||
|
|
||||||
[tool.uv.sources]
|
[tool.uv.sources]
|
||||||
pyqtgraph = { git = "https://github.com/pikers/pyqtgraph.git" }
|
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" }
|
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
|
# 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://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 = "piker_pin" }
|
||||||
# tractor = { git = "https://pikers.dev/goodboy/tractor", branch = "main" }
|
|
||||||
# ------ goodboy ------
|
# goodboy's dev-env
|
||||||
# hackin dev-envs, usually there's something new he's hackin in..
|
# XXX for @goodboy's hackin dev env, usually there's something new in
|
||||||
# tractor = { path = "../tractor", editable = true }
|
# 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
|
from piker.log import get_console_log
|
||||||
|
|
||||||
|
|
||||||
# include `tractor`'s built-in fixtures!
|
|
||||||
pytest_plugins: tuple[str] = (
|
|
||||||
"tractor._testing.pytest",
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
def pytest_addoption(parser):
|
def pytest_addoption(parser):
|
||||||
parser.addoption("--ll", action="store", dest='loglevel',
|
parser.addoption("--ll", action="store", dest='loglevel',
|
||||||
default=None, help="logging level to set when testing")
|
default=None, help="logging level to set when testing")
|
||||||
|
|
|
||||||
|
|
@ -12,14 +12,12 @@ from piker import config
|
||||||
from piker.accounting import (
|
from piker.accounting import (
|
||||||
Account,
|
Account,
|
||||||
calc,
|
calc,
|
||||||
open_account,
|
|
||||||
load_account,
|
|
||||||
load_account_from_ledger,
|
|
||||||
open_trade_ledger,
|
|
||||||
Position,
|
Position,
|
||||||
TransactionLedger,
|
TransactionLedger,
|
||||||
|
open_trade_ledger,
|
||||||
|
load_account,
|
||||||
|
load_account_from_ledger,
|
||||||
)
|
)
|
||||||
import tractor
|
|
||||||
|
|
||||||
|
|
||||||
def test_root_conf_networking_section(
|
def test_root_conf_networking_section(
|
||||||
|
|
@ -55,17 +53,12 @@ def test_account_file_default_empty(
|
||||||
)
|
)
|
||||||
def test_paper_ledger_position_calcs(
|
def test_paper_ledger_position_calcs(
|
||||||
fq_acnt: tuple[str, str],
|
fq_acnt: tuple[str, str],
|
||||||
debug_mode: bool,
|
|
||||||
):
|
):
|
||||||
broker: str
|
broker: str
|
||||||
acnt_name: str
|
acnt_name: str
|
||||||
broker, acnt_name = fq_acnt
|
broker, acnt_name = fq_acnt
|
||||||
|
|
||||||
accounts_path: Path = (
|
accounts_path: Path = config.repodir() / 'tests' / '_inputs'
|
||||||
config.repodir()
|
|
||||||
/ 'tests'
|
|
||||||
/ '_inputs' # tests-local-subdir
|
|
||||||
)
|
|
||||||
|
|
||||||
ldr: TransactionLedger
|
ldr: TransactionLedger
|
||||||
with (
|
with (
|
||||||
|
|
@ -84,7 +77,6 @@ def test_paper_ledger_position_calcs(
|
||||||
ledger=ldr,
|
ledger=ldr,
|
||||||
|
|
||||||
_fp=accounts_path,
|
_fp=accounts_path,
|
||||||
debug_mode=debug_mode,
|
|
||||||
|
|
||||||
) as (dfs, ledger),
|
) as (dfs, ledger),
|
||||||
|
|
||||||
|
|
@ -110,87 +102,3 @@ def test_paper_ledger_position_calcs(
|
||||||
df = dfs[xrp]
|
df = dfs[xrp]
|
||||||
assert df['cumsize'][-1] == 0
|
assert df['cumsize'][-1] == 0
|
||||||
assert pos.cumsize == 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,
|
unpack_fqme,
|
||||||
)
|
)
|
||||||
from piker.accounting import (
|
from piker.accounting import (
|
||||||
open_account,
|
open_pps,
|
||||||
Position,
|
Position,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
@ -136,7 +136,7 @@ def load_and_check_pos(
|
||||||
|
|
||||||
) -> None:
|
) -> None:
|
||||||
|
|
||||||
with open_account(ppmsg.broker, ppmsg.account) as table:
|
with open_pps(ppmsg.broker, ppmsg.account) as table:
|
||||||
|
|
||||||
if ppmsg.size == 0:
|
if ppmsg.size == 0:
|
||||||
assert ppmsg.symbol not in table.pps
|
assert ppmsg.symbol not in table.pps
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue