What are the three rows that you need? I would have thought two is enough
(my code only requires two).
You need to determine what event will invoke the calculation. This could be
simply a decision to run a process that updates the database, or it could be
an event signalled by the arrival of a new item of data, or it could be a
timer event that causes you to go look at the database and see if something
new has happened.
Whatever the event is, the procedure is essentially the same.
When you need to update the database with the result of the calculation,
simply get the two lines of data from the database (T and T-1), extract the
prices you need, call the calculation routine, and put the result back into
the database at row T. A form of my original function rewritten to be
called
once for each row would look something like this:
Private Function CrossOverTest(PriceA1 As Double, ByVal PriceA2 As Double, _
PriceB1 As Double, ByVal PriceB2 As Double) _
As Boolean
'Test these two price pairs and return true on change from previous
value of 0 to +
Dim C0 As Integer = Math.Sign(PriceA1 - PriceA2)
Dim C1 As Integer = Math.Sign(PriceB1 - PriceB2)
If C0 = 0 And C1 = +1 Then
Result = True
Else
Result = False
End If
Return Result
End Function
You would call the function using code like this:
'Get crossover value (true/false) for last (most recent) row of table
Dim T as integer = dt.rows.count-1
Dim R1 as DataRow = dt.Rows(T-1)
Dim R2 as DataRow = dt.Rows(T)
Dim PA1 As Double = R1.Columns("Price1")
Dim PA2 As Double = R1.Columns("Price2")
Dim PB1 As Double = R2.Columns("Price1")
Dim PB2 As Double = R2.Columns("Price2")
R2.Columns("Crossed") = CrossOverTest(PA1, PA2, PB1, PB2)