Back

Count the number of days the security closed below the previous day's close before it closed above it. 

Pretty big newbie looking for some guidance here...

I have a script already, but I am stuck at some basic calculations which are easy to do in other platforms like TradingView (but have their own limitations) which have me guessing at what to do next. I have the data frame of the security (date and close columns). Now I want to count the number of days the security closed below the previous day's close before it closed above it. 

For instance, Day 1: 192 Day 2: 191: day 3: 192 would be 2. 

How would I do this? 

Update Backtest







When creating a function like this it is important to think about the parameters and the return value. In this case, the return value is just an integer for the number of days. But what would the parameters be? In the fixed case above a simple script would be:

# for this example a list of prices was chosen to iterate through
def sample(list):
#the number of days
counter = 0
#index in the list
i = 0
while(True):
previous_close = list[i]
current_close = list[i+1]
#checks if previous_close is less
if(previous_close < current_close):
#goes to next day and increases the day count
counter++
i++
else:
#returns the number of days
return counter


An implementation using the same code provided would be creating a list, calling the function on each day in the period, and adding the number to the list. The resulting list would have the number of days "the security closed below the previous day's close before it closed above it" for each day in the period.

0

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.


Thanks so much for the response! I follow the logic of the formula you provided but I could not get it to work with my dataframe. Could you help me use it on my dataframe?

0

Update Backtest





0

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.


Loading...

This discussion is closed