Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed float dust bug on zero position #470

Merged
merged 10 commits into from
Mar 3, 2023
Merged

Conversation

guilledk
Copy link
Contributor

@guilledk guilledk commented Feb 27, 2023

In #462 we discovered a bug on the positioning system when trying to get to a zero position state, after investigation we found out it was caused by float precision, the solution is to truncate based on the maximun supported on a symbol by symbol basis.

@guilledk guilledk added the bug guille broke it prolly label Feb 27, 2023
@guilledk guilledk self-assigned this Feb 27, 2023
@goodboy goodboy requested review from jaredgoldman and goodboy and removed request for jaredgoldman February 28, 2023 16:47
@goodboy goodboy mentioned this pull request Feb 28, 2023
3 tasks
@@ -530,7 +535,7 @@ async def trades_dialogue(

):

with open_pps(broker, 'paper', False) as table:
with open_pps(broker, 'paper') as table:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not quite following why you're not writing immediately here any more?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm pretty sure that open_pps call is just meant to load the pps.toml file into the _positions module var and then iterate on it to send position messages to the ems.

@algorandpa algorandpa force-pushed the paper_trade_improvements_rebase branch from 9e2ac6d to 1323981 Compare February 28, 2023 18:53
piker/pp.py Outdated Show resolved Hide resolved
piker/pp.py Outdated Show resolved Hide resolved
piker/pp.py Outdated Show resolved Hide resolved
piker/data/_source.py Outdated Show resolved Hide resolved
Copy link
Contributor

@goodboy goodboy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks excellent yet again @guilledk.

I have a few comments but nothing blocking afaict.

Great work and feel free to merge into the target dev branch whenever ready.
I presume you might need to rebase after the suggested changes made in the last review on #462 land.

Base automatically changed from paper_trade_improvements_rebase to master February 28, 2023 19:30
@guilledk guilledk force-pushed the decimalization_take_2 branch from abc2d45 to 3b4f123 Compare March 1, 2023 18:23
@guilledk guilledk requested a review from jaredgoldman March 1, 2023 18:31
Copy link
Contributor

@jaredgoldman jaredgoldman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM other than the first comment

@goodboy
Copy link
Contributor

goodboy commented Mar 1, 2023

I'm just gonna manually test with the same scenario I got in #462, presuming it's resolved I'll merge 🏄🏼

@guilledk guilledk force-pushed the decimalization_take_2 branch from 327aa5c to 501fe9a Compare March 1, 2023 21:33
@goodboy
Copy link
Contributor

goodboy commented Mar 1, 2023

@guilledk another thing I think should change is to just put the .symbol section fields under the position entry no?

screenshot-2023-03-01_18-13-13

when are we ever going to need to have a separate section?

  • a given asset_type = "crypto" price_tick_size = 0.01 lot_tick_size = 1e-5 set is always going to be specific to the particular market/symbol/fqsn for a given broker right?
  • i don't see any case where this extra sibling section is going to be shared/re-used right?

ad discussed in chat i think we can just stick all the symbol info fields directly in the pp entry since they're one to one:

[binance.paper."btcusdt.binance"]
size = 0.04235
ppu = 23611.15
bsuid = "btcusdt"
asset_type = "crypto"
price_tick_size = 0.01
lot_tick_size = 1e-5
clears = [
 { dt = "2023-03-01T23:37:51.559601+00:00", ppu = 23611.15, accum_size = 0.04235, price = 23611.15, size = 0.04235, cost = 0, tid = "0c162a01-9e63-4121-9475-182cf80753e8" },
]

piker/pp.py Outdated
@@ -957,7 +981,8 @@ def open_pps(
expiry = pendulum.parse(expiry)

pp = pp_objs[bsuid] = Position(
Symbol.from_fqsn(fqsn, info={}),
Symbol.from_fqsn(
fqsn, entry['symbol']),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As mentioned in chat, we need to do a entry.get('symbol') here and instead just fill in a default Symbol here to continue supporting old pps.toml files.

Further for entries detected as crypto we should probably put a big warning that the entry needs to be recalculated to avoid the precision error this patch was originally meant to fix!

Copy link
Contributor

@goodboy goodboy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added in the bit about backward compat for existing pps.toml files (since this definitely breaks for me 😂

I'm more then happy to make a patch to this PR before landing 🏄🏼

guilledk added 5 commits March 1, 2023 21:05
Add decimal quantize API to Symbol to simplify by-broker truncation
Add symbol info to `pps.toml`
Move _assert call to outside the _async_main context manager
Minor indentation and styling changes, also convert a few prints to log calls
Fix multi write / race condition on open_pps call
Switch open_pps to not write by default
Fix integer math kraken syminfo _tick_size initialization
…ps.toml causing it to load pp wrong on second open, also fix header leak bug
@guilledk guilledk force-pushed the decimalization_take_2 branch from 501fe9a to 6be96a9 Compare March 2, 2023 00:07
goodboy added 3 commits March 2, 2023 19:22
In order to support existing `pps.toml` files in the wild which don't
have the `asset_type, price_tick_size, lot_tick_size` fields, we need to
only optionally read them and instead expect that backends will write
the fields going forward (coming in follow patches).

Further this makes some small asset-size (vlm accounting) quantization
related adjustments:
- rename `Symbol.decimal_quant()` -> `.quantize_size()` since that is
  explicitly what this method is doing.
- and expect an input `size: float` which we cast to decimal instead of
  doing it inside the `.calc_size()` caller code.
- drop `Symbol.iterfqsns()` which wasn't being used anywhere at all..

Additionally, this drafts out a new replacement market-trading-pair data
type to eventually replace `.data._source.Symbol` -> `MktPair` which we
aren't using yet, but serves as the documentation-driven motivator ;)
and, it relates to #467.
Also includes a retyping of `Client._pair: dict[str, Pair]` to look up
pair structs and map all alt-key-name-sets to each for easy precision
info lookup to set the `.sym` field for each transaction including for
on-chain transfers which kraken provides as an "asset decimals" field,
presumably pulled from the particular block-token's limitation info.
…info

Backward compat support for `Transaction.sym: Symbol`
@goodboy
Copy link
Contributor

goodboy commented Mar 3, 2023

With the merging of my sub-pr i think this is ready to land 🏄🏼

i saw one last msg that was interesting with a -0.0, not sure what that's about.

 piker.clearing._ems _ems.py:694 Received broker trade event:
{'account': 'paper',
 'avg_price': 22409.6,
 'broker': 'binance',
 'currency': 'btcusdt',
 'name': 'position',
 'size': -0.0,
 'symbol': 'btcusdt.binance'}

gonna do one more review pass.

@goodboy goodboy mentioned this pull request Mar 3, 2023
6 tasks
Copy link
Contributor

@goodboy goodboy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pretty sure this is finally ready!

I guess we just need to make sure the tests are all good in CI 🚀

@goodboy goodboy merged commit 201f86e into master Mar 3, 2023
@goodboy goodboy deleted the decimalization_take_2 branch March 3, 2023 22:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug guille broke it prolly
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants