danpaquin / coinbasepro-python

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

Issue with trading with websocketclient #446

Open aestella opened 3 years ago

aestella commented 3 years ago

Hi all,

Is it not possible to submit orders when using the websocket client? I am attempting to submit orders upon receiving messages from the client, but for whatever reason it does not seem to work. It works on_open and on_close, and works separately, but for some reason does not work on_message.

Thanks! Andrew

aestella commented 3 years ago

code example....

def web_socket_testing(): 
    class myWebsocketClient(cbpro.WebsocketClient):
        def on_open(self):
            self.auth_client = cbpro.AuthenticatedClient(api_key, api_secret, passphrase)

def on_message(self, msg):
if some criteria....
self.auth_client.place_market_order(product_id='xxx', side='xxx', size='xxx') 

wsClient = myWebsocketClient()
wsClient.start()
time.sleep(7000)
wsClient.close()

if __name__ == "__main__":
web_socket_testing()

indentations are being weird

bishopandco commented 3 years ago

This is bad code ... you'd want to define auth_client outside of this class on it's own. Nothing wrong with the package.

import cbpro

auth_client = cbpro.AuthenticatedClient("api key", "base64secret", "some password")

class WebSocketTesting(cbpro.WebsocketClient):
    def on_open(self):
        print("Opening up again!")
        self.url = "wss://ws-feed.pro.coinbase.com/"
        self.products = ["ETH-USD"]
        self.channels = ["ticker"]

    def on_message(self, msg):
        if msg['type'] == "ticker":
            print(msg['trade_id'], ",", msg['time'])
            if (int(msg['price']) > 2000):
                auth_client.place_market_order(product_id='xxx', side='xxx', size='xxx')
            else:
                auth_client.place_market_order(product_id='xxx', side='xxx', size='xxx')

    def on_close(self):
        print("-- Goodbye! --")
aestella commented 3 years ago

I am pretty new to python so please forgive any bad code. I guess my question turns then to how do you call this in effect?

Something like this does not seem to work, although it does output the message while it is running, the order does not execute...

auth_client = cbpro.AuthenticatedClient("some_api_key", "some_api_secret", "some_passphrase")
class myWebsocketClient(cbpro.WebsocketClient):
    def on_open(self):
        self.url = "wss://ws-feed.pro.coinbase.com/"
        self.products = trade_symbol
        # do some stuff
    def on_message(self, msg):
        print(msg)
        auth_client.place_market_order(product_id = "ETH-USD", side = 'sell', size= '0.0001')
    def on_close(self):
        print("closing...")
if __name__ == "__main__":
    wsClient = myWebsocketClient()
    wsClient.start()
    time.sleep(10)
    wsClient.close()

I am definitely missing something obvious. Thanks again 4 the help