Python Forum

Full Version: how to modify moth_ws for different coins
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
i am working on this article so that i can add a socket to excel. the demo works perfectly, now i want to add to it.

so this opens up ws-feed.pro.coinbase.com so that it can display the attributes for the feed selected (the demo uses BTC-Bitcoin). Now i want to add another moth_ws.py module (called moth_wsL.py)

when i make the changes to moth_wsL.py, nothing appears to happen. can someone give me some tips on how to do this?

here are the changes to moth_wsL.py:
"""
PyXLL Examples: Real time data

As well as returning static values from functions, PyXLL functions
can return special 'RTD' instances that can notify Excel of
updates to their value.

This could be used for any real time data feed, such as live
prices or the status of a service.
"""
from pyxll import RTD, xl_func, xl_app
import threading
import logging
import json
import websocket

ws = websocket.create_connection("wss://ws-feed.pro.coinbase.com")
ws.send(json.dumps({
        "type": "subscribe",
        "channels": [
            {
                "name": "ticker",
                "product_ids": ["LTC-USD"]
            }
        ]
    }))
#Check contents of result to see if subscription has succeeded
result =  ws.recv()

_log = logging.getLogger(__name__)

class GDAXRTDL(RTD):

    def __init__(self):
        initial_value = ""
        super(GDAXRTDL, self).__init__(value=initial_value)
        self.__running = True
        self.__thread = threading.Thread(target=self.__thread_func)
        self.__thread.start()

    def connect(self):
        # Called when Excel connects to this RTD instance, which occurs
        # shortly after an Excel function has returned an RTD object.
        _log.info("GDAXRTDL Connected")

    def disconnect(self):
        # Called when Excel no longer needs the RTD instance. This is
        # usually because there are no longer any cells that need it
        # or because Excel is shutting down.
        self.__running = False
        _log.info("GDAXRTDL Disconnected")

    def __thread_func(self):
        while self.__running:
            # Setting 'value' on an RTD instance triggers an update in Excel
            try:
                result =  ws.recv()
                print(result)
            except Exception as e:
                result = e
            self.value = result

@xl_func(": rtd")
def gdax_rtdL():   
    return GDAXRTDL()
the original moth_ws.py looks like this:

"""
PyXLL Examples: Real time data

As well as returning static values from functions, PyXLL functions
can return special 'RTD' instances that can notify Excel of
updates to their value.

This could be used for any real time data feed, such as live
prices or the status of a service.
"""
from pyxll import RTD, xl_func, xl_app
import threading
import logging
import json
import websocket

ws = websocket.create_connection("wss://ws-feed.gdax.com")
ws.send(json.dumps({
        "type": "subscribe",
        "channels": [
            {
                "name": "ticker",
                "product_ids": ["LTC-USD"]
            }
        ]
    }))
#Check contents of result to see if subscription has succeeded
result =  ws.recv()

_log = logging.getLogger(__name__)

class GDAXRTDL(RTD):

    def __init__(self):
        initial_value = ""
        super(GDAXRTDL, self).__init__(value=initial_value)
        self.__running = True
        self.__thread = threading.Thread(target=self.__thread_func)
        self.__thread.start()

    def connect(self):
        # Called when Excel connects to this RTD instance, which occurs
        # shortly after an Excel function has returned an RTD object.
        _log.info("GDAXRTDL Connected")

    def disconnect(self):
        # Called when Excel no longer needs the RTD instance. This is
        # usually because there are no longer any cells that need it
        # or because Excel is shutting down.
        self.__running = False
        _log.info("GDAXRTDL Disconnected")

    def __thread_func(self):
        while self.__running:
            # Setting 'value' on an RTD instance triggers an update in Excel
            try:
                result =  ws.recv()
                print(result)
            except Exception as e:
                result = e
            self.value = result

@xl_func(": rtd")
def gdax_rtdL():   
    return GDAXRTDL()
note that this works perfectly, but when i make changes to pyxll.cfg to add moth_wsL.py, nothing appears to happen. when i am in excel and i type in a cell, i use "=gdax_rtdL()", but i get a #NAME? in the cell which tells me the socket is not working. what am i doing wrong?

well, somehow i got this running as per the modules with no changes. all i had to do was reload pyxll.
The only difference I see, is that you're connecting to a different server. Is that the difference you're testing?

Are you sure that the other server implements the same interface? What do you get if you print the result out?
nilamo, i realized that i posted the wrong code (code #2) in there. i will post the correct code here for your review.

"""
PyXLL Examples: Real time data

As well as returning static values from functions, PyXLL functions
can return special 'RTD' instances that can notify Excel of
updates to their value.

This could be used for any real time data feed, such as live
prices or the status of a service.
"""
from pyxll import RTD, xl_func, xl_app
import threading
import logging
import json
import websocket

ws = websocket.create_connection("wss://ws-feed.pro.coinbase.com")
ws.send(json.dumps({
        "type": "subscribe",
        "channels": [
            {
                "name": "ticker",
                "product_ids": ["BTC-USD"]
            }
        ]
    }))
#Check contents of result to see if subscription has succeeded
result =  ws.recv()

_log = logging.getLogger(__name__)

class GDAXRTD(RTD):

    def __init__(self):
        initial_value = ""
        super(GDAXRTD, self).__init__(value=initial_value)
        self.__running = True
        self.__thread = threading.Thread(target=self.__thread_func)
        self.__thread.start()

    def connect(self):
        # Called when Excel connects to this RTD instance, which occurs
        # shortly after an Excel function has returned an RTD object.
        _log.info("GDAXRTD Connected")

    def disconnect(self):
        # Called when Excel no longer needs the RTD instance. This is
        # usually because there are no longer any cells that need it
        # or because Excel is shutting down.
        self.__running = False
        _log.info("GDAXRTD Disconnected")

    def __thread_func(self):
        while self.__running:
            # Setting 'value' on an RTD instance triggers an update in Excel
            try:
                result =  ws.recv()
                print(result)
            except Exception as e:
                result = e
            self.value = result

@xl_func(": rtd")
def gdax_rtd():   
    return GDAXRTD()
so yes i am using the new server (as the old one is getting decommissioned this year). everything is working. but i do have a question on sockets. i can safely use three sockets that run at the same time in an excel workbook. but when i add the fourth, then i get an error message that states: "error loading moth_wsH:Handshake status 429 rate limit exceeded". is there a way to write the code so that the error doesn't occur?
(Jan-16-2019, 11:49 AM)danmcg Wrote: [ -> ]"error loading moth_wsH:Handshake status 429 rate limit exceeded"
That's unrelated to sockets. That's the server you're connecting to, telling you that you're asking for data too often. It's rate limiting you so you don't overwhelm it.
thanks nilamo, appreciate the help.