Posted by: Zeeshan Amjad | August 30, 2012

Yield to Maturity Curve with Mindscape


Last time I created an amortization graph with the chart of mindscape library. This time I was thinking to use their line graph. I again used the financial domain for my demo application. I selected the Yield to Maturity. Please not that this is not Yield curve that is related but different topic.

Here is a basic idea of Yield to Maturity. Suppose we purchased one bound and for simplicity we are working on Zero Coupon Bound. In Zero Coupon Bound there is no coupon or in term payments, we will get payment only at the end of the maturity period. Now if the face value of that bound is $1100, its maturity period is 1 year and its interest rate is 10% then we are going to pay $1000 now. This is because after 1 year there is going to be $100 interest with 10% interest rate and we will get $1100.

Now let’s change a question little bit. Now my question is if the face value of the bound is $1100, maturity period is 1 year and today’s value is $100 then what is the interest rate. In one year period it is quite easy to calculate. If the period is more than one year then we can calculate it with this formula.

YTM

If we calculate the YTM for different time period then its value decrease for the longer time period and it perfectly make sense. Let’s take a look an example. Suppose we bought a bound of $1000 face value with current prize of $800. Now if it is one year bound then its interest rate should be higher to give me enough money that I will be able to get $1000 after a year. Now if the same bound’s maturity is 5 year then I have longer time period to reach $1000. It means there will be a lower interest rate for this bound as compare to the same bound with maturity period of 1 year.

Let’s make a simple program to display the YTM graphically for 30 years of given current and face value of the bound.

First we make a class to store the YTM information. Here is our class.

Code Snippet
public class YTMData : INotifyPropertyChanged
{
    private double _currentValue;
    private double _faceValue;
    private ObservableCollection<double> _interestRates;

    public YTMData()
    {
        _interestRates = new ObservableCollection<double>();
    }

    public double CurrentValue
    {
        get
        {
            return _currentValue;
        }
        set
        {
            _currentValue = value;
            RaisePropertyChanged("CurrentValue");
        }
    }

    public double FaceValue
    {
        get
        {
            return _faceValue;
        }
        set
        {
            _faceValue = value;
            RaisePropertyChanged("FaceValue");
        }
    }

    public ObservableCollection<double> InterestRates
    {
        get
        {
            return _interestRates;
        }
        set
        {
            _interestRates = value;
            RaisePropertyChanged("InterestRates");
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    private void RaisePropertyChanged(string propertyName)
    {
        PropertyChangedEventHandler handler = this.PropertyChanged;

        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

 

Note that we created an ObservableCollection property to store the YTM for different year. Here is a simple method to calculate YTM for different 30 years.

Code Snippet
double ratio = ytmData.FaceValue / ytmData.CurrentValue;

for (int i = 2; i <= 30; i++)
{
    double rate = (Math.Pow(ratio, (double)1/i) – 1) * 100;
    ytmData.InterestRates.Add(rate);
}

 

Now let’s come to the charting part of Mindscape. Like Bar graph, creating line graph is very easy and similar. This time I used one more feature of the chart to not only display the X-Axis and Y-Axis title, but also display the Ticking on the axes. Here is a code to make a line graph.

Code Snippet
<ms:Chart Grid.Column="0" Grid.ColumnSpan="2" Grid.Row="2" Margin="5" Title="Year to Maturity Curve">
    <ms:Chart.XAxis>
        <ms:ChartAxis Title="Years" MajorTickSpacing="1"/>
    </ms:Chart.XAxis>
    <ms:Chart.YAxis>
        <ms:ChartAxis Title="Interest Rate" MajorTickSpacing="1"/>
    </ms:Chart.YAxis>
    <ms:LineSeries ItemsSource="{Binding InterestRates}" Title="Rates"/>
</ms:Chart>

 

Rest of the program and code is very straight forward. Here is a complete C# code of the program.

Code Snippet
using System;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Windows;

namespace YTMCurve
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private YTMData ytmData = new YTMData();

        public MainWindow()
        {
            InitializeComponent();

            DataContext = ytmData;
        }

        private void btnClose_Click(object sender, RoutedEventArgs e)
        {
            Close();
        }

        private void btnCalculate_Click(object sender, RoutedEventArgs e)
        {
            ytmData.InterestRates.Clear();

            double ratio = ytmData.FaceValue / ytmData.CurrentValue;

            for (int i = 2; i <= 30; i++)
            {
                double rate = (Math.Pow(ratio, (double)1/i) – 1) * 100;
                ytmData.InterestRates.Add(rate);
            }
        }
    }

    public class YTMData : INotifyPropertyChanged
    {
        private double _currentValue;
        private double _faceValue;
        private ObservableCollection<double> _interestRates;

        public YTMData()
        {
            _interestRates = new ObservableCollection<double>();
        }

        public double CurrentValue
        {
            get
            {
                return _currentValue;
            }
            set
            {
                _currentValue = value;
                RaisePropertyChanged("CurrentValue");
            }
        }

        public double FaceValue
        {
            get
            {
                return _faceValue;
            }
            set
            {
                _faceValue = value;
                RaisePropertyChanged("FaceValue");
            }
        }

        public ObservableCollection<double> InterestRates
        {
            get
            {
                return _interestRates;
            }
            set
            {
                _interestRates = value;
                RaisePropertyChanged("InterestRates");
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        private void RaisePropertyChanged(string propertyName)
        {
            PropertyChangedEventHandler handler = this.PropertyChanged;

            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }
}

 

And here is complete XAML code of our program.

Code Snippet
<Window x:Class="YTMCurve.MainWindow"
       xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation&quot;
       xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml&quot;
       xmlns:ms="clr-namespace:Mindscape.WpfElements.Charting;assembly=Mindscape.WpfElements"
       Title="Year to Matuarity rate" Height="400" Width="500">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition/>
                        <RowDefinition/>
            <RowDefinition Height="6*"/>
            <RowDefinition/>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition/>
            <ColumnDefinition/>
        </Grid.ColumnDefinitions>
        <TextBlock Grid.Column="0" Grid.Row="0" Margin="5" VerticalAlignment="Center">Current Price</TextBlock>
        <TextBox Grid.Column="1" Grid.Row="0" Margin="5" VerticalAlignment="Center" Text="{Binding CurrentValue}"/>
        <TextBlock Grid.Column="0" Grid.Row="1" Margin="5" VerticalAlignment="Center">Face Value</TextBlock>
        <TextBox Grid.Column="1" Grid.Row="1" Margin="5" VerticalAlignment="Center" Text="{Binding FaceValue}"/>
        <ms:Chart Grid.Column="0" Grid.ColumnSpan="2" Grid.Row="2" Margin="5" Title="Year to Maturity Curve">
            <ms:Chart.XAxis>
                <ms:ChartAxis Title="Years" MajorTickSpacing="1"/>
            </ms:Chart.XAxis>
            <ms:Chart.YAxis>
                <ms:ChartAxis Title="Interest Rate" MajorTickSpacing="1"/>
            </ms:Chart.YAxis>
            <ms:LineSeries ItemsSource="{Binding InterestRates}" Title="Rates"/>
        </ms:Chart>
        <Button Grid.Column="0" Grid.Row="3" Margin="5" Width="85" Name="btnClose" Click="btnClose_Click">Close</Button>
        <Button Grid.Column="1" Grid.Row="3" Margin="5" Width="85" Name="btnCalculate" Click="btnCalculate_Click">Calculate</Button>
    </Grid>
</Window>

 

This is the output of the program.

YTM_Output

About these ads

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Categories

Follow

Get every new post delivered to your Inbox.

Join 512 other followers

%d bloggers like this: