-
Notifications
You must be signed in to change notification settings - Fork 23
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
Simulate position between maker and coordinator #1216
Conversation
33c1fd5
to
a298aae
Compare
@@ -20,3 +20,6 @@ lightning-net-tokio = { git = "https://github.com/p2pderivatives/rust-lightning/ | |||
lightning-persister = { git = "https://github.com/p2pderivatives/rust-lightning/", rev = "3b69cecf" } | |||
|
|||
rust-bitcoin-coin-selection = { git = "https://github.com/p2pderivatives/rust-bitcoin-coin-selection" } | |||
|
|||
# Waiting for the next release | |||
xtra = { git = "https://github.com/Restioson/xtra/", rev = "d98393a" } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we really want to introduce xtra?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since xtra
is such a small dependency and the actor pattern is useful here, I am keen to introduce it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That reminds me to cut a release from master on xtra
a298aae
to
89ed143
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The change is definitely simpler than before. I like it. However it is certainly not as powerful as your other proposal.
While before the coordinator was relying entirely on the dlc states (which is the most reliable source of truth) with this change the maker solely relies on the updates from the coordinator / orderbook.
I think that is the way to go, but it will also put us at the risk that the position reflected to the maker may not be entirely correct. However, I think we need to find those scenarios and have to update the maker accordingly.
@luckysori @bonomat what about existing positions? Would it make sense to add an api to the coordinator returning all orders where the maker has been matched / is matched to an active position? That way the maker is not relying solely on getting updates in deltas.
89ed143
to
c291d58
Compare
@holzeis: I've updated the PR with a proposal to ensure that the maker's simulated position is resilient to restarts. Unfortunately I still cannot test this e2e locally because my app won't build. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think we can rely on the AllOrders
event here, as they won't include expired orders.
maker/src/orderbook_ws.rs
Outdated
) -> Result<()> { | ||
tracing::trace!(%msg, "New message from orderbook"); | ||
|
||
let msg = serde_json::from_str::<OrderbookMsg>(&msg).context("Deserialization failed")?; | ||
|
||
match msg { | ||
OrderbookMsg::AllOrders(orders) => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🚨You will only received non-expired orders here. You won't get consistently the taken orders here over time as the makers orders expire after 60 seconds. (unless configured differently)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the tip. The name definitely does not convey this.
I'm going to add an API to the WebSocket to get all the matched/taken orders for a trader.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It turns out none of the current orderbook messages work well for this purpose, so I just ended up adding a new one and letting the maker ask for it via a new orderbook request.
It would be much nicer if the orderbook could drive these updates instead, but it's not easy to implement, specially since we want to avoid adding maker-specific logic to the orderbook/coordinator.
83a34f9
to
3c84706
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have a hard time imagine how this evolves once we have a bitmex position as well. But let's get this in if it works :)
let _ = position_manager.send(PositionUpdateTenTenOne(orders)).await; | ||
} | ||
OrderbookMsg::Match(FilledWith { order_id, .. }) => { | ||
// We cannot rely directly on this message because the match does not specify the |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We cannot rely directly on this message because the match does not specify the direction.
Why not? We know the order_id
and given we created the order, we know the direction.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I left the comment because I tried it and it didn't work. The problem stems from the fact that the quantity
in the Match
is absolute, so the sign is always positive and there is no way to figure out the direction. I didn't want to change the semantics of quantity
because it was created for a different purpose and the fact that it is absolute might be important.
If we remembered the orders we created as the maker we could figure it out, but that information is not persisted on the maker.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we remembered the orders we created as the maker we could figure it out, but that information is not persisted on the maker.
Right ... there is this ticket for that #227
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good point!
I think it wouldn't hurt to persist the limit orders and it might be handy in this scenario, but it's still pretty limited as we only learn about the match and we cannot guarantee that the trade is executed. At least once the order is filled we know that the protocol was successfully started, so it's very likely that the trade will be executed.
The issue also mentions persisting the position, but in the current version where the coordinator is the only source of truth for this I actually think relying solely on the orderbook/coordinator is best, as there can't be disagreements. Once we introduce maker-coordinator DLCs we can get rid of all this, and then persisting some position metadata will be more interesting.
let quantity = match direction_maker { | ||
Direction::Long => quantity, | ||
Direction::Short => -quantity, | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like this logic, maybe we should start using this accross the board.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How do you think we should action your comment? I assume we are already mapping like this elsewhere.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, maybe we are already doing this :)
} | ||
|
||
#[cfg(test)] | ||
fn get_tentenone(&self, contract_symbol: &ContractSymbol) -> Decimal { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we add an HTTP Api to query this ? Could come handy in our production setup to know what is going on .
@@ -0,0 +1,220 @@ | |||
use async_trait::async_trait; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Honest rust question: why is this not in ./position/mod.rs
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's optional to use mod.rs
since Rust 2018, although I prefer not to use it: https://doc.rust-lang.org/edition-guide/rust-2018/path-changes.html#no-more-modrs.
At some point @holzeis wanted to try out introducing mod.rs
, but it's a bit annoying when you have multiple files open with the same mod.rs
name. I would be keen to align it and remove all mod.rs
files in favour of the newer standard.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the clarification. I'm happy to follow this best practice :)
Nice work! Thank you for reworking it! 🚀 |
3c84706
to
3e8b310
Compare
Fixes #973.