Using Yahoo! Finance for Currency Exchange Rate Retrieval

Last modified by J.T. on Fri, June 25, 2010 09:55
Source|Old Revisions  

This is an old revision of the document!

In a bid to find a more reliable source for currency exchange rates and faster servers to boot, I’ve created this Module to get rates from Yahoo! Finance. Note that this may be subject to Yahoo! T’s & C’s so you better read them before using this.

I’ve made this upgrade-proof to the best of my knowledge; it uses local xml and Module code. But of course, do take back-ups and test your back-ups. This is supplied as is and is not implied to work at all. You know the drill.

Step 1 - Create Folders & File Structure

I have mimicked the Webservicex currency retrieval code. That one resides in /app/code/core/Mage/Directory/Model/Currency/Import/Webservicex.php so you can have a look yourself. In line with that code and structure, first create the following folder structure and file:


Step 2 - Paste Code into Yahoofinance.php

  1. <?php
  2. /**
  3. * JT
  4. *
  6. *
  7. * This source file is subject to the Open Software License (OSL 3.0)
  8. * that is bundled with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. *
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to so we can send you a copy immediately.
  14. *
  15. * @category   JT
  16. * @package    JT_Directory
  17. * @copyright  Copyright (c) 2009 JT
  18. * @license  Open Software License (OSL 3.0)
  19. *
  20. * All credits to Magento Core Development Team who's Webservicex code I liberally borrowed
  21. * as well as Mark Alexander Bain's idea and code as displayed on the following url, accessed Feb 16th 2009
  22. *
  23. */
  25. /**
  26. * Currency rate import model (From
  27. *
  28. * @category   JT
  29. * @package    JT_Directory
  30. * @author      Magento Enthusiast <J.T.>
  31. */
  32. class JT_Directory_Model_Currency_Import_Yahoofinance extends Mage_Directory_Model_Currency_Import_Abstract
  33. {
  34.     protected $_url = '{{CURRENCY_FROM}}{{CURRENCY_TO}}=X&f=l1&e=.csv';
  35.     protected $_messages = array();
  37.     protected function _convert($currencyFrom, $currencyTo, $retry=0)
  38.     {
  39.         $url = str_replace('{{CURRENCY_FROM}}', $currencyFrom, $this->_url);
  40.         $url = str_replace('{{CURRENCY_TO}}', $currencyTo, $url);
  42.         try {
  43.             sleep(1); //Be nice to Yahoo, they don't have a lot of hi-spec servers
  45.             $handle = fopen($url, "r");
  47.             $exchange_rate = fread($handle, 2000);
  49.             fclose($handle);
  51.             if( !$exchange_rate ) {
  52.                 $this->_messages[] = Mage::helper('directory')->__('Cannot retrieve rate from %s', $url);
  53.                 return null;
  54.             }
  55.             return (float) $exchange_rate * 1.0; // change 1.0 to influence rate;
  56.         }
  57.         catch (Exception $e) {
  58.             if( $retry == 0 ) {
  59.                 $this->_convert($currencyFrom, $currencyTo, 1);
  60.             } else {
  61.                 $this->_messages[] = Mage::helper('directory')->__('Cannot retrieve rate from %s', $url);
  62.             }
  63.         }
  64.     }
  65. }

Step 3 - Amend local.xml

That’s /app/etc/local.xml - add this after the <global> tag:

  1.         <!-- JT Yahoo Finance Integration-->
  2.         <currency>
  3.             <import>
  4.                 <services>
  5.                     <yahoofinance>
  6.                         <name>Yahoo Finance</name>
  7.                         <model>JT_Directory_Model_Currency_Import_Yahoofinance</model>
  8.                     </yahoofinance>
  9.                 </services>
  10.             </import>
  11.         </currency>
  12.         <!-- EOF JT Yahoo Finance Integration-->

Step 4 - Run it!

In your Magento Admin go to System > Manage Currency Rates

In the drop-down, Webservicex is still the top one so simply select Yahoo Finance from there and click the Import button.

Step 5 - Optional - Cron Setup

In Admin under System > Configuration go to Currency Setup under the Default Configuration scope. Enable the cron for Yahoo Finance here under Scheduled Import Settings (I’ve not yet tested this).

Important Notes

Just to not piss off Yahoo, there’s a sleep(1) one second interval built in. If you only use two or three currencies, you won’t even notice this delay. If like me you use 23 currencies, this may make it look slow but at least you’ll avoid potential auto-throttling and/or Yahoo! firewall banning by making requests look more “real”.

Yahoo! supplies these rates in csv files to the public. It’s up to you whether you ass-u-me that you can now use them in an automated fashion for commercial benefit or kindly request permission, or at least read the small print.

Advanced Usage

Note line 55 where the exchange rate is being returned. By default it’s returned as is, but some may want to influence this. For example, right now Yahoo! reckons one GBP is worth 1.1157 EUR. A 100 Pound item will be displayed as 115.70 Euro. When paying by bank transfer or credit card, they will be surprised when they read their bank statements as the bank’s rate will be more like 1.19 so they get deducted the grand sum of 119 Euro for that product. They may think you are falsely advertising the rates and pocketing the difference, even though your T’s & C’s will mention this possible discrepancy. So with this rate influencer ratio, you can dampen the fun a bit by using for example 1.085 instead of 1.0. Now the products will look more expensive but there won’t be any surprises on their bank statements.


If you think the implementation lacks or it can be better in a certain way, feel free to discuss here (click edit and amend this “discussion” part) and PM me to discuss. Or post in the “official” thread here: