-
Notifications
You must be signed in to change notification settings - Fork 761
/
price-quotes.scm
757 lines (690 loc) · 29.4 KB
/
price-quotes.scm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; price-quotes.scm - manage sub-processes.
;;; Copyright 2001 Rob Browning <rlb@cs.utexas.edu>
;;;
;;; This program is free software; you can redistribute it and/or
;;; modify it under the terms of the GNU General Public License as
;;; published by the Free Software Foundation; either version 2 of
;;; the License, or (at your option) any later version.
;;;
;;; This program is distributed in the hope that it will be useful,
;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;;; GNU General Public License for more details.
;;;
;;; You should have received a copy of the GNU General Public License
;;; along with this program; if not, contact:
;;;
;;; Free Software Foundation Voice: +1-617-542-5942
;;; 51 Franklin Street, Fifth Floor Fax: +1-617-542-2652
;;; Boston, MA 02110-1301, USA gnu@gnu.org
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(define-module (gnucash price-quotes))
(export yahoo-get-historical-quotes)
(export gnc:book-add-quotes) ;; called from gnome/dialog-price-edit-db.c
(export gnc:price-quotes-install-sources)
(use-modules (gnucash process))
(use-modules (gnucash main)) ;; FIXME: delete after we finish modularizing.
(use-modules (gnucash gnc-module))
(use-modules (gnucash core-utils))
(gnc:module-load "gnucash/gnome-utils" 0) ;; for gnucash-ui-is-running
(gnc:module-load "gnucash/app-utils" 0)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;; Yahoo-based Historical Quotes
;;
(use-modules (www main))
(use-modules (srfi srfi-1))
;; (use-modules (srfi srfi-19)) when available (see below).
(define (item-list->hash! lst hash
getkey getval
hashref hashset
list-duplicates?)
;; Takes a list of the form (item item item item) and returns a hash
;; formed by traversing the list, and getting the key and val from
;; each item using the supplied get-key and get-val functions, and
;; building a hash table from the result using the given hashref and
;; hashset functions. list-duplicates? determines whether or not in
;; the resulting hash, the value for a given key is a list of all
;; the values associated with that key in the input or just the
;; first one encountered.
(define (handle-item item)
(let* ((key (getkey item))
(val (getval item))
(existing-val (hashref hash key)))
(if (not list-duplicates?)
;; ignore if not first value.
(if (not existing-val) (hashset hash key val))
;; else result is list.
(if existing-val
(hashset hash key (cons val existing-val))
(hashset hash key (list val))))))
(for-each handle-item lst)
hash)
(define (yahoo-get-historical-quotes symbol
start-year start-month start-day
end-year end-month end-day)
;; symbol must be a string naming the item of interest
;; (i.e. "LNUX"), and all the other arguments must be integers.
;; Abbreviated 2 digit years are not allowed, and months and days
;; are numbered starting with 1.
;;
;; This function returns a list of alists containing the quote data,
;; or #f on failure. Each alist will look like this:
;;
;; ((date . "21-Dec-1999")
;; (open . 108.5)
;; (high . 110.12)
;; (low . 108.06)
;; (close . 110.12)
;; (volume . 4775500))
;;
;; Note that the dates are left as strings, but the years will
;; always be 4 digits. The dates are left as strings for now
;; because without additional timezone information, it is impossible
;; to perform the conversion to UTC here. Further, it's not at all
;; clear which UTC instant each price should represent...
;;
;; NOTE: right now, we can't handle dates before 1970. That's
;; because to properly handle the fact that yahoo returns lame-ass 2
;; digit years, we need to be able to compare the dates it returns
;; using (+ year 1900) and (+ year 2000) to see which one is within
;; the star-end range requested (at least that's one of the easiest
;; ways to handle the problem). The most straightforward way to do
;; this is via mktime, but it can't handle anything before the
;; epoch. However, I believe SRFI-19 can
;; (http://srfi.schemers.org/srfi-19/srfi-19.html), so as soon as we
;; have a working implementation in guile, we can switch to that and
;; handle essentially arbitrary ranges.
;;
;; For now we'll leave in the mktime based conversion code
;; (commented out) so it'll be easy to switch later, but we'll
;; actually use a simpler (and more broken) approach -- any 2 digit
;; date >= 70 gets 1900 added, and any 2 digit date < 70 gets 2000
;; added.
; (define (str->month month-str)
; (cond
; ((string-ci=? "Jan" month-str) 1)
; ((string-ci=? "Feb" month-str) 2)
; ((string-ci=? "Mar" month-str) 3)
; ((string-ci=? "Apr" month-str) 4)
; ((string-ci=? "May" month-str) 5)
; ((string-ci=? "Jun" month-str) 6)
; ((string-ci=? "Jul" month-str) 7)
; ((string-ci=? "Aug" month-str) 8)
; ((string-ci=? "Sep" month-str) 9)
; ((string-ci=? "Oct" month-str) 10)
; ((string-ci=? "Nov" month-str) 11)
; ((string-ci=? "Dec" month-str) 12)
; (else #f)))
; (define (ymd->secs year month day)
; (let ((timevec (localtime 0)))
; (display (list 'foo year month day)) (newline)
; (set-tm:sec timevec 59)
; (set-tm:min timevec 59)
; (set-tm:hour timevec 23)
; (set-tm:mday timevec day)
; (set-tm:mon timevec (- month 1))
; (set-tm:year timevec (- year 1900))
; (set-tm:wday timevec -1)
; (set-tm:yday timevec -1)
; (set-tm:isdst timevec -1)
; (display timevec) (newline)
; (car (mktime timevec))))
; (define (fix-lame-ass-date-if-needed! quote)
; (let* ((date-str (vector-ref quote 0))
; (date-list (and date-str (string-split date-str #\-)))
; (year-str (and date-list (caddr date-list))))
; (if (= (string-length year-str) 2)
; (let* ((day (string->number (car date-list)))
; (month (str->month (cadr date-list)))
; (year (string->number year-str))
; (start-secs (ymd->secs start-year start-month start-day))
; (end-secs (ymd->secs end-year end-month end-day))
; (guess-1900 (ymd->secs (+ year 1900) month day)))
; (if (and (>= guess-1900 start-secs)
; (<= guess-1900 end-secs))
; (vector-set! quote 0 (string-append (car date-list) "-"
; (cadr date-list) "-"
; (number->string
; (+ year 1900))))
; (let ((guess-2000 (ymd->secs (+ year 2000) month day)))
; (if (and (>= guess-2000 start-secs)
; (<= guess-2000 end-secs))
; (vector-set! quote 0 (string-append (car date-list) "-"
; (cadr date-list) "-"
; (number->string
; (+ year 2000))))
; (vector-set! quote 0 #f))))))))
(define (fix-lame-ass-date-if-needed! quote-data)
(let* ((date-str (assq-ref quote-data 'date))
(date-list (and date-str (string-split date-str #\-)))
(year-str (and date-list (caddr date-list))))
(if (= (string-length year-str) 2)
(let* ((day (car date-list))
(month (cadr date-list))
(year (string->number year-str)))
(assq-set!
quote-data
'date
(string-append (car date-list) "-"
(cadr date-list) "-"
(number->string
(+ year (if (>= year 70)
1900
2000)))))))))
(define (quote-line->quote-alist line)
(let ((fields (string-split line #\,)))
(cond
((= 6 (length fields))
(map
(lambda (name value) (cons name value))
'(date open high low close volume)
(cons (car fields) (map string->number (cdr fields)))))
((zero? (string-length line))
'ignore)
(else
#f))))
(define (csv-str->quotes str)
(let ((lines (string-split str #\newline)))
(if (string=? (car lines) "Date,Open,High,Low,Close,Volume")
(let ((result (map quote-line->quote-alist (cdr lines))))
(if (any not result)
#f
(begin
(set! result
(filter (lambda (x) (not (eq? 'ignore x))) result))
(for-each fix-lame-ass-date-if-needed! result)
result)))
#f)))
(if (< start-year 1970)
#f
(let* ((request (string-append
"http://chart.yahoo.com/table.csv?"
"s=" symbol
"&a=" (number->string start-month)
"&b=" (number->string start-day)
"&c=" (number->string start-year)
"&d=" (number->string end-month)
"&e=" (number->string end-day)
"&f=" (number->string end-year)
"&g=d&q=q&y=0"))
(result (www:get request)))
(if result
(or (csv-str->quotes result)
result)
#f))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(define gnc:*finance-quote-check*
(g-find-program-in-path "gnc-fq-check"))
(define (gnc:fq-check-sources)
(let ((program #f))
(define (start-program)
(set! program (gnc:run-sub-process #f
gnc:*finance-quote-check*
gnc:*finance-quote-check*)))
(define (get-sources)
(and program
(let ((from-child (cadr program))
(results #f))
(catch
#t
(lambda ()
(set! results (read from-child))
(gnc:debug (list 'results results))
results)
(lambda (key . args)
key)))))
(define (kill-program)
(and program
(let ((pid (car program)))
(close-input-port (cadr program))
(close-output-port (caddr program))
(gnc:cleanup-sub-process (car program) 1))))
(dynamic-wind
start-program
get-sources
kill-program)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;; Finance::Quote based instantaneous quotes -- used by the
;; --add-price-quotes command line option, etc.
;;
;; Note From: Dave Peticolas <dave@krondo.com> Date: Sun, 01 Apr 2001
;; Those aren't pricedb functions, those are online quote functions,
;; i.e., low-level functions for getting online-quotes and putting
;; them into the price db. Reports should not be using those
;; functions, they should be using the price db. See
;; src/engine/gnc-pricedb.h
(define gnc:*finance-quote-helper*
(g-find-program-in-path "gnc-fq-helper"))
(define (gnc:fq-get-quotes requests)
;; requests should be a list where each item is of the form
;;
;; (<fq-method> sym sym ...)
;;
;; i.e. (yahoo "RHAT" "LNUX" "IBM")
;;
;; for currencies, we have
;;
;; (currency "USD" "AUD") for the conversion from USD to AUD,
;; i.e., the price of USD in AUD.
;;
;; This function will return #f on catastrophic failure or a list
;; where, for each element in requests, the output list will contain
;; a quote-result element. This element will be #f or an error
;; symbol if the corresponding method call fails, or a list
;; otherwise. A quote-result list will contain the symbol
;; representing the item being quoted, followed by an alist
;; detailing the quote data from gnc-fq-helper.
;;
;; Possible error symbols and their meanings are:
;; missing-lib One of the required perl libs is missing
;;
;; So for the example method call above, the resulting item in the
;; output list might look like this:
;;
;; (("RHAT" (symbol . "RHAT") (gnc:time-no-zone . "...")
;; (last . 6.59375) (currency . "USD"))
;; ("LNUX" (symbol . "LNUX") (gnc:time-no-zone . "...")
;; (last . 3.5) (currency . "USD"))
;; ("IBM" (symbol . "IBM") (gnc:time-no-zone . "...")
;; (last . 104.42) (currency . "USD")))
;;
;; Also note that any given value in the alist might be
;; 'failed-conversion if the Finance::Quote result for that field
;; was unparsable. See the gnc-fq-helper for more details
;; about it's output.
(let ((quoter #f))
(define (start-quoter)
(set! quoter (gnc:run-sub-process #f
gnc:*finance-quote-helper*
gnc:*finance-quote-helper*)))
(define (get-quotes)
(and quoter
(let ((to-child (caddr quoter))
(from-child (cadr quoter))
(results #f))
(map
(lambda (request)
(catch
#t
(lambda ()
(gnc:debug (list 'handling-request request))
;; we need to display the first element (the method, so it
;; won't be quoted) and then write the rest
(display #\( to-child)
(display (car request) to-child)
(display " " to-child)
(for-each (lambda (x) (write x to-child)) (cdr request))
(display #\) to-child)
(newline to-child)
(force-output to-child)
(set! results (read from-child))
(gnc:debug (list 'results results))
results)
(lambda (key . args)
key)))
requests))))
(define (kill-quoter)
(and quoter
(let ((pid (car quoter)))
(close-input-port (cadr quoter))
(close-output-port (caddr quoter))
(gnc:cleanup-sub-process (car quoter) 1))))
(dynamic-wind
start-quoter
get-quotes
kill-quoter)))
(define (gnc:book-add-quotes window book)
(define (book->commodity->fq-call-data book)
;; Call helper that walks all of the defined commodities to see if
;; any are marked for quote retrieval. This function returns a
;; list of info needed for the relevant Finance::Quote calls, and
;; a list of the corresponding commodities. Also perform a bit of
;; optimization, merging calls for symbols to the same
;; Finance::Quote method.
;;
;; Returns a list of the info needed for a set of calls to
;; gnc-fq-helper. Each item will of the list will be of the
;; form:
;;
;; (("yahoo" (commodity-1 currency-1 tz-1)
;; (commodity-2 currency-2 tz-2) ...)
;; ("fidelity_direct" (commodity-3 currency-3 tz-3)
;; (commodity-4 currency-4 tz-4) ...)
;; ...)
(let* ((ct (gnc-commodity-table-get-table book))
(big-list
(gnc-commodity-table-get-quotable-commodities
ct))
(commodity-list #f)
(currency-list (filter
(lambda (a) (not (equal? (cadr a) (caddr a))))
(call-with-values
(lambda () (partition!
(lambda (cmd)
(not (string=? (car cmd) "currency")))
big-list))
(lambda (a b) (set! commodity-list a) b))))
(quote-hash (make-hash-table 31)))
(if (and (null? commodity-list) (null? currency-list))
#f
(begin
;; Now collect symbols going to the same backend.
(item-list->hash! commodity-list quote-hash car cdr hash-ref hash-set! #t)
;; Now translate to just what gnc-fq-helper expects.
(append
(hash-fold
(lambda (key value prior-result)
(cons (cons key value)
prior-result))
'()
quote-hash)
(map (lambda (cmd) (cons (car cmd) (list (cdr cmd))))
currency-list))))))
(define (fq-call-data->fq-calls fq-call-data)
;; take an output element from book->commodity->fq-call-data and
;; return a list where the gnc_commodities have been converted to
;; their fq-suitable symbol strings. i.e. turn the former into
;; the latter:
;;
;; ("yahoo" (commodity-1 currency-1 tz-1)
;; (commodity-2 currency-2 tz-2) ...)
;;
;; ("yahoo" "IBM" "AMD" ...)
;;
(if (equal? (car fq-call-data) "currency")
(map (lambda (quote-item-info)
(list (car fq-call-data)
(gnc-commodity-get-mnemonic (car quote-item-info))
(gnc-commodity-get-mnemonic (cadr quote-item-info))))
(cdr fq-call-data))
(list
(cons (car fq-call-data)
(map
(lambda (quote-item-info)
(gnc-commodity-get-mnemonic (car quote-item-info)))
(cdr fq-call-data))))))
(define (fq-results->commod-tz-quote-triples fq-call-data fq-results)
;; Change output of gnc:fq-get-quotes to a list of (commod
;; timezone quote) triples using the matching commodity items from
;; fq-call-data.
;;
;; This function presumes that fq-call-data is "correct" -- it
;; contains the correct number of calls, and has the commodity
;; pointers in all the right places. If not, then the results of
;; this function are undefined.
;;
;; If there's a catatstrophic error, this function might return
;; #f. If there's an error for any given input element, there
;; will be a pair like this in the output (#f . <commodity>)
;; indicating the commodity for which the quote failed.
;;
;; If this function doesn't return #f, it will return a list with
;; as many elements as there were commodities in the fq-call-data.
;;
;; We might want more sophisticated error handling later, but this
;; will do for now .
(let ((result-list '()))
(define (process-a-quote call-data call-result)
;; data -> (commod-1 currency-1 tz-1)
;; result -> (commod-1-sym . result-alist) or some kind of garbage.
(if (and (list? call-result)
(not (null? call-result))
(list? (cdr call-result))
(every
(lambda (alist-item)
(and (pair? alist-item)
(not (eq? 'failed-conversion (cdr alist-item)))))
(cdr call-result)))
;; OK, data is good (as far as we can tell.
(set! result-list
(cons (list (car call-data)
(caddr call-data)
(cdr call-result))
result-list))
(set! result-list
(cons (cons #f (car call-data))
result-list))))
(define (process-call-result-pair call-data call-result)
(if (and (list? call-result)
(= (length call-data) (+ 1 (length call-result))))
;; OK, continue.
(for-each process-a-quote (cdr call-data) call-result)
;; else badly formed result, must assume all garbage.
(for-each
(lambda (call-item)
(set! result-list (cons (cons #f (car call-item)) result-list)))
(cdr call-data))))
(and (list? fq-call-data)
(list? fq-results)
(= (length fq-call-data) (length fq-results))
(begin
(for-each process-call-result-pair
fq-call-data
fq-results)
(reverse result-list)))))
(define (timestr->time-pair timestr time-zone)
(let ((broken-down (strptime "%Y-%m-%d %H:%M:%S" timestr)))
(if (not (= (string-length timestr) (cdr broken-down)))
#f
(cons (car (if time-zone
(mktime (car broken-down) time-zone)
(mktime (car broken-down))))
0))))
(define (commodity-tz-quote-triple->price book c-tz-quote-triple)
;; return a string like "NASDAQ:CSCO" on error, or a price on
;; success. Presume that F::Q currencies are ISO4217 currencies.
(let* ((commodity (first c-tz-quote-triple))
(time-zone (second c-tz-quote-triple))
(quote-data (third c-tz-quote-triple))
(gnc-time (assq-ref quote-data 'gnc:time-no-zone))
(price #f)
(price-type #f)
(currency-str (assq-ref quote-data 'currency))
(commodity-table (gnc-commodity-table-get-table book))
(currency
(and commodity-table
(string? currency-str)
(gnc-commodity-table-lookup commodity-table
"ISO4217"
(string-upcase currency-str)))))
(or-map (lambda (price-sym)
(let ((p (assq-ref quote-data price-sym)))
(if p
(begin (set! price p)
(set! price-type price-sym)
#t)
#f)))
'(last nav price))
(set! price-type
(case price-type
((last) "last")
((nav) "nav")
((price) "unknown")
(else #f)))
;; FIXME: SIGFIGS is not what we want here...
(if price
(set! price
(double-to-gnc-numeric price
GNC-DENOM-AUTO
(logior (GNC-DENOM-SIGFIGS 9)
GNC-RND-ROUND))))
(if gnc-time
(set! gnc-time (timestr->time-pair gnc-time time-zone))
(set! gnc-time (gnc:get-today)))
(if (not (and commodity currency gnc-time price price-type))
(string-append
currency-str ":" (gnc-commodity-get-mnemonic commodity))
(let ((gnc-price (gnc-price-create book)))
(if (not gnc-price)
(string-append
currency-str ":" (gnc-commodity-get-mnemonic commodity))
(begin
(gnc-price-set-commodity gnc-price commodity)
(gnc-price-set-currency gnc-price currency)
(gnc-price-set-time gnc-price gnc-time)
(gnc-price-set-source gnc-price "Finance::Quote")
(gnc-price-set-type gnc-price price-type)
(gnc-price-set-value gnc-price price)
gnc-price))))))
(define (book-add-prices! book prices)
(let ((pricedb (gnc-pricedb-get-db book)))
(for-each
(lambda (price)
(gnc-pricedb-add-price pricedb price)
(gnc-price-unref price))
prices)))
;; FIXME: uses of gnc:warn in here need to be cleaned up. Right
;; now, they'll result in funny formatting.
(let* ((group (xaccGetAccountGroup book))
(fq-call-data (book->commodity->fq-call-data book))
(fq-calls (and fq-call-data
(apply append
(map fq-call-data->fq-calls fq-call-data))))
(fq-results (and fq-calls (gnc:fq-get-quotes fq-calls)))
(commod-tz-quote-triples
(and fq-results (list? (car fq-results))
(fq-results->commod-tz-quote-triples fq-call-data fq-results)))
;; At this point commod-tz-quote-triples will either be #f or a
;; list of items. Each item will either be (commodity
;; timezone quote-data) or (#f . problem-commodity)
(problem-syms
(and commod-tz-quote-triples
(filter-map (lambda (cq-pair)
(if (car cq-pair)
#f
(string-append
(gnc-commodity-get-namespace (cdr cq-pair))
":"
(gnc-commodity-get-mnemonic (cdr cq-pair)))))
commod-tz-quote-triples)))
;; strip out the "bad" ones from above.
(ok-syms
(and commod-tz-quote-triples
(filter car commod-tz-quote-triples)))
(keep-going? #t))
(cond
((eq? fq-call-data #f)
(set! keep-going? #f)
(if (gnucash-ui-is-running)
(gnc-error-dialog window (_ "No commodities marked for quote retrieval."))
(gnc:warn (_ "No commodities marked for quote retrieval."))))
((eq? fq-results #f)
(set! keep-going? #f)
(if (gnucash-ui-is-running)
(gnc-error-dialog window (_ "Unable to get quotes or diagnose the problem."))
(gnc:warn (_ "Unable to get quotes or diagnose the problem."))))
((member 'missing-lib fq-results)
(set! keep-going? #f)
(if (gnucash-ui-is-running)
(gnc-error-dialog window
(_ "You are missing some needed Perl libraries.
Run 'gnc-fq-update' as root to install them."))
(gnc:warn (_ "You are missing some needed Perl libraries.
Run 'gnc-fq-update' as root to install them.") "\n")))
((member 'system-error fq-results)
(set! keep-going? #f)
(if (gnucash-ui-is-running)
(gnc-error-dialog window
(_ "There was a system error while retrieving the price quotes."))
(gnc:warn (_ "There was a system error while retrieving the price quotes.") "\n")))
((not (list? (car fq-results)))
(set! keep-going? #f)
(if (gnucash-ui-is-running)
(gnc-error-dialog window
(_ "There was an unknown error while retrieving the price quotes."))
(gnc:warn (_ "There was an unknown error while retrieving the price quotes.") "\n")))
((and (not commod-tz-quote-triples) (gnucash-ui-is-running))
(gnc-error-dialog window
(_ "Unable to get quotes or diagnose the problem."))
(set! keep-going? #f))
((not commod-tz-quote-triples)
(gnc:warn (_ "Unable to get quotes or diagnose the problem."))
(set! keep-going? #f))
((not (null? problem-syms))
(if (gnucash-ui-is-running)
(if (and ok-syms (not (null? ok-syms)))
(set!
keep-going?
(gnc-verify-dialog window #t
(call-with-output-string
(lambda (p)
(display (_ "Unable to retrieve quotes for these items:") p)
(newline p)
(display " " p)
(display (string-join problem-syms "\n ") p)
(newline p)
(display (_ "Continue using only the good quotes?") p)))))
(begin
(gnc-error-dialog window
(call-with-output-string
(lambda (p)
(display
(_ "Unable to retrieve quotes for these items:") p)
(newline p)
(display " " p)
(display (string-join problem-syms "\n ") p))))
(set! keep-going? #f)))
(gnc:warn
(call-with-output-string
(lambda (p)
(display (_ "Unable to retrieve quotes for these items:") p)
(newline p)
(display " " p)
(display (string-join problem-syms "\n ") p)
(newline p)
(display (_ "Continuing with good quotes.") p)
(newline p)))))))
(if
keep-going?
(let ((prices (map (lambda (triple)
(commodity-tz-quote-triple->price book triple))
ok-syms)))
(if (any string? prices)
(if (gnucash-ui-is-running)
(set!
keep-going?
(gnc-verify-dialog window #t
(call-with-output-string
(lambda (p)
(display (_ "Unable to create prices for these items:") p)
(newline p)
(display " " p)
(display (string-join (filter string? prices) "\n ") p)
(newline p)
(display (_ "Add remaining good quotes?") p)))))
(gnc:warn
(call-with-output-string
(lambda (p)
(display (_ "Unable to create prices for these items:") p)
(newline p)
(display " " p)
(display (string-join (filter string? prices) "\n ") p)
(newline p)
(display (_ "Adding remaining good quotes.") p)
(newline p))))))
(if keep-going?
(book-add-prices! book (filter
(lambda (x) (not (string? x)))
prices)))))))
; (define (get-1-quote exchange . items)
; (let ((cmd (apply list 'fetch exchange items))
; (quoter (run-sub-process #f
; gnc:*finance-quote-helper*
; gnc:*finance-quote-helper*)))
; (and quoter
; (write cmd (caddr quoter))
; (newline (caddr quoter))
; (force-output (caddr quoter))
; (let ((result (read (cadr quoter))))
; (close-input-port (cadr quoter))
; (close-output-port (caddr quoter))
; result))))
(define (gnc:price-quotes-install-sources)
(let ((sources (gnc:fq-check-sources)))
(if (list? sources)
(begin
(gnc:msg "Found Finance::Quote version " (car sources))
(gnc-quote-source-set-fq-installed (cdr sources))))))