Let attach a sample that should answer many of the questions. There will a wick if statement to do this, and I'll include my code so far.
Work in progress not finished
Code: Select all
Function CalcProfit(Profit)
'This Function Will Calculate The Profit
Dim db As DAO.Database
Dim Rs As DAO.Recordset
Dim Fld2 As Field, Fld3 As Field
Dim Fld4 As Field, Fld6 As Field
Dim Fld11 As Field, Fld12 As Field
Dim I As Long, intCurQty As Integer, intnextQty As Integer, intbackRec As Integer, ContractsLeft As Integer
Dim dblNextAmt As Double, dblCurAmt As Double
Dim strCurSymbol As String, strNextSymbol As String, strCurCon As String, strNextCon As String
Set db = CurrentDb
Set Rs = db.OpenRecordset("Select * From Trades Order By Tradedate")
Set Fld2 = Rs!Tradedate
Set Fld3 = Rs!Symbol
Set Fld4 = Rs!ContractMonth
Set fld5 = Rs!Quantity
Set Fld6 = Rs!ActionID
Set Fld11 = Rs!Amount
Set Fld12 = Rs!Profit
Rs.MoveFirst
Rs.MoveLast
strCurSymbol = Fld3
strCurCon = Fld4
intCurQty = fld5
dblCurAmt = Fld11
'Rs.MoveLast
For I = Rs.RecordCount To 2 Step -1
If I < Rs.RecordCount Then
If Fld6 = 46 Or Fld6 = 48 Then
'Find prev opening trades
If Fld3 = strCurSymbol And Fld4 = strCurCon Then 'And Abs(intCurQty) - Abs(fld5) = 0
'Found prev opening symbol and contract
If Abs(fld5) - Abs(intCurQty) <> 0 Then
ContractsLeft = fld5 + intCurQty
'closing quantity equals prev opening quan
dblNextAmt = Fld11 / Abs(fld5)
Rs.MoveLast ' move to the closing trade
'fld5 = ContractsLeft=
Rs.Edit
Fld12 = dblNextAmt + dblCurAmt
Rs.Update
If ContractsLeft = 0 Then
Exit For
End If
End If
End If
End If
End If
Rs.MovePrevious
Next I
Set Rs = Nothing
Set db = Nothing
End Function