Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6580809
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T16:04:25+00:00 2026-05-25T16:04:25+00:00

I lost ideas (with my limited R knowledge) how to tackle following problem in

  • 0

I lost ideas (with my limited R knowledge) how to tackle following “problem” in a performant (vectorized) way.

I would like to determine days when SPX closed up 3 or more days in a row and, at the same time, is not coming from a 50-day low. I programmed this for fixed look back for three days but do not know how to make it dynamic. Here is the code:

require(quantmod)
getSymbols(c("^GSPC"), adjust=TRUE, from="1990-01-01")
assign("SPX", GSPC, envir=.GlobalEnv)
names(SPX) <- c("SPX.Open", "SPX.High", "SPX.Low", "SPX.Close", "SPX.Volume",     "SPX.Adjusted")

SPX.ClCl.positive <- ifelse(ClCl(SPX) > 0, 1, 0)
SPX.ClCl.positive[is.na(SPX.ClCl.positive)] <- 0
numDaysPositive <- cumsum(SPX.ClCl.positive) - cummax(cumsum(SPX.ClCl.positive)*    (!SPX.ClCl.positive))
numDaysPositiveGreaterThan3 <- ifelse(numDaysPositive >= 3, 1, 0)

SPX.Lo.gt.50day.low <- ifelse(lag.xts(Lo(SPX), k=3) <= runMin(Lo(SPX), n=50), 1, 0)

What I would like be able to do is something like this:

SPX.Lo.gt.50day.low <- ifelse(lag.xts(Lo(SPX), k=numDaysPositive) <= runMin(Lo(SPX), n=50), 1, 0)

EDIT START

I would like to see, if we are up on SPX more than three days in a row (3, 4, 5,…) (kept in variable numDaysPositive) whether this rise came from 50-day low. I would like to look back 3, 4, 5,… days to see if on that particular date (3, 4, 5,…) days ago SPX made a 50-day low. The “logic” or assumption is, that for rallies from 50-day lows being up 3 or more days in a row is not uncommon, but, if we are up 3, 4, 5, … days in a row and it did not start out of a 50-day low, then, this might be worth considering as one of the “evidences” market may stop or even drop for a while.

For now I am using lag.xts with k=3 in last ifelse but would like to use k=numDaysPositive (dynamic).

EDIT END

So, I would like that k in lag is dynamic based on value in numDaysPositive. I am sure this is easy if only one can see how… I am looking at this for the whole day now and nothing comes to mind.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T16:04:26+00:00Added an answer on May 25, 2026 at 4:04 pm

    The code below will allow you to see which series of cumulative up days began on (or near) a 50-day low.

    # load quantmod and pull dada
    library(quantmod)
    SPX <- getSymbols("^GSPC", from="1990-01-01", auto.assign=FALSE)
    names(SPX) <- gsub("GSPC","SPX",names(SPX))
    
    # up (TRUE) and down (FALSE) days
    b <- c(FALSE, ClCl(SPX)[-1] > 0)
    # run length of each stretch of up/down days
    x <- rle(as.vector(b))
    # use rle results to create a vector of zeros (down days) and n
    # where n is the number of consecutive up days
    y <- unlist(lapply(seq_along(x$value), function(i)
      rep(if(x$value[i]) x$lengths[i] else 0,x$lengths[i])))
    # 50-day low
    z <- runMin(Lo(SPX), 50) == Lo(SPX)
    # convert results to xts
    y <- xts(y, index(SPX))
    z <- xts(z, index(SPX))
    # look at results
    tail(merge(SPX,cumUpDays=y,z),50)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I lost my temper few days ago because of this simple problem. Can you
I'm a little lost with the best way to process a record from a
I'm a little lost on how to achieve this problem. We have a list
I'm completely lost and apparently have fall way behind with regards to my web
Looking for some ideas/pattern to solve a design problem for a system I will
I lost my last commit because I accidentally ran git reset --hard HEAD^. Note:
I have lost the debug panel/toolbar (with the yellow curly arrow step tools) in
I find my self lost while trying to create an actually pretty easy SQL
I'm a little lost on moving a div within the dom. I basically need
I'm completely lost on this, I'm not sure what is causing this error: Fatal

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.