Calculate # repurchases of a stock on same-day as a sale

M10Ant

New Member
Joined
Dec 30, 2021
Messages
36
Office Version
  1. 365
Platform
  1. Windows
I thought it best to post this separately but is a continuation of Calculate # repurchases within 30 days of last sale

In the source trade table shown on the left below, the "Rebuys within 30 days" column tallies each time a stock has been re-bought within 30 days of a previous sale of that same stock. The summary table then sums all these up in "# Rebuys within 30 days" column

This works perfectly and a big thanks to Peter_SSs and shaowu459 for providing me with the solution for this in previous post.

I've tried to tweak the "Rebuys within 30 days" formula to tally all buy trades of a stock done on the same day as a sale of that same stock, putting this in the "Rebuys on same day" helper column. I know it's not quite correct as it should be showing a "1" in the row for the buy of MSFT on 11/12/2021. Hoping someone might know how to tweak this further to get the helper column to tally correctly.

Stock Repurchases.xlsx
ABCDEFGHIJ
1
2Trade Execution TimeOrder TypeStockRebuys on same dayRebuys within 30 daysStock# Same-day Rebuys# Rebuys within 30 days
303/06/2021BuyAPPL  APPL02
407/06/2021BuyAPPL  MSFT04
514/06/2021BuyMSFT  
615/06/2021BuyAPPL  
722/06/2021BuyMSFT  
810/07/2021SellAPPL  
916/07/2021BuyAPPL 1
1022/07/2021BuyAPPL 1
1123/07/2021SellMSFT  
1226/07/2021BuyMSFT 1
1305/11/2021BuyMSFT  
1406/12/2021SellMSFT  
1510/12/2021BuyMSFT 1
1611/12/2021SellMSFT  
1711/12/2021BuyMSFT 1
1810/01/2022BuyMSFT 1
Sheet2
Cell Formulas
RangeFormula
E3:E18E3=LET(tet,[Trade Execution Time],ctet,[@[Trade Execution Time]],IF([@[Order Type]]="Buy",IF(ctet-IFERROR(AGGREGATE(14,6,tet/((tet<ctet)*([Stock]=[@Stock])*([Order Type]="Sell")),1),0)<=1,1,""),""))
F3:F18F3=LET(tet,[Trade Execution Time],ctet,[@[Trade Execution Time]],IF([@[Order Type]]="Buy",IF(ctet-IFERROR(AGGREGATE(14,6,tet/((tet<ctet)*([Stock]=[@Stock])*([Order Type]="Sell")),1),0)<=30,1,""),""))
I3:I4I3=SUMIF(tblTradeOrders[Stock],[@Stock],tblTradeOrders[Rebuys on same day])
J3:J4J3=SUMIF(tblTradeOrders[Stock],[@Stock],tblTradeOrders[Rebuys within 30 days])
 
Last edited:

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
Formula in E3 should be
=LET(tet,[Trade Execution Time],ctet,[@[Trade Execution Time]],IF([@[Order Type]]="Buy",IF(ctet-IFERROR(AGGREGATE(14,6,tet/((tet=ctet)*([Stock]=[@Stock])*([Order Type]="Sell")),1),0)<=1,1,""),""))
 
Upvote 0
Actually, try this shorter one in E3
=IF([@[Order Type]]="Buy",IF(COUNTIFS([Trade Execution Time],[@[Trade Execution Time]],[Stock],[@Stock],[Order Type],"Sell"),1,""),"")
 
Upvote 0
Hi Peter. Thank you for this. I've plugged both formulas in and they do both work for those dates. I've just testing making the date column a full date-time column and putting in sample times for the buy and sell of MSFT on 11/12/21. When I do that, the tally disappears on the buy order. Is there a way to still make the buy entry tally a "1" (the buy being within 24 hours of the sell order)?

Stock Repurchases.xlsx
ABCDEF
1
2Trade Execution TimeOrder TypeStockRebuys on same dayRebuys within 30 days
303-06-21 00:00BuyAPPL  
407-06-21 00:00BuyAPPL  
514-06-21 00:00BuyMSFT  
615-06-21 00:00BuyAPPL  
722-06-21 00:00BuyMSFT  
810-07-21 00:00SellAPPL  
916-07-21 00:00BuyAPPL 1
1022-07-21 00:00BuyAPPL 1
1123-07-21 00:00SellMSFT  
1226-07-21 00:00BuyMSFT 1
1305-11-21 00:00BuyMSFT  
1406-12-21 00:00SellMSFT  
1510-12-21 00:00BuyMSFT 1
1611-12-21 11:00SellMSFT  
1711-12-21 14:00BuyMSFT 1
1810-01-22 00:00BuyMSFT 1
19  
Sheet2
Cell Formulas
RangeFormula
E3:E19E3=IF([@[Order Type]]="Buy",IF(COUNTIFS([Trade Execution Time],[@[Trade Execution Time]],[Stock],[@Stock],[Order Type],"Sell"),1,""),"")
F3:F19F3=LET(tet,[Trade Execution Time],ctet,[@[Trade Execution Time]],IF([@[Order Type]]="Buy",IF(ctet-IFERROR(AGGREGATE(14,6,tet/((tet<ctet)*([Stock]=[@Stock])*([Order Type]="Sell")),1),0)<=30,1,""),""))
 
Upvote 0
There has never been any mention (or sample data) of times before - only dates - so no surprise that the formulas are not working as you want. ;)
Can you confirm that the buy order has to be after the sell order but within 24 hours?
That is a Buy at 10:00 am and a Sell at 11:00 am would not count?
 
Upvote 0
That is a very fair point and I can't disagree with that! :)
The source table will already be sorted in ascending trade execution time it that helps, so the sell at 11:00 am would come after the buy at 10:00am. But in case it wasn't, yes I'd need to still count (tally) the sell at 11:00am if it was reordered and happened to appear before the buy at 10:00am row entry (on same day of course).
 
Upvote 0
That did not make sense to me.
still count (tally) the sell at 11:00am if it was reordered and happened to appear before the buy at 10:00am row entry

How can a sell at 11;00 be before a buy at 10:00 am?

Let us have some clear descriptions and clear sample data and expected results.
 
Upvote 0
Okay. Sorry for the confusion. You're right that a sell at 11:00 can't be before a buy at 10:00. Only in terms of if the source rows happened to be re-ordered somehow.

I just meant if the formula was clever enough to still work even when the source rows were reordered.
E.g. in below example, a "1" tally should still be recorded for the 11-12-21 buy for MSFT, despite it coming before the 11-12-21 sell for MSFT.

Saying that, the source data will generally always be kept in ascending order of trade execution date. So if it's easier to rely on the data being that way that's okay.



Stock Repurchases.xlsx
BCDEF
23Trade Execution TimeOrder TypeStockRebuys on same dayRebuys within 30 days
2403-06-21 00:00BuyAPPL  
2507-06-21 00:00BuyAPPL  
2614-06-21 00:00BuyMSFT  
2715-06-21 00:00BuyAPPL  
2822-06-21 00:00BuyMSFT  
2910-07-21 00:00SellAPPL  
3016-07-21 00:00BuyAPPL 1
3122-07-21 00:00BuyAPPL 1
3223-07-21 00:00SellMSFT  
3326-07-21 00:00BuyMSFT 1
3405-11-21 00:00BuyMSFT  
3506-12-21 00:00SellMSFT  
3610-12-21 00:00BuyMSFT 1
3711-12-21 14:00BuyMSFT 1
3811-12-21 11:00SellMSFT  
3910-01-22 00:00BuyMSFT 1
40  
Sheet2
Cell Formulas
RangeFormula
E24:E40E24=IF([@[Order Type]]="Buy",IF(COUNTIFS([Trade Execution Time],[@[Trade Execution Time]],[Stock],[@Stock],[Order Type],"Sell"),1,""),"")
F24:F40F24=LET(tet,[Trade Execution Time],ctet,[@[Trade Execution Time]],IF([@[Order Type]]="Buy",IF(ctet-IFERROR(AGGREGATE(14,6,tet/((tet<ctet)*([Stock]=[@Stock])*([Order Type]="Sell")),1),0)<=30,1,""),""))
 
Last edited:
Upvote 0
Is this what you want then?

Excel Formula:
=LET(tet,[Trade Execution Time],ctet,[@[Trade Execution Time]],IF([@[Order Type]]="Buy",IF(ctet-IFERROR(AGGREGATE(14,6,tet/((tet<ctet)*([Stock]=[@Stock])*([Order Type]="Sell")),1),0)<=1,1,""),""))
 
Upvote 0
Almost. Sorry I think I've just clocked on to what you were trying to query with me.

It shouldn't be tallying a "1" in the row 12-12-21 10:00 buy for MSFT row in below sample. I can see that is still within 24 hours of the sell on 11-12-21 11:00 entry.
I meant for the formula only to tally a "1" for all buys of a stock done on the same day as the sell (24 hour period of the very start of that sell day until the end of that day)


Stock Repurchases.xlsx
ABCDEFGHIJ
1
2Trade Execution TimeOrder TypeStockRebuys on same dayRebuys within 30 daysStock# Same-day Rebuys# Rebuys within 30 days
303-06-21 00:00BuyAPPL  APPL02
407-06-21 00:00BuyAPPL  MSFT25
514-06-21 00:00BuyMSFT  
615-06-21 00:00BuyAPPL  
722-06-21 00:00BuyMSFT  
810-07-21 00:00SellAPPL  
916-07-21 00:00BuyAPPL 1
1022-07-21 00:00BuyAPPL 1
1123-07-21 00:00SellMSFT  
1226-07-21 00:00BuyMSFT 1
1305-11-21 00:00BuyMSFT  
1406-12-21 00:00SellMSFT  
1510-12-21 00:00BuyMSFT 1
1611-12-21 11:00SellMSFT  
1711-12-21 14:00BuyMSFT11
1812-12-21 10:00BuyMSFT11
1910-01-22 00:00BuyMSFT 1
20  
Sheet2
Cell Formulas
RangeFormula
E3:E20E3=LET(tet,[Trade Execution Time],ctet,[@[Trade Execution Time]],IF([@[Order Type]]="Buy",IF(ctet-IFERROR(AGGREGATE(14,6,tet/((tet<ctet)*([Stock]=[@Stock])*([Order Type]="Sell")),1),0)<=1,1,""),""))
F3:F20F3=LET(tet,[Trade Execution Time],ctet,[@[Trade Execution Time]],IF([@[Order Type]]="Buy",IF(ctet-IFERROR(AGGREGATE(14,6,tet/((tet<ctet)*([Stock]=[@Stock])*([Order Type]="Sell")),1),0)<=30,1,""),""))
I3:I4I3=SUMIF(tblTradeOrders[Stock],[@Stock],tblTradeOrders[Rebuys on same day])
J3:J4J3=SUMIF(tblTradeOrders[Stock],[@Stock],tblTradeOrders[Rebuys within 30 days])
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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