5
$\begingroup$

Let's say I want to know the 5 year swap rate and the 10 year swap rate right now, is there a way to get this with FinancialData[]?

$\endgroup$
1

2 Answers 2

3
$\begingroup$

This is my first time using URLFetch so there is probably a cleaner method of doing this.

urlFetch10 = URLFetch["https://ycharts.com/indicators/10_year_swap_rate",
   "Content"];

tenYrSwapRatePercent = StringSplit[StringDrop[urlFetch10, 
     StringPosition[urlFetch10, "10 Year Swap Rate is at "][[1, -1]]], 
    "%"][[1]] // ToExpression

2.25

tenYrSwapRate = tenYrSwapRatePercent/100

0.0225

urlFetch5 = URLFetch["https://ycharts.com/indicators/5_year_swap_rate",
   "Content"];

fiveYrSwapRatePercent = StringSplit[StringDrop[urlFetch5, 
     StringPosition[urlFetch5, "5 Year Swap Rate is at "][[1, -1]]], 
    "%"][[1]] // ToExpression

1.69

fiveYrSwapRate = fiveYrSwapRatePercent/100

0.0169

$\endgroup$
2
$\begingroup$

Finding no solution directly in FinancialData[], I decided to pull the yield curve from the U.S. Treasury.

urlIn = URLFetch[
   "http://www.treasury.gov/resource-center/data-chart-center/\
interest-rates/Pages/TextView.aspx?data=yield"];
lastFiveDays = 
  Table[DateString[
    DatePlus[Take[DateList[], 3], i], {"Month", "/", "Day", "/", 
     "YearShort"}], {i, 0, -5, -1}];
urlSplits = Map[{#, StringSplit[urlIn, #]} &, lastFiveDays];
mostRecentCurve = 
  Map[{#[[1]], StringSplit[#[[2]][[2]], "</table>"][[1]]} &, 
    Select[urlSplits, Length[#[[2]]] == 2 &]][[1]];
mostRecentCurveNumbers = 
  StringSplit[mostRecentCurve[[2]], {">", "<"}];

dateList = Map[DateObject[
 Take[DateList[
   DatePlus[
    DateList[{mostRecentCurve[[1]], {"Month", "Day", 
       "YearShort"}}], #]], 3]] &, {{1, "Month"}, {3, 
 "Month"}, {6, "Month"}, {1, "Month"}, {2, "Year"}, {3, 
 "Year"}, {5, "Year"}, {7, "Year"}, {10, "Year"}, {20, 
 "Year"}, {30, "Year"}}];
Off[Set::write];
numbersList = 
  Select[Map[
    If[SyntaxQ[#] && NumberQ[ToExpression[#]], ToExpression[#], 
      Null] &, mostRecentCurveNumbers], NumberQ];
On[Set::write];
yieldCurveTS = TimeSeries[Transpose[{dateList, numbersList}]]

This gives a TimeSeries object with the most recent USD yield curve. One of the nice things about this is that you can specify any future date with, for example yieldCurveTS[{2022, 4, 2}] and you will get back an appropriately interpolated answer.

A guru at string parsing would probably be able to extract the most recent data more elegantly than I do above, but it works.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.