How to Use CountIfs to Calculate a Running Total.

I was trying to configure this code:




=IF(
  'Enter up to 10x Legs/Strategy - Tied to 10x BSM Banks'::$D$5 = "N/A",
  "",
  IF(
    OR(
      'Enter up to 10x Legs/Strategy - Tied to 10x BSM Banks'::$D$5 = "LC",
      'Enter up to 10x Legs/Strategy - Tied to 10x BSM Banks'::$D$5 = "SC",
      'Enter up to 10x Legs/Strategy - Tied to 10x BSM Banks'::$D$5 = "LP",
      'Enter up to 10x Legs/Strategy - Tied to 10x BSM Banks'::$D$5 = "SP",
      'Enter up to 10x Legs/Strategy - Tied to 10x BSM Banks'::$D$5 = "LA",
      'Enter up to 10x Legs/Strategy - Tied to 10x BSM Banks'::$D$5 = "SA"
    ),
    I4,
    0
  )
) / COUNTIFS(

  'Enter up to 10x Legs/Strategy - Tied to 10x BSM Banks'::$D$5:$D$14, "<>N/A",
  ROW('Enter up to 10x Legs/Strategy - Tied to 10x BSM Banks'::$D$5:$D$14) <= ROW('Enter up to 10x Legs/Strategy - Tied to 10x BSM Banks'::$D$5),
  ROW('Enter up to 10x Legs/Strategy - Tied to 10x BSM Banks'::$D$5:$D$14) - ROW('Enter up to 10x Legs/Strategy - Tied to 10x BSM Banks'::$D$5) + 1
)


But get an error: 


COUNTIFS requires that all range arguments are the same size.I am trying to calculate a running total of breakeven values for my 10 x Leg calculator and figuring out how to code it.


I have a dropdown menu (in rows 'Enter up to 10x Legs/Strategy - Tied to 10x BSM Banks'::$D$5:$D$14) that can select whether any trades are valid or non positions. 



So any one of the 10 trades could be a valid trade i.e. an option/asset (LC, SC, LP, SP, LA, SA) or a non trade (N/A) where no position is taken (i.e you’re ”flat”). 


If it is the former, (i.e. a valid trades, LC, SC, LP, SP, LA, SA), then the b/even values are a simple case of adding up all the current running total valid Legs b/even values and dividing the current total breakeven value by the number of valid trades (LC, SC, LP, SP, LA, SA) but only up to the last current b/even value, — eg in Leg 4, — because Legs 5 to 10 could be already set to N/A and I don’t want those non 0.00 b/even trades (F8), counted as part of the denominator for the running b/even totals. 


So in the image Leg 1’s b/even value of 11500 is to be counted, as is Leg 2’s, but not Leg 3 (0.00), but it needs to include Leg 4 (12100), so the total is 11500 +11500 +12100 but divided by 3 Legs not 4, 35,100/3 = 11,700.


Any help would be gratefully appreciated,


Cheers.

MacBook Pro 15″, macOS 12.3

Posted on Oct 10, 2023 7:00 AM

Reply
8 replies

Oct 10, 2023 1:13 PM in response to Recycleur

And to reply specifically your question about the error you get: COUNTIFS requires that all range arguments are the same size.


To start with:

ROW('Enter up to 10x Legs/Strategy - Tied to 10x BSM Banks'::$D$5:$D$14)

will always returns 5, the first row number of the range D5:D14. Maybe you meant to use ROWS instead, the count of rows, it's really hard to say.


Then D5:D14 is filled with values of different types, so you probably meant to use D5:M5 like in my formulas.


Then to COUNTIFS.


COUNTIFS(

  'Enter up to 10x Legs/Strategy - Tied to 10x BSM Banks'::$D$5:$D$14, "<>N/A",

Here we have a range where we compare each cell to N/A. So far so good.


  ROW('Enter up to 10x Legs/Strategy - Tied to 10x BSM Banks'::$D$5:$D$14) <= ROW('Enter up to 10x Legs/Strategy - Tied to 10x BSM Banks'::$D$5),

  ROW('Enter up to 10x Legs/Strategy - Tied to 10x BSM Banks'::$D$5:$D$14) - ROW('Enter up to 10x Legs/Strategy - Tied to 10x BSM Banks'::$D$5) + 1

)

Here we have a boolean introduced by the <=, compared to a numerical value.


So the error message tells you that the range arguments are not the same size, obviously D5:D14 compared to a single boolean value.


Maybe you meant as the second test

ROW('Enter up to 10x Legs/Strategy - Tied to 10x BSM Banks'::$D$5:$D$14), "<="&ROW('Enter up to 10x Legs/Strategy - Tied to 10x BSM Banks'::$D$5)

but then what happens to the last part? Frankly I cannot understand where you were going with that.


Look at the following and meditate on it.


Oct 10, 2023 12:23 PM in response to Recycleur

Taking into account N/A and empty cells, the formula becomes:


=IFS('Enter up to 10x Legs/Strategy - Tied to 10x BSM Banks'::$D$5="N/A","",


'Enter up to 10x Legs/Strategy - Tied to 10x BSM Banks'::$D$5="",0,


COUNTMATCHES("LC,SC,LP,SP,LA,SA",'Enter up to 10x Legs/Strategy - Tied to 10x BSM Banks'::$D$5)>0,
 AVERAGEIFS('Enter up to 10x Legs/Strategy - Tied to 10x BSM Banks'::$D$8:$M$8,


'Enter up to 10x Legs/Strategy - Tied to 10x BSM Banks'::$D$5:$M$5,"<>N/A",


'Enter up to 10x Legs/Strategy - Tied to 10x BSM Banks'::$D$5:$M$5,"<>"))


Oct 11, 2023 12:40 PM in response to Recycleur

Thanks very much for the input and more efficient shorter code.


I think some of the Table references and reference ranges for the Count part are not correct and I probably could have explained the locations better — hopefully this image helps?



The range for the trade type (LC, LP or N/A etc) in the top table is correct: 

'Enter up to 10x Legs/Strategy - Tied to 10x BSM Banks'::$D$5:$M$14' (10 trades is the max the calculator can calculate).


The running total of valid trade types corresponding b/even results will go in the large blue/red/green table, Leg 1, Leg 2, etc table named 'P&L Payoff @ Expiration and @ Now Matrix'


Once the trade type (valid LC, LP, etc or non valid N/A) from the top table: 

'Enter up to 10x Legs/Strategy - Tied to 10x BSM Banks' 

has been determined the running b/even totals results will be located in the large P/L Payoff Leg table:


'P&L Payoff @ Expiration and @ Now Matrix'::$D$4, the next at:

'PP&L Payoff @ Expiration and @ Now Matrix'::$I$4 

'P&L Payoff @ Expiration and @ Now Matrix'::$N$4 

'P&L Payoff @ Expiration and @ Now Matrix'::$S$4 

'P&L Payoff @ Expiration and @ Now Matrix'::$X$4 

'P&L Payoff @ Expiration and @ Now Matrix'::$AC$4 

'P&L Payoff @ Expiration and @ Now Matrix'::$AH$4 

'P&L Payoff @ Expiration and @ Now Matrix'::$AR$4 

'P&L Payoff @ Expiration and @ Now Matrix'::$AW$4 


This is where the I4 came from (second row of code). 


If the trade type in the top table is N/A. a 0 is returned in anyone of those cells …Matrix'::$D$4 to …Matrix'::$AW$4 


The running b/even totals (which needs the consecutive countifs) are located at:


'P&L Payoff @ Expiration and @ Now Matrix'::$G$4 

'P&L Payoff @ Expiration and @ Now Matrix'::$L$4 

'P&L Payoff @ Expiration and @ Now Matrix'::$Q$4 

'P&L Payoff @ Expiration and @ Now Matrix'::$V$4 

'P&L Payoff @ Expiration and @ Now Matrix'::$AA$4 

'P&L Payoff @ Expiration and @ Now Matrix'::$AF$4 

'P&L Payoff @ Expiration and @ Now Matrix'::$AK$4 

'P&L Payoff @ Expiration and @ Now Matrix'::$AP$4 

'P&L Payoff @ Expiration and @ Now Matrix'::$AU$4 

'P&L Payoff @ Expiration and @ Now Matrix'::$AZ$4 


The 11861 running total values in the large table, G4, L$ Q4 etc is the result using the code you provided which calculates only a 10 trade average.


Thanks again,

Oct 11, 2023 1:20 PM in response to Bardonicloud

On rows 2 to 4 and 7 to 9 the yellow cells are those that return a TRUE result to the adjacent one-condition COUNTIFS. Column Result shows their count.


Rows 5 and 10 have no values in Test-values because the combined COUNTIFS actually uses the three rows of Test-values above for three separate tests. Result is the count of yellow cells resulting from the combined COUNTIFS as opposed to the individual ones on previous rows.


It's a visual aid to understand what a multi-conditions COUNTIFS does really: all values of same order in each test-range must respond TRUE to their respective test to add 1 to the count, i.e. to get a yellow cell on the fourth row all three cells above must be yellow too (it's an implicit AND function), which is what all values of 2nd rank in their respective test-range do in the first part. In the second part you see that you don't have three yellow cells in any rank, therefore the count is 0.


Oct 11, 2023 1:45 PM in response to Bardonicloud

Is this a question/report of a bug or just an explanation/thank you?


Are you getting all the numbers you expected if you do a manual calculation for comparison?


I see that your breakevens @ expiry (G4, L4, etc.) are identical for the first three then different in every leg afterwards, but my formula would probably yield the same result everywhere. If this need to be fixed you have to be much clearer about the source of data because your ROW thing (which should probably be COLUMN actually) is impossible to understand. Explain it in words not in formulas, like "breakeven @ expiry of leg 4 is the average of breakevens of Option/Assets #4 to #10 if they are not N/A" or something like this, and why #4 an not #3, you know what I mean?

Oct 12, 2023 5:31 AM in response to Recycleur

It was a "How to use Code" post and the comment with the image with arrows was to help better explain my original post.


The aim:

The calculator top table works out each legs individual b/even value then populates it on the left side column (Eg D4, I4, N4) of each Leg in the large lower table.


In the large Leg table a few columns over to the right of these (in Leg 1 = G5, Leg 2 = L4, Leg 3 = Q4) will be the actual accumulated total average b/evens of new positions that are entered starting from Leg 1 going to Leg 2, where a count will be need to be made of valid trade types to use in the average b/even calculation.


Typically with my currency option trading strategies only 2 to 4 Legs will be populated (using Legs 1-4) and the remaining eg top table H5 to M5 drop down values will set be N/A (no trade) and a 0 will populate the left hand side large Leg column b/even value.


A consecutive count will be needed but one that excludes N/A, zero values from being included in the denominator for the running total average b/even calculation, like the very first example above:


Leg 1’s b/even value of 11500 is to be counted, as is Leg 2’s, but not Leg 3 (0.00), but it needs to include Leg 4 (12100), so the total is 11500 +11500 +12100 but divided by 3 Legs not 4, 35,100/3 = 11,700..


..or looking at my last image above, use 9 as the denominator not 10 because top table F5 = 0 and isn't a valid trade.


Cheers

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

How to Use CountIfs to Calculate a Running Total.

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.