-
Notifications
You must be signed in to change notification settings - Fork 3
/
mdRun.bas
135 lines (96 loc) · 4.14 KB
/
mdRun.bas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
Attribute VB_Name = "mdRun"
Option Explicit
Public Sub StartMonteCarloSimulation()
'*******************************************************************************************************
'Purpose: This is the main routine that takes the parameters from the worksheet and runs the simulation
'*******************************************************************************************************
Dim vntTradeList As Variant
Dim iCalc As Integer
Dim blnScreenUpdating As Boolean
Dim collFinalResults As Collection
Dim oResult As clsResult
Dim lRow As Long
Dim iCol As Integer
Dim oSimulation As clsSimulation
Dim ws As Worksheet
Dim intTotalRuns As Integer
Dim intLotSize As Integer
Dim dblStartEquity As Double
Dim dblMarginLimit As Double
Dim intTradesInYear As Integer
iCalc = Application.Calculation
Application.Calculation = xlManual
blnScreenUpdating = Application.ScreenUpdating
Application.ScreenUpdating = False
' clear the output from previous results
Call ClearUI
' get the list of trades from the input worksheet
vntTradeList = fncGetTrades()
If UBound(vntTradeList) = 0 Then
MsgBox "No trade list found!", vbExclamation + vbOKOnly, "Input Data"
GoTo Exit_Here
End If
Set ws = ThisWorkbook.Sheets("Control")
With ws
' parameters for the simulation
intTotalRuns = .Range("TOTAL_RUNS").value
intLotSize = .Range("LOT_SIZE").value
intTradesInYear = .Range("TRADES_IN_YEAR").value
dblStartEquity = .Range("START_EQUITY").value
dblMarginLimit = .Range("MARGIN_LIMIT").value
'create a simulation object to run with the parameters
Set oSimulation = mdFactory.CreateSimulation(totalRuns:=intTotalRuns, _
tradesInYear:=intTradesInYear, lotSize:=intLotSize, TradeList:=vntTradeList, _
startEquity:=dblStartEquity, margin:=dblMarginLimit)
If Not oSimulation Is Nothing Then
lRow = .Range("OUTPUT_START_CELL").Row
iCol = .Range("OUTPUT_START_CELL").Column
'run the simulation
Set collFinalResults = oSimulation.fncRunProcess()
'output the results of the simulation
If Not collFinalResults Is Nothing Then
For Each oResult In collFinalResults
.Cells(lRow, iCol).value = oResult.equity
.Cells(lRow, iCol + 1).value = oResult.Ruin
.Cells(lRow, iCol + 2).value = oResult.MedianDrawdown
.Cells(lRow, iCol + 3).value = oResult.MedianProfit
.Cells(lRow, iCol + 4).value = oResult.MedianReturn
.Cells(lRow, iCol + 5).value = oResult.MedianReturnDD
lRow = lRow + 1
Next oResult
End If
End If
ws.Select
End With
MsgBox "Process complete!", vbOKOnly + vbInformation, "Trade Simulation"
Exit_Here:
Set ws = Nothing
Set oResult = Nothing
Set collFinalResults = Nothing
Set oSimulation = Nothing
Application.Calculation = iCalc
Application.ScreenUpdating = blnScreenUpdating
End Sub
Function fncGetTrades() As Variant
'Purpose: return the input pnl trades as a one dimensional array
Dim ws As Worksheet
Dim rng As Range
Dim arr As Variant
Dim lnglastRow As Long
Dim lngfirstRow As Long
Set ws = ThisWorkbook.Worksheets("InputData")
lnglastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
lngfirstRow = 2
Set rng = ws.Range("A" & lngfirstRow & ":A" & lnglastRow)
arr = rng.value
fncGetTrades = Application.Transpose(rng.value)
Set ws = Nothing
Set rng = Nothing
End Function
Public Sub ClearUI()
'Purpose: Reset this tool and any input ranges
Dim ws As Worksheet
Set ws = ThisWorkbook.Sheets("Control")
ws.Range("OUTPUT").ClearContents
Set ws = Nothing
End Sub