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

Issue with trading with websocketclient #446

Open
aestella opened this issue Jun 13, 2021 · 4 comments
Open

Issue with trading with websocketclient #446

aestella opened this issue Jun 13, 2021 · 4 comments

Comments

@aestella
Copy link

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
Copy link
Author

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

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
Copy link
Author

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

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

No branches or pull requests

3 participants
@bishopandco @aestella and others