danpaquin / coinbasepro-python

The unofficial Python client for the Coinbase Pro API
MIT License
1.82k stars 740 forks source link

OrderBook in Sandbox #328

Open adrug1 opened 5 years ago

adrug1 commented 5 years ago

Did anybody have any luck getting the OrderBook working in the sandbox? I keep getting IndexError: list index out of range, no matter how I try to pass the correct URL. What is the correct implementation, if there is one?

danpaquin commented 5 years ago

Sandbox is hit-or-miss. It's unofficially unsupported.

adrug1 commented 5 years ago

Ha - thank you. I find that it works perfectly fine with an authenticated sandbox client, it's just the OrderBook that keeps failing. I guess it's time to debug with real money!

danpaquin commented 5 years ago

If it works in production, then we should assume it's an issue with the sandbox api endpoints rather than this package.

Thank you for raising your concern! I'll leave this here to give others an opportunity to chime in

jamesprinc3 commented 5 years ago

I'm not sure what URL you are injecting or how, but to make this work you need to inject both the WebSocket URL and the Public API URL.

I think this could be an issue with the implementation. I haven't done a massive amount of testing but in v1.12 the WebsocketClient has a default url argument which points to the real coinbase pro WebSocket. Additionally, the PublicClient also has a default url argument pointing to the real public api endpoint. The OrderBook starts by taking a snapshot of the orderbook and then applying further messages to the order book state.

I've slapped together some code which seems to work, I'm going to go forward with it this afternoon and see if I run into any issues. If this might be of interest to others I can prepare a PR with the changes I made.

jamesprinc3 commented 5 years ago

So I did a bit more investigation. Because of the low number of orders that are submitted to the sandboxes, the set up code as is didn't work if you didn't place your own orders between order_book.start() and performing some other action.

I dug into the code and I'm not sure that the code as is strictly follows CBP API spec. When the space between consecutive messages is big enough, then errors can and do occur.

I now seem to have a version of this library which works with the sandbox, I can prepare a PR with everything if anyone else is interested in this. I think this fix could also reduce the possibility of spurious errors in weird market conditions on real markets too.

adrug1 commented 5 years ago

Good job. I think anything that improves the OrderBook reliability would be most useful - having a replicated order-book locally speeds up any algo by orders of magnitude.

On 9 September 2018 at 13:48, James Prince notifications@github.com wrote:

So I did a bit more investigation. Because of the low number of orders that are submitted to the sandboxes, the set up code as is didn't work if you didn't place your own orders between order_book.start() and performing some other action.

I dug into the code and I'm not sure that the code as is strictly follows CBP API spec. When the space between consecutive messages is big enough, then errors can and do occur.

I now seem to have a version of this library which works with the sandbox, I can prepare a PR with everything if anyone else is interested in this. I think this fix could also reduce the possibility of spurious errors in weird market conditions on real markets too.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/danpaquin/coinbasepro-python/issues/328#issuecomment-419713639, or mute the thread https://github.com/notifications/unsubscribe-auth/Af38buzMNigERsYgrfxXsq-FvbODPm4wks5uZQ4bgaJpZM4WbEaE .

kkuette commented 5 years ago

The no data error isn't a big issue, you can load and start the orderbook in a while loop like this :

def runOrderBook(self):
        while self.is_running:
            self.orderbook.start()
            try:
                while not self.orderbook.stop:
                    time.sleep(1)
            except KeyboardInterrupt:
                pass

When it restart you still have previous replicated orderbook, it just up to date when reconnecting if any order is passed. You can see more use case in my own cbpro wrapper