+ Reply to Thread
Results 1 to 5 of 5

Trend formula- consecutive positive and negative numbers

Hybrid View

  1. #1
    Registered User
    Join Date
    06-03-2014
    Posts
    3

    Trend formula- consecutive positive and negative numbers

    Hi all,
    I have a large data set with dates in column A and the daily price percentage change of an asset in column B. Over the last 10 days, the price of the asset has fallen and I was wondering whether there is a formula of determining when the last time the pattern occurred?
    I'm having to do it manually and I'm going out of my mind. Any help would be utterly appreciated

    Here is my data set.
    Attached Files Attached Files

  2. #2
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: Trend formula- consecutive positive and negative numbers

    So for your example, what result would you like to see?
    Entia non sunt multiplicanda sine necessitate

  3. #3
    Registered User
    Join Date
    06-03-2014
    Posts
    3

    Re: Trend formula- consecutive positive and negative numbers

    Hi,
    I'm looking to see when the last time before June 3 when prices fell for 10 consecutive sessions, is this possible?

  4. #4
    Forum Expert
    Join Date
    05-01-2014
    Location
    California, US
    MS-Off Ver
    Excel 2010
    Posts
    1,795

    Re: Trend formula- consecutive positive and negative numbers

    Quote Originally Posted by Col83 View Post
    I have a large data set with dates in column A and the daily price percentage change of an asset in column B. Over the last 10 days, the price of the asset has fallen and I was wondering whether there is a formula of determining when the last time the pattern occurred?
    If you are amenable to a VBA solution, try the following procedure. It finds the dates of all runs that greater than or equal to the goal (10).

    In Excel, right-click on the worksheet tab at the bottom, then click on View Code. Copy the text of the procedure below, and paste into the VBA edit on the right.

    You can close the VBA window. It would be prudent to save the Excel workbook as macro-enabled (xlsm).

    In Excel, press alt-F8 and double-click on "findit" to execute the macro.

    Sub findit()
        ' Assumes data are in column A (date) and B (%change).
        ' Select location for result.
        ' Results:  1st column is date; 2nd column is run length
        
        Const nGoal As Long = 10
        Dim v As Variant, nV As Long
        Dim nRun As Long, nRun0 As Long
        Dim i As Long, nRes As Long
        
        ' input data
        v = Range("a1", Cells(Rows.Count, "b").End(xlUp))
        nV = UBound(v, 1)
        
        ' find dates when run of -%change >= nGoal
        ReDim res(1 To nV, 1 To 2) As Variant
        nRun = 0: nRes = 0
        For i = 1 To UBound(v, 1)
            nRun0 = nRun
            If Not WorksheetFunction.IsNumber(v(i, 2)) Then nRun = 0 _
            Else If v(i, 2) >= 0 Then nRun = 0 _
            Else nRun = nRun + 1
            If nRun = 0 And nRun0 >= nGoal Then
                nRes = nRes + 1
                res(nRes, 1) = v(i - 1, 1)
                res(nRes, 2) = nRun0
            End If
        Next
        ' in case end of data is run of -%change
        If nRun >= nGoal Then
            nRes = nRes + 1
            res(nRes, 1) = v(nV, 1)
            res(nRes, 2) = nRun
        End If
        
        ' output results
        Selection.Resize(nRes, 2) = res
        MsgBox "done"
    End Sub

  5. #5
    Registered User
    Join Date
    06-03-2014
    Posts
    3

    Re: Trend formula- consecutive positive and negative numbers

    Thank you. Do you know how I could change the formula to find the last time prices rose for X number of days as opposed to fell, which the above formula can achieve? I really, really appreciate the help
    Last edited by Col83; 06-05-2014 at 02:58 AM.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Find consecutive 15 positive or negative numbers in a column
    By Sarangsood in forum Excel Formulas & Functions
    Replies: 6
    Last Post: 11-19-2013, 05:26 AM
  2. [SOLVED] Count Consecutive Positive/Negative numbers
    By Taislin in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 10-16-2013, 01:02 PM
  3. Sum of time for consecutive positive/ negative numbers
    By DexterG in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 12-25-2012, 04:03 AM
  4. Replies: 6
    Last Post: 11-13-2012, 07:33 AM
  5. Convert negative to positive in sheet containing both positive/negative numbers
    By sa02000 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 05-29-2010, 07:52 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1