How to do an average on time series data?

I need to generate average hold times for various stock of companies as follows:
The data looks like:
stock        timestamp (sec)            quantity
GOOG          12459.6                    -100        <-- SALE
GOOG          12634.0                    +100        <-- PURCHASE
GOOG          12636.2                    +200
GOOG          12464.8                    -100
GOOG          12568.3                    -300
GOOG          12678.0                    +200
The rules are
1. begin and end day with balance 0
2. can short sell, i.e. can sell shares even if balance is currently 0
3. hold time is defined as number of seconds stock was held before it was sold
4. first stock purchased are sold first
I need to generate the average hold times seconds per share.  I'd prefer to do this using SQL and NOT a procedure.
Any tips on how to go about calculating this?  I have looked at various analytic functions, but still not sure.
Thank you.

I'm afraid you might be after something like below:
this is a simplified scenario where the quantity balance always reaches 0 before changing sign (not very probable in real life)
Simple examples are reserved for the lecturer was a pretty common phrase in my university times
I dont know how to deal with a general case yet
select * from trade_0 order by position,time
TIME
POSITION
DIRECTION
QUANTITY
8
GOOG
S
100
13
GOOG
B
20
16
GOOG
B
30
17
GOOG
B
30
19
GOOG
B
20
22
GOOG
B
20
25
GOOG
B
30
26
GOOG
B
20
30
GOOG
B
30
34
GOOG
B
20
38
GOOG
B
30
41
GOOG
S
150
7
YHOO
S
10
12
YHOO
S
20
15
YHOO
S
30
16
YHOO
S
40
18
YHOO
S
60
21
YHOO
S
30
24
YHOO
S
10
25
YHOO
B
100
29
YHOO
B
300
33
YHOO
S
100
37
YHOO
S
80
40
YHOO
S
20
your condition 4. first stock purchased are sold first requires a procedural solution so model clause must be used if you want to do it in SQL.
Model Men, bear with me, please !
select m.*,
       avg(abs(x_time - decode(kind,'B',time_b,time_s))) over (partition by position
                                                                   order by rn rows between unbounded preceding
                                                                                        and unbounded following
          ) average
  from (select *
          from (select nvl(b.position,s.position) position,
                       nvl(b.rn,s.rn) rn,
                       nvl(b.cnt,0) cnt_b,
                       nvl(s.cnt,0) cnt_s,
                       b.time time_b,
                       s.time time_s,
                       b.quantity qty_b,
                       s.quantity qty_s
                  from (select time,position,quantity,
                               row_number() over (partition by position order by time) rn,
                               count(*) over (partition by position) cnt
                          from trade_0
                         where direction = 'B'
                       ) b
                       full outer join
                       (select time,position,quantity,
                               row_number() over (partition by position order by time) rn,
                               count(*) over (partition by position) cnt
                          from trade_0
                         where direction = 'S'
                       ) s
                    on b.position = s.position
                   and b.rn = s.rn
         model
         partition by (position)
         dimension by (rn)
         measures (0 loc,
                   case when cnt_b >= cnt_s then 'B' else 'S' end kind,
                   time_b,
                   time_s,
                   qty_b,
                   qty_s,
                   0 qty_left,
                   0 x_time
         rules iterate (1000000) until (loc[iteration_number] is null)
          loc[0] = nvl2(loc[0],loc[0],1),
          qty_left[loc[0]] = case when iteration_number > 0
                                  then qty_left[loc[0]] + case when kind[iteration_number] = 'B'
                                                               then qty_b[iteration_number]
                                                               else qty_s[iteration_number]
                                                          end
                                  else 0
                             end,
          x_time[iteration_number] = case when kind[iteration_number] = 'B'
                                          then time_s[loc[0]]
                                          else time_b[loc[0]]
                                     end,
          loc[0] = loc[0] + case when qty_left[loc[0]] = case when kind[iteration_number] = 'B'
                                                              then qty_s[loc[0]]
                                                              else qty_b[loc[0]]
                                                         end
                                 then 1
                                 else 0
                            end
       ) m
where kind is not null
order by position,rn
POSITION
RN
LOC
KIND
TIME_B
TIME_S
QTY_B
QTY_S
QTY_LEFT
X_TIME
AVERAGE
GOOG
1
0
B
13
8
20
100
100
8
10.4
GOOG
2
0
B
16
41
30
150
150
8
10.4
GOOG
3
0
B
17
30
8
10.4
GOOG
4
0
B
19
20
0
8
10.4
GOOG
5
0
B
22
20
0
41
10.4
GOOG
6
0
B
25
30
0
41
10.4
GOOG
7
0

Similar Messages

  • How do you analyse a binary time series using labview?

    I am new with time series data analysis and labview. I was wondering if anyone could help me out with analyzing a binary time series (1 or 0 output) using labview. ecifically I want to find out the period/frequency of a
    oscillating signal. Can I use the Walsh transform. If so how do I convert 'sequency' to time period. Are there any beginners text book out there? I would reallly appreciate it if anyone could help me out with this.

    Your comment about an indicator getting "clogged up with data" doesn't make any sense. The contents of a string or array indicator can get large and slow things down but a numeric indicator cannot get clogged up. If the data stops making sense, then you are incorrectly reading the instrument and converting that data to a numeric. With your comments about the device transmit buffer, I suspect you have occasionaly set the byte count too low and unread bytes are there that you then read the next time. As long as the instrument is fairly new, it will send out a termination character (typically EOI) that will terminate the read. You can then set the read count to some arbitrarily high number to ensure you've got the entire tr
    ansmit buffer contents. It's also possible that you periodicaly have an error condition where the instrument is sending unexpected information. For example, if it normally sends a floating point number as a result and then it sends an error message string, you might not be intrepreting it correctly.

  • How can I move (copy) my Time Capsule date to a new 2T hard drive and then wipe the TC and start as a new (clean) Time MACHINE backup?

    How can I move (copy) my Time Capsule date to a new 2T hard drive and then wipe the TC and start as a new (clean) Time MACHINE backup?

    Just use the archive utility on the disk page of the airport utility manual setup..
    If you have lion and v6 airport utility.. it missed out. On lots of things.
    Download and install the real.. UTILITY
    5.6 http://support.apple.com/kb/DL1482

  • Read optimization time-series data

    I am using Berkeley DB JE to store fairly high frequency (10hz) time-series data collected from ~80 sensors. The idea is to import a large number of csv files with this data, and allow quick access to time ranges of data to plot with a web front end. I have created a "sample" entity to hold these sampled metrics, indexed by the time stamp. My entity looks like this.
    @Entity
    public class Sample {
         // Unix time; seconds since Unix epoch
         @PrimaryKey
         private double time;
         private Map<String, Double> metricMap = new LinkedHashMap<String, Double>();
    as you can see, there is quite a large amount of data for each entity (~70 - 80 doubles), and I'm not sure storing them in this way is best. This is my first question.
    I am accessing the db from a web front end. I am not too worried about insertion performance, as this doesn't happen that often, and generally all at one time in bulk. For smaller ranges (~1-2 hr worth of samples) the read performance is decent enough for web calls. For larger ranges, the read operations take quite a while. What would be the best approach for configuring this application?
    Also, I want to define granularity of samples. Basically, If the number of samples returned by a query is very large, I want to only return a fraction of the samples. Is there an easy way to count the number of entities that will be iterated over with a cursor without actually iterating over them?
    Here are my current configuration params.
    environmentConfig.setAllowCreateVoid(true);
              environmentConfig.setTransactionalVoid(true);
              environmentConfig.setTxnNoSyncVoid(true);
              environmentConfig.setCacheModeVoid(CacheMode.EVICT_LN);
              environmentConfig.setCacheSizeVoid(1000000000);
              databaseConfig.setAllowCreateVoid(true);
              databaseConfig.setTransactionalVoid(true);
              databaseConfig.setCacheModeVoid(CacheMode.EVICT_LN);

    Hi Ben, sorry for the slow response.
    as you can see, there is quite a large amount of data for each entity (~70 - 80 doubles), and I'm not sure storing them in this way is best. This is my first question.That doesn't sound like a large record, so I don't see a problem. If the map keys are repeated in each record, that's wasted space that you might want to store differently.
    For larger ranges, the read operations take quite a while. What would be the best approach for configuring this application?What isolation level do you require? Do you need the keys and the data? If the amount you're reading is a significant portion of the index, have you looked at using DiskOrderedCursor?
    Also, I want to define granularity of samples. Basically, If the number of samples returned by a query is very large, I want to only return a fraction of the samples. Is there an easy way to count the number of entities that will be iterated over with a cursor without actually iterating over them?Not currently. Using the DPL, reading with a key-only cursor is the best available option. If you want to drop down to the base API, you can use Cursor.skipNext and skipPrev, which are further optimized.
    environmentConfig.setAllowCreateVoid(true);Please use the method names without the Void suffix -- those are just for bean editors.
    --mark                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               

  • Discoverer 4i - Time Series Data type support

    Does Discoverer 4i support time-series data type i.e. the ability to store an entire string of
    numbers representing for example daily or weekly data points?
    Thanks & Regards,
    Deepti

    Hi O G-M,
    Each model must contain one numeric or date column that is used as the case series, which defines the time slices that the model will use. The data type for the key time column can be either a datetime data type or a numeric data type. However, the column must
    contain continuous values, and the values must be unique for each series. The case series for a time series model cannot be stored in two columns, such as a Year column and a Month column. For more information about it, please see:
    http://msdn.microsoft.com/en-us/library/ms174923(v=sql.100).aspx
    Thanks,
    Eileen
    Eileen Zhao
    TechNet Community Support

  • Time-Series Data storage

    What's the most effiecient way to store time-series data in oracle?.
    Thanks,
    Jay.

    937054 wrote:
    Hello,
    1. Usally time-series data goes in multiple millions, so timeseries databases like FAME,KDB,SYBASE-IQ are used. Does oracle11gr2 provide storage optmizations, compressions, columnar database like FAME,KDB,SYBASE-IQ?
    The only methods of optimization are partitioning of the data by some date or if data set is narrow (few columns)l enough, partitioned IOT.
    2. http://www.oracle.com/us/corporate/press/1515738
    Link is about R statistical language and data mining integration with Oracle database 11gR2. Does this come default during installation or with BigData - EXADATA? OR this is a seperate license?I am not sure about the licensing, you will need to ask your sales person, but it looks like it might be apart of ODM (oracle data mining - a licensed product)
    Take a read through this case study.
    http://www.oracle.com/technetwork/database/options/advanced-analytics/odm/odmtelcowhitepaper-326595.pdf?ssSourceSiteId=ocomen
    >
    Thanks

  • Power Spectrum Density conversion to Time Series Data

    Hi,
    This may seem an odd request but is there a way to convert power spectrum density data back to the time series data that generated it in the first place. I have lost the original time series data but still have the PSD and need the time series to do other analysis.
    Thanks,
    Rhys Williams

    Hate to be the bearer of bad news, but there are an infinite number of time series that will generate a given PSD.  You lose all phase information upon taking the PSD.  For this reason I almost always save time domain data, or at least complex FFT values.  

  • Relevance of Time series data in PPDS

    Hi, we have an SNP planning area with some times series KFs in it. if we delete the time series for the PA what impact it would have on PPDS..
    or what would be the relevance of times series data in PPDS?..
    kindly explain.

    the only relation of time series data to PP/DS I know of is  Time-dependent safety stock planning in PP/DS.
    Safety and Target Stock Level Planning in PP/DS
    In Customizing for PP/DS, define the following:
    ·         Make SNP key figures available
    You define which SNP key figures PP/DS should use as safety stock or daysu2019 supply. (Make SNP Key Figures Available)
    ·         Define planning area
    You specify an SNP planning area that contains the key figures for the time-dependent safety stock/daysu2019 supply levels. (Global Settings ® Maintain Global Parameters and Defaults)
    if you did not use the planning area and key figure there should be no influence.
    Frank

  • SSM - Doing a Weighted Average in Time Series Consolidation

    Hi,
    We have a requirement wherein the Yearly/ YTD View of a scorecard has to be a weighted average of the Quarterly Values.
    Please let me know if there is any way to implement this while doing a time series consolidation.
    Thanks,
    Peeyush

    Hi Peeyush,
    You can use the following standard methods for time consolidation:
    SUM,
    FIRST DATA VALUE,
    LAST DATA VALUE,
    INCLUSIVE AVERAGE,
    EXCLUSIVE AVERAGE,
    WEIGHTED ON ANOTHER MEASURE
    The command in IDQL to set the time consolidation for a measure  to WEIGHTED is the FOLLOWING:
    SET VAR kpi1 WEIGHTED kpi2
    You can also use the Measure Properties dialog box (in the tab 'Numeric') to set this property. You may need to first issue the command SET SHORT in IDQL in order to correctly choose a variable selected from the dropdown list.
    If the measure kpi1 is monthly, for example, then the quarterly and yearly values will now display as weighted average with kpi2.
    Hope this helps!
    Best regards!
    Ricardo

  • How much is the average standy time of your iphone4s? Mine is 11 hours ONLY!

    How much is the average stand by time of your Iphone 4s? Mine is 11 hours ONLY. I think this is not normal.

    Please search this forum before posting.  Your issue has been addressed in hundreds of posts already.  Either use the search box at the upper right hand corner, or look in the "More Like This" box immediately to your right.

  • Time Series Data Mining

    All,
    I'd like to know how to model a time series table to submit it to Oracle Data Mining. I've already tried a reverse pivot, but is there something different?
    Regards,
    Paulo de Tarso Costa de Sousa

    If you are trying to do something like create a demand forecasting model like ARIMA, ODM does not have explicit support for this type of modeling. Data mining is usually more about creating a "prediction" rather than a forecast. You may want to look at Oracle's OLAP capabilities for this.
    If you are trying to include variables that contain the element of "time", such as "blood pressure before" and "blood pressure after", you can include these a variables (attributes) in the model. ODM has no real limit on the number of variables it can include in the model, so you don't have to worry about creating too many of them (usually).
    You may want to "clump" the data so as to create a set of variables at certain check points in time like the "before" and "after" approach above. Rather than entering for example the measurement off an instrument ever 10 seconds (which would ordinarily create new variables for each time period), you may want to only detect "events". That is, only record the amount of time between events--sort of Mean Time Between Failure (MTBF) type of modeling.
    Hope this helps with your thinking about how to approach your problem

  • How do you add the absolute time and date to a spreadshee​t file?

    I am creating a spreadsheet file, and I want to write the absolute time and date to its first column. Does anybody know how to do this?
    Thank You,

    BrianL answer is still valid !
    Attached is an example file with an express vi configured to use the absolute time generated from a get time/date node.
    Chilly Charly    (aka CC)
             E-List Master - Kudos glutton - Press the yellow button on the left...        
    Attachments:
    Save with absolute time.vi ‏135 KB

  • How do i turn on the time and date info on my iphone camera?

    My old iphone, when i would upload the photos to my computer and i would put the mouse over the photo it would have time and date information. I just had to have it replaced so I uploaded some photos from the new one and it did not have this information. How do I turn that feature on so i can have that information inbedded in the photo information?

    Hi,
    if you are using PC then you can go to the picture then press right click on it then press properties and you will be able to see the time the picture created on the bottom half,
    hope this what you looking for

  • How do I display BOTH the Time AND Date in the menu bar

    When I, System Preferences > Date and Time > Clock.
    I choose "show date and time in menu bar".
    But I only get the Time and Day of week, but NOT the date.
    I am trying to get the display to look something like this.
    *12-29-07 6:34 PM*
    I tried a program called 'MagiCal' that fixed this "bug" with OS 10.4, however with 10.5 'MagiCal' prevented my mail sounds from working properly (so it had to go).
    Any ideas on how I can display the date and the time with 10.5.1

    Try this:
    http://www.nateklaiber.com/2006/02/13/os-x-add-the-date-to-the-menu-bar/

  • How can i know the logging time per data acquisition?

    Hi There,
    I'm using SignalExpress version 2.5.1 Tektronix Edition.
    I'm connecting my PC with a Tektronix Oscilloscope through Ethernet cross-cable.
    I use SignalExpress to log the data, the question is how can i know what will the time be for each data that acquire and saved in the log?
    As inside the log, under the time row, it just only show number in sequence but not time.
    Hope that some one can help me on the matter.
    Thanks in advance.
    Good Day!

    hi Priyanka,
    you can try
    1. check in metadata repository to get the data target name. rsa1->metadata repository->query
    2. if query exist in system, go to BEx Analyzer, type in query technical name or description, and 'change'/display the query in query designer, data target name is on right side (cube/ods).
    or 3. go to table RSRREPDIR, COMPID = query technical name, and INFOCUBE is the data target name. (if you only know query description, go to table RSZELTTXT, get ELTUID and put the value to COMPUID in RSRREPDIR to get INFOCUBE the data target name)
    after got the data target, go to rsa1->infoprovider, locate the data target, and right click 'show dataflow', you will get the datasource name.
    hope this helps.

Maybe you are looking for