AW: Wechselnde Werte in VBA
Oliver
Hallo, danke erstmal Franz, aber ich habe es noch nicht ganz richtig erklärt.
Gegeben:
-in Spalte 1 das jeweilige Monatsultimo (31.01.2005 - 31.07.2008)
-in Spalte 2 die Renditen am Markt für Anleihen (oder aktuelle Marktzinsen), Spalte_Anlagemarkt
(-Spalte 3 Rendite am Geldmarkt - EURIBOR, falls dir das etwas sagt)
-Spalte 4, hier steht "Hoch" oder "Tief", Spalte_Aktion
Welche zahlen da nun genau stehen, ist ja nicht so entscheidend. Aus 3 Zinspunkten (Spalte 2) kannst du ja nun ein "Hoch", "Tief" oder eben nichts bestimmen.
Kurzes Beispiel: "Hoch" = Kurs -1 Kurs +1 ... et vice versa für "Tief"3
Für mich ist ein "Hoch" ein Kaufsignal und ein "Tief" ein Verkaufssignal (nicht verwirren lassen, sind in dem Fall keine Aktien). Die Problematik die ich noch nicht überwinden kann liegt nun darin, dass ein "Hoch", also die Position im Wertpapier mind. 3 Monate gehalten werden muss. Also muss der Abstand von einem "Hoch"punkt zum "Tief" 3 Monate oder 2 leere Zeilen betragen. Naürlich kann ich bei "tief" und Verkauf meines Wertpapieres sofort wieder kaufen einen Monat später, also kann "Hoch" sofort auf "Tief" folgen.
Ist das verständlich, sorry, sicherlich verwirrend, aber Danke für die Mühe, dass ihr drüber nachdenkt, hier nochmal ein längerer Codeteil, wie er ursprünglich ist, ohne diese 3 Monatsregel (variabel wäre das natürlich auch nicht schlecht, aber fix 3 Monate ist für mich entscheidend):
VIELEN DANK und sorry, kann hier leider nicht dieses Darstellungstool für VBA Codes installieren.
hier das ganze (Anlagemarkt und Geldmarkt sind extra functions):
Sub BestStrategy()
Dim intZeile As Integer
Dim ZeilenStart As Integer
Dim AnzahlDatenzeilen As Integer
Dim Datum_Start_H As Date
Dim Datum_Ende_H As Date
Dim Datum_Diff_H As Integer
Dim Datum_Start_G As Date
Dim Datum_Ende_G As Date
Dim Datum_Start_F As Date
Dim Datum_Ende_F As Date
Static Hoch As Integer
Static Tief As Integer
Dim Datum_Differenz As Integer
Static Depot As Double
Dim Marktzins_G As Double
Dim Marktzins_F As Double
Dim Marktzins_z As Double
Dim Kupon As Double
Dim Spalte_Datum As Integer
Dim Spalte_Geldmarkt As Integer
Dim Spalte_Anlagemarkt As Integer
Dim Spalte_Aktion As Integer
Dim Spalte_Haltedauer As Integer
Dim Spalte_Depot As Integer
Dim Spalte_Kupons As Integer
Dim Spalte_Performance As Integer
Dim Spalte_Zeitraum As Integer
Dim Kuponzahlung As Double
Dim cnt As Integer
Dim Differenz As Integer
Dim i As Integer
Dim Kupon_z As Double
Dim Kupon_k As Double
Dim TageseitletzterKupon As Double
Dim Kupon_Neuanlage As Double
Dim Depot_Neuanlage As Double
Dim Datum_Differenz_Neuanlage As Integer
Dim Datum_Start_Neuanlage As Date
Dim Marktzins_Neuanlage As Double
Spalte_Datum = 1
Spalte_Anlagemarkt = 2
Spalte_Geldmarkt = 3
Spalte_Aktion = 4
Spalte_Haltedauer = 5
Spalte_Depot = 6
Spalte_Kupons = 7
Spalte_Performance = 8
Spalte_Zeitraum = 9
ZeilenStart = 2
AnzahlDatenzeilen = 32767
Range("D1:I1").EntireColumn.Value = ""
Cells(1, Spalte_Aktion) = "Aktion"
Cells(1, Spalte_Haltedauer) = "Haltedauer (d)"
Cells(1, Spalte_Depot) = "Depot Best"
Cells(1, Spalte_Kupons) = "Kupons"
Cells(1, Spalte_Performance) = "Performance (%)"
Cells(1, Spalte_Zeitraum) = "Zeitraum (y)"
For intZeile = ZeilenStart To AnzahlDatenzeilen
If intZeile = ZeilenStart Then
Depot = 100
Datum_Start_H = 0
End If
If IsEmpty(Cells(intZeile, Spalte_Anlagemarkt)) Then
Datum_Ende_H = Cells(intZeile - 1, Spalte_Datum)
Datum_Diff_H = DateDiff("d", Datum_Start_H, Datum_Ende_H)
Cells(2, Spalte_Performance) = (Depot - 100)
Cells(2, Spalte_Zeitraum) = Datum_Diff_H / 365
Sheets("Auswertung").Cells(4, 5) = Sheets("Daten").Cells(2, Spalte_Performance)
Sheets("Auswertung").Cells(4, 6) = Sheets("Daten").Cells(2, Spalte_Zeitraum)
Sheets("Auswertung").Cells(4, 7) = (((Depot) / 100) ^ (1 / (Datum_Diff_H / 365)) - _
1) * 100 'jährliche Rendite
Exit For
End If
If IsEmpty(Cells(intZeile + 1, Spalte_Anlagemarkt)) Then
If Hoch = 1 Then
Tief = 1
Hoch = 0
Else
Tief = 0
Hoch = 1
End If
End If
If IsEmpty(Cells(intZeile + 1, Spalte_Anlagemarkt)) = False Then
If Cells(intZeile, Spalte_Anlagemarkt) > Cells(intZeile - 1, Spalte_Anlagemarkt) _
And Cells(intZeile, Spalte_Anlagemarkt) > Cells(intZeile + 1, Spalte_Anlagemarkt) Then
Hoch = 1
Cells(intZeile, Spalte_Aktion) = "Hoch"
Else
Hoch = 0
End If
If Cells(intZeile, Spalte_Anlagemarkt) Cells(intZeile - 1, Spalte_Anlagemarkt) _
And Cells(intZeile,
Spalte_Anlagemarkt) = Cells(intZeile + 1, Spalte_Anlagemarkt) Then
Do Until Cells(intZeile + 1, Spalte_Anlagemarkt) Cells(intZeile, _
Spalte_Anlagemarkt)
intZeile = intZeile + 1
Loop
If Cells(intZeile + 1, Spalte_Anlagemarkt) Cells(intZeile - 1, _
Spalte_Anlagemarkt) And Cells
(intZeile, Spalte_Anlagemarkt) = Cells(intZeile + 1, Spalte_Anlagemarkt)
intZeile = intZeile - 1
Loop
Hoch = 1
Cells(intZeile, Spalte_Aktion) = "Hoch"
End If
End If
End If
If Cells(intZeile, Spalte_Anlagemarkt) Cells(intZeile, _
Spalte_Anlagemarkt)
intZeile = intZeile + 1
Loop
If Cells(intZeile + 1, Spalte_Anlagemarkt) > Cells(intZeile, Spalte_Anlagemarkt) _
Then
Do Until Cells(intZeile, Spalte_Anlagemarkt) ZeilenStart And Datum_Start_G > "00.01.1900" Then
Datum_Ende_G = Cells(intZeile, Spalte_Datum)
Cells(intZeile, Spalte_Haltedauer) = DateDiff("d", Datum_Start_G, Datum_Ende_G)
Datum_Differenz = DateDiff("d", Datum_Start_G, Datum_Ende_G)
Depot = Geldmarkt(Depot, Datum_Differenz, Marktzins_G)
Cells(intZeile, Spalte_Depot) = Depot
End If
If Hoch = 1 Then
Datum_Start_F = Cells(intZeile, Spalte_Datum)
Kupon = Cells(intZeile, Spalte_Anlagemarkt)
Kupon_z = Cells(intZeile, Spalte_Anlagemarkt)
cnt = intZeile
End If
If Tief = 1 And intZeile > ZeilenStart And Datum_Start_F > "00.01.1900" Then
Datum_Ende_F = Cells(intZeile, Spalte_Datum)
Cells(intZeile, Spalte_Haltedauer) = DateDiff("d", Datum_Start_F, Datum_Ende_F)
Datum_Differenz = DateDiff("d", Datum_Start_F, Datum_Ende_F)
Kuponzahlung = WorksheetFunction.RoundDown(Datum_Differenz / 365, 0)
TageseitletzterKupon = Datum_Differenz - Kuponzahlung * 365
Marktzins_F = Cells(intZeile, Spalte_Anlagemarkt)
If Kuponzahlung > 0 Then
For i = 1 To Kuponzahlung
Do Until (Differenz >= i * 365 - 1) And (Differenz