TO BE INSERTED IN THE TRANSACTION CLASS ----------------------------------------------------------------------- Public Function CalcEffectOfTransactionOnMargin(targdate As Date) As Double ' The code is provided to you as a link from the homework. ' Make sure that you understand the financial logic. ' This function calculates the effect of the current transaction on your overall margin Select Case type Case "Sell" ' Sell has no effect on margin because you can only sell what you have long Return 0 ' effect of transaction on margin Case "Buy" If currentPositionInAP >= 0 Then Return 0 Else If qty >= Math.Abs(currentPositionInAP) Then Return -currentPositionInAP * CalcMTM(symbol, targdate) ' buying eliminates all margin for this symbol Else Return (qty * CalcMTM(symbol, targdate)) ' buying reduces the margin End If End If Case "SellShort" Return -qty * CalcMTM(symbol, targdate) ' Selling short is easy: it always increases the margin Case "CashDiv" Return 0 Case "X-Call" ' Two effects on margin: the change in options and the change in stocks Dim OptionEffect As Double = 0 underlier = GetUnderlier(symbol) underlierCurrentPositionInAP = GetCurrentPositionInAP(underlier) ' first the effect of exercising the call on the call position If currentPositionInAP < 0 Then OptionEffect = qty * CalcMTM(symbol, targdate) ' i.e., it reduces the margin Else OptionEffect = 0 End If ' next, the effect of the called stock ' two cases: long and short calls. If currentPositionInAP >= 0 Then ' long call is like buying If underlierCurrentPositionInAP >= 0 Then Return OptionEffect Else If qty >= Math.Abs(underlierCurrentPositionInAP) Then Return OptionEffect - (underlierCurrentPositionInAP * CalcMTM(underlier, targdate)) ' X-call eliminates all margin for this symbol Else Return OptionEffect + (qty * CalcMTM(underlier, targdate)) ' X-call reduces the margin End If End If Else ' exercising short calls is like selling If underlierCurrentPositionInAP <= 0 Then Return OptionEffect - (qty * CalcMTM(underlier, targdate)) Else ' underlier positive If underlierCurrentPositionInAP >= qty Then Return OptionEffect Else Return OptionEffect - ((qty - underlierCurrentPositionInAP) * CalcMTM(underlier, targdate)) End If End If End If Case "X-Put" ' Two effects on margin: the change in options and the change in stocks Dim OptionEffect As Double = 0 underlier = GetUnderlier(symbol) underlierCurrentPositionInAP = GetCurrentPositionInAP(underlier) ' first the effect of exercising the option on the put position If currentPositionInAP < 0 Then OptionEffect = qty * CalcMTM(symbol, targdate) ' and it reduces the margin Else OptionEffect = 0 End If ' next, the effect of the stock ' two cases: long and short puts If currentPositionInAP < 0 Then ' short put is like buying If underlierCurrentPositionInAP >= 0 Then Return OptionEffect Else If qty >= Math.Abs(underlierCurrentPositionInAP) Then Return OptionEffect - (underlierCurrentPositionInAP * CalcMTM(underlier, targdate)) ' X-put eliminates all margin for this symbol Else Return OptionEffect + (qty * CalcMTM(underlier, targdate)) ' X-put reduces the margin End If End If Else ' long put is like selling If underlierCurrentPositionInAP <= 0 Then Return OptionEffect - (qty * CalcMTM(underlier, targdate)) Else ' underlier positive If underlierCurrentPositionInAP >= qty Then Return OptionEffect Else Return OptionEffect - ((qty - underlierCurrentPositionInAP) * CalcMTM(underlier, targdate)) End If End If End If End Select MessageBox.Show("Holy BatApples! I could not figure out the impact of " + symbol + " on margin. I returned $0.") Return 0 End Function TO BE INSERTED IN THE CONTROLS MODULE -------------------------------------------------------------------------------------- Public Function IsValid(t As Transaction) As Boolean ' this function enforces some of the rules of the tournament If (currentDate.DayOfWeek = DayOfWeek.Saturday Or currentDate.DayOfWeek = DayOfWeek.Sunday) And (t.type = "Buy" Or t.type = "Sell" Or t.type = "SellShort" Or t.type = "CashDiv") Then MessageBox.Show("Holy BatSmoke! Weekend. Can't do that.", "Controls", MessageBoxButtons.OK, MessageBoxIcon.Hand) Return False End If If t.qty = 0 Then MessageBox.Show("Holy BatSmog! Zero quantity. Not sent.", "Controls", MessageBoxButtons.OK, MessageBoxIcon.Hand) Return False End If If IsInIP(t.symbol) And (t.type = "Buy" Or t.type = "Sell" Or t.type = "SellShort") Then MessageBox.Show("Holy BatFog! You cannot trade securities in IP. Not sent.", "Controls", MessageBoxButtons.OK, MessageBoxIcon.Hand) Return False End If If t.type = "CashDiv" And t.dividend = 0 Then MessageBox.Show("Holy BatCloud! No dividend. Not sent.", "Controls", MessageBoxButtons.OK, MessageBoxIcon.Hand) Return False End If If t.type = "SellShort" And t.currentPositionInAP > 0 Then ' <----- MessageBox.Show("Holy BatSteam! Cannot SellShort if you have it. Not sent.", "Controls", MessageBoxButtons.OK, MessageBoxIcon.Hand) Return False End If Return True ' if all controls are passed End Function Public Function IsOptionInputValid() As Boolean ' To be complete, a transaction needs qty, symbol/ticker And a type. ' first, check symbol If Globals.Dashboard.SymbolsCBox.SelectedItem = Nothing Then MessageBox.Show("Picking options is hard, I know. Do your best, Dave.", "No symbol", MessageBoxButtons.OK, MessageBoxIcon.Error) Return False Else CT.symbol = Globals.Dashboard.SymbolsCBox.SelectedItem End If 'check type If CT.type = "" Then MessageBox.Show("To buy or not to buy, that is the question.", "No transaction type", MessageBoxButtons.OK, MessageBoxIcon.Error) Return False End If 'check qty Try CT.qty = Integer.Parse(Globals.Dashboard.OptionQtyTBox.Text) Catch MessageBox.Show("Quantity, Dave?", "No quantity", MessageBoxButtons.OK, MessageBoxIcon.Error) Return False End Try If CT.qty = 0 Then MessageBox.Show("Trading zero qty, Dave?", "No quantity", MessageBoxButtons.OK, MessageBoxIcon.Error) Return False End If Return True End Function