Moving Average Uptrend >= 20 days

bullman1083

New member
Hi guys, trying to find a simpler way to code that the 200 day Moving average has been in an uptrend for at least 20 days and also add that the stock has not declined more than 50% in the past 5 yrs. here's what i have so far for the 200DMA...


#200D sma 1month uptrend indicator

def smaup = simplemovingAvg(close,200) > simplemovingAvg(close,200)[1] and simplemovingAvg(close,200)[1] > simplemovingAvg(close,200)[2] and simplemovingAvg(close,200)[2] > simplemovingAvg(close,200)[3] and simplemovingAvg(close,200)[3] > simplemovingAvg(close,200)[4] and simplemovingAvg(close,200)[4] > simplemovingAvg(close,200)[5] and simplemovingAvg(close,200)[5] > simplemovingAvg(close,200)[6] and simplemovingAvg(close,200)[6] > simplemovingAvg(close,200)[7] and simplemovingAvg(close,200)[7] > simplemovingAvg(close,200)[8] and simplemovingAvg(close,200)[8] > simplemovingAvg(close,200)[9] and simplemovingAvg(close,200)[9] > simplemovingAvg(close,200)[10] and simplemovingAvg(close,200)[10] > simplemovingAvg(close,200)[11] and simplemovingAvg(close,200)[11] > simplemovingAvg(close,200)[12] and simplemovingAvg(close,200)[12] > simplemovingAvg(close,200)[13] and simplemovingAvg(close,200)[13] > simplemovingAvg(close,200)[14] and simplemovingAvg(close,200)[14] > simplemovingAvg(close,200)[15] and simplemovingAvg(close,200)[15] > simplemovingAvg(close,200)[16] and simplemovingAvg(close,200)[16] > simplemovingAvg(close,200)[17] and simplemovingAvg(close,200)[17] > simplemovingAvg(close,200)[18] and simplemovingAvg(close,200)[18] > simplemovingAvg(close,200)[19] and simplemovingAvg(close,200)[19] > simplemovingAvg(close,200)[20] ;
 
Last edited:
Solution
goodness me...
let's be a bit more efficient...
Code:
input uptrend_length = 20;
def ma = simplemovingAvg(close,200);
def up_down_day = if ma > ma[1] then 1 else 0;
def in_uptrend = if sum(up_down_day, uptrend_length) == uptrend_length then 1 else 0;

# 252 days per trading year
input years_back = 5;
def days_back = years_back * 252;
def low_point = lowest(close, days_back);
def fixed_point = close[days_back];
def drawdown_ok = if low_point >= fixed_point * 0.5 then 1 else 0;

plot hit = if in_uptrend == 1 AND drawdown_ok == 1 then 1 else double.nan;

I wasn't sure how you want to calculate the max drawdown. This is calculated against the price at close of day, 5 years ago. It does not look through the days in order at all; it does not look...
goodness me...
let's be a bit more efficient...
Code:
input uptrend_length = 20;
def ma = simplemovingAvg(close,200);
def up_down_day = if ma > ma[1] then 1 else 0;
def in_uptrend = if sum(up_down_day, uptrend_length) == uptrend_length then 1 else 0;

# 252 days per trading year
input years_back = 5;
def days_back = years_back * 252;
def low_point = lowest(close, days_back);
def fixed_point = close[days_back];
def drawdown_ok = if low_point >= fixed_point * 0.5 then 1 else 0;

plot hit = if in_uptrend == 1 AND drawdown_ok == 1 then 1 else double.nan;

I wasn't sure how you want to calculate the max drawdown. This is calculated against the price at close of day, 5 years ago. It does not look through the days in order at all; it does not look for a high before a low and calculate half of that high as a minimum point. It is a starting point for you more than anything else.

-mashume
 
Last edited:
Solution
Is there a way to code it so that it looks from 5yrs back to present and to look for a high then a low as the stock declines?
The scan you are thinking of is simple to conceptualize in a sequential world -- we look at a chart and find a high and then look to the right of that high for a low. This is the way that traders have come to look at charts, and it is easy to understand. Thinkscript is not really a sequential language in that way. It is not so easy to program it to look for a high, and then a low. It can find highs, and lows, but which came first is more difficult. There are, of course, ways to do it but they can be cumbersome, or require artificially defined 'zones' (find a stock where the highest high happened between 5 and 2 1/2 years ago and the lowest low happened between 2 1/2 years ago and now).

Thinkscript is a capable language and makes some great tools, but learning to think thinkscript is not straight forward, especially as most everything else we do in trading is sequential.

Not so helpful, perhaps. Hopefully informative.

-mashume
 

Join useThinkScript to post your question to a community of 21,000+ developers and traders.

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
287 Online
Create Post

Similar threads

Similar threads

The Market Trading Game Changer

Join 2,500+ subscribers inside the useThinkScript VIP Membership Club
  • Exclusive indicators
  • Proven strategies & setups
  • Private Discord community
  • ‘Buy The Dip’ signal alerts
  • Exclusive members-only content
  • Add-ons and resources
  • 1 full year of unlimited support

Frequently Asked Questions

What is useThinkScript?

useThinkScript is the #1 community of stock market investors using indicators and other tools to power their trading strategies. Traders of all skill levels use our forums to learn about scripting and indicators, help each other, and discover new ways to gain an edge in the markets.

How do I get started?

We get it. Our forum can be intimidating, if not overwhelming. With thousands of topics, tens of thousands of posts, our community has created an incredibly deep knowledge base for stock traders. No one can ever exhaust every resource provided on our site.

If you are new, or just looking for guidance, here are some helpful links to get you started.

What are the benefits of VIP Membership?
VIP members get exclusive access to these proven and tested premium indicators: Buy the Dip, Advanced Market Moves 2.0, Take Profit, and Volatility Trading Range. In addition, VIP members get access to over 50 VIP-only custom indicators, add-ons, and strategies, private VIP-only forums, private Discord channel to discuss trades and strategies in real-time, customer support, trade alerts, and much more. Learn all about VIP membership here.
How can I access the premium indicators?
To access the premium indicators, which are plug and play ready, sign up for VIP membership here.
Back
Top