Stock Quote Plugin?

Posted on
Tue Apr 07, 2020 8:48 pm
Ramias offline
Posts: 272
Joined: Nov 24, 2015

Stock Quote Plugin?

I didn't see a plugin to retrieve stock quotes (to display on a Control Panel). Thinking of writing one of these. Looking for any input or suggestions on approach.

I'm looking at the Alphavantage API here: https://www.alphavantage.co/documentation/

500 quotes/day for free.

Ideas:
Plugin Config
1. To preserve API calls, only call during market hours. (0930-1630 Eastern Time (probably shift this by 5-15 minutes)). Question: have the user provide the time zone or can I query this from Indigo/MacOS?
2. Allow user to select API query interval
3. User provide API key

Each ticker symbol is its own device.
1. On creation, resolve symbol name to stock name on the US market (doesn't work with their API for Indexes like DJI; maybe filter this out for the three major US indexes)
This API call returns multiple responses based on what is entered. Should I force the user to enter it correctly, or present some sort of menu on device creation allowing them to select from the array of responses? https://www.alphavantage.co/query?funct ... pikey=demo

Create the device; stock ticker as address; resolved name as device name.

2. Query at specified interval: https://www.alphavantage.co/query?funct ... pikey=demo
Code: Select all
{
    "Global Quote": {
        "01. symbol": "IBM",
        "02. open": "118.8000",
        "03. high": "119.5700",
        "04. low": "114.8700",
        "05. price": "114.9400",
        "06. volume": "5528913",
        "07. latest trading day": "2020-04-07",
        "08. previous close": "114.8200",
        "09. change": "0.1200",
        "10. change percent": "0.1045%"
    }
}

if price is greater than open, display in green icon for device state; red if down.

Anybody have any other design ideas/suggestions? I'm not a developer by trade or training; more of a stackexchange and google hacker, but I have some time at the moment so may start tinkering with this.

Thanks

Posted on
Fri Apr 10, 2020 4:56 am
DaveL17 offline
User avatar
Posts: 6742
Joined: Aug 20, 2013
Location: Chicago, IL, USA

Re: Stock Quote Plugin?

That sounds like a good idea. I grabbed an Alpa Vantage API key a while back, but never did much with it other than confirm that it works with the GhostXML plugin.

1. To preserve API calls, only call during market hours. (0930-1630 Eastern Time (probably shift this by 5-15 minutes)). Question: have the user provide the time zone or can I query this from Indigo/MacOS?

You could compute the current time in the Eastern time zone yourself without worrying about the users' local TZ. Start with UTC and work your way from there. But you can query the users' local time through a call to the Indigo server if that's what you want to do.

2. Allow user to select API query interval

I find that it's best to allow the user to select the query interval with a "buffer" to ensure that they never run afoul of the limit. Some APIs will send a query count with any result payload, but others you'd have to count for yourself. I can't remember if AV sends the query count or not. I recommend saving the query count to something that's perpetual so that it doesn't get wiped out with a plugin restart. You could do this by saving it to a file location (like a custom plist file), but I prefer to save it to a plugin property (then Indigo will save it for you). Also, bear in mind that sometimes things can go sideways and a bug could cause the plugin to hammer the API--and the user could hit their limit in no time. This is why I feel it's important to track API calls closely and then shut down communication for the day if need be. I typically let the user enter the maximum number of calls themselves in case some users have paid subscriptions that allow for a higher call limit.

3. User provide API key

I agree that it's best to have the user do this.

I like the idea of the 1 device = 1 symbol idea with the icons showing price. I usually default to providing the user with a set of options to choose from, and then construct the query URL within the plugin to ensure that it's configured correctly. I usually think of up or down in relation to yesterday's close. You might consider a toggle to allow the user to decide for themselves whether to use one or the other.

Thanks for considering a new plugin for Indigo!

I came here to drink milk and kick ass....and I've just finished my milk.

[My Plugins] - [My Forums]

Posted on
Fri Apr 10, 2020 8:10 am
MarcoGT offline
Posts: 1091
Joined: Sep 11, 2014
Location: Germany

Re: Stock Quote Plugin?

Few months back I started to write a Stock Plugin and had a look here:

https://github.com/ranaroussi/yfinance

Posted on
Fri Apr 10, 2020 11:06 am
kw123 offline
User avatar
Posts: 8333
Joined: May 12, 2013
Location: Dallas, TX

Re: Stock Quote Plugin?

I actually do not want to look at the stock market for the next 6 months.

Karl


Sent from my iPhone using Tapatalk

Posted on
Fri Apr 10, 2020 2:30 pm
MarcoGT offline
Posts: 1091
Joined: Sep 11, 2014
Location: Germany

Re: Stock Quote Plugin?

LOL you are right
I also said that, but I actually look every day :D

BTW, Yahoo Finance deprecated the API, Google Finance also.
Have a look at finnhub or Tiingo

Posted on
Sat Apr 11, 2020 9:38 pm
Ramias offline
Posts: 272
Joined: Nov 24, 2015

Re: Stock Quote Plugin?

Thanks for the feed back. I have working version of this.

It offers:
User-provided API key
User-configurable polling interval
user-defined daily API limit
API counter

Each stock is created as its own device. Ticker symbol displayed in address column (All caps).
State shows green or red dot and +/1, percentage and current value.

Users can select either "$" or "points" when they create the ticker device. This sets the diplay value with our without the $ for that ticker symbol (i.e. of you are doing Dow Jones).

I don't poll on weekends and only poll during 1300-2100 UTC (9am - 5pm Eastern time; markets are :0930 - 4pm). This is because I take the %-H value for time which is an integer and I wanted to account for any lag in quotes (i.e. final value at 4:15 vs market close).

any suggestions on how to better do this? Also how to make this part of the code more bullet proof with Standard Time vs DST?

You can also see I reset the API counter at 0 UTC; I do it multiple times during this hour -- once per each stock's refresh interval. But that shouldn't be an issue since markets are closed so it is not polling during that period. Should I make polling hours user configurable in plugin settings? The way I reset the counter also means max polling interval is 60 minutes. Otherwise it would sleep through the opportunity to reset it,

Code: Select all
      try:
         while True:
            for deviceId in self.deviceList:
               time = datetime.datetime.utcnow().strftime('%-H')
               dev = indigo.devices[deviceId]
               # if market open, do this check
               day = datetime.datetime.utcnow().strftime('%w')
               self.debugLog(u"UTC day is %s." % day)
               if int(day) == 0 or int(day) == 6:
                  self.debugLog(u"This is a Weekend in UTC.  Markets are closed.")
               else:
                  self.debugLog(u"This is a UTC Weekday.  Markets are probably open.")
                  self.debugLog(u"Time is %s." % time)
                  if int(time) > 13 and int(time) < 21:
                     self.debugLog("Markets are open.  Calling updateStatus.")
                     self.updateStatus(dev)
                  else:
                     self.debugLog("Markets are closed.  Skipping update.")
               if int(time) < 1:
                  self.pluginPrefs['dailyCallCounter'] = "0"      
            self.debugLog("Sleeping...")
            self.sleep(int(self.interval))
      except self.StopThread:
         pass

Posted on
Sun Apr 12, 2020 12:14 pm
DaveL17 offline
User avatar
Posts: 6742
Joined: Aug 20, 2013
Location: Chicago, IL, USA

Re: Stock Quote Plugin?

Sounds like you're well on your way and I don't have any suggestions at this point. Fortunately (for my plugins anyway) there's always room for refinements later on.

I came here to drink milk and kick ass....and I've just finished my milk.

[My Plugins] - [My Forums]

Posted on
Sun Apr 12, 2020 3:55 pm
Ramias offline
Posts: 272
Joined: Nov 24, 2015

Re: Stock Quote Plugin?

Thanks for the input.

I have this working the way I want now. Not great on Error checking (if the user enters a pad stock symbol) and not great on error handling if there's an API error, but it works now for my purposes.

The purpose of this is to put stock quotes on Control Pages. I'd like green text if UP and red if Down. I know I can put the text on top of a dynamic image that changes color, but I'd like the text to change color itself.

Looking at a script that calls PIL that generates a transparent PNG with the text strings I want in the correct colors, then use the refreshing image control on the control page. I'll probably just do this as a standalone script; though I did not see a plugin that exists.

Other ideas?

Posted on
Sun Apr 12, 2020 4:23 pm
DaveL17 offline
User avatar
Posts: 6742
Joined: Aug 20, 2013
Location: Chicago, IL, USA

Re: Stock Quote Plugin?

If you go the route of using PIL, I think you'll have to either rely on users to install it or else include it in your plugin package.

There are some other options to consider on this thread.

I came here to drink milk and kick ass....and I've just finished my milk.

[My Plugins] - [My Forums]

Posted on
Mon Apr 13, 2020 8:34 pm
Ramias offline
Posts: 272
Joined: Nov 24, 2015

Re: Stock Quote Plugin?

Thanks DaveL17.

I got PIL working but the text in PNG was too blurry. Did some research and apparently PIL doesn’t support anti-aliasing. Going to stick with text-on-changing color background.

Would be great if a future version of Indigo allowed dynamic font color based on device state.

Going to let this plug in burn in for a few days and think of if I need to add anything then will push to the store.

Posted on
Tue Apr 14, 2020 4:25 am
DaveL17 offline
User avatar
Posts: 6742
Joined: Aug 20, 2013
Location: Chicago, IL, USA

Re: Stock Quote Plugin?

+1 on background color (that's how Stocks.app does it). :D

I came here to drink milk and kick ass....and I've just finished my milk.

[My Plugins] - [My Forums]

Page 1 of 1

Who is online

Users browsing this forum: No registered users and 12 guests