Average Cost of stock from buy/sell transactions

Status
Not open for further replies.

Toadstool

Well-known Member
Joined
Mar 5, 2018
Messages
2,549
Office Version
  1. 2016
Platform
  1. Windows
I have stock buy/sell transactions and want to calculate my position.
Here is some sample data:
2qv5fu0.png


I can calculate most things I need but I'm struggling with Average Cost.

So XXX bought 100 at $1 then sold 50 at $2, so my average cost is $1.
Then bought 100 at $2 and sold 100 at $3 so I have a Holding of 50.
I'm OK with assuming the oldest was sold first so the Average Cost of my 50 would be $2 (50 of the 7 Jan transaction).
I'm OK with assuming the newest would be sold first so the Average Cost would be $1 being 50 of the 100 bought 1 Jan.

It gets trickier with ZZZ as the 19 Jan transaction takes my holding to 0 so I really need an Average Cost of $1 being either 22 or 25 Jan buys.

It may be that I just haven't had enough coffee yet but this is hurting my head.

Any ideas for a formula/function solution? (avoiding macros or VBA).

Thanks!
 

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
Surely this type of calculation is needed all the time?
 
Upvote 0
Do you want to have the average cost as an additional row? Why not just have a couple helper cells with total position and average cost, you can have a more accurate average aswell.
 
Upvote 0
To calculate the real AVG price maybe something like this...


[TABLE="class: grid"]
<tbody>[TR]
[TD="bgcolor: #DCE6F1"][/TD]
[TD="bgcolor: #DCE6F1"]
A
[/TD]
[TD="bgcolor: #DCE6F1"]
B
[/TD]
[TD="bgcolor: #DCE6F1"]
C
[/TD]
[TD="bgcolor: #DCE6F1"]
D
[/TD]
[TD="bgcolor: #DCE6F1"]
E
[/TD]
[TD="bgcolor: #DCE6F1"]
F
[/TD]
[TD="bgcolor: #DCE6F1"]
G
[/TD]
[TD="bgcolor: #DCE6F1"]
H
[/TD]
[/TR]
[TR]
[TD="bgcolor: #DCE6F1"]
1
[/TD]
[TD]
Symbol​
[/TD]
[TD]
Buy/Sell QTY​
[/TD]
[TD]
Price​
[/TD]
[TD]
Date​
[/TD]
[TD]
Transaction Value​
[/TD]
[TD]
Current QTY​
[/TD]
[TD]
Current Cost​
[/TD]
[TD]
Current AVG Price​
[/TD]
[/TR]
[TR]
[TD="bgcolor: #DCE6F1"]
2
[/TD]
[TD]
XXX​
[/TD]
[TD]
100​
[/TD]
[TD]
1,00​
[/TD]
[TD]
01-jan-18​
[/TD]
[TD]
100,00​
[/TD]
[TD]
100​
[/TD]
[TD]
100​
[/TD]
[TD]
1,00​
[/TD]
[/TR]
[TR]
[TD="bgcolor: #DCE6F1"]
3
[/TD]
[TD]
XXX​
[/TD]
[TD]
-50​
[/TD]
[TD]
2,00​
[/TD]
[TD]
04-jan-18​
[/TD]
[TD]
-100,00​
[/TD]
[TD]
50​
[/TD]
[TD]
0​
[/TD]
[TD]
0,00​
[/TD]
[/TR]
[TR]
[TD="bgcolor: #DCE6F1"]
4
[/TD]
[TD]
XXX​
[/TD]
[TD]
100​
[/TD]
[TD]
2,00​
[/TD]
[TD]
07-jan-18​
[/TD]
[TD]
200,00​
[/TD]
[TD]
150​
[/TD]
[TD]
200​
[/TD]
[TD]
1,33​
[/TD]
[/TR]
[TR]
[TD="bgcolor: #DCE6F1"]
5
[/TD]
[TD]
XXX​
[/TD]
[TD]
-100​
[/TD]
[TD]
3,00​
[/TD]
[TD]
10-jan-18​
[/TD]
[TD]
-300,00​
[/TD]
[TD]
50​
[/TD]
[TD]
-100​
[/TD]
[TD]
0,00​
[/TD]
[/TR]
[TR]
[TD="bgcolor: #DCE6F1"]
6
[/TD]
[TD]
ZZZ​
[/TD]
[TD]
200​
[/TD]
[TD]
2,00​
[/TD]
[TD]
13-jan-18​
[/TD]
[TD]
400,00​
[/TD]
[TD]
200​
[/TD]
[TD]
400​
[/TD]
[TD]
2,00​
[/TD]
[/TR]
[TR]
[TD="bgcolor: #DCE6F1"]
7
[/TD]
[TD]
ZZZ​
[/TD]
[TD]
-100​
[/TD]
[TD]
1,00​
[/TD]
[TD]
16-jan-18​
[/TD]
[TD]
-100,00​
[/TD]
[TD]
100​
[/TD]
[TD]
300​
[/TD]
[TD]
3,00​
[/TD]
[/TR]
[TR]
[TD="bgcolor: #DCE6F1"]
8
[/TD]
[TD]
ZZZ​
[/TD]
[TD]
-100​
[/TD]
[TD]
1,00​
[/TD]
[TD]
19-jan-18​
[/TD]
[TD]
-100,00​
[/TD]
[TD]
0​
[/TD]
[TD]
200​
[/TD]
[TD]
0,00​
[/TD]
[/TR]
[TR]
[TD="bgcolor: #DCE6F1"]
9
[/TD]
[TD]
ZZZ​
[/TD]
[TD]
100​
[/TD]
[TD]
1,00​
[/TD]
[TD]
22-jan-18​
[/TD]
[TD]
100,00​
[/TD]
[TD]
100​
[/TD]
[TD]
300​
[/TD]
[TD]
3,00​
[/TD]
[/TR]
[TR]
[TD="bgcolor: #DCE6F1"]
10
[/TD]
[TD]
ZZZ​
[/TD]
[TD]
200​
[/TD]
[TD]
1,00​
[/TD]
[TD]
25-jan-18​
[/TD]
[TD]
200,00​
[/TD]
[TD]
300​
[/TD]
[TD]
500​
[/TD]
[TD]
1,67​
[/TD]
[/TR]
[TR]
[TD="bgcolor: #DCE6F1"]
11
[/TD]
[TD]
ZZZ​
[/TD]
[TD]
-100​
[/TD]
[TD]
1,00​
[/TD]
[TD]
28-jan-18​
[/TD]
[TD]
-100,00​
[/TD]
[TD]
200​
[/TD]
[TD]
400​
[/TD]
[TD]
2,00​
[/TD]
[/TR]
</tbody>[/TABLE]


Formula in F2 copied down
=SUMIF(A$2:A2,A2,B$2:B2)

Formula in G2 copied down
=SUMIF(A$2:A2,A2,E$2:E2)

Formula in H2 copied down
=IF(F2>0,MAX(0,G2/F2),0)

M.
 
Upvote 0
To calculate the real AVG price maybe something like this...


[TABLE="class: grid"]
<tbody>[TR]
[TD="bgcolor: #DCE6F1"][/TD]
[TD="bgcolor: #DCE6F1"]
A
[/TD]
[TD="bgcolor: #DCE6F1"]
B
[/TD]
[TD="bgcolor: #DCE6F1"]
C
[/TD]
[TD="bgcolor: #DCE6F1"]
D
[/TD]
[TD="bgcolor: #DCE6F1"]
E
[/TD]
[TD="bgcolor: #DCE6F1"]
F
[/TD]
[TD="bgcolor: #DCE6F1"]
G
[/TD]
[TD="bgcolor: #DCE6F1"]
H
[/TD]
[/TR]
[TR]
[TD="bgcolor: #DCE6F1"]
1
[/TD]
[TD]
Symbol​
[/TD]
[TD]
Buy/Sell QTY​
[/TD]
[TD]
Price​
[/TD]
[TD]
Date​
[/TD]
[TD]
Transaction Value​
[/TD]
[TD]
Current QTY​
[/TD]
[TD]
Current Cost​
[/TD]
[TD]
Current AVG Price​
[/TD]
[/TR]
[TR]
[TD="bgcolor: #DCE6F1"]
2
[/TD]
[TD]
XXX​
[/TD]
[TD]
100​
[/TD]
[TD]
1,00​
[/TD]
[TD]
01-jan-18​
[/TD]
[TD]
100,00​
[/TD]
[TD]
100​
[/TD]
[TD]
100​
[/TD]
[TD]
1,00​
[/TD]
[/TR]
[TR]
[TD="bgcolor: #DCE6F1"]
3
[/TD]
[TD]
XXX​
[/TD]
[TD]
-50​
[/TD]
[TD]
2,00​
[/TD]
[TD]
04-jan-18​
[/TD]
[TD]
-100,00​
[/TD]
[TD]
50​
[/TD]
[TD]
0​
[/TD]
[TD]
0,00​
[/TD]
[/TR]
[TR]
[TD="bgcolor: #DCE6F1"]
4
[/TD]
[TD]
XXX​
[/TD]
[TD]
100​
[/TD]
[TD]
2,00​
[/TD]
[TD]
07-jan-18​
[/TD]
[TD]
200,00​
[/TD]
[TD]
150​
[/TD]
[TD]
200​
[/TD]
[TD]
1,33​
[/TD]
[/TR]
[TR]
[TD="bgcolor: #DCE6F1"]
5
[/TD]
[TD]
XXX​
[/TD]
[TD]
-100​
[/TD]
[TD]
3,00​
[/TD]
[TD]
10-jan-18​
[/TD]
[TD]
-300,00​
[/TD]
[TD]
50​
[/TD]
[TD]
-100​
[/TD]
[TD]
0,00​
[/TD]
[/TR]
[TR]
[TD="bgcolor: #DCE6F1"]
6
[/TD]
[TD]
ZZZ​
[/TD]
[TD]
200​
[/TD]
[TD]
2,00​
[/TD]
[TD]
13-jan-18​
[/TD]
[TD]
400,00​
[/TD]
[TD]
200​
[/TD]
[TD]
400​
[/TD]
[TD]
2,00​
[/TD]
[/TR]
[TR]
[TD="bgcolor: #DCE6F1"]
7
[/TD]
[TD]
ZZZ​
[/TD]
[TD]
-100​
[/TD]
[TD]
1,00​
[/TD]
[TD]
16-jan-18​
[/TD]
[TD]
-100,00​
[/TD]
[TD]
100​
[/TD]
[TD]
300​
[/TD]
[TD]
3,00​
[/TD]
[/TR]
[TR]
[TD="bgcolor: #DCE6F1"]
8
[/TD]
[TD]
ZZZ​
[/TD]
[TD]
-100​
[/TD]
[TD]
1,00​
[/TD]
[TD]
19-jan-18​
[/TD]
[TD]
-100,00​
[/TD]
[TD]
0​
[/TD]
[TD]
200​
[/TD]
[TD]
0,00​
[/TD]
[/TR]
[TR]
[TD="bgcolor: #DCE6F1"]
9
[/TD]
[TD]
ZZZ​
[/TD]
[TD]
100​
[/TD]
[TD]
1,00​
[/TD]
[TD]
22-jan-18​
[/TD]
[TD]
100,00​
[/TD]
[TD]
100​
[/TD]
[TD]
300​
[/TD]
[TD]
3,00​
[/TD]
[/TR]
[TR]
[TD="bgcolor: #DCE6F1"]
10
[/TD]
[TD]
ZZZ​
[/TD]
[TD]
200​
[/TD]
[TD]
1,00​
[/TD]
[TD]
25-jan-18​
[/TD]
[TD]
200,00​
[/TD]
[TD]
300​
[/TD]
[TD]
500​
[/TD]
[TD]
1,67​
[/TD]
[/TR]
[TR]
[TD="bgcolor: #DCE6F1"]
11
[/TD]
[TD]
ZZZ​
[/TD]
[TD]
-100​
[/TD]
[TD]
1,00​
[/TD]
[TD]
28-jan-18​
[/TD]
[TD]
-100,00​
[/TD]
[TD]
200​
[/TD]
[TD]
400​
[/TD]
[TD]
2,00​
[/TD]
[/TR]
</tbody>[/TABLE]


Formula in F2 copied down
=SUMIF(A$2:A2,A2,B$2:B2)

Formula in G2 copied down
=SUMIF(A$2:A2,A2,E$2:E2)

Formula in H2 copied down
=IF(F2>0,MAX(0,G2/F2),0)

M.


MArcelo,
Thanks for the response!

The data is from a CSV file but I can add helper columns in another tab. I think you're saying my average costs for the current holding of 50 XXX and 200 ZZZ is 1.33 and 2 respectively.

So I'd state XXX Average Cost of holding is 1.33*50=$66.5 and for ZZZ is 2*200=$400 being the latest date for a stock with a positive purchase qty?

Thanks!
 
Upvote 0
I was not sure what exactly you wanted.

My formulas try to indicate the current cost of each stock. I'll try to explain my logic.

For example, in the case of XXX: after some buying operations that cost $300 (100 + 200) and some sales operations that yielded $400, 50 shares were left with a profit of $100, so the actual cost of these shares is zero because there was a profit on these trades.

M.
 
Upvote 0
I was not sure what exactly you wanted.

My formulas try to indicate the current cost of each stock. I'll try to explain my logic.

For example, in the case of XXX: after some buying operations that cost $300 (100 + 200) and some sales operations that yielded $400, 50 shares were left with a profit of $100, so the actual cost of these shares is zero because there was a profit on these trades.

M.


Ah! No, that's not what I was looking for. I have a stock holding, say 50, and over time there were buys and sells but I want to know what the average cost was for those 50.

Thanks anyway.
 
Upvote 0
Ah! No, that's not what I was looking for. I have a stock holding, say 50, and over time there were buys and sells but I want to know what the average cost was for those 50.

Thanks anyway.

Not sure how do you want to calculate the AVG price.
Let's consider XXX:
100 shares were bought by $1.00
and
100 shares were bought by $2.00

Do you want a weighted average?
=(100*1 + 100*2)/(100+100)
= 300/200
= $1,50

M.
 
Upvote 0
Not sure how do you want to calculate the AVG price.
Let's consider XXX:
100 shares were bought by $1.00
and
100 shares were bought by $2.00

Do you want a weighted average?
=(100*1 + 100*2)/(100+100)
= 300/200
= $1,50

M.


It's the selling as well as the buying I want to account for. Let's look at ZZZ

[TABLE="class: cms_table_grid"]
<tbody>[TR]
[TD="bgcolor: #DCE6F1"]
6
[/TD]
[TD]ZZZ
[/TD]
[TD]
200​
[/TD]
[TD]
2,00​
[/TD]
[TD]
13-jan-18​
[/TD]
[TD]
400,00​
[/TD]
[TD]
200​
[/TD]
[TD]
400​
[/TD]
[TD]
2,00​
[/TD]
[/TR]
[TR]
[TD="bgcolor: #DCE6F1"]
7
[/TD]
[TD]ZZZ
[/TD]
[TD]
-100​
[/TD]
[TD]
1,00​
[/TD]
[TD]
16-jan-18​
[/TD]
[TD]
-100,00​
[/TD]
[TD]
100​
[/TD]
[TD]
300​
[/TD]
[TD]
3,00​
[/TD]
[/TR]
[TR]
[TD="bgcolor: #DCE6F1"]
8
[/TD]
[TD]ZZZ
[/TD]
[TD]
-100​
[/TD]
[TD]
1,00​
[/TD]
[TD]
19-jan-18​
[/TD]
[TD]
-100,00​
[/TD]
[TD]
0​
[/TD]
[TD]
200​
[/TD]
[TD]
0,00​
[/TD]
[/TR]
[TR]
[TD="bgcolor: #DCE6F1"]
9
[/TD]
[TD]ZZZ
[/TD]
[TD]
100​
[/TD]
[TD]
1,00​
[/TD]
[TD]
22-jan-18​
[/TD]
[TD]
100,00​
[/TD]
[TD]
100​
[/TD]
[TD]
300​
[/TD]
[TD]
3,00​
[/TD]
[/TR]
[TR]
[TD="bgcolor: #DCE6F1"]
10
[/TD]
[TD]ZZZ
[/TD]
[TD]
200​
[/TD]
[TD]
1,00​
[/TD]
[TD]
25-jan-18​
[/TD]
[TD]
200,00​
[/TD]
[TD]
300​
[/TD]
[TD]
500​
[/TD]
[TD]
1,67​
[/TD]
[/TR]
[TR]
[TD="bgcolor: #DCE6F1"]
11
[/TD]
[TD]ZZZ
[/TD]
[TD]
-100​
[/TD]
[TD]
1,00​
[/TD]
[TD]
28-jan-18​
[/TD]
[TD]
-100,00​
[/TD]
[TD]
200​
[/TD]
[TD]
400​
[/TD]
[TD]
2,00​
[/TD]
[/TR]
</tbody>[/TABLE]

6. My first purchase is 200 at $2 so my holding cost is $400. Easy so far.
7. Then I sell -100 at $1 so I have 100 left but they still cost me $2 each so my holding cost is $200.
8. Now I sell -100 at $1 so I have 0 left, so no holding cost. Any profit/loss doesn't matter as that's a different calculation.
9. Now I buy 100 at $1...
10 ... another 200 at $1 so holding is 300 with cost $300.
Then I sell -100 at $1 so holding is 200 and average cost should be $200. It can't take account of any transactions before 19 Jan as that's when holding went to 0 so no average should be carried forward.

Does that make sense?
 
Upvote 0
Just to clarify you want a LIFO method here. I.e., when you sell a stock you want to sell the "oldest" stock.
I'm not sure how that's how it would be done in practice (say at Ameritrade or ETrade), but that's what you are describing. What would make it interesting was if you bought 100 for 1, then 100 more for 1.20, then sold 75, then sold 10, then sold another 30, then sold 85. These would dip into your "LIFO" layers but wouldn't so conveniently remove entire layers in a single transaction, and could remove from two layers at once. However, you can also count backwards - if the current inventory exceeds the last layer, then you have stock from two layers, and so on until the current stock is fully accounted for. Probably software that does this would re-calculate the layers on each buy or sell transaction.
 
Last edited:
Upvote 0
Status
Not open for further replies.

Forum statistics

Threads
1,224,823
Messages
6,181,176
Members
453,021
Latest member
Justyna P

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