N
NorTor
Hello everybody!
I am stuck in how to solve the following problem in Excel VBA.
Here is the situation;
I have two tables, the first one contains all the 'buy' transactions
of one particular financial share. Every row of the table is one
buy-transaction of the share. The columns gives info on buy-date,
number of shares bought, price per share, and total cost.
The second table contains the same info, but with the 'sales' of the
same particular stock. (Same here, e.g. every row is one transaction,
the columns of every row containing the same information as for the
'buy' table.
Now here is my challenge; I need to find a way to calculate a weighted
average of the buy costs related to every 'sales' row... this is to be
able to calculate profit / loss for every transaction (row) in the
'sales' table. In other words, calculation of profit / loss should be
according to the FIFO (First In First Out) principle.
This is easy of course if there is one sale for every buy, e.g. a buy
of 1000 shares is followed by a sale of 1000 share; then the buy price
of that sale would be the exact price of the corresponding buy.
But in fact the quantities of the 'sale' transaction is totally
independent of the quantities of the 'buy' transactions, the only
restriction is that there at all times can be no larger sale quantity
then the stock of shares in total (no short-selling allowed).
So for instance the buys can be like:
Date Qty. Share price
01.01.02 5,000 10.50
04.01.02 8,000 11.20
09.10.03 4,000 11.40
03.08.04 9,500 11.20
And the sales can be like:
05.08.03 7,000 11.40
10.09.04 19,500 11.60
In this example the cost price for the first sale should be calculated
as follows:
((5000 / 7000) * 10.50 + (2000 / 7000) * 11.20) = 10.70
Reason: Sale of 7,000 "eats" first buy of 5,000 + 2,000 of the next
buy (of 8,000) and corresponding cost price for this sale is hereby
the weighted average of these two factors.
The second sale, the corresponding cost is:
((6000 / 19500) * 11.20) + (4000 / 19,500) * 11.40) +
(9500 / 19500) * 11.20)) = 11.241
Reason is the same: Sale of 19,500 remaining share "eats" the
remaining 6,000 from the second buy, and all other remaining buys.
I need a VBA procedure that can handle these fully flexible buy / sell
structures, and calculate the weighted buy-average and have that
inserted as a new column in the 'sales' table.
Please pretty please help me if you can; I have headaches and
nightmares (well, almost ;-)) trying to solve this myself...
Best regards
-NorTor-
I am stuck in how to solve the following problem in Excel VBA.
Here is the situation;
I have two tables, the first one contains all the 'buy' transactions
of one particular financial share. Every row of the table is one
buy-transaction of the share. The columns gives info on buy-date,
number of shares bought, price per share, and total cost.
The second table contains the same info, but with the 'sales' of the
same particular stock. (Same here, e.g. every row is one transaction,
the columns of every row containing the same information as for the
'buy' table.
Now here is my challenge; I need to find a way to calculate a weighted
average of the buy costs related to every 'sales' row... this is to be
able to calculate profit / loss for every transaction (row) in the
'sales' table. In other words, calculation of profit / loss should be
according to the FIFO (First In First Out) principle.
This is easy of course if there is one sale for every buy, e.g. a buy
of 1000 shares is followed by a sale of 1000 share; then the buy price
of that sale would be the exact price of the corresponding buy.
But in fact the quantities of the 'sale' transaction is totally
independent of the quantities of the 'buy' transactions, the only
restriction is that there at all times can be no larger sale quantity
then the stock of shares in total (no short-selling allowed).
So for instance the buys can be like:
Date Qty. Share price
01.01.02 5,000 10.50
04.01.02 8,000 11.20
09.10.03 4,000 11.40
03.08.04 9,500 11.20
And the sales can be like:
05.08.03 7,000 11.40
10.09.04 19,500 11.60
In this example the cost price for the first sale should be calculated
as follows:
((5000 / 7000) * 10.50 + (2000 / 7000) * 11.20) = 10.70
Reason: Sale of 7,000 "eats" first buy of 5,000 + 2,000 of the next
buy (of 8,000) and corresponding cost price for this sale is hereby
the weighted average of these two factors.
The second sale, the corresponding cost is:
((6000 / 19500) * 11.20) + (4000 / 19,500) * 11.40) +
(9500 / 19500) * 11.20)) = 11.241
Reason is the same: Sale of 19,500 remaining share "eats" the
remaining 6,000 from the second buy, and all other remaining buys.
I need a VBA procedure that can handle these fully flexible buy / sell
structures, and calculate the weighted buy-average and have that
inserted as a new column in the 'sales' table.
Please pretty please help me if you can; I have headaches and
nightmares (well, almost ;-)) trying to solve this myself...
Best regards
-NorTor-