[GNC] How to "Get Quotes" for a specific day?

ml enquirer ml.hnel.227484 at gmail.com
Mon Jan 2 10:16:11 EST 2023


Hi Michael,

I've faced the same problem. I use the gnucash python bindings and the
following script, to import prices from a text file which has date
(YYYY-MM-DD) and price with a new date on each line.

Hope it could be some use. As always, test it before deploying! Best wishes.

```python
import datetime
from gnucash import Session, Account, Split, GncCommodity, GncPrice

url = "personal.gnucash"

# Initialize Gnucash session
session = Session(url, True, False, False)
book = session.book
pdb = book.get_price_db()
comm_table = book.get_table()

# Define commodities
sec_gbp = GncCommodity(book,"Pound Sterling", "CURRENCY", "GBP", "826", 100)
sec_chf = GncCommodity(book,"Swiss Franc", "CURRENCY", "CHF", "756", 100)

def extractFXrateFromFile(filename,book,pdb,sec_base,sec_curr):
  with open(filename) as myFile :
    for line in myFile.readlines() :
      splitLine = line.split(' ')
      y,m,d = (int(x) for x in splitLine[0].split("-"))

      price = GncPrice(book)
      price.set_time64( datetime.datetime(y,m,d) )
      value = price.get_value()
      value.denom = int( 1e10 )
      rate = float(splitLine[1])
      value.num = int( rate * 1e10 )
      price.set_value(value)
      price.set_currency( sec_curr )
      price.set_commodity( sec_base )
      pdb.add_price(price)

extractFXrateFromFile("chf_per_gbp.txt", book, pdb, sec_chf, sec_gbp)

session.save()
session.end()
session.destroy()
```

On Fri, Dec 30, 2022 at 5:19 AM <gnucash_2022 at michaelaltfield.net> wrote:

> How can I tell GNU Cash to update its "Price Database" for all known
> currencies, stocks, bonds, mutual funds, etc for a *specific* day?
>
> I'm trying trying to prepare a report for a date in the past. I've set the
> `Options` for the report to `nearest in time` so that it will grab the
> prices for the commodities from the Price Database that are closest to the
> date of the report as possible.
>
>  *
> https://gnucash-docs-rst.readthedocs.io/en/latest/guide/C/ch_invest.html?highlight=price%20database#the-price-source-in-reports
>
> Well, I checked the Price Database, and I have gaps in the prices for some
> of my stocks. For many stocks, I don't have any prices defined for the day
> of the report. Therefore, my report may be inaccurate.
>
> I know that I can go to `Tools` -> `Price Database` and click the `Get
> Quotes` button. This will fetch the current quote (price) and add it to the
> Price Database for all defined currencies, stocks, bonds, mutual funds,
> etc. But that will fetch and add the prices for *today's date* only.
>
> How can I do the equivalent of `Get Quotes` but for a specific day in the
> *past*?
>
> (see also
> https://superuser.com/questions/1760157/how-to-get-quotes-for-all-currencies-and-stocks-for-a-specific-day-gnu-cash
> )
> _______________________________________________
> gnucash-user mailing list
> gnucash-user at gnucash.org
> To update your subscription preferences or to unsubscribe:
> https://lists.gnucash.org/mailman/listinfo/gnucash-user
> -----
> Please remember to CC this list on all your replies.
> You can do this by using Reply-To-List or Reply-All.
>


More information about the gnucash-user mailing list