I have a simple table with some dummy data to simulate stock trades. I know the its model is not exactly right, but it does comply with the basics and that's what I'm looking for.
The answer I'm looking for is how would I calculate the data on column 12. It's supposed to show the Average Price of the position at that date. If the position opened with the current row then the average price is the actual price of the trade, however the position can be increased or decreased with new prices and I want to know the average price up to that date (last two rows have this example).
Column 13 (What I got so far) has this definition*: =SUMPRODUCT([Delta Position];--([Date Trade]<=[#[Date Trade]]);--([Position After Trade]=[#[Position After Trade]]))
*actually the there's two more columns, Trader and Maturity Date, these two are used to filter data and are not shown to make it more readable: --([Person]=[#Person]);--([Month]=[#month])
(second row just shows the definition of the simpler columns)
Date Trade
Price
op
Qty
Total
Accumulated Before
Accumulated After
Position After Trade
Qty Delta Position
Delta Position
Value of Open Position
What I got so far
Average Price Open Position
C1
C2
C3
C4
C5 = C2 x C4
C6
C7
C8
C9
C10 = C9 * C2
C11
C12
C13 = C11 / C7
2021-12-06
328
Buy
99
32472
0
99
long
99
32472
HELP: 32472
32472
328
2021-12-13
291
Sell
99
28809
99
0
zero
0
0
HELP: 0
0
0
2022-01-31
190
Sell
120
22800
0
-120
short
-120
-22800
HELP: -22800
-22800
190
2022-02-14
165
Buy
120
19800
-120
0
zero
0
0
HELP: 0
0
0
2022-04-20
113,7
Sell
200
22740
0
-200
short
-200
-22740
HELP: -22740
-45540
113,7
2022-06-21
74,333
Buy
300
22299,9
-200
100
long
100
7433,3
HELP: 7433.3
39905,3
74,333
2022-08-16
86
Buy
50
4300
100
150
long
50
4300
HELP: 11733.3
44205,3
78,222
The average price of an open position is calculated by this fórmula
Average price fórmula
Iteratively you can obtain this fórmula:
Iterative fórmula
First row (first operation) => Average price = M3 = If(C3='Buy';B3;-B3)
Second row => Average price => Average price = M4 = ((M3F4)+(If(C4='Buy';B4C4;-B4*C4) )) / G4
Next rows you only have to iterate fórmula.
I hope this can help you.
Related
This is my sheet:
Goods Type Quantity / Geram Gold Price 750K Price of Exchange
Gold 100 1,554.00 0
Silver 500 235.00 0
Euro 200 0.00 1.01
Pond 50 0.00 0.97
Gold 100 1,554.00 0
Silver 500 235.00 0
Euro 200 0.00 1.00
Pond 50 0.00 0.99
I want to do to this:
If Goods Type is Gold: multiply Quantity / Geram by Gold Price.
In my case it should be: (100 * 1554) + (100 * 1554) = 310800, and if I add more entries in the future, it should add more.
I have this cell in my current sheet: If Goods Type is Gold, then sumif all Quantity / Geram in another cell which is now 200.
Use SUMPRODUCT: sumproduct
=SUMPRODUCT((A2:A9="Gold")*B2:B9*C2:C9)
It multiplies TRUE/FALSE (result of A2:A9="Gold") with quantity and price and sums everything (in this case = 155400 + 0 + 0 + 0 + 15540 + 0 + 0 + 0))
Result:
Adjust ranges to your data.
You can make a SumProduct() for three columns, so you can create an IF() formula for getting 1 in case of "Gold" and 0 else. This leads to following kind of formula:
=SUMPRODUCT(IF(A2:A6="Gold",1,0),B2:B6,C2:C6)
See following screenshot:
How do I do this is excel. The example is provided below. If we don't sell any products on a specific day I would like to move those hours to the next date.
If you need to compute partial sums of Hours for all the consecutive rows between rows that satisfy the condition that the value Sold is greater than zero, you could do this with an auxiliary column
A B C D
---------------------------------
1 |Hours Sold Sums Solution
2 | 300 30 300 300
3 | 30 0 300 0
4 | 0 0 300 0
5 | 30 0 300 0
6 | 300 50 660 360
7 | 23 0 660 0
8 | 100 25 783 123
Here Sums is defined by a formula for C2
=IF(B2>0,SUM(A2:$A$2),C1)
You can automatically populate the cells below.
This formula puts in a cell a partial sum of Hours up to the current row if Sold is nonzero, otherwise copies the previous partial sum of hours. We need this to subtract this value on the next step.
When you have the column C filled, it is sufficient to put the following formula in D2 and populate the cells below
=IF(ROW(B2)>2,IF(B2>0,C2-C1,0),C2)
This formula handles correctly both D2 that does not have a preceding row with values and the remaining cells in column D.
In fact you could combine the two formulas together and avoid the need to have an auxiliary column. Put the following formula in C2 and spread it down to the rest of the cells in column C
=IF(ROW(B2)>2,IF(B2>0,SUM(A2:$A$2)-SUM(C1:$C$2),0),A2)
to get
A B C
---------------------------------
1 |Hours Sold Solution
2 | 300 30 300
3 | 30 0 0
4 | 0 0 0
5 | 30 0 0
6 | 300 50 360
7 | 23 0 0
8 | 100 25 123
I have different entries for the weight over different periods of time and wanted to see the weight difference from the max point in time to the min point in time based on a specific ID.
ID Date Weight Difference
12345 1/1/22 100 -10
12345 2/1/22 95 -10
12345 3/1/22 90 -10
44440 1/1/22 90 10
44440 2/1/22 95 10
44440 3/1/22 100 10
18258 2/1/22 105 -1
18258 3/1/22 104 -1
23584 3/1/22 110 0
So far, I have used the following formula: =MAX(IF(A:A=A2,U:U))-MIN(IF(A:A=A2,U:U))
A is the ID, U is the Weight
The formula is not taking the max date into account but the max weight and subtracting that from the min weight, instead of taking the weight from the max date and the weight from the min date.
I have the following Excel spreadsheet:
A B C D E F G
1 Q1 Q2 Q3 Q4 Nearest negative value to 0 -10
2 Asset 1 -50 85 -90 70 Nearest positive value to 0 5
3 Asset 2 -28 -80 -45 60
4 Asset 3 -30 50 55 -10
5 Asset 4 -20 5 -80 -15
6 Asset 5 35 -30 27 -98
7
In Cells A1:E6 I have different assets with their performance from quarter Q1-Q4. The performance can be positive or negative.
In cell G1 and G2 I want to find the nearest negatvie and positive value to 0 in Cells B2:E6. In this case this would -10 and 5. Therefore, I tried to go with the solution here:
G1 = {MAX(IF($B$2:$E$6<0,1,0)*($B$2:$E$6))}
G2 = {MIN(IF($B$2:$E$6>0,1,0)*($B$2:$E$6))}
However, both formulas give me 0 as a result.
Do you have any idea how to solve this issue?
NOTE: All values in Cells B2:E6 are unique.
For largest negative number
=AGGREGATE(15,6,B2:E6/(B2:E6>0),1)
For smallest positive number
=AGGREGATE(14,6,B2:E6/(B2:E6<0),1)
You do not need the *:
=MAX(IF($B$2:$E$6<0,$B$2:$E$6))
And
=MIN(IF($B$2:$E$6>0,$B$2:$E$6))
These are Array formula and need to be confirmed with Ctrl-Shift-Enter.
Along with the AGGREGATE given by Forward Ed if on has Office 365 Excel:
=MAXIFS($B$2:$E$6,$B$2:$E$6,"<"0)
And
=MINIFS($B$2:$E$6,$B$2:$E$6,">"0)
I need to generate 13 numbers from 13 different intervals which will add up to 1360. In the chart below, "index" means the index of the 13 different numbers. Mean means the mean (average) of the intervals. The range will be plus or minus 15% of the mean as shown below. I will prefer to have the random numbers generated based on the normal distribution with N(mean, 7.5% of mean). I take it back. No normal distribution. Please use +- 15% as hard limits of the intervals.
It will be great if anyone could figure out how to do it in excel. Algorithms will be appreciated as well.
Index mean 15% low high
A 288 43 245 331
B 50 8 43 58
C 338 51 287 389
D 50 8 43 58
E 16 2 14 18
F 66 10 56 76
G 118 18 100 136
H 17 3 14 20
I 91 14 77 105
J 26 4 22 30
K 117 18 99 135
L 165 25 140 190
M 18 3 15 21
I would sort the table by increasing mean:
and use a column for a helper value (column H above).
The idea is to maintain -- while going to the next row -- the current deviation from a perfect aim for the final target. Perfect would mean that every random value coincides with the mean for that row. If a value is 2 less than the mean, then that 2 will appear in the H column for the next row. The random number generated for that next row will then not aim for the given mean, but for 2 less than the mean. The range for the random number will appropriately be reduced so that the low/high values will never be crossed.
By first sorting the rows, we can be sure that this corrected mean will always fall within the next row's low/high range, and so it will always be possible to generate an acceptable random number there.
The final value will be calculated differently: it will be the remainder that is needed to achieve the target sum. For the same reason as above, this value is guaranteed to be within the low/high range.
The formulas used are as follows:
| F | H
--+--------------------------------------------------+------------------------------
2 | =RANDBETWEEN(D2, E2) |
3 | =RANDBETWEEN(B3+H3-C3+ABS(H3), B3+H3+C3-ABS(H3)) | =SUM($B$2:$B2)-SUM($F$2:$F2)
4 | (copy above formula) | (copy above formula)
...| ... | ...
13 | (copy above formula) | (copy above formula)
14 | =SUM($B$2:$B14)-SUM($F$2:$F13) |
In theory the rows do not need to be sorted first, but then the formulas cannot be copied down like above, but must reference the correct rows. That would make it quite complicated.
If it is absolutely necessary that the rows are presented in order of the Index column (A, B, C...), then use another sheet to do the above. Then in the main sheet read the value into the F column with a VLOOKUP from the other sheet. So in F2 you would have:
=VLOOKUP(A2, OtherSheet!$A$2:$F$14, 6, 0)
Get the random number like this
num = Int ((300 - 200 + 1) * Rnd + 200) //between 200 and 300
Click here for more information
and the random number need to be the total sum minus the sum that you already got and the last one will be that left.
for example: (if we have 4 numbers sum up to 100)
A is a random number between 0 to 100 //lets say 42
then B is a random number between 0 to (100-42) => 0 to 78 //lets say 18
then C is a random number between 0 to (100-42-18) => 0 to 40 //lets say 25
then, in the end D is 100-42-18-25 => D is 15
*100-42-18-25 is the same as 100-Sum(A,B,C)
Here is my example generate random number based on low and high.
The formula in column F is just a RANDBETWEEN:
=RANDBETWEEN($D2,$E2)
Then you can get the result always equal to 1360 with the formula below for column G:
=F2/SUM($F$2:$F$14)*1360
So cell G15 will always be 1360 which is the sum of all those 13 intervals.