Skip to content

Create advanced example to fetch orderbook via websocket #9

Open

Description

@mschneider

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

Activity

  1. papadpickle commented on Feb 21, 2022

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

  2. mschneider commented on Feb 21, 2022

    @mschneider
    OwnerAuthor
  3. mschneider commented on Feb 21, 2022

    @mschneider
    OwnerAuthor

    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.

  4. papadpickle commented on Feb 21, 2022

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

  5. mschneider commented on Feb 21, 2022

    @mschneider
    OwnerAuthor

    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.

  6. mschneider commented on Feb 22, 2022

    @mschneider
    OwnerAuthor

    9deb4c9

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

  7. papadpickle commented on Feb 22, 2022

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

  8. mschneider commented on Feb 23, 2022

    @mschneider
    OwnerAuthor

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

  9. papadpickle commented on Feb 23, 2022

    @papadpickle
    Contributor

    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.

  10. papadpickle commented on Feb 25, 2022

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

  11. mschneider commented on Feb 25, 2022

    @mschneider
    OwnerAuthor

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

  12. sean-thorburn commented on Feb 25, 2022

    @sean-thorburn

    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);
    
    
  13. linked a pull request that will close this issueExample orderbook subscribe sol. #19on Mar 1, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions