Richard said:
Show us a complete program that has the problem.
Hi Richard,
Here's the complete program. Thank you.
(This is the problematic module - any of the values passed to this
function apart from steps, returns a very odd number)
#include "stdafx.h"
#include "binomialtree.h"
double treeNPV(double s, double k, double r, double v, double t, int steps)
{
double time_step, up, dn, rt, p, npv;
time_step = t / (double)(steps);
rt = exp(r*time_step);
up = exp(v*sqrt(time_step));
dn = 1/up;
p = (rt-dn)/(up-dn);
npv = 0.0;
//for (int i=0; i<=steps; i++)
npv = v;
return npv;
}
Main modules:
#include "stdafx.h"
#include "binomialtree.h"
int main(void)
{
double s, k, r, v, t;
double call_npv;
int steps;
printf("Enter asset price S: ");
scanf_s("%f", &s);
printf("Enter strike price K: ");
scanf_s("%f", &k);
printf("Enter riskfree rate R: ");
scanf_s("%f", &r);
printf("Enter volatility V: ");
scanf_s("%f", &v);
printf("Enter time to expiry T: ");
scanf_s("%f", &t);
printf("Enter number of steps: ");
scanf_s("%d", &steps);
call_npv = treeNPV(s,k,r,v,t,steps);
printf("Call option NPV is: %5.5f", call_npv);
scanf_s("%d", &steps);
return 0;
}
Header files:
// stdafx.h : include file for standard system include files,
#pragma once
#define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows
headers
#include <stdio.h>
#include <tchar.h>
// binomialtree.h
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define pi 3.141592653589793238
double treeNPV(double s, double k, double r, double v, double t, int steps);