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

Simulate position between maker and coordinator #1216

Merged
merged 1 commit into from
Sep 19, 2023

Conversation

luckysori
Copy link
Contributor

Fixes #973.

@luckysori luckysori self-assigned this Sep 5, 2023
@luckysori luckysori force-pushed the feat/maker-simulated-position branch 2 times, most recently from 33c1fd5 to a298aae Compare September 6, 2023 02:05
holzeis

This comment was marked as resolved.

@@ -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" }
Copy link
Contributor

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?

Copy link
Contributor Author

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.

Copy link
Contributor

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

bonomat

This comment was marked as resolved.

@luckysori luckysori force-pushed the feat/maker-simulated-position branch from a298aae to 89ed143 Compare September 14, 2023 06:31
@luckysori
Copy link
Contributor Author

@bonomat @holzeis Please review this again.

I have not been able to test it locally because my new environment is not letting me start the app locally atm. I will only merge once I am able to resolve those problems and verify that the order matches are propagated correctly.

Copy link
Contributor

@holzeis holzeis left a 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.

maker/src/orderbook_ws.rs Outdated Show resolved Hide resolved
maker/src/position.rs Show resolved Hide resolved
@luckysori luckysori force-pushed the feat/maker-simulated-position branch from 89ed143 to c291d58 Compare September 18, 2023 10:30
@luckysori luckysori requested a review from holzeis September 18, 2023 10:30
@luckysori
Copy link
Contributor Author

@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.

Copy link
Contributor

@holzeis holzeis left a 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.

) -> Result<()> {
tracing::trace!(%msg, "New message from orderbook");

let msg = serde_json::from_str::<OrderbookMsg>(&msg).context("Deserialization failed")?;

match msg {
OrderbookMsg::AllOrders(orders) => {
Copy link
Contributor

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)

Copy link
Contributor Author

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.

Copy link
Contributor Author

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.

@luckysori luckysori requested a review from holzeis September 18, 2023 12:59
@luckysori luckysori force-pushed the feat/maker-simulated-position branch from 83a34f9 to 3c84706 Compare September 19, 2023 00:22
Copy link
Contributor

@bonomat bonomat left a 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
Copy link
Contributor

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.

Copy link
Contributor Author

@luckysori luckysori Sep 19, 2023

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.

Copy link
Contributor

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

Copy link
Contributor Author

@luckysori luckysori Sep 19, 2023

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.

Comment on lines +345 to +350
let quantity = match direction_maker {
Direction::Long => quantity,
Direction::Short => -quantity,
};
Copy link
Contributor

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.

Copy link
Contributor Author

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.

Copy link
Contributor

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 {
Copy link
Contributor

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;
Copy link
Contributor

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?

Copy link
Contributor Author

@luckysori luckysori Sep 19, 2023

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.

Copy link
Contributor

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 :)

@holzeis
Copy link
Contributor

holzeis commented Sep 19, 2023

Nice work! Thank you for reworking it! 🚀

@luckysori luckysori force-pushed the feat/maker-simulated-position branch from 3c84706 to 3e8b310 Compare September 19, 2023 14:43
@luckysori luckysori enabled auto-merge September 19, 2023 14:46
@luckysori luckysori added this pull request to the merge queue Sep 19, 2023
Merged via the queue into main with commit b543f43 Sep 19, 2023
@luckysori luckysori deleted the feat/maker-simulated-position branch September 19, 2023 15:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Simulate position with coordinator
4 participants