Module ScoringRules ' NOTE: this code is given to you As-Is. Its parameters are suboptimal. ' You need To understand it and make all the necessary changes. Public Sub ScoreBuyingStock(baseScore As Integer, recommendation As Transaction, targetDate As Date) Dim cRec As New Transaction ' this is the CANDIDATE rec, do not confuse with recommendation! cRec.securityType = "Stock" cRec.symbol = recommendation.familyTicker cRec.type = "Buy" cRec.familyDelta = recommendation.familyDelta Dim adjustment As Decimal = 0 Dim maxBuy As Decimal If IsInIP(cRec.symbol) Then Exit Sub ' cannot buy stock if in IP End If If AvailableCashIsLow() Then Exit Sub End If cRec.currentPositionInAP = GetCurrentPositionInAP(cRec.symbol) If cRec.currentPositionInAP < 0 Then ' if short then we need a buyback Exit Sub Else cRec.delta = 1 cRec.hedgeQty = CalcQtyNeededToHedge(cRec) maxBuy = MaxPurchasePossible(cRec.symbol, targetDate) ' how many can we afford? If maxBuy < cRec.hedgeQty Then cRec.qty = maxBuy adjustment = -50 ' very mild. Else cRec.qty = cRec.hedgeQty End If If cRec.qty > 0 Then cRec.score = baseScore + adjustment CandidateRecommendationsList.Add(cRec) End If End If End Sub Public Sub ScoreSellingStock(baseScore As Integer, recommendation As Transaction, tDate As Date) Dim adjustment As Decimal = 0 Dim cRec As New Transaction ' this is a CANDIDATE rec, do not confuse with (final) recommendation! cRec.securityType = "Stock" cRec.symbol = recommendation.familyTicker cRec.type = "Sell" cRec.familyDelta = recommendation.familyDelta If IsInIP(cRec.symbol) Then Exit Sub ' cannot sell if in IP Else cRec.currentPositionInAP = GetCurrentPositionInAP(cRec.symbol) If cRec.currentPositionInAP <= 0 Then ' we cannot sell since we are not long Exit Sub Else cRec.delta = 1 cRec.hedgeQty = CalcQtyNeededToHedge(cRec) If cRec.hedgeQty = 0 Then Exit Sub ' nothing to sell Else If cRec.hedgeQty > cRec.currentPositionInAP Then ' you have fewer than needed, so cRec.qty = cRec.currentPositionInAP ' sell all you have. adjustment = -50 ' Arbitrary adjustment! Else cRec.qty = cRec.hedgeQty End If cRec.score = baseScore + adjustment CandidateRecommendationsList.Add(cRec) End If End If End If End Sub Public Sub ScoreSellingCall(baseScore As Integer, recommendation As Transaction, tDate As Date) Dim adjustment As Decimal = 0 Dim cRec As Transaction ' this is a CANDIDATE rec, do not confuse with recommendation! For Each APRow As DataRow In AcquiredPositionsDT.Rows cRec = New Transaction cRec.type = "Sell" cRec.securityType = "Option" cRec.symbol = APRow("Symbol").Trim() cRec.familyDelta = recommendation.familyDelta cRec.familyTicker = recommendation.familyTicker adjustment = 0 If IsACall(cRec.symbol) Then If IsInTheFamily(cRec.symbol, cRec.familyTicker) Then cRec.currentPositionInAP = APRow("Units") If cRec.currentPositionInAP > 0 Then ' to sell, you need to have If GetBid(cRec.symbol, tDate) > 0 Then ' no price, no selling cRec.delta = CalcDelta(cRec.symbol, tDate) cRec.hedgeQty = CalcQtyNeededToHedge(cRec) If cRec.hedgeQty > 0 Then If cRec.currentPositionInAP < cRec.hedgeQty Then cRec.qty = cRec.currentPositionInAP ' sell all you have adjustment = -50 ' because incomplete hedge Else cRec.qty = cRec.hedgeQty End If cRec.score = baseScore + adjustment CandidateRecommendationsList.Add(cRec) End If End If End If End If End If Next End Sub Public Sub ScoreSellingShortCall(baseScore As Integer, recommendation As Transaction, tDate As Date) Dim adjustment, maxshort Dim cRec As Transaction If TooCloseToMaxMargins() Then Exit Sub ' we have no more credit End If For Each partialSymbol As String In {"_COCTE", "_COCTD", "_COCTC", "_COCTB", "_COCTA"} ' only these options will be considered, in this order cRec = New Transaction ' you might add/subtract To/from the list, for example adding the JULY otions cRec.type = "SellShort" cRec.securityType = "Option" cRec.symbol = recommendation.familyTicker + partialSymbol cRec.familyDelta = recommendation.familyDelta cRec.familyTicker = recommendation.familyTicker adjustment = 0 If Not IsInIP(cRec.symbol) Then cRec.currentPositionInAP = GetCurrentPositionInAP(cRec.symbol) If cRec.currentPositionInAP <= 0 Then ' because if long cannot sell short If GetBid(cRec.symbol, tDate) > 0 Then cRec.delta = CalcDelta(cRec.symbol, tDate) cRec.hedgeQty = CalcQtyNeededToHedge(cRec) maxshort = MaxShortWithinConstraints(cRec.symbol, tDate) If cRec.hedgeQty > maxshort Then cRec.qty = maxshort adjustment = -50 Else cRec.qty = cRec.hedgeQty End If If cRec.qty > 0 Then Select Case partialSymbol Case "_COCTE" adjustment = adjustment + 1 Case "_COCTD" adjustment = adjustment + 2 Case "_COCTC" adjustment = adjustment + 4 Case "_COCTB" adjustment = adjustment + 5 Case "_COCTA" adjustment = adjustment + 3 End Select cRec.score = baseScore + adjustment CandidateRecommendationsList.Add(cRec) End If End If End If End If Next End Sub Public Sub ScoreBuyingBackPut(baseScore As Integer, recommendation As Transaction, tDate As Date) Dim adjustment, maxbuy As Decimal Dim cRec As Transaction ' this is a CANDIDATE rec, do not confuse with recommendation! If AvailableCashIsLow() Then Exit Sub End If For Each dr As DataRow In AcquiredPositionsDT.Rows cRec = New Transaction cRec.familyDelta = recommendation.familyDelta cRec.type = "Buy" cRec.securityType = "Option" cRec.symbol = dr("Symbol").Trim() cRec.familyTicker = recommendation.familyTicker If IsAStock(cRec.symbol) Or cRec.symbol = "CAccount" Then ' skip Else cRec.currentPositionInAP = dr("Units") If cRec.currentPositionInAP < 0 Then ' no short no buyback If IsAPut(cRec.symbol) Then cRec.underlier = GetUnderlier(cRec.symbol) If cRec.underlier = cRec.familyTicker Then cRec.delta = CalcDelta(cRec.symbol, tDate) cRec.hedgeQty = CalcQtyNeededToHedge(cRec) If cRec.hedgeQty > Math.Abs(cRec.currentPositionInAP) Then cRec.hedgeQty = Math.Abs(cRec.currentPositionInAP) ' buyback all shorts adjustment = -50 End If maxbuy = MaxPurchasePossible(cRec.symbol, tDate) ' how much can you afford? If maxbuy < cRec.hedgeQty Then cRec.qty = maxbuy adjustment = adjustment - 50 Else cRec.qty = cRec.hedgeQty End If If cRec.qty > 0 Then cRec.score = baseScore + adjustment CandidateRecommendationsList.Add(cRec) End If End If End If End If End If Next End Sub Public Sub ScoreBuyingPut(baseScore As Integer, recommendation As Transaction, tDate As Date) Dim adjustment As Decimal Dim maxBuy As Decimal Dim cRec As Transaction ' this is a CANDIDATE rec, do not confuse with recommendation! If AvailableCashIsLow() Then Exit Sub End If ' only considers OCT options - you can easily change this: talk to me For Each partialSymbol As String In {"_POCTA", "_POCTB", "_POCTC", "_POCTD", "_POCTE"} cRec = New Transaction cRec.type = "Buy" cRec.securityType = "Option" cRec.symbol = recommendation.familyTicker + partialSymbol cRec.familyDelta = recommendation.familyDelta cRec.familyTicker = recommendation.familyTicker adjustment = 0 If Not IsInIP(cRec.symbol) Then If GetAsk(cRec.symbol, tDate) <> 0 Then ' no price no buy cRec.currentPositionInAP = GetCurrentPositionInAP(cRec.symbol) If cRec.currentPositionInAP >= 0 Then ' if short it is a buyback cRec.delta = CalcDelta(cRec.symbol, tDate) cRec.hedgeQty = CalcQtyNeededToHedge(cRec) maxBuy = MaxPurchasePossible(cRec.symbol, tDate) ' how much can you afford? If maxBuy < cRec.hedgeQty Then cRec.qty = maxBuy adjustment = -50 Else cRec.qty = cRec.hedgeQty End If If cRec.qty > 0 Then Select Case partialSymbol Case "_POCTE" adjustment = adjustment + 4 ' preferences - need work Case "_POCTD" adjustment = adjustment + 2 Case "_POCTC" adjustment = adjustment + 3 Case "_POCTB" adjustment = adjustment + 5 Case "_POCTA" adjustment = adjustment + 1 End Select cRec.score = baseScore + adjustment CandidateRecommendationsList.Add(cRec) End If End If End If End If Next End Sub Public Sub ScoreSellingShortStock(baseScore As Integer, recommendation As Transaction, tDate As Date) If TooCloseToMaxMargins() Then Exit Sub End If Dim adjustment, maxShort As Decimal Dim cRec As New Transaction ' this is a CANDIDATE rec, do not confuse with recommendation! cRec.symbol = recommendation.familyTicker cRec.type = "SellShort" cRec.securityType = "Stock" cRec.familyDelta = recommendation.familyDelta If IsInIP(cRec.symbol) Then Exit Sub ' cannot sellshort if in IP Else cRec.currentPositionInAP = GetCurrentPositionInAP(cRec.symbol) If cRec.currentPositionInAP > 0 Then ' we cannot sellshort if long Exit Sub Else cRec.delta = 1 cRec.hedgeQty = CalcQtyNeededToHedge(cRec) maxShort = MaxShortWithinConstraints(cRec.symbol, tDate) If cRec.hedgeQty > maxShort Then cRec.qty = maxShort adjustment = -50 Else cRec.qty = cRec.hedgeQty End If If cRec.qty > 0 Then cRec.score = baseScore + adjustment CandidateRecommendationsList.Add(cRec) End If End If End If End Sub Public Sub ScoreSellingPut(baseScore As Integer, recommendation As Transaction, tDate As Date) Dim adjustment As Decimal = 0 Dim cRec As Transaction ' this is a CANDIDATE rec, do not confuse with recommendation! For Each APRow As DataRow In AcquiredPositionsDT.Rows cRec = New Transaction cRec.type = "Sell" cRec.securityType = "Option" cRec.symbol = APRow("Symbol").Trim() cRec.familyDelta = recommendation.familyDelta cRec.familyTicker = recommendation.familyTicker adjustment = 0 If IsAPut(cRec.symbol) Then If IsInTheFamily(cRec.symbol, cRec.familyTicker) Then cRec.currentPositionInAP = APRow("Units") If cRec.currentPositionInAP > 0 Then ' to sell, you need to have cRec.delta = CalcDelta(cRec.symbol, tDate) cRec.hedgeQty = CalcQtyNeededToHedge(cRec) If cRec.hedgeQty > 0 Then If cRec.currentPositionInAP < cRec.hedgeQty Then cRec.qty = cRec.currentPositionInAP ' sell all you have adjustment = -50 ' because incomplete hedge Else cRec.qty = cRec.hedgeQty End If cRec.score = baseScore + adjustment CandidateRecommendationsList.Add(cRec) End If End If End If End If Next End Sub Public Sub ScoreSellingShortPut(baseScore As Integer, recommendation As Transaction, tDate As Date) Dim adjustment, maxshort Dim cRec As Transaction If TooCloseToMaxMargins() Then Exit Sub ' we have no more credit End If For Each partialSymbol As String In {"_POCTE", "_POCTD", "_POCTC", "_POCTB", "_POCTA"} ' only these options will be considered, in this order cRec = New Transaction ' you might add/subtract To/from the list, for example adding the JULY otions cRec.type = "SellShort" cRec.securityType = "Option" cRec.symbol = recommendation.familyTicker + partialSymbol cRec.familyDelta = recommendation.familyDelta cRec.familyTicker = recommendation.familyTicker adjustment = 0 If Not IsInIP(cRec.symbol) Then If GetBid(cRec.symbol, tDate) > 0 Then cRec.currentPositionInAP = GetCurrentPositionInAP(cRec.symbol) If cRec.currentPositionInAP <= 0 Then ' because if long cannot sell short cRec.delta = CalcDelta(cRec.symbol, tDate) cRec.hedgeQty = CalcQtyNeededToHedge(cRec) maxshort = MaxShortWithinConstraints(cRec.symbol, tDate) If cRec.hedgeQty > maxshort Then cRec.qty = maxshort adjustment = -50 Else cRec.qty = cRec.hedgeQty End If If cRec.qty > 0 Then Select Case partialSymbol Case "_POCTE" adjustment = adjustment + 5 Case "_POCTD" adjustment = adjustment + 4 Case "_POCTC" adjustment = adjustment + 3 Case "_POCTB" adjustment = adjustment + 2 Case "_POCTA" adjustment = adjustment + 1 End Select cRec.score = baseScore + adjustment CandidateRecommendationsList.Add(cRec) End If End If End If End If Next End Sub Public Sub ScoreBuyingBackCall(baseScore As Integer, recommendation As Transaction, tDate As Date) Dim adjustment, maxbuy As Decimal Dim cRec As Transaction ' this is a CANDIDATE rec, do not confuse with recommendation! If AvailableCashIsLow() Then Exit Sub End If For Each dr As DataRow In AcquiredPositionsDT.Rows cRec = New Transaction cRec.type = "Buy" cRec.securityType = "Option" cRec.symbol = dr("Symbol").Trim() cRec.familyDelta = recommendation.familyDelta cRec.familyTicker = recommendation.familyTicker adjustment = 0 If IsAStock(cRec.symbol) Or cRec.symbol = "CAccount" Then ' skip Else If IsACall(cRec.symbol) Then cRec.underlier = GetUnderlier(cRec.symbol) If GetUnderlier(cRec.symbol) = cRec.familyTicker Then cRec.currentPositionInAP = dr("Units") If cRec.currentPositionInAP < 0 Then cRec.delta = CalcDelta(cRec.symbol, tDate) cRec.hedgeQty = CalcQtyNeededToHedge(cRec) If Math.Abs(cRec.currentPositionInAP) < cRec.hedgeQty Then cRec.hedgeQty = Math.Abs(cRec.currentPositionInAP) ' buy back all that you have adjustment = -50 End If maxbuy = MaxPurchasePossible(cRec.symbol, tDate) ' how much can you afford? If maxbuy < cRec.hedgeQty Then cRec.qty = maxbuy adjustment = adjustment - 50 Else cRec.qty = cRec.hedgeQty End If If cRec.qty > 0 Then cRec.score = baseScore + adjustment CandidateRecommendationsList.Add(cRec) End If End If End If End If End If Next End Sub Public Sub ScoreBuyingBackStock(baseScore As Integer, recommendation As Transaction, tDate As Date) If AvailableCashIsLow() Then Exit Sub End If Dim cRec As New Transaction ' this is the CANDIDATE rec, do not confuse with recommendation! cRec.symbol = recommendation.familyTicker cRec.type = "Buy" cRec.securityType = "Stock" cRec.familyDelta = recommendation.familyDelta cRec.familyTicker = recommendation.familyTicker Dim adjustment, maxBuy As Decimal If IsInIP(cRec.symbol) Then Exit Sub ' cannot buy if in IP End If cRec.currentPositionInAP = GetCurrentPositionInAP(cRec.symbol) If cRec.currentPositionInAP < 0 Then ' buy back requires a short cRec.delta = 1 cRec.hedgeQty = CalcQtyNeededToHedge(cRec) maxBuy = MaxPurchasePossible(cRec.symbol, tDate) ' how many can we afford? If maxBuy < cRec.hedgeQty Then cRec.hedgeQty = maxBuy adjustment = -50 End If If cRec.hedgeQty > Math.Abs(cRec.currentPositionInAP) Then cRec.qty = Math.Abs(cRec.currentPositionInAP) ' buy back all adjustment = -50 Else cRec.qty = cRec.hedgeQty End If If cRec.qty > 0 Then cRec.score = baseScore + adjustment CandidateRecommendationsList.Add(cRec) End If End If End Sub Public Sub ScoreBuyingCall(baseScore As Integer, recommendation As Transaction, tDate As Date) Dim adjustment As Decimal = 0 Dim maxBuy As Decimal Dim cRec As Transaction ' this is a CANDIDATE rec, do not confuse with recommendation! If AvailableCashIsLow() Then Exit Sub End If ' only considers OCT options - you can easily change this: talk to me For Each partialSymbol As String In {"_COCTE", "_COCTD", "_COCTC", "_COCTB", "_COCTA"} cRec = New Transaction cRec.type = "Buy" cRec.securityType = "Option" cRec.symbol = recommendation.familyTicker + partialSymbol cRec.familyDelta = recommendation.familyDelta cRec.familyTicker = recommendation.familyTicker adjustment = 0 If Not IsInIP(cRec.symbol) Then cRec.currentPositionInAP = GetCurrentPositionInAP(cRec.symbol) If cRec.currentPositionInAP >= 0 Then ' if short is a buyback cRec.delta = CalcDelta(cRec.symbol, tDate) cRec.hedgeQty = CalcQtyNeededToHedge(cRec) maxBuy = MaxPurchasePossible(cRec.symbol, tDate) ' how much can you afford? If maxBuy < cRec.hedgeQty Then cRec.qty = maxBuy adjustment = -50 Else cRec.qty = cRec.hedgeQty End If If cRec.qty > 0 Then Select Case partialSymbol ' think about these scores Case "_COCTE" adjustment = adjustment + 1 Case "_COCTD" adjustment = adjustment + 2 Case "_COCTC" adjustment = adjustment + 3 Case "_COCTB" adjustment = adjustment + 4 Case "_COCTA" adjustment = adjustment + 5 End Select cRec.score = baseScore + adjustment CandidateRecommendationsList.Add(cRec) End If End If End If Next End Sub End Module