Formula for if 2 criteria are met

Trie1977

New Member
Joined
Mar 7, 2022
Messages
22
Office Version
  1. 2019
Platform
  1. Windows
Hi there,

Played around now for some days.
Hope to find an answer here, and I would be very happy.

Count.xlsx
ABCDEFGHIJKLMNOPQR
1
2I am not able to combine A10 with A12 for exampleFormula triedShould Be
3Pips+ & OI +99why this seems to work?Query 1 : count how many times it occures that the week closed positive AND the Open Interest was higher than the previous week.
4Pips- & OI +5213and why not this?Query 2 : count how many times it occures that the week closed negative AND the Open Interest was higher than the previous week.
5Pips+ & OI loweri have no clueQuery 3 : count how many times it occures that the week closed positive AND the Open Interest was lower than the previous week.
6Pips- & OI loweri have no clueQuery 4 : count how many times it occures that the week closed negative AND the Open Interest was lower than the previous week.
7Pips- & OI lower streaki have no clueQuery 5 : count how many times it occures that the week closed negative AND the Open Interest was higher for 2 weeks in a row.
8
9Already figured out
10Pips+31Query 6 : count how many times bars closed higher
11Pips-30Query 7 : count how many times bars closed lower
12OI Higher Streaks >16Query 8 : count how many times it occures that the Open Interest was higher for 2 weeks in a row.
13OI Lower Streaks >19Query 9 : count how many times it occures that the Open Interest was lower for 2 weeks in a row.
Formulas
Cell Formulas
RangeFormula
B3B3=COUNTIFS('GBPUSD10080 Result after'!$J$5:$J$65,">1"&SUMPRODUCT(--('GBPUSD10080 Result after'!$N$5:$N$65>'GBPUSD10080 Result after'!$N$6:$N$66)))
B4B4=COUNTIFS('GBPUSD10080 Result after'!$J$5:$J$65,"<1"&SUMPRODUCT(--('GBPUSD10080 Result after'!$N$5:$N$65>'GBPUSD10080 Result after'!$N$6:$N$66)))
B10B10=COUNTIFS('GBPUSD10080 Result after'!$J$5:$J$65,">0")
B11B11=COUNTIFS('GBPUSD10080 Result after'!$J$5:$J$65,"<0")
B12B12=SUM(--(FREQUENCY( IF('GBPUSD10080 Result after'!$N$5:$N$65>'GBPUSD10080 Result after'!$N$6:$N$66, ROW('GBPUSD10080 Result after'!$N$5:$N$65)), IF('GBPUSD10080 Result after'!$N$5:$N$65<='GBPUSD10080 Result after'!$N$6:$N$65, ROW( 'GBPUSD10080 Result after'!$N$5:$N$65))) >1))
B13B13=SUM(--(FREQUENCY( IF('GBPUSD10080 Result after'!$N$5:$N$65<'GBPUSD10080 Result after'!$N$6:$N$66, ROW('GBPUSD10080 Result after'!$N$5:$N$65)), IF('GBPUSD10080 Result after'!$N$5:$N$65>='GBPUSD10080 Result after'!$N$6:$N$65, ROW( 'GBPUSD10080 Result after'!$N$5:$N$65))) >1))
Press CTRL+SHIFT+ENTER to enter array formulas.


DataSheet;

Count.xlsx
BCDEHJKLNOPQRSTUVWXYZAA
2BRITISH POUND - CHICAGO MERCANTILE EXCHANGEUSD INDEX - ICE FUTURES U.S.
3
4COT GetCOT DateStart CandleOpenCloseClosesPipsUpDownOI GBPOI USDGBP LongGBP ShortTotalGBP % LGBP % SNet Pos.USD LongUSD ShortTotalUSD % LongUSD % ShortNet Pos.
5############2022.02.27########-78128-107211869566514767948016####49,82%50,18%-337437618987####83%17%34774
6############2022.02.20########-17753-313188443549224224948058####46,78%53,22%-58094372676425136885%15%36084
7############2022.02.13########4492-64195302542835015147914####51,14%48,86%22374234969634931286%14%35386
8############2022.02.06########27111-42197948536034470953254####45,64%54,36%-8545403706605####86%14%33765
9############2022.01.30########134235-6184007564772959753202####35,75%64,25%-23605438979326####82%18%34571
10############2022.01.23########-15614-193182040523283666644429####45,21%54,79%-7763417724911####89%11%36861
11############2022.01.16########-12117-128183234532833978040007####49,87%50,15%-247423695935####88%12%36434
12############2022.01.09########103169-48#####572183050659672####33,83%66,17%-29166458727980####85%15%37892
13############2022.01.02########6269-99#####5808925980651519113128,51%71,49%-39171477508672####85%15%39078
14############2021.12.26########150158-3#####578922309373812####23,83%76,17%-507194716910380####49%11%36789
15############2021.12.19########152198-66#####561622082478510####20,96%79,04%-576864622211107####47%11%35115
16############2021.12.12########-12128-75213210520982949780245####26,88%73,12%-507484226311022####39%10%31241
17############2021.12.05########4571-57238310599944895087227####35,95%64,05%-382774305881935125132%6%34865
18############2021.11.28########-10238-138#####521725209990998####36,41%63,59%-388994201061314814129%4%35879
19############2021.11.21########-1202-174213313576945012284701####37,18%62,82%-345794711611341####35%8%35775
20############2021.11.14########36107-11#####593875044382042####38,07%61,93%-3159947959130516101036%10%34908
21############2021.11.07########-64130-124186898577135400466097####44,97%55,03%-120934652111073####39%9%35448
22############2021.10.31########-18024-251166780579475725542208####57,56%42,44%150474539510413####46%10%34982
23############2021.10.24########-4987-75156495589935191236959####58,41%41,59%1495345189107325592151%12%34457
24############2021.10.17########179-46158961607314911247497####50,84%49,16%16154756811634####49%12%35934
25############2021.10.10########131158-46172829628364679458773####44,33%55,67%-119794939414332####47%14%35062
26############2021.10.03########5898-27182338571944813768155####41,39%58,61%-200184471512689####38%11%32026
GBPUSD10080 Result after
Cell Formulas
RangeFormula
J5:J26J5=(H5-E5)/10
K5:K26K5=(F5-E5)/10
L5:L26L5=(G5-E5)/10
Y14:Y26,S5:S26S5=IF(P5="","",P5/$R5)
Z14:Z26,T5:T26T5=IF(P5="","",Q5/$R5)
Y5:Y13Y5=IF(V5="","",V5/$X5)
Z5:Z13Z5=IF(V5="","",W5/$X5)
X14:X26,R14:R26R14=P14+Q14
AA14:AA26,U14:U26U14=P14-Q14
Cells with Conditional Formatting
CellConditionCell FormatStop If True
Q5:AA13Cell Value<Q6textNO
Q5:AA13Cell Value>Q6textNO
P6:P13Cell Value<P7textNO
P6:P13Cell Value>P7textNO
P5,P14:AA64Cell Value<P6textNO
P5,P14:AA64Cell Value>P6textNO
O5:O64Cell Value<$O6textNO
O5:O64Cell Value>$O6textNO
N5:N64Cell Value<$N6textNO
N5:N64Cell Value>$N6textNO
L5:L65Cell Value<0textNO
L5:L65Cell Value>0textNO
K5:K65Cell Value<0textNO
K5:K65Cell Value>0textNO
J5:J65Cell Value<0textNO
J5:J65Cell Value>0textNO



Thank anyways for your time.
Tony.
 

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
Tony, Have a look at this and see if it produces the answers you expect. I can't see the entire range referred to on the data sheet, so I'm not certain. I've offered some alternative formulas for rows 10-13 as well.
MrExcel_20220310_Trie.xlsx
ABCDEF
1
2I am not able to combine A10 with A12 for exampleFormula triedShould BeResults
3Pips+ & OI +49why this seems to work?Query 1 : count how many times it occures that the week closed positive AND the Open Interest was higher than the previous week.6
4Pips- & OI +1813and why not this?Query 2 : count how many times it occures that the week closed negative AND the Open Interest was higher than the previous week.5
5Pips+ & OI loweri have no clueQuery 3 : count how many times it occures that the week closed positive AND the Open Interest was lower than the previous week.6
6Pips- & OI loweri have no clueQuery 4 : count how many times it occures that the week closed negative AND the Open Interest was lower than the previous week.5
7Pips- & OI lower streaki have no clueQuery 5 : count how many times it occures that the week closed negative AND the Open Interest was higher for 2 weeks in a row.3
8
9Already figured out
10Pips+12Query 6 : count how many times bars closed higher12
11Pips-10Query 7 : count how many times bars closed lower10
12OI Higher Streaks >12Query 8 : count how many times it occures that the Open Interest was higher for 2 weeks in a row.6
13OI Lower Streaks >14Query 9 : count how many times it occures that the Open Interest was lower for 2 weeks in a row.7
Formulas
Cell Formulas
RangeFormula
B3B3=COUNTIFS('GBPUSD10080 Result after'!$J$5:$J$65,">1"&SUMPRODUCT(--('GBPUSD10080 Result after'!$N$5:$N$65>'GBPUSD10080 Result after'!$N$6:$N$66)))
B4B4=COUNTIFS('GBPUSD10080 Result after'!$J$5:$J$65,"<1"&SUMPRODUCT(--('GBPUSD10080 Result after'!$N$5:$N$65>'GBPUSD10080 Result after'!$N$6:$N$66)))
F3F3=SUMPRODUCT(--('GBPUSD10080 Result after'!$J$5:$J$65>0),--('GBPUSD10080 Result after'!$N$5:$N$65>'GBPUSD10080 Result after'!$N$6:$N$66))
F4F4=SUMPRODUCT(--('GBPUSD10080 Result after'!$J$5:$J$65<0),--('GBPUSD10080 Result after'!$N$5:$N$65>'GBPUSD10080 Result after'!$N$6:$N$66))
F5F5=SUMPRODUCT(--('GBPUSD10080 Result after'!$J$5:$J$65>0),--('GBPUSD10080 Result after'!$N$5:$N$65<'GBPUSD10080 Result after'!$N$6:$N$66))
F6F6=SUMPRODUCT(--('GBPUSD10080 Result after'!$J$5:$J$65<0),--('GBPUSD10080 Result after'!$N$5:$N$65<'GBPUSD10080 Result after'!$N$6:$N$66))
F7F7=SUMPRODUCT(--('GBPUSD10080 Result after'!$J$5:$J$65<0),--('GBPUSD10080 Result after'!$N$5:$N$65>'GBPUSD10080 Result after'!$N$6:$N$66),--('GBPUSD10080 Result after'!$N$6:$N$66>'GBPUSD10080 Result after'!$N$7:$N$67))
B10B10=COUNTIFS('GBPUSD10080 Result after'!$J$5:$J$65,">0")
B11B11=COUNTIFS('GBPUSD10080 Result after'!$J$5:$J$65,"<0")
B12B12=SUM(--(FREQUENCY( IF('GBPUSD10080 Result after'!$N$5:$N$65>'GBPUSD10080 Result after'!$N$6:$N$66, ROW('GBPUSD10080 Result after'!$N$5:$N$65)), IF('GBPUSD10080 Result after'!$N$5:$N$65<='GBPUSD10080 Result after'!$N$6:$N$65, ROW( 'GBPUSD10080 Result after'!$N$5:$N$65))) >1))
B13B13=SUM(--(FREQUENCY( IF('GBPUSD10080 Result after'!$N$5:$N$65<'GBPUSD10080 Result after'!$N$6:$N$66, ROW('GBPUSD10080 Result after'!$N$5:$N$65)), IF('GBPUSD10080 Result after'!$N$5:$N$65>='GBPUSD10080 Result after'!$N$6:$N$65, ROW( 'GBPUSD10080 Result after'!$N$5:$N$65))) >1))
F10F10=SUMPRODUCT(--('GBPUSD10080 Result after'!$J$5:$J$65>0))
F11F11=SUMPRODUCT(--('GBPUSD10080 Result after'!$J$5:$J$65<0))
F12F12=SUMPRODUCT(--('GBPUSD10080 Result after'!$N$5:$N$65>'GBPUSD10080 Result after'!$N$6:$N$66),--('GBPUSD10080 Result after'!$N$6:$N$66>'GBPUSD10080 Result after'!$N$7:$N$67))
F13F13=SUMPRODUCT(--('GBPUSD10080 Result after'!$N$5:$N$65<'GBPUSD10080 Result after'!$N$6:$N$66),--('GBPUSD10080 Result after'!$N$6:$N$66<'GBPUSD10080 Result after'!$N$7:$N$67))
 
Upvote 0
Solution
Hi there!

Thank you so much!

I just used the formulas and it seems it produced the right answers.

Regarding the streaks, I guess I can use my own formula if I want to count streaks with a minimum of lets say 3 weeks in a row?
Because your code counts it for every 1 time. Or is there a better way to count streaks of lets say 3 in a row like I did?

Again, thank so much.
 
Upvote 0
I may have misunderstood your "streaks" description. Taking Query 9 as an example, "count how many times it occures that the Open Interest was lower for 2 weeks in a row." The phrase "Open Interest was lower" doesn't describe what it is relative to. I interpreted this to mean the current week's Open Interest was lower than the Open Interest from the previous week AND that the previous week's Open Interest was lower than the Open Interest from the week before it. If we use some shorthand and describe Open Interest from week number 27 as "OI_27", then (OI_27<OI_26) AND (OI_26<OI_25).

If we have OI values of 1,2,3 for OI_27, OI_26, and OI_25, respectively, then with my interpretation, 1<2 AND 2<3, so this streak would be counted.
If we have OI values of 1,3,3 for OI_27, OI_26, and OI_25, respectively, then with my interpretation, 1<3 but the second condition is not met (3 is not less than 3), so this streak would not be counted.
If we have a streak from current to prior weeks of 1,2,3,4,5, then 1<2 AND 2<3, so this counts as one streak...and 2<3 AND 3<4 counts as another streak...and 3<4 AND 4<5 counts as another streak, so this particular sequence would result in another 3 streaks of 2. Or do you want to count this entire sequence as 1 streak of 2 or more weeks in a row?

Are these interpretations correct?
 
Upvote 0
You did not misunderstood.
I was very happy with the answer.
I played with it and I want some statistics on it but I do not know how to change the formula or use another formula.
Basically what I like is:

  • Count how many times it occures that the Open Interest was higher for 2 weeks in a row.
  • Count how many times a streak occures for 3 or 4 or 5 weeks in a row.
  • And yes, also I like to count a specific streak as an entire sequence.
To clarify;

211869
188443
195302
197948
184007

182040
183234
200493
200007
205378
207747
213210
238310
230423
213313
207099
186898
166780

156495
158961
172829
182338

Bolded is 2 weeks in a row.
Happened 6 times (your current formula).

I also like to measure it like:

Bolded is also 3 weeks in a row. Happended 1 time.
Bolded is also 4 weeks in a row. Happended 1 time.
et cetera.

Lastly, I also want to measure it as like: count this entire sequence as 1 streak of 2 or more weeks in a row.
So in this case it happended 2 times that a streak of 2 weeks occurs.

Hope this explanation helps.

And thank you again for your efforts.
 
Upvote 0
Thanks for the clarification. You're correct...my formula counts each instance of a streak of two consecutive weeks, rather than treating the streak as a single occurrence of two or more weeks in a row, so my streak counting method is probably not what you want. One way of looking at this is to examine what your formula (using FREQUENCY) does. For query 8, which is what your post #5 describes, the FREQUENCY formula essentially goes to each red cell in the Open Interest column and then counts how many green cells are above it until the next red cell is encountered above it. The array formed is a compilation of the number of consecutive green cells encountered each time. So the FREQUENCY function returns {1;0;2;0;1;0;0;0;6;0;0;1;0...}. Your logic check on the FREQUENCY array can be changed to whatever you'd like to examine for a count of the number of green cells or some streak length (currently >1, but could be >2, >=2, >3, etc.). This might be the better approach for handling streaks because it gives you an easily adjustable parameter to examine streaks of specified lengths. A minor point about the FREQUENCY function, your formula uses <= or >= (rather than < or >) when forming the bins array, which means you are identifying streaks where the Open Interest is the same or lower/higher than the previous week's Open Interest, rather than lower/higher than the previous week's.

This leaves one issue unresolved: how to handle the closing positive/negative condition with the streak counting approach, which is what query 5 requires. Could you clarify something about this, please? For Query 5 (count how many times the week closed negative AND there was a streak of at least 2 consecutive weeks where Open Interest was higher), does the condition for "week closed negative" apply only to a target week whose Open Interest was higher than the previous week (and previous week's Open Interest was higher than the week before), regardless of what the closings were for those previous weeks...OR does the language mean that the negative closing must occur for each of the weeks considered in the Open Interest streak? Taking a small section of your data table, when we encounter a closing of -102.4, we see that that week's Open Interest was higher than the previous week's and that the streak of higher Open Interest continued in the previous 4 weeks. But that streak also had one instance where the closing was positive (36.3). What is desired here? Does the positive 36.3 closing break the streak (meaning the AND conditions apply to all elements in a streak) or does the AND condition apply only to the target week? In other words, does this small section show one streak of 5 weeks in length (beginning with a closing of -102.4), or does it show two streaks of 2 weeks in length (one begins with a closing of -102.4, the other with a closing of -64.2)?
ClosesPipsOI GBP
-12.1213210
44.5238310
-102.4230423
-120213313
36.3207099
-64.2186898
-179.5166780
-49.2156495
 
Upvote 0
The conditions for a streak on query 5 is:

The language mean that the negative closing must occur for each of the weeks considered in the Open Interest streak.

In your image we can see that the week closed negative, (starting from -179.5) with positive OI (166780). This is where the streak begins. Because week closed negative AND OI was positive at the same time. So it is like a combination that have to occur.

The next week is number 2 in the sequence. ( -64.2 AND 186898 ).
Then it stops. So this would be a streak of 2 in a row.
In the image we can see that it occured 2 times.

As I like statistics and excel so much :) I ask myself questions, a lot. As for this Sheet, I would like to know as well the other combinations like Positive week AND Higher OI. But I guess can with figure it out when I have the formula for 1 instance/combination.

Is it OK if I ask for another solution in a new thread? It's about counting combination but the results are messes up when I order a table... I found many questions about it over the internet but does not solve the problem.

Anyways, I can't describe how greatful I am for your time.
 
Upvote 0
I'm happy to help. By all means, feel free to begin a new thread to ask for a solution to a different type of problem. This forum offers a lot of excellent help.

Have a look at the following where there are three fundamental forms to the formulas, depending on whether you want a basic counting operation (blue), a more complex counting operation involving streaks (yellow), or a counting operation involving a combination of the two (green). You'll see the strategy for the combo counting involves forming one array inside the FREQUENCY function using an AND operation (or in this case, multiplying two arrays), while the second array inside the FREQUENCY function needs to include all row numbers that are not in the first array, so this is achieved using an OR operation (or in this case, adding two arrays and then performing a logic check to determine which elements are >0). You should be able to piece together other variations: I inserted a Query 5B for the number of times the week closed positive AND there was a streak of at least 2 consecutive weeks where OI was higher (AND applies to all elements in streak). Please post back if something needs further attention.
MrExcel_20220310_Trie.xlsx
ABC
1
2Results
3Pips+ & OI higher6Query 1: count how many times the week closed positive AND the Open Interest was higher than the previous week
4Pips- & OI higher5Query 2: count how many times the week closed negative AND the Open Interest was higher than the previous week
5Pips+ & OI lower6Query 3: count how many times the week closed positive AND the Open Interest was lower than the previous week
6Pips- & OI lower5Query 4: count how many times the week closed negative AND the Open Interest was lower than the previous week
7Pips- & OI lower streak >=22Combo Query 5A: count how many times the week closed negative AND there was a streak of at least 2 consecutive weeks where Open Interest was higher (AND applies to all elements in streak)
8Pips+ & OI higher streak >=21Combo Query 5B: how many times the week closed positive AND there was a streak of at least 2 consecutive weeks where OI was higher (AND applies to all elements in streak)
9
10Pips+12Query 6: count how many times bars closed higher
11Pips-10Query 7: count how many times bars closed lower
12OI Higher Streaks >12Query 8: count how many streaks of at least 2 consecutive weeks occur where Open Interest was higher than the previous week
13OI Lower Streaks >14Query 9: count how many streaks of at least 2 consecutive weeks occur where Open Interest was lower than the previous week
141Query 10: count how many streaks of exactly 2 consecutive weeks occur where Open Interest was higher than the previous week
150Query 11: count how many streaks of exactly 3 consecutive weeks occur where Open Interest was higher than the previous week
161Query 12: count how many streaks of at least 3 consecutive weeks occur where Open Interest was higher than the previous week
170Query 12: count how many streaks of exactly 4 consecutive weeks occur where Open Interest was higher than the previous week
180Query 12: count how many streaks of exactly 5 consecutive weeks occur where Open Interest was higher than the previous week
191Query 12: count how many streaks of exactly 6 consecutive weeks occur where Open Interest was higher than the previous week
Formulas
Cell Formulas
RangeFormula
B3B3=SUMPRODUCT(--('GBPUSD10080 Result after'!$J$5:$J$65>0),--('GBPUSD10080 Result after'!$N$5:$N$65>'GBPUSD10080 Result after'!$N$6:$N$66))
B4B4=SUMPRODUCT(--('GBPUSD10080 Result after'!$J$5:$J$65<0),--('GBPUSD10080 Result after'!$N$5:$N$65>'GBPUSD10080 Result after'!$N$6:$N$66))
B5B5=SUMPRODUCT(--('GBPUSD10080 Result after'!$J$5:$J$65>0),--('GBPUSD10080 Result after'!$N$5:$N$65<'GBPUSD10080 Result after'!$N$6:$N$66))
B6B6=SUMPRODUCT(--('GBPUSD10080 Result after'!$J$5:$J$65<0),--('GBPUSD10080 Result after'!$N$5:$N$65<'GBPUSD10080 Result after'!$N$6:$N$66))
B7B7=SUM(--(FREQUENCY(IF(('GBPUSD10080 Result after'!$J$5:$J$65<0)*('GBPUSD10080 Result after'!$N$5:$N$65>'GBPUSD10080 Result after'!$N$6:$N$66),ROW('GBPUSD10080 Result after'!$N$5:$N$65)),IF((('GBPUSD10080 Result after'!$J$5:$J$65>=0)+('GBPUSD10080 Result after'!$N$5:$N$65<='GBPUSD10080 Result after'!$N$6:$N$65))>0,ROW( 'GBPUSD10080 Result after'!$N$5:$N$65)))>1))
B8B8=SUM(--(FREQUENCY(IF(('GBPUSD10080 Result after'!$J$5:$J$65>0)*('GBPUSD10080 Result after'!$N$5:$N$65>'GBPUSD10080 Result after'!$N$6:$N$66),ROW('GBPUSD10080 Result after'!$N$5:$N$65)),IF((('GBPUSD10080 Result after'!$J$5:$J$65<=0)+('GBPUSD10080 Result after'!$N$5:$N$65<='GBPUSD10080 Result after'!$N$6:$N$65))>0,ROW( 'GBPUSD10080 Result after'!$N$5:$N$65)))>1))
B10B10=SUMPRODUCT(--('GBPUSD10080 Result after'!$J$5:$J$65>0))
B11B11=SUMPRODUCT(--('GBPUSD10080 Result after'!$J$5:$J$65<0))
B12B12=SUM(--(FREQUENCY( IF('GBPUSD10080 Result after'!$N$5:$N$65>'GBPUSD10080 Result after'!$N$6:$N$66, ROW('GBPUSD10080 Result after'!$N$5:$N$65)), IF('GBPUSD10080 Result after'!$N$5:$N$65<='GBPUSD10080 Result after'!$N$6:$N$65, ROW( 'GBPUSD10080 Result after'!$N$5:$N$65))) >1))
B13B13=SUM(--(FREQUENCY( IF('GBPUSD10080 Result after'!$N$5:$N$65<'GBPUSD10080 Result after'!$N$6:$N$66, ROW('GBPUSD10080 Result after'!$N$5:$N$65)), IF('GBPUSD10080 Result after'!$N$5:$N$65>='GBPUSD10080 Result after'!$N$6:$N$65, ROW( 'GBPUSD10080 Result after'!$N$5:$N$65))) >1))
B14B14=SUM(--(FREQUENCY( IF('GBPUSD10080 Result after'!$N$5:$N$65>'GBPUSD10080 Result after'!$N$6:$N$66, ROW('GBPUSD10080 Result after'!$N$5:$N$65)), IF('GBPUSD10080 Result after'!$N$5:$N$65<='GBPUSD10080 Result after'!$N$6:$N$65, ROW( 'GBPUSD10080 Result after'!$N$5:$N$65))) =2))
B15B15=SUM(--(FREQUENCY( IF('GBPUSD10080 Result after'!$N$5:$N$65>'GBPUSD10080 Result after'!$N$6:$N$66, ROW('GBPUSD10080 Result after'!$N$5:$N$65)), IF('GBPUSD10080 Result after'!$N$5:$N$65<='GBPUSD10080 Result after'!$N$6:$N$65, ROW( 'GBPUSD10080 Result after'!$N$5:$N$65))) =3))
B16B16=SUM(--(FREQUENCY( IF('GBPUSD10080 Result after'!$N$5:$N$65>'GBPUSD10080 Result after'!$N$6:$N$66, ROW('GBPUSD10080 Result after'!$N$5:$N$65)), IF('GBPUSD10080 Result after'!$N$5:$N$65<='GBPUSD10080 Result after'!$N$6:$N$65, ROW( 'GBPUSD10080 Result after'!$N$5:$N$65))) >=3))
B17B17=SUM(--(FREQUENCY( IF('GBPUSD10080 Result after'!$N$5:$N$65>'GBPUSD10080 Result after'!$N$6:$N$66, ROW('GBPUSD10080 Result after'!$N$5:$N$65)), IF('GBPUSD10080 Result after'!$N$5:$N$65<='GBPUSD10080 Result after'!$N$6:$N$65, ROW( 'GBPUSD10080 Result after'!$N$5:$N$65))) =4))
B18B18=SUM(--(FREQUENCY( IF('GBPUSD10080 Result after'!$N$5:$N$65>'GBPUSD10080 Result after'!$N$6:$N$66, ROW('GBPUSD10080 Result after'!$N$5:$N$65)), IF('GBPUSD10080 Result after'!$N$5:$N$65<='GBPUSD10080 Result after'!$N$6:$N$65, ROW( 'GBPUSD10080 Result after'!$N$5:$N$65))) =5))
B19B19=SUM(--(FREQUENCY( IF('GBPUSD10080 Result after'!$N$5:$N$65>'GBPUSD10080 Result after'!$N$6:$N$66, ROW('GBPUSD10080 Result after'!$N$5:$N$65)), IF('GBPUSD10080 Result after'!$N$5:$N$65<='GBPUSD10080 Result after'!$N$6:$N$65, ROW( 'GBPUSD10080 Result after'!$N$5:$N$65))) =6))
 
Upvote 0
Thank you. I was a bit confused about your comment in column C for the queries.

Combo Query 5A: count how many times the week closed negative AND there was a streak of at least 2 consecutive weeks where Open Interest was higher (AND applies to all elements in streak)
Combo Query 5B: how many times the week closed positive AND there was a streak of at least 2 consecutive weeks where OI was higher (AND applies to all elements in streak)

While in column A says; Pips- & OI lower streak >=2
I changes my comment to Pips- & OI higher streak >1 So it match with your formula :)

I try to understand the formula for Query 5C & 5D.
I see the difference but I can't figure out when I want this:

Combo Query 5C: count how many times the week closed positive AND there was a streak of at least 2 consecutive weeks where Open Interest was lower (AND applies to all elements in streak)
Combo Query 5D: how many times the week closed negative AND there was a streak of at least 2 consecutive weeks where OI was lower (AND applies to all elements in streak)

In other words, I can see how to change from positive/negative regarding Pips for the weeks. But not how I can change the negative Open Interest.
So:

Pips+ & OI lower streak >1
Pips- & OI lower streak >1

Can you please show how I can do this?

Second, are you willing to explain the code piece by piece for that? Like:

How do I read this exactly?
IF(('GBPUSD10080 Result after'!$J$5:$J$65<0)*('GBPUSD10080 Result after'!$N$5:$N$65>'GBPUSD10080 Result after'!$N$6:$N$66),ROW('GBPUSD10080 Result after'!$N$5:$N$65)),

and:
IF((('GBPUSD10080 Result after'!$J$5:$J$65>=0)+('GBPUSD10080 Result after'!$N$5:$N$65<='GBPUSD10080 Result after'!$N$6:$N$65))>0,ROW( 'GBPUSD10080 Result after'!$N$5:$N$65)))>1))


I guess when I understand this and have the formula, then I guess I can figure out what to change the next time. Now I am scared to mess the formulas up and have no confidence in the results when I do that.
 
Upvote 0
The change you made in in column A is perfectly fine. Since we are counting (using whole numbers), there is no difference between >1 and >=2. In the column C verbose descriptions, I used the phrase "at least two consecutive weeks" so I made the column A short description follow that same idea and used ">=2", but ">1" is completely consistent, and in fact is the way I wrote the formula.

I've found the best approach for understanding more complicated formulas involves splitting them into manageable parts and examining what each does. Let's review how the FREQUENCY function works before we develop formulas for Options 5C and 5D. Taking Query 5A as an example, and changing the worksheet name for the source data to simply "Data" so that the formula is a little shorter and easier to follow, we have:
=SUM(--(FREQUENCY(IF((Data!$J$5:$J$65<0)*(Data!$N$5:$N$65>Data!$N$6:$N$66),ROW(Data!$N$5:$N$65)),IF(((Data!$J$5:$J$65>=0)+(Data!$N$5:$N$65<=Data!$N$6:$N$65))>0,ROW( Data!$N$5:$N$65)))>1))

Starting inside, we have a FREQUENCY function that has the general form of FREQUENCY(data_array, bins_array)
This returns an array of the count of values in the data_array that fall within ranges defined by the bins_array. Isolating just the FREQUENCY function we have:
FREQUENCY(IF( (Data!$J$5:$J$65<0) * (Data!$N$5:$N$65>Data!$N$6:$N$66) , ROW(Data!$N$5:$N$65) ) , IF( ( (Data!$J$5:$J$65>=0) + (Data!$N$5:$N$65<=Data!$N$6:$N$65) )>0, ROW( Data!$N$5:$N$65) )

The data_array is formed by the first IF function:
IF( (Data!$J$5:$J$65<0) * (Data!$N$5:$N$65>Data!$N$6:$N$66), ROW(Data!$N$5:$N$65) )

  1. The first argument in the IF function is the logical_test that establishes which rows in the data table satisfy our filtering criteria. Since we want to consider all rows where the closing was negative, we write (Data!$J$5:$J$65<0), which returns an array of TRUE/FALSE values.
  2. We also want to consider all rows where the Open Interest was higher than the previous week, so we write (Data!$N$5:$N$65>Data!$N$6:$N$66), which also returns an array of TRUE/FALSE values depending on whether a given week's OI is greater than the previous week's OI.
  3. Since we want both of these conditions to apply at the same time, we multiply these two arrays together. This effectively is similar to a logical "AND" statement. The multiplication of the TRUE/FALSE arrays coerces TRUE's to values of 1 and FALSE's to values of 0, so the resultant array consists of 1's and 0's. Since this filtering criteria array of 1's and 0's is the logical_test argument in the IF statement, a value of 1 means the particular row corresponding to this array element satisfies all of the desired filtering criteria, and because "1" is interpreted as TRUE, the IF statement will return the ROW number...that's what the second argument appearing in the IF statement does, where we see ROW(Data!$N$5:$N$65). Where the values in the filtering criteria array are 0, the IF statement will return FALSE since the IF statement gives no instruction in that regard.
  4. The final result of this IF statement for the sample data set is an array that looks like this:
    {5;FALSE;FALSE;FALSE;FALSE;FALSE;FALSE;FALSE;FALSE;FALSE;FALSE;FALSE;FALSE;18;19;FALSE;21;22;FALSE;FALSE;FALSE;FALSE;FALSE;FALSE;FALSE;FALSE;FALSE;FALSE;FALSE;FALSE;FALSE;FALSE;FALSE;FALSE;FALSE;FALSE;FALSE;FALSE;FALSE;FALSE;FALSE;FALSE;FALSE;FALSE;FALSE;FALSE;FALSE;FALSE;FALSE;FALSE;FALSE;FALSE;FALSE;FALSE;FALSE;FALSE;FALSE;FALSE;FALSE;FALSE;FALSE}
    This means that rows 5, 18, 19, 21, 22 are the only ones of interest to us.
The bins_array is formed by the second IF function:
IF( ( (Data!$J$5:$J$65>=0) + (Data!$N$5:$N$65<=Data!$N$6:$N$66) )>0, ROW(Data!$N$5:$N$65) )

  1. We want this second IF function to return all of the row numbers corresponding to data that do not meet our filtering criteria used in the first IF statement. Then the second IF statement will establish limits (upper and lower row numbers) that will isolate various groupings (or streaks) that are of interest to us. So in the first IF statement, where the first argument is (Data!$J$5:$J$65<0) for our filtering criteria for negative closings, we now use the expression (Data!$J$5:$J$65>=0) to identify the rows that will be part of the bins_array returned by the second IF statement. This expression returns an array of TRUE/FALSE values.
  2. Similarly, where we have (Data!$N$5:$N$65>Data!$N$6:$N$66) in the first IF statement for the filtering criterion for where the Open Interest was higher than the previous week, we change this to (Data!$N$5:$N$65<=Data!$N$6:$N$66) to identify rows that will be part of the bins_array returned by the second IF statement. This expression also returns an array of TRUE/FALSE values.
  3. Since either one of the conditions above is sufficient for a row to be included when forming the bins_array, we add the two arrays (+), which is effectively similar to a logical "OR" statement. This arithmetic operation again coerces TRUE's to 1's and FALSE's to 0's, and we obtain a resultant array containing elements of 0, 1, or 2 --- 0 where none of these criteria are met (these should coincide with the rows of interest determined from the first IF statement), 1 where one of the conditions is met, and 2 where both criteria are met. Since we want to identify any of the non-zero elements, we perform a logic check on the resultant array---that's what the >0 does (see red). The result is an array containing only TRUE or FALSE, with TRUE indicating that at least one condition is met. This TRUE/FALSE array is the logical_test array of the second IF function, and it returns ROW(Data!$N$5:$N$65) for any of the TRUE elements, meaning the row numbers corresponding to data that do not meet our filtering criteria used in the first IF statement will be returned. Since the IF statement includes no result to return for FALSE elements, those will remain as FALSE.
  4. The final array returned from this bins_array expression will look like this:
    {FALSE;6;7;8;9;10;11;12;13;14;15;16;17;FALSE;FALSE;20;FALSE;FALSE;23;24;25;26;27;28;29;30;31;32;33;34;35;36;37;38;39;40;41;42;43;44;45;46;47;48;49;50;51;52;53;54;55;56;57;58;59;60;61;62;63;64;65} Examining the bin limits in this array, we see that rows numbers before 6, between 17 & 20, between 20 & 23, and after 65 will be the groupings to be counted. All other elements in bin_array are consecutive numbers, so there can be no row numbers between them.
Recap of the FREQUENCY function and the resultant array it returns:
  1. To recap, the two arrays (data_array and bins_array) are used in the FREQUENCY function, where the bins_array establishes range limits for grouping elements in the data_array. Then the FREQUENCY function will count the number of elements in each grouping. Any FALSE array elements are ignored. The resultant array from the FREQUENCY function is:{1;0;0;0;0;0;0;0;0;0;0;0;2;2;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0}
  2. We see that there is one group of 1 and two groups of 2. These are the "streaks" that satisfy our filter criteria.
Operating on the resultant array from the FREQUENCY function:
  1. Next we subject the resultant array from the FREQUENCY function to a logic check to determine how many of these groupings (or streaks) satisfy our streak length criteria. For this query, we want to know how many streaks have a length of 2 or more. Using some shorthand to strip the entire FREQUENCY function out of the formula and replace it with its "resultant array," we are left with this expression for our formula:
    =SUM( --({resultant array}>1) )
  2. Here we check whether the streaks in the resultant array have a length >1...that's what ({resultant array}>1) does. This will return another array consisting of elements that are either TRUE or FALSE.
  3. Then we apply the double unary operator (--) to the TRUE/FALSE array to coerce values of 1's and 0's from the TRUE's and FALSE's, and by doing so, we can sum the array elements to determine the total number of streaks satisfying the desired criteria.
Hopefully, this explanation will clarify how to make changes to introduce variations of the filter criteria and streak length. Formulas following this approach for Queries 5C and 5D are shown in the worksheet. As a side note, upon reviewing the arrays in detail, I discovered an error (probably inconsequential) in the earlier formulas where $N$6:$N$65 was referenced (in multiple places). As that range refers to an offset of one week from the normal data table range of $N$5:$N$65, to represent the "previous" weeks, the reference for previous weeks should be $N$6:$N$66 (note the last digit). This correction is also reflected in the sample worksheet below.
MrExcel_20220310_Trie.xlsx
ABC
2Results
3Pips+ & OI higher6Query 1: count how many times the week closed positive AND the Open Interest was higher than the previous week
4Pips- & OI higher5Query 2: count how many times the week closed negative AND the Open Interest was higher than the previous week
5Pips+ & OI lower6Query 3: count how many times the week closed positive AND the Open Interest was lower than the previous week
6Pips- & OI lower5Query 4: count how many times the week closed negative AND the Open Interest was lower than the previous week
7Pips- & OI lower streak >=22Combo Query 5A: count how many times the week closed negative AND there was a streak of at least 2 consecutive weeks where Open Interest was higher (AND applies to all elements in streak)
8Pips+ & OI higher streak >=21Combo Query 5B: count how many times the week closed positive AND there was a streak of at least 2 consecutive weeks where OI was higher (AND applies to all elements in streak)
9Pips+ & OI lower streak >=22Combo Query 5C: count how many times the week closed positive AND there was a streak of at least 2 consecutive weeks where Open Interest was lower (AND applies to all elements in streak)
10Pips- & OI lower streak >=21Combo Query 5D: count how many times the week closed negative AND there was a streak of at least 2 consecutive weeks where OI was lower (AND applies to all elements in streak)
11
12Pips+12Query 6: count how many times bars closed higher
13Pips-10Query 7: count how many times bars closed lower
14OI Higher Streaks >12Query 8: count how many streaks of at least 2 consecutive weeks occur where Open Interest was higher than the previous week
15OI Lower Streaks >14Query 9: count how many streaks of at least 2 consecutive weeks occur where Open Interest was lower than the previous week
161Query 10: count how many streaks of exactly 2 consecutive weeks occur where Open Interest was higher than the previous week
170Query 11: count how many streaks of exactly 3 consecutive weeks occur where Open Interest was higher than the previous week
181Query 12: count how many streaks of at least 3 consecutive weeks occur where Open Interest was higher than the previous week
190Query 12: count how many streaks of exactly 4 consecutive weeks occur where Open Interest was higher than the previous week
200Query 12: count how many streaks of exactly 5 consecutive weeks occur where Open Interest was higher than the previous week
211Query 12: count how many streaks of exactly 6 consecutive weeks occur where Open Interest was higher than the previous week
Formulas
Cell Formulas
RangeFormula
B3B3=SUMPRODUCT(--('GBPUSD10080 Result after'!$J$5:$J$65>0),--('GBPUSD10080 Result after'!$N$5:$N$65>'GBPUSD10080 Result after'!$N$6:$N$66))
B4B4=SUMPRODUCT(--('GBPUSD10080 Result after'!$J$5:$J$65<0),--('GBPUSD10080 Result after'!$N$5:$N$65>'GBPUSD10080 Result after'!$N$6:$N$66))
B5B5=SUMPRODUCT(--('GBPUSD10080 Result after'!$J$5:$J$65>0),--('GBPUSD10080 Result after'!$N$5:$N$65<'GBPUSD10080 Result after'!$N$6:$N$66))
B6B6=SUMPRODUCT(--('GBPUSD10080 Result after'!$J$5:$J$65<0),--('GBPUSD10080 Result after'!$N$5:$N$65<'GBPUSD10080 Result after'!$N$6:$N$66))
B7B7=SUM(--(FREQUENCY(IF(('GBPUSD10080 Result after'!$J$5:$J$65<0)*('GBPUSD10080 Result after'!$N$5:$N$65>'GBPUSD10080 Result after'!$N$6:$N$66),ROW('GBPUSD10080 Result after'!$N$5:$N$65)),IF((('GBPUSD10080 Result after'!$J$5:$J$65>=0)+('GBPUSD10080 Result after'!$N$5:$N$65<='GBPUSD10080 Result after'!$N$6:$N$66))>0,ROW( 'GBPUSD10080 Result after'!$N$5:$N$65)))>1))
B8B8=SUM(--(FREQUENCY(IF(('GBPUSD10080 Result after'!$J$5:$J$65>0)*('GBPUSD10080 Result after'!$N$5:$N$65>'GBPUSD10080 Result after'!$N$6:$N$66),ROW('GBPUSD10080 Result after'!$N$5:$N$65)),IF((('GBPUSD10080 Result after'!$J$5:$J$65<=0)+('GBPUSD10080 Result after'!$N$5:$N$65<='GBPUSD10080 Result after'!$N$6:$N$66))>0,ROW( 'GBPUSD10080 Result after'!$N$5:$N$65)))>1))
B9B9=SUM(--(FREQUENCY(IF(('GBPUSD10080 Result after'!$J$5:$J$65>0)*('GBPUSD10080 Result after'!$N$5:$N$65<'GBPUSD10080 Result after'!$N$6:$N$66),ROW('GBPUSD10080 Result after'!$N$5:$N$65)),IF((('GBPUSD10080 Result after'!$J$5:$J$65<=0)+('GBPUSD10080 Result after'!$N$5:$N$65>='GBPUSD10080 Result after'!$N$6:$N$66))>0,ROW( 'GBPUSD10080 Result after'!$N$5:$N$65)))>1))
B10B10=SUM(--(FREQUENCY(IF(('GBPUSD10080 Result after'!$J$5:$J$65<0)*('GBPUSD10080 Result after'!$N$5:$N$65<'GBPUSD10080 Result after'!$N$6:$N$66),ROW('GBPUSD10080 Result after'!$N$5:$N$65)),IF((('GBPUSD10080 Result after'!$J$5:$J$65>=0)+('GBPUSD10080 Result after'!$N$5:$N$65>='GBPUSD10080 Result after'!$N$6:$N$66))>0,ROW( 'GBPUSD10080 Result after'!$N$5:$N$65)))>1))
B12B12=SUMPRODUCT(--('GBPUSD10080 Result after'!$J$5:$J$65>0))
B13B13=SUMPRODUCT(--('GBPUSD10080 Result after'!$J$5:$J$65<0))
B14B14=SUM(--(FREQUENCY( IF('GBPUSD10080 Result after'!$N$5:$N$65>'GBPUSD10080 Result after'!$N$6:$N$66, ROW('GBPUSD10080 Result after'!$N$5:$N$65)), IF('GBPUSD10080 Result after'!$N$5:$N$65<='GBPUSD10080 Result after'!$N$6:$N$66, ROW( 'GBPUSD10080 Result after'!$N$5:$N$65))) >1))
B15B15=SUM(--(FREQUENCY( IF('GBPUSD10080 Result after'!$N$5:$N$65<'GBPUSD10080 Result after'!$N$6:$N$66, ROW('GBPUSD10080 Result after'!$N$5:$N$65)), IF('GBPUSD10080 Result after'!$N$5:$N$65>='GBPUSD10080 Result after'!$N$6:$N$66, ROW( 'GBPUSD10080 Result after'!$N$5:$N$65))) >1))
B16B16=SUM(--(FREQUENCY( IF('GBPUSD10080 Result after'!$N$5:$N$65>'GBPUSD10080 Result after'!$N$6:$N$66, ROW('GBPUSD10080 Result after'!$N$5:$N$65)), IF('GBPUSD10080 Result after'!$N$5:$N$65<='GBPUSD10080 Result after'!$N$6:$N$66, ROW( 'GBPUSD10080 Result after'!$N$5:$N$65))) =2))
B17B17=SUM(--(FREQUENCY( IF('GBPUSD10080 Result after'!$N$5:$N$65>'GBPUSD10080 Result after'!$N$6:$N$66, ROW('GBPUSD10080 Result after'!$N$5:$N$65)), IF('GBPUSD10080 Result after'!$N$5:$N$65<='GBPUSD10080 Result after'!$N$6:$N$66, ROW( 'GBPUSD10080 Result after'!$N$5:$N$65))) =3))
B18B18=SUM(--(FREQUENCY( IF('GBPUSD10080 Result after'!$N$5:$N$65>'GBPUSD10080 Result after'!$N$6:$N$66, ROW('GBPUSD10080 Result after'!$N$5:$N$65)), IF('GBPUSD10080 Result after'!$N$5:$N$65<='GBPUSD10080 Result after'!$N$6:$N$66, ROW( 'GBPUSD10080 Result after'!$N$5:$N$65))) >=3))
B19B19=SUM(--(FREQUENCY( IF('GBPUSD10080 Result after'!$N$5:$N$65>'GBPUSD10080 Result after'!$N$6:$N$66, ROW('GBPUSD10080 Result after'!$N$5:$N$65)), IF('GBPUSD10080 Result after'!$N$5:$N$65<='GBPUSD10080 Result after'!$N$6:$N$66, ROW( 'GBPUSD10080 Result after'!$N$5:$N$65))) =4))
B20B20=SUM(--(FREQUENCY( IF('GBPUSD10080 Result after'!$N$5:$N$65>'GBPUSD10080 Result after'!$N$6:$N$66, ROW('GBPUSD10080 Result after'!$N$5:$N$65)), IF('GBPUSD10080 Result after'!$N$5:$N$65<='GBPUSD10080 Result after'!$N$6:$N$66, ROW( 'GBPUSD10080 Result after'!$N$5:$N$65))) =5))
B21B21=SUM(--(FREQUENCY( IF('GBPUSD10080 Result after'!$N$5:$N$65>'GBPUSD10080 Result after'!$N$6:$N$66, ROW('GBPUSD10080 Result after'!$N$5:$N$65)), IF('GBPUSD10080 Result after'!$N$5:$N$65<='GBPUSD10080 Result after'!$N$6:$N$66, ROW( 'GBPUSD10080 Result after'!$N$5:$N$65))) =6))
 
Upvote 0

Forum statistics

Threads
1,223,908
Messages
6,175,306
Members
452,633
Latest member
DougMo

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top