Reply to topic

KMyMoney & no stock quotes

User avatar watchstar
Registered Member
Posts
26
Karma
0

Re: KMyMoney & no stock quotes

Wed Mar 21, 2018 11:47 pm
Thanks. A couple of stock symbols needed to be changed. For example RDS.A needed to be changed to RDS-A. Thanks for posting.
mcarpino
Registered Member
Posts
73
Karma
0
OS

Re: KMyMoney & no stock quotes

Sat Mar 24, 2018 2:30 pm
[quote="mdolnik"]It looks like the https://finance.google.com/finance?q=%1 link is no longer working as Google has updated their Finance site.

I haven't been able to figure out how to get google working anymore with its new changes, but I have found a way to get stock quotes and currency working with Yahoo (working as of 2018-03-17).

Thank You Very Much for posting this fix! I really appreciate your support.
empenoso
Registered Member
Posts
31
Karma
0

Re: KMyMoney & no stock quotes

Fri May 04, 2018 7:15 am
mdolnik wrote:
empenoso wrote:I'm thinking about the "regularMarketTime" field. How to get date from this field?
Code: Select all
%Y-%m-%d %H:%M:%S


I don't know much about how kmymoney deals with dates, but I am under the assumption that the settings I mentioned above are using the dates correctly.

The yahoo url (eg https://query1.finance.yahoo.com/v7/fin ... mbols=goog) returns a json string of information and the regularMarketTime (or any date fields for that matter) appear to have their values in a unix timestamp format.

My assumption is that kmymoney stores dates as unix timestamps anyways so we don't need to provide it with date patterns as we are providing it with the raw value.

Someone please correct me if I'm wrong.


I found the date format :) . It's Unix Time Stamp.
https://query1.finance.yahoo.com/v7/fin ... mbols=goog -> "regularMarketPrice":1023.72,"regularMarketTime":1525377602

Timestamp Converter - https://www.unixtimestamp.com/index.php
1525377602
Is equivalent to:
05/03/2018 @ 8:00pm (UTC)
2018-05-03T20:00:02+00:00 in ISO 8601
Thu, 03 May 2018 20:00:02 +0000 in RFC 822, 1036, 1123, 2822
Thursday, 03-May-18 20:00:02 UTC in RFC 2822
2018-05-03T20:00:02+00:00 in RFC 3339
empenoso
Registered Member
Posts
31
Karma
0

Re: KMyMoney & no stock quotes

Fri May 04, 2018 7:21 am
Can KMyMoney convert Unix time into a regular time format?
User avatar ipwizard
KDE Developer
Posts
928
Karma
4
OS

Re: KMyMoney & no stock quotes

Fri May 04, 2018 7:43 am
Unfortunately, this is currently not possible. If it makes sense to have such a feature (I recommend to use %s as format specifier then - see man date for details) please feel free to open a wishlist item on the KMyMoney bug tracker.


ipwizard, proud to be a member of the KMyMoney forum since its beginning. :-D
openSuSE Leap 15.1 64bit, KF5
User avatar Section_8
Registered Member
Posts
17
Karma
0
OS

Re: KMyMoney & no stock quotes

Thu Sep 20, 2018 6:41 am
Hello everyone,
I haven't posted here before, but I've been running kmymoney on linux for 3 or 4 years now (since quicken cut off my online quote access) and I found this thread searching for a working online quote source for my investments in kmymoney.

I've played with various quote sources, like marketwatch.com, but really like the formatted output produced by query1.finance.yahoo.com in this thread. I've dabbled in python, so since kmymoney lets you asign a script as an online quote source, I hacked up the python script below to retrieve online quotes from query1.finance.yahoo.com and handle the unix time_t date stamp that is the issue here.

It requires python 3 and should be downloaded and marked as executable. It passes the ticker price and date to kymoney as a string:
price="999.99" date="mm/dd/yyyy"

In kmymoney, to use kmquotes.py, create on online quote source with:

URL: file:/path/to/kmquotes.py %1
Identifier: %1
Identify by: Symbol
Price: price="([^"]+)
Date: date="([^"]+)
Date Format: %m %d %y

Troubleshooting -
This script uses the python logging facility to produce a log file - kmquotes.log - in the user's default working directory (/home/userid/kmquotes.log in linux). By default, this log file is only updated if an error occurs retrieving a quote. For troubleshooting purposes, the logging level can be passed to kmquotes.py in the kmymoney URL:

info - a single line containing the ticker symbol, date, and price is logged every time kmquotes.py is invoked to retrieve a quote
debug - more data, such as the full URL passed to finance.yahoo.com, and the full response, are logged every time kmquotes is called

So set the kmymoney online quote URL to: file:/path/to/kmquotes.py %1 debug
if you want to see the debug data in kmquotes.log whenever kmquotes.py is called from kmymoney.

Windows issues -
I normally run kmymoney and this script in linux. Before posting this here, I installed kmymoney 4.8.2, and python 3.7 in a Windows 10 VM in virtualbox, to test this in windows. I can run kmquotes.py from a windows terminal (cmd.exe) and retrieve quotes, so kmquotes can run in windows.

However, I haven't figured out how to create a kmymoney online quote source in windows that runs a script. Regardless of how I set the URL to kmquotes, it fails in kmymoney when I try to retrieve online quotes with:
Unable to launch: kmquotes.py XXXX
Unable to update price for XXXX (empty quote data)

If anyone has setup a kmymoney quote source in windows to run a script, please respond.

I'm an amateur with python, so any comments are welcome.

Code: Select all
#!/usr/bin/env python
# encoding: utf-8
'''
kmquotes -- script called by kmymoney to retrieve investment quotes and dates from query1.finance.yahoo.com

usage:  kmquotes ticker loglevel

loglevel is the logging level: DEBUG, INFO, or ERROR
'''

import sys, os, logging, re
import urllib.request
from datetime import datetime, timedelta, timezone
from pathlib import Path

#  user_agent = 'Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Firefox/60.0'

# sys.argv = [ sys.argv[0], 'GOOG' ]
#
#  Retrieve a stock quote and date from finance.yahoo.com
#
def YahooQuote( ticker ):
    url = 'https://query1.finance.yahoo.com/v7/finance/quote?fields='
    fields = [ 'regularMarketPrice',
               'regularMarketTime',
               'gmtOffSetMilliseconds' ]
   
    url = url + ','.join( fields ) + '&symbols=' + ticker
    req = urllib.request.Request( url )
    #  req.add_header( 'User-Agent', user_agent )
   
    logger = logging.getLogger( __name__ )
    logger.debug( 'URL to retrieve %s:\n%s', ticker, url )
   
    try:
        with urllib.request.urlopen( req ) as response:
            mktdata = response.read().decode()
           
    except urllib.error.URLError as e:
        logger.error( 'URLError on URL\n%s:\nError: %s', url, e.reason )
        return ( None, None )
     
    logger.debug( "finance.yahoo.com response:\n%s", mktdata )
   
    #  extract the stock price:
    mat = re.search( r'"regularMarketPrice":([\d.]+)', mktdata )
    if  mat: 
        price = mat.group( 1 )
    else:
        logger.error( 'No price match in Yahoo data for %s  URL:\n%s\nQuote data:\n%s', ticker, url, mktdata )
        price = None
       
    #  extract the quote date. This is a unix time_t time stamp:
    mat = re.search( r'"regularMarketTime":([\d]+)', mktdata )
    if  mat: 
        date = datetime.fromtimestamp( int( mat.group( 1 ) ) )
    else:
        logger.error( 'No date match in Yahoo data for %s  URL:\n%s\nQuote data:\n%s', ticker, url, mktdata )
        date = None
           
    #  Adjust the quote date for the time zone:           
    if  date:
        mat = re.search( r'"gmtOffSetMilliseconds":([-\d]+)', mktdata )
        if  mat:
            millisecs = int( mat.group( 1 ) )
            tdelt = timedelta( milliseconds=millisecs )
            date = date.replace( tzinfo=timezone( tdelt ) )
        else:
            logger.error( 'Unable to match TZ offset in Yahoo data for %s  URL:\n%s\nQuote data:\n%s', ticker, url, mktdata )
            logger.error( 'Date is left as-is' )
                   
    return ( date, price )   
   
   
def main():
   
    #  set path name for the log file:
    homedir = str( Path.home() )
    logfile = os.path.join( homedir, 'kmquotes.log' )
   
    logging.basicConfig( level=logging.ERROR,
                    format='%(asctime)s %(levelname)-8s %(message)s',
                    datefmt='%a, %d %b %Y %H:%M:%S',
                    filename=logfile, filemode='a' )
   
    numargs = len( sys.argv )
    if  numargs < 2:
        logging.error( "A ticker symbol is required" )
        return 8
   
    ticker = sys.argv[1].upper()
    logger = logging.getLogger( __name__ )
   
    loglevel = 'ERROR'
    if  numargs > 2:
        loglevel = sys.argv[2].upper()
       
    try:
        logger.setLevel( loglevel )
    except ValueError as e:
        logger.error( "Error setting log level: %s", e )
        return 8
   
    logger.debug( '%s symbol:%s loglevel:%s', sys.argv[0], ticker, loglevel )
   
    date, price = YahooQuote( ticker )         
    if  date:
        date = date.strftime( '%m/%d/%Y' )
    else:
        date = 'invalid date'
       
    if  price is None:  price = 'invalid price'
       
    logger.info( '%s from finance.yahoo.com date:%s  price:%s', ticker, date, price )
   
    # emit price and date to kmymoney:
    strout = 'price="{}" date="{}"'.format( price, date )
    logger.debug( 'Reply to kmymoney:' )
    logger.debug( strout )
    print( strout )
    return 0


if __name__ == "__main__":
    sys.exit( main() )
language
Registered Member
Posts
9
Karma
0
OS

Re: KMyMoney & no stock quotes

Sun Nov 03, 2019 9:59 am
Is this still working for anyone?
Code: Select all
URL: https://query1.finance.yahoo.com/v7/finance/quote?fields=regularMarketPrice&symbols=%1
Symbol: %1
Price: "regularMarketPrice":((\d+|\d{1,3}(?:[,]\d{3})).\d+)
Date: "regularMarketTime":([\d]+)
Date Format: [empty]
Skip HTML stripping: [checked]

Running KMM 5.03 on Kubuntu, I'm getting this response:

Fetching URL https://query1.finance.yahoo.com/v7/fin ... s=WBC.AX...
Identifier found: ''
Unable to update price for WBC.AX (no price or no date)

I'm noticing that the "Symbol:" field seems to be gone from "Settings > Configure KMyMoney > Online Quotes". Could that have anything to do with it?

Grateful for any assistance - this is my first time trying to update stock prices via KMM and would like to get it working...!

(NB Could not get Google Finance working either...)
mcarpino
Registered Member
Posts
73
Karma
0
OS

Re: KMyMoney & no stock quotes

Mon Nov 04, 2019 2:15 pm
For me using Yahoo to update stock price is still working. I'm running Ubuntu 19.10.

Do you have the field "Identify by" field in the online quotes tab? That should be set to "Symbol". If you do then check the symbol for the stock on Yahoo's web site.
language
Registered Member
Posts
9
Karma
0
OS

Re: KMyMoney & no stock quotes

Mon Nov 11, 2019 9:56 am
Thanks, false alarm - apparently, I had just clicked "ok" in the settings box, not "update" first and then "ok". The Yahoo API recipe as posted above still works!
mdolnik
Registered Member
Posts
10
Karma
0

Re: KMyMoney & no stock quotes

Fri Apr 03, 2020 6:02 pm
FYI I found another way to import historical prices that doesn't need a script https://forum.kde.org/viewtopic.php?f=69&t=165308&p=430514#p430514
uncadon
Registered Member
Posts
6
Karma
0

Re: KMyMoney & no stock quotes

Sun Apr 26, 2020 5:27 pm
I tired using the Yahoo-API, but could not get it to work, If I plug https://query1.finance.yahoo.com/v7/fin ... bols=FMAGX into a browser, I get a response. Does this method work for mutual funds? In Windows?

I've checked for trailing spaces and hit Update as needed, but no luck.

Any ideas ???

 
Reply to topic

Bookmarks



Who is online

Registered users: Baidu [Spider], Bing [Bot], Garthhh, Google [Bot], ostroffjh, Sogou [Bot], Yahoo [Bot]