Class VerteilungenWindow
Inherits Window
// Controls
ControlInstance StaticText1
End Control
ControlInstance EditField1
End Control
ControlInstance PushButton1
Sub Action() Handles Event
calc
redraw
End Sub
End Control
ControlInstance out
Sub Paint(g As Graphics) Handles Event
draw g
End Sub
End Control
ControlInstance PopupMenu1
End Control
ControlInstance StaticText2
End Control
ControlInstance EditField2
End Control
ControlInstance StaticText3
End Control
ControlInstance EditField3
End Control
ControlInstance StaticText4
End Control
// Properties
Protected Dim values(100) as integer
// Event implementations
Sub Activate() Handles Event
if app.vollbild then
me.FullScreen=true
me.MenuBarVisible=False
end if
End Sub
Function KeyDown(Key As String) As Boolean Handles Event
End Function
Sub Moved() Handles Event
End Sub
Sub Paint(g As Graphics) Handles Event
End Sub
Sub Open() Handles Event
calc
End Sub
// Methods
Protected Sub draw(g as graphics)
dim maxv as integer
dim x,y,i,a as integer
a=UBound(values)
for i=0 to a
maxv=max(maxv,values(i))
next
maxv=maxv+1
g.ClearRect 0,0,g.Width,g.Height
g.ForeColor=rgb(0,0,0)
for i=0 to a
x=g.Width/(a+1)*i
y=(1-values(i)/maxv)*g.Height
g.FillRect x,y,g.Width/(a+1)+1,g.Height-y
next
End Sub
Protected Sub redraw()
draw out.Graphics
End Sub
Protected Sub calc()
dim n,i,v as integer
dim d as Double
dim r,a,maxv as integer
dim va(-1) as Double
n=Val(EditField1.Text)
r=Val(EditField2.Text)
a=Val(EditField3.Text)
if a<10 then
a=10
EditField3.Text="10"
end if
if r<5 then
r=5
EditField2.Text="5"
end if
if n<5 then
n=5
EditField1.Text="5"
end if
redim values(a)
redim va(n)
for i=0 to a
values(i)=0
next
Select case PopupMenu1.ListIndex
case 0 // gleichverteilt
for i=1 to n
va(i)=(rnd-0.5)
next
case 1 // normal verteilt
for i=1 to n
va(i)=(math.RandomNormal)/7.0
next
case 2 // gleichverteilt quadriert
for i=1 to n
va(i)=math.RandomSquared
next
case 3 // Random Walk verteilt
for i=1 to n
va(i)=(math.RandomWalk(r))/7.0
Next
case 4 // Random Walk verteilt
for i=1 to n
va(i)=(math.RandomWalk2(r))/7.0
next
end Select
for i=1 to n
d=a*(va(i)+0.5)
v=floor(d)
if v>=0 and v<a then
values(v)=Values(v)+1
end if
next
v=0
d=0
maxv=0
a=UBound(values)
for i=0 to a
maxv=max(maxv,values(i))
v=v+values(i)
next
StaticText4.Text=cstr(v)+" values, max: "+cstr(maxv)
End Sub
End Class
Class App
Inherits Application
// Properties
Dim Vollbild as boolean
// Event implementations
Sub EnableMenuItems() Handles Event
FensterVollbild.Checked=Vollbild
End Sub
Sub Open() Handles Event
#if TargetWin32
VerteilungenWindow.Show
#endif
End Sub
End Class
Module Math
// Constants
Protected Const e = 2.7182818284
Protected Const pi = 3.14159265
// Properties
Private Dim NormalVerteilungArray(-1) as double
// Methods
Protected Function RandomWalk(r as integer) As double
#pragma DisableAutoWaitCursor
#pragma DisableBackgroundTasks
dim i as integer
dim v as integer
v=0
for i=1 to r
if rnd<0.5 then
v=v+1
else
v=v-1
end if
next
Return v/Sqrt(r)
End Function
Protected Function RandomSquared() As double
#pragma DisableAutoWaitCursor
#pragma DisableBackgroundTasks
dim d as double
d=rnd*rnd
if rnd<0.5 then
Return d
else
Return -d
end if
End Function
Protected Function RandomNormal() As double
dim w,z,v1,v2 as Double
do
V1 = 2.0 * rnd - 1.0
V2 = 2.0 * rnd - 1.0
w = v1 * v1 + v2 * v2
loop until w < 1.0 and w<>0
z = V1 * sqrt(-2.0 * log(w) / w)
Return z
End Function
Protected Function RandomWalk2(r as integer) As double
#pragma DisableAutoWaitCursor
#pragma DisableBackgroundTasks
dim i as integer
dim v as double
v=0
for i=1 to r
v=v+RandomNormal
next
Return v/Sqrt(r)
End Function
Protected Function WertOption(s0 as Double, r as double, sigma as double, t as double, k as Double, n as integer) As double
#pragma DisableAutoWaitCursor
#pragma DisableBackgroundTasks
dim i as integer
dim Summe as Double
dim s1 as Double
dim wt,x as Double
dim sqrtT as Double
Summe=0
x=(R-0.5*sigma*sigma)*t
sqrtT=sqrt(t)
for i=1 to n
wt=sqrtT*math.RandomNormal
s1=s0*exp(x+sigma*wt)
Summe=Summe+max(s1-k,0)
next
Return exp(-r*t)*Summe/n
End Function
Protected Function NormalVerteilung(x as double) As double
#pragma DisableAutoWaitCursor
#pragma DisableBackgroundTasks
dim fx,z as double
const a1=0.319381530
const a2=-0.356563782
const a3=1.781477937
const a4=-1.821255978
const a5=1.330274429
if x<0 then
Return 1.0-NormalVerteilung(-x)
else
z=1.0/(1.0+0.2316419*x)
'fx=1.0/sqrt(2.0*pi)*exp(-x*x/2.0)
fx=0.3989422804*exp(-x*x/2.0)
Return 1.0-fx*z*((((a5*z+a4)*z+a3)*z+a2)*z+a1)
end if
End Function
End Module
Class AktienkursWindow
Inherits Window
// Controls
ControlInstance Canvas1
Sub Open() Handles Event
End Sub
Sub Paint(g As Graphics) Handles Event
Draw g
End Sub
End Control
ControlInstance StaticTextMu
End Control
ControlInstance EditFieldMu
End Control
ControlInstance EditFieldS
End Control
ControlInstance EditFieldSigma
End Control
ControlInstance EditFieldN
End Control
ControlInstance EditFieldZ
End Control
ControlInstance EditFieldK
End Control
ControlInstance StaticTextS
End Control
ControlInstance StaticTextSigma
Sub Action() Handles Event
Redraw
End Sub
End Control
ControlInstance StaticTextN
Sub Action() Handles Event
Redraw
End Sub
End Control
ControlInstance CheckS
End Control
ControlInstance CheckV
End Control
ControlInstance StaticTextK
End Control
ControlInstance StaticTextZ
Function KeyDown(Key As String) As Boolean Handles Event
End Function
End Control
ControlInstance CheckFA
Sub Action() Handles Event
Redraw
End Sub
End Control
ControlInstance CheckFB
Sub Action() Handles Event
Redraw
End Sub
End Control
ControlInstance CanvasS
Sub Changed() Handles Event
Redraw
End Sub
Sub Open() Handles Event
me.TheColor=rgb(255,255,255)
End Sub
Sub Paint(g As Graphics) Handles Event
End Sub
End Control
ControlInstance CanvasV
Sub Open() Handles Event
me.TheColor=rgb(0,0,255)
End Sub
Sub Changed() Handles Event
Redraw
End Sub
End Control
ControlInstance CanvasFA
Sub Open() Handles Event
me.TheColor=rgb(0,255,0)
End Sub
Sub Changed() Handles Event
Redraw
End Sub
End Control
ControlInstance CanvasFB
Sub Open() Handles Event
me.TheColor=rgb(255,0,0)
End Sub
Sub Changed() Handles Event
Redraw
End Sub
Sub Close() Handles Event
End Sub
End Control
ControlInstance StaticText7
Sub Open() Handles Event
End Sub
End Control
ControlInstance RadioButton1
Sub Action() Handles Event
calc
Redraw
End Sub
End Control
ControlInstance RadioButton2
Sub Action() Handles Event
calc
Redraw
End Sub
End Control
ControlInstance StaticText8
End Control
ControlInstance Slider1
Sub ValueChanged() Handles Event
Redraw
End Sub
End Control
ControlInstance LittleArrows1
Sub Up() Handles Event
Slider1.Value=Slider1.Value+1
End Sub
Sub Down() Handles Event
Slider1.Value=Slider1.Value-1
End Sub
End Control
// Properties
Protected Dim Values(-1) as double
Protected Dim MaxValue as double
Protected Dim useWalk as boolean
Dim Option1(-1) as double
Dim FAs(-1) as double
Dim FBs(-1) as double
Dim Option2(-1) as double
Protected Dim TheK as double
// Event implementations
Sub Minimize() Handles Event
End Sub
Sub Deactivate() Handles Event
End Sub
Function KeyDown(Key As String) As Boolean Handles Event
if asc(key)=13 then
calc
Redraw
Return true
end if
End Function
Sub Paint(g As Graphics) Handles Event
End Sub
Sub Moved() Handles Event
End Sub
Sub Open() Handles Event
if app.vollbild then
me.FullScreen=true
me.MenuBarVisible=False
end if
calc
End Sub
// Methods
Protected Sub calc()
dim s0 as double
dim s as Double // Aktienpreis aktuell
dim i as double
dim AnzahlKurse as integer
dim r as double
dim m as double
dim k as double
dim t as double // t
dim dt as double // delta t
dim sigma as double
dim an as double // a nenner
dim at as double // a teiler
dim a as double
dim b as double
dim fa as double
dim fb as Double
dim x as integer
dim b0,Bi,LFA,lBI,o1,o2 as Double
dim RW(-1) as Double
dim sqrtAnzahlKurse as Double
const TT=1.0 // gesamtzeit = 1 Jahr
my=cdbl(EditFieldMu.text)
s0=CDbl(EditFieldS.text)
k=CDbl(EditFieldK.text)
sigma=CDbl(EditFieldSigma.text)
r=CDbl(EditFieldZ.text)
AnzahlKurse=CDbl(EditFieldN.text)
redim values(AnzahlKurse)
redim Option1(AnzahlKurse)
redim Option2(AnzahlKurse)
redim FAs(AnzahlKurse)
redim FBs(AnzahlKurse)
m=s0
s=s0
Values(0)=s
dt=TT/AnzahlKurse
t=0.0
an=log(s/K)+(r+sigma*sigma/2.0)*(TT-t)
at=sigma*sqrt(TT-t)
a=an/at
b=a-sigma*sqrt(TT-t)
fa=math.NormalVerteilung(a)
fb=Math.NormalVerteilung(b)
Option1(0)= s*fa-K*exp(-r*(TT-t))*fb
FAs(0)=fa*s
B0=K*exp(-r*TT)*fb
Option2(0)= s*fa-B0
FBs(0)=b0
lbi=b0
lfa=fa
TheK=k
if RadioButton1.Value=false then
redim RW(AnzahlKurse)
x=0
sqrtAnzahlKurse=sqrt(AnzahlKurse)
for i=1 to AnzahlKurse
if rnd<0.5 then
x=x+1
else
x=x-1
end if
RW(i)=x/sqrtAnzahlKurse
next
end if
for i=1 to AnzahlKurse
if RadioButton1.Value then
s=s*(1+my*dt+sigma*sqrt(dt)*math.RandomNormal) // Normalverteilung
else
s=s0*exp((my-1/2*sigma*sigma)*(i*dt)+sigma*RW(i)) // Brownsche Bewegung
end if
Values(i)=s
if s>m then
m=s
end if
t=i*(1.0/AnzahlKurse)
an=log(s/K)+(r+sigma*sigma/2.0)*(TT-t)
at=sigma*sqrt(TT-t)
if at<>0 then
a=an/at
b=a-sigma*sqrt(TT-t)
fa=math.NormalVerteilung(a)
fb=Math.NormalVerteilung(b)
o1=s*fa-K*exp(-r*(TT-t))*fb
Option1(i)= o1 // theoretischer Preis
FAs(i)=fa*s
Bi=-(s*(lfa-fa)-lbi*exp(r*dt))
o2=s*fa-bi
Option2(i)= o2 // Praktischer Preis
FBs(i)=bi
lfa=fa
lbi=bi
end if
next
StaticText7.text=str(o1-o2)
MaxValue=m
End Sub
Protected Sub Draw(g as graphics)
dim n as integer
dim i,AnzahlKurse,AnzahlKurse1,AnzahlKurseL,AnzahlKurseL1 as integer
dim lx,ly,x,y as integer
dim xf,yf as Double
dim c1,c2 as color
dim s as string
AnzahlKurse=UBound(Values)
AnzahlKurse1=AnzahlKurse-1
AnzahlKurseL=AnzahlKurse*Slider1.Value/100
AnzahlKurseL1=AnzahlKurseL-1
yf=0.9*g.Height/MaxValue
xf=g.Width/AnzahlKurse
g.ForeColor=Rgb(0,0,0)
g.FillRect 0,0,g.Width,g.Height
g.ForeColor=rgb(100,100,100)
y=g.Height-TheK*yf
g.DrawLine 0,y,g.Width,y
if CheckS.Value then // Aktienkurs
g.ForeColor=CanvasS.TheColor
lx=0
ly=g.Height-values(0)*yf
for i=1 to AnzahlKurseL
x=i*xf
y=g.Height-Values(i)*yf
g.DrawLine lx,ly,x,y
lx=x
ly=y
next
end if
if CheckV.Value then
g.ForeColor=CanvasV.TheColor
lx=0
ly=g.Height-Option1(0)*yf
for i=1 to AnzahlKurseL1
x=i*xf
y=g.Height-Option1(i)*yf
g.DrawLine lx,ly,x,y
lx=x
ly=y
next
g.ForeColor=rgb(255,255,0)
lx=0
ly=g.Height-Option2(0)*yf
for i=1 to AnzahlKurseL1
x=i*xf
y=g.Height-Option2(i)*yf
g.DrawLine lx,ly,x,y
lx=x
ly=y
next
end if
if CheckFA.Value then
g.ForeColor=CanvasFA.TheColor
lx=0
ly=g.Height-FAs(0)*yf
for i=1 to AnzahlKurseL1
x=i*xf
y=g.Height-FAs(i)*yf
g.DrawLine lx,ly,x,y
lx=x
ly=y
next
end if
if CheckFB.Value then
g.ForeColor=CanvasFB.TheColor
lx=0
ly=g.Height-FBs(0)*yf
for i=1 to AnzahlKurseL1
x=i*xf
y=g.Height-FBs(i)*yf
g.DrawLine lx,ly,x,y
lx=x
ly=y
next
end if
y=g.TextHeight
if CheckS.Value then
g.ForeColor=CanvasS.TheColor
s="Aktienpreis"
y=Y+g.TextHeight
g.DrawString s,10,y
end if
if CheckV.Value then
g.ForeColor=CanvasV.TheColor
s="Optionspreis theoretisch"
y=Y+g.TextHeight
g.DrawString s,10,y
g.ForeColor=rgb(255,255,0)
s="Optionspreis praktisch"
y=Y+g.TextHeight
g.DrawString s,10,y
end if
if CheckFA.Value then
g.ForeColor=CanvasFA.TheColor
s="Geld in Aktien"
y=Y+g.TextHeight
g.DrawString s,10,y
end if
if CheckFB.Value then
g.ForeColor=CanvasFB.TheColor
s="Schulden"
y=Y+g.TextHeight
g.DrawString s,10,y
end if
g.ForeColor=Rgb(255,255,255)
s="Zeit"
g.DrawString s,g.Width-30-g.StringWidth(s),g.Height-g.TextHeight
End Sub
Protected Sub Redraw()
Draw Canvas1.Graphics
End Sub
Sub Init(zeige as boolean)
CheckFA.Visible=zeige
CheckFB.Visible=zeige
CheckS.Visible=zeige
CheckV.Visible=zeige
CanvasFA.Visible=zeige
CanvasFB.Visible=zeige
CanvasS.Visible=zeige
CanvasV.Visible=zeige
StaticTextK.Visible=zeige
EditFieldK.Visible=zeige
End Sub
End Class
Class ColorPicker
Inherits Canvas
// Properties
Dim TheColor as color
// New events
Event Changed()
// Event implementations
Function MouseDown(X As Integer, Y As Integer) As Boolean Handles Event
dim c as color
c=TheColor
if SelectColor(c,"Select color:") then
TheColor=c
Changed
redraw
end if
End Function
Sub Paint(g As Graphics) Handles Event
draw g
End Sub
// Methods
Sub draw(g as graphics)
g.ForeColor=rgb(255-TheColor.red,255-thecolor.green,255-TheColor.blue)
g.DrawRect 0,0,g.Width-1,g.Height-1
g.ForeColor=TheColor
g.FillRect 1,1,g.Width-3,g.Height-3
End Sub
Sub redraw()
draw Graphics
End Sub
End Class
Class FilmWindow
Inherits Window
// Controls
ControlInstance Canvas1
Sub Open() Handles Event
End Sub
Sub Paint(g As Graphics) Handles Event
Draw g
End Sub
End Control
ControlInstance Info
End Control
ControlInstance StaticText1
End Control
ControlInstance EditField1
End Control
ControlInstance StaticText2
End Control
ControlInstance EditField2
End Control
ControlInstance EditField5
End Control
ControlInstance StaticText3
End Control
ControlInstance EditField3
End Control
ControlInstance StaticText4
End Control
ControlInstance EditField4
End Control
ControlInstance StaticText5
End Control
ControlInstance StaticText6
End Control
ControlInstance EditField6
End Control
ControlInstance SliderProzent
Sub ValueChanged() Handles Event
recalc
Redraw
End Sub
End Control
ControlInstance PushButton1
Sub Action() Handles Event
Dim i as Integer
Dim theEffect as QTEffect
Dim track as QTVideoTrack
Dim m as EditableMovie
Dim f as FolderItem
dim p as Picture
Dim mybool as Boolean
dim t as Double
f=GetSaveFolderItem("video/quicktime","new movie.mov")
if f<>nil then
m=f.CreateMovie //create null movie
if m<>nil then
p=NewPicture(Canvas1.Width,Canvas1.Height,32)
if p<>Nil then
track=m.NewVideoTrack(p.Width,p.Height, 24)
if track.SelectCompressionSettings then
For i=0 to 200
t=i/200.0
calc t
draw p.Graphics
track.appendpicture p
Next
end if
end if
end if
end if
End Sub
End Control
ControlInstance LittleArrows1
Sub Up() Handles Event
SliderProzent.Value=SliderProzent.Value+1
End Sub
Sub Down() Handles Event
SliderProzent.Value=SliderProzent.Value-1
End Sub
End Control
// Properties
Protected Dim Values(-1) as double
Protected Dim MaxValue as double
Protected Dim useWalk as boolean
Protected Dim TheT as double
Protected Dim TheS0 as double
Private Dim Exotisch as boolean
// Event implementations
Sub Minimize() Handles Event
End Sub
Sub Deactivate() Handles Event
End Sub
Function KeyDown(Key As String) As Boolean Handles Event
if asc(key)=13 then
recalc
Redraw
Return true
end if
End Function
Sub Paint(g As Graphics) Handles Event
End Sub
Sub Moved() Handles Event
End Sub
Sub Open() Handles Event
if app.vollbild then
me.FullScreen=true
me.MenuBarVisible=False
end if
End Sub
// Methods
Protected Sub calc(t as double)
#pragma DisableAutoWaitCursor
#pragma DisableBackgroundTasks
dim i,rt,TeilAusdruck as double
dim AnzahlKurse as integer
dim r as double
dim k,s as double
dim s0,m,va as double
dim sigma as double
dim dt,an,at,a,b,fa,fb as Double
TheT=t
const TT=1.0 // gesamtzeit = 1 Jahr
s0=CDbl(EditField2.text)
k=CDbl(EditField5.text)
sigma=CDbl(EditField3.text)
r=CDbl(EditField6.text)
AnzahlKurse=CDbl(EditField4.text)
TheS0=s0
redim values(AnzahlKurse)
dt=TT/AnzahlKurse
TeilAusdruck=(r+sigma*sigma/2.0)
for i=1 to AnzahlKurse
rt=TT-t
S=s0*i/AnzahlKurse*2
an=log(S/K)+TeilAusdruck*rt
at=sigma*sqrt(rt)
if at=0 then
at=0.00001
end if
a=an/at
b=a-at ' at=sigma*sqrt(TT-t)
FA=Math.NormalVerteilung(a)
FB=Math.NormalVerteilung(b)
if Exotisch then
va=FA
else
va=FA*S-K*exp(-r*rt)*FB
end if
values(i)= va
if va>m then
m=va
end if
next
MaxValue=m
End Sub
Protected Sub Draw(g as graphics)
#pragma DisableAutoWaitCursor
#pragma DisableBackgroundTasks
dim n as integer
dim i,AnzahlKurse as integer
dim lx,ly,x,y,dy as Double
dim xf,yf as Double
dim c1,c2 as color
dim s as string
AnzahlKurse=UBound(Values)
dy=-g.Height*0.05
yf=0.9*g.Height/MaxValue
xf=g.Width/AnzahlKurse
g.ForeColor=Rgb(0,0,0)
g.FillRect 0,0,g.Width,g.Height
g.ForeColor=rgb(100,100,100)
y=dy+g.Height-values(0)*yf
g.DrawLine 0,y,g.Width,y
g.ForeColor=rgb(255,255,255)
s="Aktienpreis"
g.DrawString s,g.Width-g.StringWidth(s)-5,dy+g.Height+g.TextHeight
s="Optionspreis zum Zeitpunkt "+format(Thet,"0.000")
g.DrawString s,10,g.TextHeight
g.ForeColor=rgb(255,255,255)
g.PenWidth=2
g.PenHeight=2
lx=0
ly=dy+g.Height-values(0)*yf
for i=1 to AnzahlKurse
x=i*xf
y=dy+g.Height-Values(i)*yf
g.DrawLine lx,ly,x,y
lx=x
ly=y
next
g.PenWidth=1
g.PenHeight=1
g.ForeColor=rgb(255,255,255)
for i=1 to 7
x=i*xf*AnzahlKurse/8
y=dy+g.Height
g.DrawLine x,y,x,y+g.TextHeight
s=Format(TheS0/4*i,"0.00")
g.DrawString s,x+3,y+g.TextHeight
next
End Sub
Protected Sub Redraw()
#pragma DisableAutoWaitCursor
#pragma DisableBackgroundTasks
Draw Canvas1.Graphics
End Sub
Protected Sub recalc()
#pragma DisableAutoWaitCursor
#pragma DisableBackgroundTasks
calc SliderProzent.Value/SliderProzent.Maximum
End Sub
Sub Init(e as boolean)
Exotisch=e
recalc
End Sub
End Class
Class WhiteWindow
Inherits Window
// Controls
ControlInstance Timer1
Sub Action() Handles Event
dim s as Screen
s=Screen(0)
if Width<>s.Width then
Width=s.Width
end if
if Height<>S.Height then
Height=s.Height
end if
if Left<>s.Left then
left=s.Left
end if
if top<>s.top then
top=s.top
end if
End Sub
End Control
// Event implementations
Sub Open() Handles Event
End Sub
End Class
Links
MBS Realbasic tutorial videos - Pfarrgemeinde Messdiener Nickenich