Question: In this exercise, you will create a function that takes two currency codes and converts them to a ticker suitable for downloading from yfinance. code:

In this exercise, you will create a function that takes two currency codes and converts them to a ticker suitable for downloading from yfinance.
code: The currency ISO code (e.g., USD = US Dollar, AUD = Australian Dollar). You can find a list of currency codes here.
fx: The exchange rate, i.e. the value of one currency in terms of another currency. We will represent an exchange rate like this:
AUD/USD : The price in US dollars of one Australian dollar
USD/AUD : The price in Australian dollars of one US dollar
AAA/BBB : The price in currency BBB of one unit of currency AAA
from_cur: The currency which we want to price (AAA in the example above)
to_cur: The price of one unit of the from_cur (BBB in the example above)
Yahoo finance uses the following naming rules to define the ticker of the exchange rate AAA/BBB (the price of one unit of AAA in terms of currency BBB):
1. If AAA is the USD, then the ticker is "BBB=X", i.e., the second currency code with "=X" added at the end.
2. If AAA is not the USD, then the ticker is "AAABBB=X"
For example, the ticker for AUD/USD is "AUDUSD=X", while the ticker for USD/AUD is "AUD=X"
The scaffold provides you with a declaration defining the function's arguments and a docstring that describes what the function should do. You need to fill in the body of the function. Note that a function called get_fx is provided as a reference. Once your fx_code function passes the diagnostic tests, you should be able to copy the entire file to an environment with yfinance installed (e.g., PyCharm) and use this program to download currency data.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!