📄 期权dlg.cpp
字号:
// 期权Dlg.cpp : implementation file
//
#include "stdafx.h"
#include "期权.h"
#include "期权Dlg.h"
#include "iostream.h"
#include "math.h"
#include "string.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
class CAboutDlg : public CDialog
{
public:
CAboutDlg();
// Dialog Data
//{{AFX_DATA(CAboutDlg)
enum { IDD = IDD_ABOUTBOX };
//}}AFX_DATA
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CAboutDlg)
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL
// Implementation
protected:
//{{AFX_MSG(CAboutDlg)
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
//{{AFX_DATA_INIT(CAboutDlg)
//}}AFX_DATA_INIT
}
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CAboutDlg)
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
//{{AFX_MSG_MAP(CAboutDlg)
// No message handlers
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CMyDlg dialog
CMyDlg::CMyDlg(CWnd* pParent /*=NULL*/)
: CDialog(CMyDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CMyDlg)
m_optiontime = 0.0;
m_n = 0;
m_currentprice = 0.0;
m_optionprice = 0.0;
m_rate = 0.0;
m_sigama = 0.0;
m_radio = -1;
m_radio2 = -1;
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CMyDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CMyDlg)
DDX_Control(pDX, IDC_LIST1, m_list);
DDX_Text(pDX, IDC_EDIT1, m_optiontime);
DDX_Text(pDX, IDC_EDIT2, m_n);
DDX_Text(pDX, IDC_EDIT3, m_currentprice);
DDX_Text(pDX, IDC_EDIT4, m_optionprice);
DDX_Text(pDX, IDC_EDIT5, m_rate);
DDX_Text(pDX, IDC_EDIT6, m_sigama);
DDX_Radio(pDX, IDC_RADIO1, m_radio);
DDX_Radio(pDX, IDC_RADIO5, m_radio2);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CMyDlg, CDialog)
//{{AFX_MSG_MAP(CMyDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_RADIO5, OnRadio5)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CMyDlg message handlers
BOOL CMyDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Add "About..." menu item to system menu.
// IDM_ABOUTBOX must be in the system command range.
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);
CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
CString strAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}
// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
// TODO: Add extra initialization here
m_list.SetExtendedStyle(LVS_EX_GRIDLINES);
counter=0;
m_radio=0;
m_radio2=0;
UpdateData(FALSE);
return TRUE; // return TRUE unless you set the focus to a control
}
void CMyDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialog::OnSysCommand(nID, lParam);
}
}
// If you add a minimize button to your dialog, you will need the code below
// to draw the icon. For MFC applications using the document/view model,
// this is automatically done for you by the framework.
void CMyDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting
SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
// Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;
// Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
}
}
// The system calls this to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CMyDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
///////////////////////////////////
double Max(double a,double b)
{
if(a > b) return a;
else
return b;
}
//////////////
double** Europeanup(int os,int n,double optiontime,double optionprice,double r,double sigama,double price)
{
double u,d,a,p,t;
double **option;
option=new double *[n+1];
for(int l=0;l<=n;l++)
option[l]=new double[n+1];
int i,j,m;
for(i=0;i<n+1;i++)
for(j=0;j<n+1;j++)
option[i][j]=0;
t=optiontime/12/n;
u=exp(sigama*pow(t,0.5));
d=1/u;
a=exp(r*t);
p=(a-d)/(u-d);
option[0][0] = price;
m = 2;
for(i=1;i<n+1;i++)
{
for(j=0;j<m;j++)
{
option[j][i] = option[j][i-1] * u;
if(j == m-1)
option[j][i]=option[j-1][i-1]*d;
}
m++;
}
if(os==1) return option;
m=n;
for(i=0;i<n+1;i++)
option[i][n]=Max((option[i][n])-optionprice,0);
for(i = n-1;i >= 0;i--)
{
for(j=0;j<m;j++)
option[j][i]=exp(-r*t)*(p*option[j][i+1]+(1-p)*option[j+1][i+1]);
m--;
}
return option;
}
//////////////////
double** Europeandown(int os,int n,double time,double optionprice,double r,double sigama,double price)
{
double **option;
option=new double *[n+1];
for(int l=0;l<=n;l++)
option[l]=new double[n+1];
int i,j,m;
for(i=0;i<n+1;i++)
for(j=0;j<n+1;j++)
option[i][j]=0;
double u,d,a,p,t;
t=time/12/n;
u=exp(sigama*pow(t,0.5));
d=1/u;
a=exp(r*t);
p=(a-d)/(u-d);
option[0][0] = price;
m = 2;
for(i=1;i<n+1;i++)
{
for(j=0;j<m;j++)
{
option[j][i] = option[j][i-1] * u;
if(j == m-1)
option[j][i]=option[j-1][i-1]*d;
}
m++;
}
if(os==1) return option;
m=n;
for(i=0;i<n+1;i++)
option[i][n]=Max((optionprice-option[i][n]),0);
for(i = n-1;i >= 0;i--)
{
for(j=0;j<m;j++)
option[j][i]=exp(-r*t)*(p*option[j][i+1]+(1-p)*option[j+1][i+1]);
m--;
}
return option;
}
///////////////////////
double** Americanup(int os,int n,double time,double optionprice,double r,double sigama,double price)
{
double **option;
option=new double *[n+1];
for(int l=0;l<=n;l++)
option[l]=new double[n+1];
int i,j,m;
for(i=0;i<n+1;i++)
for(j=0;j<n+1;j++)
option[i][j]=0;
double u,d,a,p,t;
t=time/12/n;
u=exp(sigama*pow(t,0.5));
d=1/u;
a=exp(r*t);
p=(a-d)/(u-d);
option[0][0] = price;
m = 2;
for(i=1;i<n+1;i++)
{
for(j=0;j<m;j++)
{
option[j][i] = option[j][i-1] * u;
if(j == m-1)
option[j][i]=option[j-1][i-1]*d;
}
m++;
}
if(os==1) return option;
m=n;
for(i=0;i<n+1;i++)
option[i][n]=Max((option[i][n])-optionprice,0);
for(i = n-1;i >= 0;i--)
{
for(j=0;j<m;j++)
option[j][i]=Max(exp(-r*t)*(p*option[j][i+1]+(1-p)*option[j+1][i+1]),(option[i][j]-price));
m--;
}
return option;
}
///////////////////////
double** Americandown(int os,int n,double time,double optionprice,double r,double sigama,double price)
{
double **option;
option=new double *[n+1];
for(int l=0;l<=n;l++)
option[l]=new double[n+1];
int i,j,m;
for(i=0;i<n+1;i++)
for(j=0;j<n+1;j++)
option[i][j]=0;
double u,d,a,p,t;
t=time/12/n;
u=exp(sigama*pow(t,0.5));
d=1/u;
a=exp(r*t);
p=(a-d)/(u-d);
option[0][0] = price;
m = 2;
for(i=1;i<n+1;i++)
{
for(j=0;j<m;j++)
{
option[j][i] = option[j][i-1] * u;
if(j == m-1)
option[j][i]=option[j-1][i-1]*d;
}
m++;
}
if(os==1) return option;
m=n;
for(i=0;i<n+1;i++)
option[i][n]=Max((optionprice-option[i][n]),0);
for(i = n-1;i >= 0;i--)
{
for(j=0;j<m;j++)
option[j][i]=Max(exp(-r*t)*(p*option[j][i+1]+(1-p)*option[j+1][i+1]),(price-option[j][i]));
m--;
}
return option;
}
//////////////////////////////////
void CMyDlg::OnOK()
{
// TODO: Add extra validation here
// CDialog::OnOK();
while(counter>=0)
{
m_list.DeleteColumn(counter);
counter--;
}
m_list.DeleteAllItems( );
UpdateData(TRUE);
int i,j=0;
double rate=m_rate/100,sigama=m_sigama/100;
if(m_n<=0)
{
i=MessageBox("Pieces must be bigger than 0 ","Input wrong number",MB_OK|MB_ICONWARNING);
if(i==IDOK) return;
}
if(m_optiontime<=0)
{
i=MessageBox("option time must be bigger than 0 ","Input wrong number",MB_OK|MB_ICONWARNING);
if(i==IDOK) return;
}
if(m_optionprice<=0)
{
i=MessageBox("option price must be bigger than 0 ","Input wrong number",MB_OK|MB_ICONWARNING);
if(i==IDOK) return;
}
if(m_rate<=0)
{
i=MessageBox("rate must be bigger than 0 ","Input wrong number",MB_OK|MB_ICONWARNING);
if(i==IDOK) return;
}
if(m_sigama<=0)
{
i=MessageBox("wave rate must be bigger than 0 ","Input wrong number",MB_OK|MB_ICONWARNING);
if(i==IDOK) return;
}
if(m_currentprice<=0)
{
i=MessageBox("current price must be bigger than 0 ","Input wrong number",MB_OK|MB_ICONWARNING);
if(i==IDOK) return;
}
counter=m_n+1;
int nItem=0;
int nSubItem=1;
CString a,b,c;
double **option;
switch(m_radio)
{
case 0: option=Europeanup(m_radio2,m_n, m_optiontime, m_optionprice , rate ,sigama , m_currentprice );
break;
case 1: option=Europeandown(m_radio2,m_n, m_optiontime, m_optionprice , rate ,sigama , m_currentprice );
break;
case 2: option=Americanup(m_radio2,m_n, m_optiontime, m_optionprice , rate ,sigama , m_currentprice );
break;
case 3: option=Americandown(m_radio2,m_n, m_optiontime, m_optionprice , rate ,sigama , m_currentprice );
break;
default: break;
}
for( i=0 ;i<=2*m_n+1;i++)
{
CString a,b;
double k=(i-1)*(m_optiontime)*30/m_n;
int k1=int(k);
int k2=int((k-k1)*10);
if(i!=0) a.Format("%d.%d天", k1,k2);
b.Format("%d", i+1);
if(i!=0&&i<=m_n+1)m_list.InsertColumn( i,a );
else if(i<=m_n+1)m_list.InsertColumn( i,"" );
m_list.SetColumnWidth( i, 70 );
if(i!=2*m_n+1)m_list.InsertItem(i, b );
}
/* for(i=0;i<m_n+1;i++)
{
for(j=0;j<=m_n+1;j++)
if(i>j)
{
m_list.SetItemText( nItem, nSubItem,"");
nSubItem++;
}
else
{
c.Format("%lf", option[i][j]);
m_list.SetItemText(nItem, nSubItem,c);
nSubItem++;
}
nItem++;
nSubItem=1;
}*/
int k,l;
double **output;
output=new double *[2*m_n+1];
for(l=0;l<=2*m_n;l++)
output[l]=new double[m_n+1];
for(i=0;i<2*m_n+1;i++)
{
for(j=0;j<m_n+1;j++)
output [i][j]=0;
}
for(i=0;i<m_n+1;i++)
{
for(j=0,k=0;j<i+1;j++)
{
output[m_n-i+k][i] = option[j][i];
k=k+2;
}
k=0;
}
/* for(i=0;i<2*m_n+1;i++)
{
for(j=0;j<=m_n;j++)
if((m_n-i)>j)
{
m_list.SetItemText( nItem, nSubItem,"");
nSubItem++;
}
else
{
c.Format("%lf", output[i][j]);
m_list.SetItemText(nItem, nSubItem,c);
nSubItem++;
}
nItem++;
nSubItem=1;
}
*/
int flag=1;
for(i=0,k=m_n;i<2*m_n+1;)
{
for(j=0;j<=m_n;j++)
{
if(j<k)
{
m_list.SetItemText( nItem, nSubItem,"");
nSubItem++;
}
else
{
if(flag)
{
c.Format("%lf", output[i][j]);
m_list.SetItemText(nItem, nSubItem,c);
nSubItem++;
flag=0;
}
else
{
m_list.SetItemText( nItem, nSubItem,"");
nSubItem++;
flag=1;
}
}
}
flag=1;
i++;
if(i<m_n+1) k--;
else
k++;
nItem++;
nSubItem=1;
}
}
void CMyDlg::OnRadio5()
{
// TODO: Add your control notification handler code here
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -