Re: How to implement a dynamic "lookback" period?
Thanks; unfortunately this did not produce the desired result. The code provided appears to say if the three conditions are True then "position exists" and don't do the returns calc. What I am actually trying to do is as follows:
- There are three flags, two of which have "lookback periods" (flags 1 and 3) - i.e., they are "on" or =1 in the current row (current i) if they were true in any of the previous rows in the lookback period (these "lookbacks" are the loops from j to)
- If the three flags are "on" or =1 then the "action_flag" is turned on and a calculation is done
- The above works fine but I am now trying to add a condition that if the "action_flag" was on in the lookback rows (2 previous rows in this case) then it should not be switched on (because I am trying to avoid triggering the action flag in consecutive periods by implementing a condition that if it was already on in either of the last 2 periods then don't switch it on again)
Recopying previous code for convenience:
Code:
Sub test_loop()
Dim i As Long
Dim data_set As Variant
data_set = Sheets("Sheet1").Range("A2:G13")
lookback = 2
For i = lookback + 1 To UBound(data_set, 1)
For j = i - lookback To i 'This is the loop with the flag that has a lookback period - flag_one based on trigger_one
trigger_one = data_set(j, 2)
If trigger_one > 0 And IsNumeric(trigger_one) Then
flag_one = 1
Exit For
End If
Next j
For j = i - lookback To i 'This is the loop with the flag that has a lookback period - flag_one based on trigger_one
trigger_three = data_set(j, 4)
If trigger_three > 0 And IsNumeric(trigger_three) Then
flag_three = 1
Exit For
End If
Next j
If flag_one = 1 Then
flag_one_count = flag_one_count + 1
total_one_value = total_one_value + data_set(i, 5)
End If
If flag_three = 1 Then
flag_three_count = flag_three_count + 1
End If
trigger_two = data_set(i, 3) 'This is the flag with no lookback period - flag_two based on trigger_two
If trigger_two > 0 And IsNumeric(trigger_two) Then
flag_two = 1
flag_two_count = flag_two_count + 1
End If
'This part not working - I am trying to implement a check to see if the "action flag" below (combination of flags 1-3) was already triggered in the lookback period; in which case I don't want it to switch on in this period
For k = i - lookback To i - 1
If flag_one = 1 And flag_two = 1 And flag_three = 1 Then
existing_position = 1
Exit For
End If
Next k
If flag_one = 1 And flag_two = 1 And flag_three = 1 And existing_position <> 1 Then 'If both flag_one (which has a lookback period) and flag_two (which does not have a lookback period) are equal to one then do the following
Action_Flag = 1
Action_Flag_Count = Action_Flag_Count + 1
total_returns = total_returns + data_set(i, 5)
End If
'///////////////////////////////
'Reset Values for the next loop|
'\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
trigger_one = 0
trigger_two = 0
flag_one = 0
flag_two = 0
trigger_three = 0
flag_three = 0
existing_position = 0
Next i
Sheets("Sheet1").Range("F17") = flag_one_count
Sheets("Sheet1").Range("F18") = flag_three_count
Sheets("Sheet1").Range("F19") = flag_two_count
Sheets("Sheet1").Range("F20") = Action_Flag_Count
Sheets("Sheet1").Range("F21") = total_returns / Action_Flag_Count
End Sub
[TABLE="width: 329"]
<colgroup><col><col span="4"></colgroup><tbody>[TR]
[TD]row_count[/TD]
[TD]flag_1[/TD]
[TD]flag_2[/TD]
[TD]flag_3[/TD]
[TD]returns[/TD]
[/TR]
[TR]
[TD="align: right"]1[/TD]
[TD="align: right"]0[/TD]
[TD="align: right"]0[/TD]
[TD="align: right"]0[/TD]
[TD][/TD]
[/TR]
[TR]
[TD="align: right"]2[/TD]
[TD="align: right"]0[/TD]
[TD="align: right"]0[/TD]
[TD="align: right"]0[/TD]
[TD][/TD]
[/TR]
[TR]
[TD="align: right"]3[/TD]
[TD="align: right"]0[/TD]
[TD="align: right"]1[/TD]
[TD="align: right"]0[/TD]
[TD="align: right"]22%[/TD]
[/TR]
[TR]
[TD="align: right"]4[/TD]
[TD="align: right"]0[/TD]
[TD="align: right"]1[/TD]
[TD="align: right"]0[/TD]
[TD="align: right"]83%[/TD]
[/TR]
[TR]
[TD="align: right"]5[/TD]
[TD="align: right"]0[/TD]
[TD="align: right"]1[/TD]
[TD="align: right"]1[/TD]
[TD="align: right"]82%[/TD]
[/TR]
[TR]
[TD="align: right"]6[/TD]
[TD="align: right"]1[/TD]
[TD="align: right"]1[/TD]
[TD="align: right"]1[/TD]
[TD="align: right"]69%[/TD]
[/TR]
[TR]
[TD="align: right"]7[/TD]
[TD="align: right"]1[/TD]
[TD="align: right"]0[/TD]
[TD="align: right"]0[/TD]
[TD="align: right"]74%[/TD]
[/TR]
[TR]
[TD="align: right"]8[/TD]
[TD="align: right"]1[/TD]
[TD="align: right"]1[/TD]
[TD="align: right"]0[/TD]
[TD="align: right"]89%[/TD]
[/TR]
[TR]
[TD="align: right"]9[/TD]
[TD="align: right"]0[/TD]
[TD="align: right"]1[/TD]
[TD="align: right"]0[/TD]
[TD="align: right"]17%[/TD]
[/TR]
[TR]
[TD="align: right"]10[/TD]
[TD="align: right"]0[/TD]
[TD="align: right"]1[/TD]
[TD="align: right"]0[/TD]
[TD="align: right"]73%[/TD]
[/TR]
[TR]
[TD="align: right"]11[/TD]
[TD="align: right"]0[/TD]
[TD="align: right"]0[/TD]
[TD="align: right"]0[/TD]
[TD="align: right"]71%[/TD]
[/TR]
[TR]
[TD="align: right"]12[/TD]
[TD="align: right"]0[/TD]
[TD="align: right"]0[/TD]
[TD="align: right"]0[/TD]
[TD="align: right"]13%[/TD]
[/TR]
</tbody>[/TABLE]