Max Pain for Deribit Options #13

Open
ntorres wants to merge 2 commits from max_pain_deribit into deribit_fix
Collaborator

Max Pain for Deribit Options

TODO: pr description here.

How to test it?

Before start: in order to get this working with uv, you must use my tractor fork 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, my system needs 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 and showing you something like this:

image
  1. Go to deribit metric page and select the same currency and expiration date, for now just works with BTC, the page looks like this:
image
## Max Pain for Deribit Options TODO: pr description here. ### 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, my system needs 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 and showing you something like this: > ![image](/attachments/d6e38aa9-4bdf-4b0e-aec4-b50eb9128348) 5. Go to [deribit metric page](https://www.deribit.com/statistics/BTC/metrics/options) and select the same currency and expiration date, for now just works with `BTC`, the page looks like this: > ![image](/attachments/73a922ca-2121-4093-9fdf-a823344d1967)
ntorres added 14 commits 2024-12-03 17:37:39 +00:00
7b02df9b49 Update api.py
This function receive a date in this format DDMMMYY and returns timestamps int
4ccda643b8 Update api.py
covers cases when option_type comes as PUT or P, same with calls CALL or C
24bf19c1fb Update api.py
get_currencies function
9abbd8ca1e Update api.py
open_interest for each strike price and each expiry date
a9110b1196 Update api.py
Now we get the data for an e specific expiration date if and expiry_date is passed, or for all expiration if expiry_date is None
ntorres added 1 commit 2024-12-04 22:18:45 +00:00
ntorres added 3 commits 2024-12-06 02:32:22 +00:00
bf86772d4b oi max_pain major refactor
Now the max_pain is calculated taking into account all strike prices and all close prices to find the intrinsic value as deribit.
ntorres added 1 commit 2024-12-06 02:43:24 +00:00
cbc5168b8d renaming variable
fixed variable name for better understanding.
ntorres added 1 commit 2024-12-06 02:49:11 +00:00
ec1da6134b Update api.py
Now intrinsic values list is initialize in the aio_open_interest_feed_relay instead in the scope above.
ntorres added 1 commit 2024-12-06 10:42:02 +00:00
ntorres force-pushed max_pain_deribit from f3d7d87303 to e37fb00e7d 2024-12-06 10:57:33 +00:00 Compare
ntorres force-pushed max_pain_deribit from e37fb00e7d to ca5e1b6ed1 2024-12-06 11:09:58 +00:00 Compare
ntorres added 3 commits 2024-12-07 13:17:50 +00:00
3e613ab2a0 fix the input values
for acm related to oi
0563b916a3 major refactor
all the logic now in the max_pain script
ntorres added 1 commit 2024-12-07 13:38:00 +00:00
ntorres added 1 commit 2024-12-07 14:15:56 +00:00
3295303e37 More configs refactor
Now in the broker config file are the log configs for cryptofeed.
ntorres added 1 commit 2024-12-08 13:51:32 +00:00
ntorres added 2 commits 2024-12-08 16:38:35 +00:00
ntorres added 6 commits 2025-01-28 15:17:53 +00:00
0e830030e6 uv migration
Migrate to uv, use msgspec from git due to python 3.13 compatibility
e1d181a21b Restore missing data at pyproject.toml
Restore keywords and classifiers for the project
38e5165e13 . Update uv docs in pyproject.toml
. Format fixes
. Add uv docs
ntorres changed title from max_pain_deribit to max_pain_deribit 2025-01-28 15:23:20 +00:00
ntorres changed target branch from fix_deribit_hist_queries to uv_magration 2025-01-28 15:23:21 +00:00
ntorres force-pushed max_pain_deribit from 38e5165e13 to a878fa57bf 2025-01-28 15:43:36 +00:00 Compare
ntorres force-pushed max_pain_deribit from a878fa57bf to f4a53b157f 2025-01-28 15:45:24 +00:00 Compare
ntorres force-pushed max_pain_deribit from f4a53b157f to e75ea473fa 2025-01-28 16:22:51 +00:00 Compare
ntorres added 1 commit 2025-01-28 17:12:43 +00:00
ntorres added 1 commit 2025-01-28 17:22:50 +00:00
ntorres force-pushed max_pain_deribit from 24ac982083 to 7e2cad1d5e 2025-01-28 17:28:44 +00:00 Compare
ntorres force-pushed max_pain_deribit from 7e2cad1d5e to 2281d64e4f 2025-01-28 17:32:15 +00:00 Compare
ntorres force-pushed max_pain_deribit from 2281d64e4f to 773656a4fc 2025-01-28 17:36:02 +00:00 Compare
ntorres added 1 commit 2025-01-28 17:43:16 +00:00
ntorres force-pushed max_pain_deribit from 9918f6b282 to 211d67c3ef 2025-01-28 17:57:55 +00:00 Compare
ntorres force-pushed max_pain_deribit from 211d67c3ef to 01a9dc3f4f 2025-01-28 18:05:48 +00:00 Compare

@ntorres looking forward to going through this patch in deep detail ma friend !

B-)

But, I think before we get too broad on getting this new options related feature set integrated to the rest of piker’s subsystems, we need to get some documentation about where all of these 5 features are implemented in this new file (mostly so that I can better understand and use this new code and thus better understand where to get us moving next!)


organization suggestions

as mentioned in our call i think we could org things a little more according to past prototyping efforts of this sort,

  • move the new max_pain.py to the ./examples/ subdir
  • maybe consider making a sub-subdir inside ^ if needed for any derivatives related lib code you feel could be factored sooner then later (eg. by making an ./examples/derivs/__init__.py or the like)

documentation of this patch

also as discussed in call, i think it would make not only review, but future guidance on where to go with all this new code/impl if you document in more detail the 5. major new features you’ve introduced by explicitly linking to the parts of the new module which correspond to each section :)

  • (as also suggested earlier) try to change this PR’s description (using markdown) to link to various parts of your patch (by using the diff view and linking to particular lines of code) such that a reader/reviewer can more easily determine which major (of the 5) feats maps to which bits of the new code :)
    • check out these prior GH PRs where I did something similar even just to remind myself which parts of the requirements/feats-set mapped to which commits and/or parts of the patch:
      • eg of the basic-buy-bot PR example (very similar prototype for a trading bot that builds on piker in a similar way to this new script)
      • the parquet-for-tsdb-storage-API PR which you’ll notice has tons of “todo check boxes” that you can write for yourself using md with “- [ ]” syntax and from which you can link to the diff feature numbered-sections from above using md url-linking syntax

driving charting features from the new options source data schema(s) introduced here

Since we’re introing new derivatives related data (stream) for consumption for various other systems in piker we likely want to start prototyping some UI/UX for (real-time) display.

The main data sources that are likely of importance are (at least for deribit opts mkts): - (real-time updated) open interest (OI) - historical OI - ideally the historical L2 (depth of book) or at least L1 data (including bid/ask queue “volume” or “size” state) which indicates the market-maker’s (mm’s) pricing model output over time according to the underlying’s price movements

Since we don’t yet have a formal API inside piker.ui for using these pyqtgraph graph types yet, it likely makes more sense to prototype with pyqtgraph’s apis directly until you get something you like and then we can discuss porting/integrating it into the more performant graphing sys already inside piker which extends pyqtgraph’s approach.

Let me know if you have any questions about any of this and feel free to break apart the last charting section into a new issue in the case we decide to land this before moving onto UI stuff 👍

@ntorres looking forward to going through this patch in deep detail ma friend ! B-) **But**, I think before we get too broad on getting this new options related feature set integrated to the rest of `piker`'s subsystems, we need to get some documentation about where all of these 5 features are implemented in this new file (mostly so that I can better understand and **use** this new code and thus better understand where to get us moving next!) ---- ### organization suggestions as mentioned in our call i think we could org things a little more according to past prototyping efforts of this sort, - [ ] move the new `max_pain.py` to the `./examples/` subdir - [ ] maybe consider making a sub-subdir inside ^ if needed for any derivatives related lib code you feel could be factored sooner then later (eg. by making an `./examples/derivs/__init__.py` or the like) ---- ### documentation of this patch also as discussed in call, i think it would make not only review, but future guidance on where to go with all this new code/impl if you document in more detail the 5. major new features you've introduced by explicitly linking to the parts of the new module which correspond to each section :) - [ ] (as also suggested earlier) try to change this PR's description (using markdown) to link to various parts of your patch (by using the diff view and linking to particular lines of code) such that a reader/reviewer can more easily determine which major (of the 5) feats maps to which bits of the new code :) - [ ] check out these prior GH PRs where I did something similar even just to remind myself which parts of the requirements/feats-set mapped to which commits and/or parts of the patch: * eg of the [basic-buy-bot PR](https://github.com/pikers/piker/pull/526) example (very similar prototype for a trading bot that builds on `piker` in a similar way to this new script) * the [parquet-for-tsdb-storage-API PR](https://github.com/pikers/piker/pull/486) which you'll notice has tons of "todo check boxes" that you can write for yourself using md with "- [ ]" syntax and from which you can link to the diff feature *numbered-sections* from above using [md url-linking syntax](https://htmlmarkdown.com/syntax/markdown-links/) ---- ### driving charting features from the new options source data schema(s) introduced here Since we're introing new derivatives related data (stream) for consumption for various other systems in `piker` we likely want to start prototyping some UI/UX for (real-time) display. The main data sources that are likely of importance are (at least for `deribit` opts mkts): - (real-time updated) open interest (OI) - historical OI - ideally the historical L2 (depth of book) or at least L1 data (including bid/ask queue "volume" or "size" state) which indicates the market-maker's (mm's) pricing model output over time according to the underlying's price movements - [ ] OI: instead of trying to dive straight to plugging a bar chart into piker's existing sub-chart system, i'd suggest instead getting to a point where you are storing the real-time updated OI (and ideally L1 or L2 price and size) in a time series (likely using `polars`/`numpy`) and then plotting a bar chart over varying contract-filtering parameters (strike-price, expiry, call/put-type) manually using the APIs avail from `pyqtgraph`: * exs in repo: https://github.com/pyqtgraph/pyqtgraph/tree/master/pyqtgraph/examples - [ ] i'd recommend looking through these and trying to run the relevant ones manually to get a handle on how `pyqtgraph`works and to be able to quickly start prototyping a UI for showing (historical) OI * site: https://www.pyqtgraph.org/ * docs: https://pyqtgraph.readthedocs.io/en/latest/ * eg. for a "bar chart": https://github.com/pyqtgraph/pyqtgraph/blob/master/pyqtgraph/examples/BarGraphItem.py * simple histogram: https://github.com/pyqtgraph/pyqtgraph/blob/master/pyqtgraph/examples/histogram.py * a "sideways" histogram: https://github.com/pyqtgraph/pyqtgraph/blob/master/pyqtgraph/examples/HistogramLUT.py (though note this is for a 2d/image main graph) Since we don't yet have a formal API inside `piker.ui` for using these `pyqtgraph` graph types yet, it likely makes more sense to prototype with `pyqtgraph`'s apis directly until you get something you like and then we can discuss porting/integrating it into the more performant graphing sys already inside `piker` which extends `pyqtgraph`'s approach. Let me know if you have any questions about any of this and feel free to break apart the last charting section into a new issue in the case we decide to land this before moving onto UI stuff 👍

@ntorres yo also check out my previous summary of possible examples to tinker way back when @zoltan thought they might be proto-ing some charting stuff:

https://github.com/pikers/piker/issues/80#issuecomment-602102432

@ntorres yo also check out my previous summary of possible examples to tinker way back when @zoltan thought they might be proto-ing some charting stuff: https://github.com/pikers/piker/issues/80#issuecomment-602102432

Also just added a new task for starting at least some docs regarding pyqtgraph use within piker 😂

#18

Also just added a new task for starting at least some docs regarding `pyqtgraph` use within `piker` :joy: https://pikers.dev/pikers/piker/issues/18
ntorres force-pushed max_pain_deribit from 01a9dc3f4f to 3fc9c1486a 2025-01-28 19:52:25 +00:00 Compare
ntorres changed title from max_pain_deribit to max_pain_deribit 2025-01-28 19:53:06 +00:00
ntorres changed target branch from uv_magration to uv_migration 2025-01-28 19:53:06 +00:00
ntorres added 1 commit 2025-01-28 21:27:02 +00:00
ntorres force-pushed max_pain_deribit from 07a417a482 to ecc89ff8c5 2025-01-28 21:39:07 +00:00 Compare
ntorres force-pushed max_pain_deribit from ecc89ff8c5 to 6cb6f8d795 2025-01-28 21:41:47 +00:00 Compare
ntorres added 1 commit 2025-01-28 21:44:38 +00:00
ntorres force-pushed max_pain_deribit from ef1fb18918 to c7c9e37a57 2025-01-28 22:09:53 +00:00 Compare
ntorres force-pushed max_pain_deribit from c7c9e37a57 to b57a745507 2025-01-28 22:26:41 +00:00 Compare
ntorres force-pushed max_pain_deribit from b57a745507 to f3ee4a7193 2025-01-28 22:28:06 +00:00 Compare
ntorres force-pushed max_pain_deribit from f3ee4a7193 to 0d9d6e15ba 2025-01-28 23:57:01 +00:00 Compare
ntorres added 1 commit 2025-01-29 01:15:33 +00:00
ntorres force-pushed max_pain_deribit from 6b7af35cd0 to cbffb6f227 2025-01-29 04:30:50 +00:00 Compare
ntorres force-pushed max_pain_deribit from cbffb6f227 to f594edd69d 2025-01-29 04:50:25 +00:00 Compare
ntorres force-pushed max_pain_deribit from f594edd69d to be44888e5f 2025-01-29 18:52:37 +00:00 Compare
ntorres force-pushed max_pain_deribit from be44888e5f to ffbca17ba2 2025-01-29 22:41:34 +00:00 Compare
ntorres added 1 commit 2025-01-30 01:37:53 +00:00
ntorres added 1 commit 2025-01-30 01:38:24 +00:00
ntorres added 1 commit 2025-01-30 01:39:01 +00:00
ntorres added 1 commit 2025-01-30 01:39:22 +00:00
ntorres added 1 commit 2025-01-30 01:39:43 +00:00
ntorres added 1 commit 2025-01-30 01:40:23 +00:00
ntorres added 1 commit 2025-01-30 01:40:58 +00:00
ntorres added 1 commit 2025-01-30 01:41:43 +00:00
bbfc9b070f Update api.py
This function receive a date in this format DDMMMYY and returns timestamps int
ntorres added 1 commit 2025-01-30 01:42:00 +00:00
db39b84ef6 Update api.py
covers cases when option_type comes as PUT or P, same with calls CALL or C
ntorres added 1 commit 2025-01-30 01:42:21 +00:00
f3ab1d1cc1 Update api.py
get_currencies function
ntorres added 1 commit 2025-01-30 01:42:44 +00:00
4e9e72f0f1 Update api.py
open_interest for each strike price and each expiry date
ntorres added 1 commit 2025-01-30 01:44:42 +00:00
92a861a397 Update api.py
Now we get the data for an e specific expiration date if and expiry_date is passed, or for all expiration if expiry_date is None
ntorres added 1 commit 2025-01-30 01:44:59 +00:00
ntorres added 1 commit 2025-01-30 01:45:38 +00:00
ntorres added 1 commit 2025-01-30 01:46:51 +00:00
ntorres added 1 commit 2025-01-30 01:47:08 +00:00
ntorres added 1 commit 2025-01-30 02:05:23 +00:00
c8d8ab524e oi max_pain major refactor
Now the max_pain is calculated taking into account all strike prices and all close prices to find the intrinsic value as deribit.
ntorres added 1 commit 2025-01-30 02:06:00 +00:00
ntorres added 1 commit 2025-01-30 02:07:41 +00:00
5f0fda28c5 renaming variable
fixed variable name for better understanding.
ntorres added 1 commit 2025-01-30 02:08:00 +00:00
fda90c6737 Update api.py
Now intrinsic values list is initialize in the aio_open_interest_feed_relay instead in the scope above.
ntorres added 1 commit 2025-01-30 02:08:24 +00:00
ntorres added 1 commit 2025-01-30 02:14:10 +00:00
ntorres added 1 commit 2025-01-30 02:14:49 +00:00
16bcd7a2c3 fix the input values
for acm related to oi
ntorres force-pushed max_pain_deribit from 16bcd7a2c3 to 212682a231 2025-01-30 02:19:32 +00:00 Compare
ntorres force-pushed max_pain_deribit from 212682a231 to f9018670b9 2025-01-30 02:21:25 +00:00 Compare
ntorres force-pushed max_pain_deribit from f9018670b9 to 3b8cec45da 2025-01-30 02:25:44 +00:00 Compare
ntorres added 1 commit 2025-01-30 02:32:39 +00:00
423b514cec fix the input values
for acm related to oi
ntorres added 1 commit 2025-01-30 02:34:51 +00:00
31af1197ac major refactor
all the logic now in the max_pain script
ntorres added 1 commit 2025-01-30 02:35:46 +00:00
ntorres added 1 commit 2025-01-30 02:36:03 +00:00
b6113295e3 More configs refactor
Now in the broker config file are the log configs for cryptofeed.
ntorres added 1 commit 2025-01-30 02:36:20 +00:00
ntorres added 1 commit 2025-01-30 02:36:57 +00:00
ntorres added 1 commit 2025-01-30 02:37:08 +00:00
83f5b635ff minor refactor
cleaning the code
ntorres added 1 commit 2025-01-30 02:40:54 +00:00
ntorres added 1 commit 2025-01-30 02:42:35 +00:00
ntorres added 1 commit 2025-01-30 02:47:01 +00:00
ntorres force-pushed max_pain_deribit from b2d3d76814 to fead79fd1d 2025-01-30 02:51:12 +00:00 Compare
ntorres force-pushed max_pain_deribit from fead79fd1d to 5423e38a75 2025-01-30 02:52:50 +00:00 Compare
ntorres added 1 commit 2025-01-30 02:53:21 +00:00
ntorres force-pushed max_pain_deribit from 5c320bc7ab to 596755a9b2 2025-01-30 02:55:46 +00:00 Compare
ntorres added 1 commit 2025-01-30 02:56:33 +00:00
ntorres force-pushed max_pain_deribit from c4b3d7109f to 22831fc071 2025-01-30 05:41:59 +00:00 Compare
ntorres added 2 commits 2025-01-30 05:42:26 +00:00
34b74a0c8a Deribit api key changes introduce:
- `get_timestamp_int`: added this is the hack, so we can aboid use the custom deribit date format.

- `get_currencies`: added so we could get all deribit's available currencies.

- `get_instruments`: for a especific expiration date, it return a list of criptofeed.Symbol.

- `get_expiration_dates`: expirations dates available for btc's option contracts .

- `get_strikes_dict`: all the strike prices for an especific expiration date.

- `aio_open_interest_feed_relay` `open_oi_feed` `maybe_open_oi_feed`: this three handles all the portal stuff and the cryptofeed callbacks for the open interest and trades, for some reason it need both to work, i need to check that out at some point.

- Also a couple of format fixes.
1272c3efad Max pain daemon:
- To calculate the `max_pain` first we need an expiration date,
get_expiration_dates()` retrieves them and the user then enters one of
the shown, then using the select expiry_date on `get_instruments()` we
are good to build the `oi_by_strikes` important!

- Add `update_oi_by_strikes()`.

- Add `check_if_complete()`.

- `get_max_pain()`: here's where all the action takes place, the
`oi_by_strikes` must be complete to start the calculations,

- Use `maybe_open_oi_feed` for open a oi_feed.

- Add `max_pain_readme.rst`
ntorres changed title from max_pain_deribit to Max Pain for Deribit Options 2025-01-30 05:49:45 +00:00
ntorres force-pushed max_pain_deribit from 1272c3efad to 7a638157ce 2025-01-30 06:27:00 +00:00 Compare
ntorres force-pushed max_pain_deribit from 7a638157ce to e5b8256ba8 2025-01-30 06:29:08 +00:00 Compare
ntorres force-pushed max_pain_deribit from e5b8256ba8 to a58595fcd7 2025-01-30 06:43:17 +00:00 Compare
ntorres force-pushed max_pain_deribit from a58595fcd7 to 5da8a587e5 2025-01-30 18:32:16 +00:00 Compare
ntorres changed title from Max Pain for Deribit Options to Max Pain for Deribit Options 2025-01-30 18:39:16 +00:00
ntorres changed target branch from uv_migration to fix_deribit_hist_queries 2025-01-30 18:39:16 +00:00
ntorres force-pushed max_pain_deribit from 5da8a587e5 to 4c5132fd57 2025-01-30 18:41:19 +00:00 Compare
ntorres force-pushed max_pain_deribit from 4c5132fd57 to b8512edd1b 2025-02-03 22:35:40 +00:00 Compare
ntorres force-pushed max_pain_deribit from b8512edd1b to 2cb7b505e1 2025-02-04 01:34:44 +00:00 Compare
ntorres changed title from Max Pain for Deribit Options to Max Pain for Deribit Options 2025-02-11 19:13:21 +00:00
ntorres changed target branch from fix_deribit_hist_queries to fix_deribit_hist_queries_NEW 2025-02-11 19:13:22 +00:00
ntorres changed title from Max Pain for Deribit Options to Max Pain for Deribit Options 2025-02-17 19:48:03 +00:00
ntorres changed target branch from fix_deribit_hist_queries_NEW to deribit_fix 2025-02-17 19:48:04 +00:00
ntorres force-pushed max_pain_deribit from 2cb7b505e1 to 18e6b8beaa 2025-02-17 22:15:38 +00:00 Compare
This pull request can be merged automatically.
You are not authorized to merge this pull request.
You can also view command line instructions.

Step 1:

From your project repository, check out a new branch and test the changes.
git checkout -b max_pain_deribit deribit_fix
git pull origin max_pain_deribit

Step 2:

Merge the changes and update on Gitea.
git checkout deribit_fix
git merge --no-ff max_pain_deribit
git push origin deribit_fix
Sign in to join this conversation.
No reviewers
No Label
No Milestone
No project
No Assignees
2 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: pikers/piker#13
There is no content yet.