我在一个文件中有交易数据:
TIME|DIRECTION|ORDERID|SYMBOL|ROUTE||QUANTITY|PRICE
2012-05-29 11:17:18|B|1337|ZNGA|ARCA||6500|5.48
2012-05-29 11:17:53|B|1389|AAPL|ARCA||200|569.85
2012-05-29 11:18:22|S|1970|BRK/B|ARCA||600|77.48
2012-05-29 11:18:24|B|2335|BRK/B|ARCA||600|79.3
2012-05-29 11:19:27|B|2416|ZNGA|ARCA||4000|6.08
2012-05-29 11:19:29|S|2997|LNKD|ARCA||500|91.27
2012-05-29 11:19:40|S|3078|ZNGA|ARCA||3900|6.04
我正在尝试编写VBA代码,该代码将使用数据并基于代码(累积数量,价格等)返回统计信息。我已经使用字典对象和类对象编写了代码。这是一项任务,所以我必须那样做。
我想以一个字典结尾,其中每个键代表一个不同的代码(例如,AAPL,ZNGA等),并且值代表不同的统计信息。最终价格,最终数量等
因此,使用上面的文件片段,我应该得到:
ticker,cashVal,positionVal,endPriceVal,maxDateVal
ZNGA,39864,6600,6.04,2012-05-29 11:19:40
AAPL,113970,200,569.85,2012-05-29 11:17:53
...等等。
我有以下VBA代码:
Sub defineVar()
myFile = Range("$A$2")
End Sub
Public Sub test_everything()
Call defineVar
'Dim line As trade
Dim lines As Collection
Set lines = New Collection
Open myFile For Input As #1
Do Until EOF(1)
Line Input #1, file_line
lines.Add file_line
Loop
Close #1
Dim posTime As Byte
Dim posDirection As Byte
Dim posSymbol As Byte
Dim posQuantity As Byte
Dim posPrice As Byte
posTime = getColPosByHeader("TIME", lines(1))
posDirection = getColPosByHeader("DIRECTION", lines(1))
posSymbol = getColPosByHeader("SYMBOL", lines(1))
posQuantity = getColPosByHeader("QUANTITY", lines(1))
posPrice = getColPosByHeader("PRICE", lines(1))
Dim pos As Long
Dim ticker As String
Dim trades As Scripting.Dictionary
Set trades = New Scripting.Dictionary
Dim trade As Stock
Set trade = New Stock
pos = 2
While pos <= lines.Count
lineItems = Split(lines(pos), "|")
ticker = lineItems(posSymbol)
If Not trades.exists(ticker) Then
trade.symbol = ticker
trade.cashVal = 0
trade.positionVal = lineItems(posQuantity)
trade.endPriceVal = lineItems(posPrice)
trade.maxDateVal = lineItems(posTime)
trades.Add trade.symbol, trade
If InStr(1, lineItems(posDirection), "B") Then
trades(ticker).cashVal = trades(ticker).cashVal - lineItems(posQuantity) * lineItems(posPrice)
Else: trades(ticker).cashVal = trades(ticker).cashVal + lineItems(posQuantity) * lineItems(posPrice)
End If
Else:
If InStr(1, lineItems(posDirection), "B") Then
trades(ticker).cashVal = trades(ticker).cashVal - lineItems(posQuantity) * lineItems(posPrice)
trades(ticker).positionVal = trades(ticker).positionVal + lineItems(posQuantity)
If lineItems(posTime) >= trades(ticker).maxDateVal Then
trades(ticker).maxDateVal = lineItems(posTime)
trades(ticker).endPriceVal = lineItems(posPrice)
End If
Else: trades(ticker).cashVal = trades(ticker).cashVal + lineItems(posQuantity) * lineItems(posPrice)
trades(ticker).positionVal = trades(ticker).positionVal - lineItems(posQuantity)
If lineItems(posTime) >= trades(ticker).maxDateVal Then
trades(ticker).maxDateVal = lineItems(posTime)
trades(ticker).endPriceVal = lineItems(posPrice)
End If
End If
End If
Debug.Print trades(ticker).positionVal
pos = pos + 1
Wend
Dim k As Variant
For Each k In trades.Keys
Debug.Print k, trades(k).cashVal, trades(k).positionVal, trades(k).endPriceVal, trades(k).maxDateVal
Next
End Sub
这是我的类对象的样子:
'Class Module: Stock
Public symbol As String
Public cashVal As Double
Public positionVal As Double
Public endPriceVal As Double
Public maxDateVal As Date
我的问题是字典返回所有附加到不同键的相同值。按键之间没有区别。我一直在梳理代码中的一个逻辑错误,但没有看到。我在做什么错了?