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

Create advanced example to fetch orderbook via websocket #9

Open
mschneider opened this issue Feb 16, 2022 · 12 comments · May be fixed by #19
Open

Create advanced example to fetch orderbook via websocket #9

mschneider opened this issue Feb 16, 2022 · 12 comments · May be fixed by #19
Assignees
Labels
good first issue Good for newcomers
Projects

Comments

@mschneider
Copy link
Owner

https://github.com/mschneider/solcpp/blob/main/examples/accountSubscribe.cpp is a basic example for how to get the recent fills. We need a more complete example that is able to fetch the orderbook in addition and prints the following to the CLI on every SOL-PERP order book update:

  1. Last traded price
  2. Mid-price
  3. Spread in basis points
  4. $ +2% depth
  5. $ -2% depth
@mschneider mschneider added the good first issue Good for newcomers label Feb 16, 2022
@papadpickle
Copy link
Contributor

i have started working on this one.
would be good to have a board to mark it as in progress lest someone else starts this from scratch in parallel.

@mschneider mschneider added this to To do in v0.2 Feb 21, 2022
@mschneider mschneider moved this from To do to In progress in v0.2 Feb 21, 2022
@mschneider
Copy link
Owner Author

created a board: https://github.com/mschneider/solcpp/projects/1

@mschneider
Copy link
Owner Author

also we are just about to roll out v3.4 from mango side, i was planning to write a simple order book parser for that purpose. lmk if that causes a conflict on your end.

@mschneider mschneider removed the good first issue Good for newcomers label Feb 21, 2022
@papadpickle
Copy link
Contributor

i am using the mango bowl wss server to subscribe to the data. not sure if the rollout to newer mango version will affect that or not.
https://github.com/papadpickle/solcpp/blob/example_orderbook/examples/orderbookSubscribe/orderbookSubscribe.cpp

let me know if you had something else in the mind re this orderbook parsing example.

@mschneider
Copy link
Owner Author

yeah, we should probably connect directly to the on-chain data rather then through mango-bowl which does a lot of intermediate processing. i'll try to make a simple example of getting the order book via json rpc.

@mschneider
Copy link
Owner Author

9deb4c9

here's a rough sketch of the code to parse the order book

@papadpickle
Copy link
Contributor

thanks for the rough idea. added lowest asks now https://github.com/papadpickle/solcpp/blob/example_orderbook_subscribe/examples/orderbookSubscribe.cpp

still unclear though how to have a websocket subscription to orderbook update instead of the rpc polling. if you have any code somewhere in other projects which wss'es solana, please let me know.

@mschneider
Copy link
Owner Author

accountSubscribe is the correct request to implement this, you can either open multiple websockets or subscribe to multiple account updates on the same socket

@papadpickle
Copy link
Contributor

papadpickle commented Feb 23, 2022

makes sense. have added two subscriber objects for bids and asks. 8760e57
will have to see how/when to log the required info since the bids and asks are separate wss messages.

@papadpickle
Copy link
Contributor

do you have any specific way to calculate market depth? that is just the volume of orders at $+% right? would be cool if you already have a logic in rust code or some other mango project to look into before i try to reinvent the wheel.

that''s the remaining part, everything else is ready at https://github.com/papadpickle/solcpp/tree/example_orderbook_subscribe_sol

@mschneider
Copy link
Owner Author

noticed you use getSpreadBsp in your code, i think BPS is a more common abbreviation for basis points

@sean-thorburn
Copy link

do you have any specific way to calculate market depth? that is just the volume of orders at $+% right? would be cool if you already have a logic in rust code or some other mango project to look into before i try to reinvent the wheel.

here is an example in C#:

IStreamingRpcClient streamingRpcClient = ClientFactory.GetStreamingClient(Cluster.MainNet);
IMangoClient mangoClient = Solnet.Mango.ClientFactory.GetClient(rpcClient, streamingRpcClient);

AccountResultWrapper<PerpMarket> perpMarket = await mangoClient.GetPerpMarketAsync("DtEcjPLyD4YtTBB4q8xwFZ9q49W89xZCZtJyrGebi5t8"); // BTC-PERP
AccountResultWrapper<OrderBookSide> bidsRequest = await mangoClient.GetOrderBookSideAsync(perpMarket.ParsedResult.Bids, Commitment.Processed); // Bids side of the orderbook
AccountResultWrapper<OrderBookSide> asksRequest = await mangoClient.GetOrderBookSideAsync(perpMarket.ParsedResult.Asks, Commitment.Processed); // Asks side of the orderbook

List<OpenOrder> bids = bidsRequest.ParsedResult.GetOrders().OrderByDescending(o => o.RawPrice).ToList();
List<OpenOrder> asks = asksRequest.ParsedResult.GetOrders().OrderBy(o => o.RawPrice).ToList();

long midPrice = (bids[0].RawPrice + asks[0].RawPrice) / 2;
double midLessDepth = midPrice * 0.98; // 2%
long depth = bids.Where(o => o.RawPrice > midLessDepth).Sum(o => o.RawQuantity);

@mschneider mschneider linked a pull request Mar 1, 2022 that will close this issue
@mschneider mschneider added the good first issue Good for newcomers label Sep 23, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Good for newcomers
Projects
v0.2
In progress
Development

Successfully merging a pull request may close this issue.

3 participants