Formula Help: Calculate Net Cost on Shares (excluding sell orders)

Tilly13

New Member
Joined
Dec 6, 2017
Messages
5
Hi All,

I have a spreadsheet with details of shares bought and sold, I have an add-in that calculates the current prices and everything is working fine but I would like to add a NET COST cell that calculates the cost of shares currently in my possession.

The below example data is transactional and chronological from top to bottom, so a positive Qty is a buy and a negative Qty is a sell. From the below data I have been calculating a net cost using a sumif formula that excludes negative "Qty Bought/Sold" values (those are sell orders), giving me a net cost of $12,154. However, I now want to calculate the net cost of shares currently in my possession (the correct value is $4,170), which means that I need to subtract the Qty sold, but multiply it by the buy price (not the sell price which is in the adjacent cell). I am working under the assumption of "First in, first out" whereby I am assuming that I am selling the oldest buy order each time.

Hope this makes sense, if anyone has any thoughts on how to do this please let me know! I can add a column to the right of the Price Bought/Sold column to calculate if that is easier, but would prefer it to calculate in one cell (assume that is impossible though). Prefer to use formulas rather than VBA if possible.

Example data (rows are in chronological order):

Net Cost (?)

Qty Bought/Sold
Price Bought/Sold
20,600
$0.24
8,000
$0.38
-20,600
$0.40
-8,000
$0.35
8,000
$0.37
2,000
$0.28
2,500
$0.26

<tbody>
</tbody>

Thanks all!
 
I know, I know and unfortunately I also do not know how to provide the desired solution using Excel. If (big IF) the lots (Buy x Sale) are the same size *maybe* it's possible to create a formula (listen, i said maybe ;))

M.

Oh, yes... I think this is the stiuation. Otherwise it makes no sense. But still can't figure it out.

Let say there is no contribution of negative numbers to the grand sum. Their total effect is zero. But when? Since you can exclude their possitive effect, too...

What I mean is there should be a --() condition which will return zero on a nagative row but also look back and find the last positive accurance of the same value (there should be one at least! But which one?!?! Probably the last one :S) and provide an array like {0,0,0,0,1,1,1} to the SUMPRODUCT function.

I don't think such condition can be written.
 
Last edited by a moderator:
Upvote 0

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
Maybe with a helper column to get the correspondent last price


A
B
C
D
E
1
Qty Bought/Sold​
Price Bought/Sold​
Price​
Result​
2
20600​
0,24​
0,24​
1210​
3
8000​
0,38​
0,38​
4
-20600​
0,4​
0,24​
5
-8000​
0,35​
0,38​
6
8000​
0,37​
0,37​
7
2000​
0,28​
0,28​
8
2500​
0,26​
0,26​
9
-8000​
0,4​
0,37​

Formula in C2 copied down
=IF(A2>0,B2,LOOKUP(2,1/(A$1:A1=-A2),B$1:B1))

Formula in E2
=SUMPRODUCT(A2:A9,C2:C9)

Note that changing the ranges to the original data setup (ignoring the added last row of data)
=SUMPRODUCT(A2:A8,C2:C8)
we get 4170 as desired

M.
 
Upvote 0
A tricky question: what happens if the data looks like below?

Qty Bought/Sold Price Bought/Sold
20,600 $0.24
8,000 $0.38
8,000 $0.36
-20,600 $0.40
-8,000 $0.35
8,000 $0.37
2,000 $0.28
2,500 $0.26

Should -8,000 on the 5th row be calculated with the 2nd row buying price or the 3rd row price?
 
Last edited by a moderator:
Upvote 0
With such scenario my suggestion above fails:(.
Another helper column? Argh!! It seems it's not possible to create a formula to solve this problem...

M.
 
Upvote 0
With one helper column:

Qty: A2:A8
Price: B2:B8
CumulativeBuys: C2:C8
C8: =MAX(0,A8)
C2: =C3+IF(A2>0,A2) Copy down to C7

C10: =SUMPRODUCT(--(ROW(Qty)>=MAX(IF(CumulativeBuys>=SUM(Qty),ROW(Qty)))),--(Qty>0),Qty-IF(CumulativeBuys>SUM(Qty),CumulativeBuys-SUM(Qty)),Price)

To do it without a helper column, you could generate CumulativeBuys using a formula, but it would be long and messy.


Excel 2010
ABC
1Qty Bought/SoldPrice Bought/SoldCumulative Buys
220,600$0.2441,100
38,000$0.3820,500
4-20,600$0.4012,500
5-8,000$0.3512,500
68,000$0.3712,500
72,000$0.284,500
82,500$0.262,500
9
10Result$4,170.00
Sheet1
 
Last edited:
Upvote 0
I think the conclusion is it really does need to be done manually ... I'll continue having a think about how to phrase this in formula-speak, but I believe it's close to impossible. Thanks all for trying to understand my nonsense and help out :)
 
Upvote 0
Thanks Stephen, I will give this a try :) Definitely seems like what I'm looking for, despite my particularly poor description of the problem
 
Upvote 0
Mate, this works perfectly. Thanks so much for understanding my nonsensical jumble and providing a solution that works! Cheers.
 
Upvote 0

Forum statistics

Threads
1,214,618
Messages
6,120,544
Members
448,970
Latest member
kennimack

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