Storing data to be used in conditional calculation

starkc69

New Member
Joined
Dec 20, 2020
Messages
14
Office Version
  1. 2016
Platform
  1. Windows
Hello All - First off, thanks in advance for any help. This is probably very simple, but eludes me. Ideally I wanted to do this with an embeded Excel function but willing to try it in a macro, VBA last resort. Goal is to run some backtesting against various data dumps with many buy/sell columns. For simplicity, I'm using one buy and one sell column and hope I can scale this solution.

Problem - Running chronologically down the timeline find the first non-zero value of 'Buy' identified and store it. Continue running chronologically down the timeline find the first non-zero value of 'Sell' identified and at that timestamp dump the result of the Stored 'Sell' - Stored 'Buy' into the P_L column. Repeat this process down the timeline until data ends, similar to the example below.

Kindest Regards,
Captain 'Can't see the forest through the Trees' starkc69

BuySellP_L
Time
9:31 AM$ 7.50$ -
9:32 AM$ -$ -
9:33 AM$ -$ -
9:34 AM$ 7.65$ -
9:35 AM$ 7.75$ -
9:36 AM$ -$ -
9:37 AM$ -$ 8.00$0.50
9:38 AM$ -$ 7.60
9:39 AM$ -$ 7.45
9:40 AM$ -$ 7.68
9:41 AM$ -$ 7.67
9:42 AM$ -$ 7.58
9:43 AM$ -$ 7.33
9:44 AM$ -$ -
9:45 AM$ -$ -
9:46 AM$ 7.00$ -
9:47 AM$ 7.02$ -
9:48 AM$ -$ -
9:49 AM$ -$ -
9:50 AM$ -$ 7.17$0.17
9:51 AM$ -$ 7.15
9:52 AM$ -$ 7.10
9:53 AM$ -$ 7.12
9:54 AM$ -$ 7.09
9:55 AM$ -$ 7.03
9:56 AM$ -$ 6.90
9:57 AM$ -$ 6.89
9:58 AM$ -$ 6.83
9:59 AM$ -$ 6.89
10:00 AM$ -$ 6.81
10:01 AM$ -$ 6.73
10:02 AM$ -$ 6.70
10:03 AM$ -$ 6.79
10:04 AM$ -$ 6.90
10:05 AM$ -$ -
10:06 AM$ -$ -
10:07 AM$ -$ -
10:08 AM$ -$ -
10:09 AM$ -$ 6.68
10:10 AM$ -$ 6.65
10:11 AM$ -$ 6.89
10:12 AM$ -$ 7.03
10:13 AM$ -$ -
10:14 AM$ 7.35$ -
10:15 AM$ 7.45$ -
10:16 AM$ 7.34$ -
10:17 AM$ 7.30$ -
10:18 AM$ 7.12$ -
10:19 AM$ -$ -
10:20 AM$ -$ -
10:21 AM$ 6.68-$0.67
10:22 AM$ 6.65
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
How about
+Fluff v2.xlsm
ABCD
1BuySellP_L
2Time
39:31 AM7.50 
49:32 AM00 
59:33 AM00 
69:34 AM7.650 
79:35 AM7.750 
89:36 AM00 
99:37 AM080.5
109:38 AM07.6 
119:39 AM07.45 
129:40 AM07.68 
139:41 AM07.67 
149:42 AM07.58 
159:43 AM07.33 
169:44 AM00 
179:45 AM00 
189:46 AM70 
199:47 AM7.020 
209:48 AM00 
219:49 AM00 
229:50 AM07.170.17
239:51 AM07.15 
249:52 AM07.1 
259:53 AM07.12 
269:54 AM07.09 
279:55 AM07.03 
289:56 AM06.9 
299:57 AM06.89 
309:58 AM06.83 
319:59 AM06.89 
3210:00 AM06.81 
3310:01 AM06.73 
3410:02 AM06.7 
3510:03 AM06.79 
3610:04 AM06.9 
3710:05 AM00 
3810:06 AM00 
3910:07 AM00 
4010:08 AM00 
4110:09 AM06.68 
4210:10 AM06.65 
4310:11 AM06.89 
4410:12 AM07.03 
4510:13 AM00 
4610:14 AM7.350 
4710:15 AM7.450 
4810:16 AM7.340 
4910:17 AM7.30 
5010:18 AM7.120 
5110:19 AM00 
5210:20 AM00 
5310:21 AM06.68-0.67
5410:22 AM06.65 
Main
Cell Formulas
RangeFormula
D3:D54D3=IF(AND(C3<>0,C2=0),IFERROR(C3-INDEX($B$2:$B3,AGGREGATE(15,6,(ROW($B$2:$B3)-ROW($B$2)+1)/($B$2:$B3>0)/(ROW($B$2:$B3)>LOOKUP(2,1/(C$1:C2>0),ROW(B$1:B2))),1)),""),"")
 
Upvote 0
You're welcome & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,215,063
Messages
6,122,927
Members
449,094
Latest member
teemeren

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