Need a stock Ticker?

I made my first plugin! I'm a .NET developer by trade. I've never written groovy until now. I wanted to have a stock ticker for the company I work for on my dashboard. So, I went and found a free api called Alpha Vantage. They allow 500 calls per day. Like I said, I use this for displaying a single stock on my dashboard. I wouldn't use this for multiple tickers as you only get so many calls for free per day. You could also use it to set alerts for a specific ticker.

I copied heavily from @staze and his gmcmap.
I would link it, but because I am a new user, I am not allowed to link it. I've also posted it in my github and will update when I'm allowed to post links, but until then, here's the code.

/*
 * alphavantage ticker device
 */


metadata {
	definition(name: "alphavantage.stockticker", namespace: "alphavantage", author: "Leonard Sperry", importUrl: "https://raw.githubusercontent.com/leosperry/hubitat/main/alphavantage") 
    {
		capability "Sensor"
		capability "Polling"
		attribute "Symbol", "STRING"
		attribute "Open", "NUMBER"
		attribute "High", "NUMBER"
		attribute "Low", "NUMBER"
		attribute "Price", "NUMBER"
		attribute "Volume", "NUMBER"
		attribute "LatestTradingDay", "DATE"
		attribute "PreviousClose", "NUMBER"
		attribute "Change", "NUMBER"
		attribute "ChangePercent", "NUMBER"
        attribute "LastestPoll", "DATE"
	}
}

preferences {
	section("URIs") {
		input name: "ApiKey", type: "text", title: "API Key", required: true
        input name: "Symbol", type: "text", title: "Symbol", required: true
        input name: 'updateMins', type: 'enum', description: "Select the update frequency", title: "Update frequency (minutes)\n0 is disabled", defaultValue: '30', options: ['0','5','10','15','30'], required: true
        input name: "logEnable", type: "bool", title: "Enable debug logging", defaultValue: true
	}
}

def logsOff() {
	log.warn "debug logging disabled..."
	device.updateSetting("logEnable", [value: "false", type: "bool"])
}

def updated() {
	unschedule()
	log.info "alphavantage updated..."
	log.warn "debug logging is: ${logEnable == true}"
	if (logEnable) runIn(1800, logsOff)
	if(updateMins != "0") {
		schedule("0 */${updateMins} * ? * *", poll)
	}
	
}

def parse(String description) {
	if (logEnable) log.debug(description)
}

def poll() {
	if (logEnable) log.debug "alphavantage polling..."
    
    def url = "https://www.alphavantage.co/query?function=GLOBAL_QUOTE&symbol=${Symbol}&apikey=${ApiKey}"
    
	try {
		httpGet(url) { resp -> 
			//if (logEnable) log.debug 
            
            resp.getData()
			
            def respValues = resp.data["Global Quote"]
            sendEvent(name: "Symbol", value: respValues["01. symbol"])
            sendEvent(name: "Open", value: respValues["02. open"])
            sendEvent(name: "High", value: respValues["03. high"])
			sendEvent(name: "Low", value: respValues["04. low"])
			sendEvent(name: "Price", value: respValues["05. price"])
            sendEvent(name: "Volume", value: respValues["06. volume"])
            sendEvent(name: "LatestTradingDay", value: respValues["07. latest trading day"])
            sendEvent(name: "PreviousClose", value: respValues["08. previous close"])
            sendEvent(name: "Change", value: respValues["09. change"])
			sendEvent(name: "ChangePercent", value: respValues["10. change percent"])
            
            sendEvent(name: "LastestPoll", value: new Date())
		}
	} catch(Exception e) {
		log.debug "error occured calling httpget ${e}"
	}
}

Edit: This is the code I copied heavily from:
hubitat-gmcmap/hubitat-gmcmap.groovy at master · staze/hubitat-gmcmap (github.com)

Here is where you can find my code in github:
hubitat/alphavantage at main · leosperry/hubitat (github.com)

To use this driver, you will need to get yourself and API Key from Alphavantage which you can get for free at the time of this writing.

1 Like

Is there a symbol that tracks the DJIA that could be used with this?

@support can fix that.

INDU

2 Likes

try now

1 Like

Thanks!

Unfortunately, I am not a market expert.

The symbol is INDU. I once was a stock ticker expert, having built systems that put out stock quotes, processed them, made real time databases from them, etc. All in another lifetime -- 1980's. I vividly remember the day DJIA broke 1000 for the very first time, something no one thought was possible. There was a brokerage building that had a 3 digit DJIA display on it -- oops -- that sign was not replaced, just taken down.

3 Likes

INDU apparently isn't a symbol that Alpha Vantage uses or supports.

Either that or the Dow closed at $5.84 today, which wouldn't surprise me.

{
"Global Quote": {
"01. symbol": "INDU",
"02. open": "5.6000",
"03. high": "5.8500",
"04. low": "5.4074",
"05. price": "5.8400",
"06. volume": "72892",
"07. latest trading day": "2022-06-21",
"08. previous close": "5.6100",
"09. change": "0.2300",
"10. change percent": "4.0998%"
}

Per programming - Symbol for the Dow Jones Industrial Average in Alphavantage.co - Quantitative Finance Stack Exchange, they don't support indices.

I've updated it to support all the attributes. Still learning, I wasn't exposing them all previously.

You could use symbol DIA...it's the ETF that tracks the DJIA index...close enough for normal people.

Updated again. Fixed updated logic. I had accidentally removed too much of what I copied from. :man_facepalming: It should :crossed_fingers: be in a good state now.

This whole process is giving me a lot of new ideas :grinning:

2 Likes