Analyze Problem With Code

Chedderbob9

New member
Hello,

Sorry for having to post this, but my think script is plotting a ranging condition when clearly based on the chart and the code, it should be a trending condition. Anything I'm missing, I've looked over this for days now and tried many different ranging conditions to change it with no luck. Any help would be greatly appreciated!


qqbmHjh.png


Code:
declare lower;

#Input Moving Average Lengths
input First_MA = 9;
input Second_MA = 21;

#Define Moving Averages and Price
def MA1 = MovAvgExponential(close, First_MA);
def MA2 = SimpleMovingAvg(close, Second_MA);

#Up Trend, Down Trend, & Ranging Conditions
def Up_Trend = MA1 > MA2 and close > MA2;
def Down_Trend1 = MA1 < MA2 and close < MA2;

def Ranging1 = MA1 > MA2 and close < MA2;
def Ranging2 = MA1 < MA2 and close > MA2;
def Ranging3 = MA1 > MA2 and close[1] < MA2;
def Ranging4 = MA1 > MA2 and close[2] < MA2;
def Ranging5 = MA1 > MA2 and close[3] < MA2;
def Ranging6 = MA1 < MA2 and close[1] > MA2;
def Ranging7 = MA1 < MA2 and close[2] > MA2;
def Ranging8 = MA1 < MA2 and close[3] > MA2;

def Ranging9 = MA1 > MA2 and close[1] < MA2;
def Ranging10 = MA1 > MA2 and close[2] < MA2;
def Ranging11 = MA1 > MA2 and close[3] < MA2;
def Ranging12 = MA1 < MA2 and close[1] > MA2;
def Ranging13 = MA1 < MA2 and close[2] > MA2;
def Ranging14 = MA1 < MA2 and close[3] > MA2;

def Ranging = Ranging1 or Ranging2 or Ranging3 or Ranging4 or Ranging5 or Ranging6 or Ranging7 or Ranging8 or Ranging9 or Ranging10 or Ranging11 or Ranging12 or Ranging13 or Ranging14;

#Plot Lower Study Line
plot MyLine = if Ranging then 0 else if Up_Trend then 1 else if Down_Trend1 then -1 else double.NaN;

#Define Upper and Lower Bounds
def UpperBound = 0.5;
def LowerBound = -0.5;

AddCloud(-0.5, -1.5, Color.PINK);
AddCloud( 1.5, 0.5, Color.LIGHT_GREEN);
 
Solution
So the trending conditions are when the 9EMA is less than the 21MA and any one the last 3 candles haven't crossed over the 21MA or when the 9EMA hasn't crossed the 21MA within the last 3 bars (These would be values of 1 or -1). This should be shown in the lower study as the line being in the red (-1) for a downtrend or the green (1) for an uptrend. The ranging conditions are defined in my code to show that the value should be 0 when there has been a change in the last 3 closing bars.

So my question is the why is this value showing 0 when clearly the ranging conditions, which I have listed in the code, are not met. Clearly the appropriate condition would be a downtrend (-1) where the 9EMA is less than the 21MA and the close is less...
Hello,

Sorry for having to post this, but my think script is plotting a ranging condition when clearly based on the chart and the code, it should be a trending condition. Anything I'm missing, I've looked over this for days now and tried many different ranging conditions to change it with no luck. Any help would be greatly appreciated!


qqbmHjh.png


Code:
declare lower;

#Input Moving Average Lengths
input First_MA = 9;
input Second_MA = 21;

#Define Moving Averages and Price
def MA1 = MovAvgExponential(close, First_MA);
def MA2 = SimpleMovingAvg(close, Second_MA);

#Up Trend, Down Trend, & Ranging Conditions
def Up_Trend = MA1 > MA2 and close > MA2;
def Down_Trend1 = MA1 < MA2 and close < MA2;

def Ranging1 = MA1 > MA2 and close < MA2;
def Ranging2 = MA1 < MA2 and close > MA2;
def Ranging3 = MA1 > MA2 and close[1] < MA2;
def Ranging4 = MA1 > MA2 and close[2] < MA2;
def Ranging5 = MA1 > MA2 and close[3] < MA2;
def Ranging6 = MA1 < MA2 and close[1] > MA2;
def Ranging7 = MA1 < MA2 and close[2] > MA2;
def Ranging8 = MA1 < MA2 and close[3] > MA2;

def Ranging9 = MA1 > MA2 and close[1] < MA2;
def Ranging10 = MA1 > MA2 and close[2] < MA2;
def Ranging11 = MA1 > MA2 and close[3] < MA2;
def Ranging12 = MA1 < MA2 and close[1] > MA2;
def Ranging13 = MA1 < MA2 and close[2] > MA2;
def Ranging14 = MA1 < MA2 and close[3] > MA2;

def Ranging = Ranging1 or Ranging2 or Ranging3 or Ranging4 or Ranging5 or Ranging6 or Ranging7 or Ranging8 or Ranging9 or Ranging10 or Ranging11 or Ranging12 or Ranging13 or Ranging14;

#Plot Lower Study Line
plot MyLine = if Ranging then 0 else if Up_Trend then 1 else if Down_Trend1 then -1 else double.NaN;

#Define Upper and Lower Bounds
def UpperBound = 0.5;
def LowerBound = -0.5;

AddCloud(-0.5, -1.5, Color.PINK);
AddCloud( 1.5, 0.5, Color.LIGHT_GREEN);

i don't understand what the question is.
ranging or trending don't mean anything to me. what do you think should be happening with close ?

you have the same math for range and trend formulas, so i would call them all trending.


i added this to the end of your code, to display values.
maybe it will help tell you what is going on

Code:
#-----------------------------
addchartbubble(1, 3,
MyLine
, color.yellow, no);

addchartbubble(1, 3,
 Ranging1 + "\n" +
 Ranging2 + "\n" +
 Ranging3 + "\n" +
 Ranging4 + "\n" +
 Ranging5 + "\n" +
 Ranging6 + "\n" +
 Ranging7 + "\n" +
 Ranging8  + "\n" +

 Ranging9  + "\n" +
 Ranging10  + "\n" +
 Ranging11  + "\n" +
 Ranging12 + "\n" +
 Ranging13 + "\n" +
 Ranging14
, (if  Ranging then color.yellow else color.gray), no);
#, color.yellow, no);
#
 
i don't understand what the question is.
ranging or trending don't mean anything to me. what do you think should be happening with close ?

you have the same math for range and trend formulas, so i would call them all trending.


i added this to the end of your code, to display values.
maybe it will help tell you what is going on

Code:
#-----------------------------
addchartbubble(1, 3,
MyLine
, color.yellow, no);

addchartbubble(1, 3,
 Ranging1 + "\n" +
 Ranging2 + "\n" +
 Ranging3 + "\n" +
 Ranging4 + "\n" +
 Ranging5 + "\n" +
 Ranging6 + "\n" +
 Ranging7 + "\n" +
 Ranging8  + "\n" +

 Ranging9  + "\n" +
 Ranging10  + "\n" +
 Ranging11  + "\n" +
 Ranging12 + "\n" +
 Ranging13 + "\n" +
 Ranging14
, (if  Ranging then color.yellow else color.gray), no);
#, color.yellow, no);
#

So the trending conditions are when the 9EMA is less than the 21MA and any one the last 3 candles haven't crossed over the 21MA or when the 9EMA hasn't crossed the 21MA within the last 3 bars (These would be values of 1 or -1). This should be shown in the lower study as the line being in the red (-1) for a downtrend or the green (1) for an uptrend. The ranging conditions are defined in my code to show that the value should be 0 when there has been a change in the last 3 closing bars.

So my question is the why is this value showing 0 when clearly the ranging conditions, which I have listed in the code, are not met. Clearly the appropriate condition would be a downtrend (-1) where the 9EMA is less than the 21MA and the close is less than the 21MA for more than 3 bars.

Qo1G3Yn.png



Code:
declare lower;

#Input Moving Average Lengths
input First_MA = 9;
input Second_MA = 21;

#Define Moving Averages and Price
def MA1 = MovAvgExponential(close, First_MA);
def MA2 = SimpleMovingAvg(close, Second_MA);

#def Close_Price = if isNaN(close) then open else close[1];

#Up Trend, Down Trend, & Ranging Conditions
def Up_Trend = MA1 > MA2 and close > MA2;
def Down_Trend = MA1 < MA2 and close < MA2;
def Ranging1 = MA1 > MA2 and close[1] < MA2;
def Ranging2 = MA1 > MA2 and close[2] < MA2;
def Ranging3 = MA1 > MA2 and close[3] < MA2;
def Ranging4 = MA1 < MA2 and close[1] > MA2;
def Ranging5 = MA1 < MA2 and close[2] > MA2;
def Ranging6 = MA1 < MA2 and close[3] > MA2;

def Ranging = Ranging1 or Ranging2 or Ranging3 or Ranging4 or Ranging5 or Ranging6;

#Plot Lower Study Line
plot MyLine =  if Ranging then 0 else if Up_Trend then 1 else if Down_Trend then -1 else 0;
#plot MyLine = if Up_Trend then 1 else if Down_Trend then -1 else if Ranging then 0 else double.NaN;

#Define Upper and Lower Bounds
def UpperBound = 0.5;
def LowerBound = -0.5;



AddCloud(-0.5, -1.5, Color.PINK);

AddCloud( 1.5, 0.5, Color.LIGHT_GREEN);
 
So the trending conditions are when the 9EMA is less than the 21MA and any one the last 3 candles haven't crossed over the 21MA or when the 9EMA hasn't crossed the 21MA within the last 3 bars (These would be values of 1 or -1). This should be shown in the lower study as the line being in the red (-1) for a downtrend or the green (1) for an uptrend. The ranging conditions are defined in my code to show that the value should be 0 when there has been a change in the last 3 closing bars.

So my question is the why is this value showing 0 when clearly the ranging conditions, which I have listed in the code, are not met. Clearly the appropriate condition would be a downtrend (-1) where the 9EMA is less than the 21MA and the close is less than the 21MA for more than 3 bars.

View attachment 18557


Code:
declare lower;

#Input Moving Average Lengths
input First_MA = 9;
input Second_MA = 21;

#Define Moving Averages and Price
def MA1 = MovAvgExponential(close, First_MA);
def MA2 = SimpleMovingAvg(close, Second_MA);

#def Close_Price = if isNaN(close) then open else close[1];

#Up Trend, Down Trend, & Ranging Conditions
def Up_Trend = MA1 > MA2 and close > MA2;
def Down_Trend = MA1 < MA2 and close < MA2;
def Ranging1 = MA1 > MA2 and close[1] < MA2;
def Ranging2 = MA1 > MA2 and close[2] < MA2;
def Ranging3 = MA1 > MA2 and close[3] < MA2;
def Ranging4 = MA1 < MA2 and close[1] > MA2;
def Ranging5 = MA1 < MA2 and close[2] > MA2;
def Ranging6 = MA1 < MA2 and close[3] > MA2;

def Ranging = Ranging1 or Ranging2 or Ranging3 or Ranging4 or Ranging5 or Ranging6;

#Plot Lower Study Line
plot MyLine =  if Ranging then 0 else if Up_Trend then 1 else if Down_Trend then -1 else 0;
#plot MyLine = if Up_Trend then 1 else if Down_Trend then -1 else if Ranging then 0 else double.NaN;

#Define Upper and Lower Bounds
def UpperBound = 0.5;
def LowerBound = -0.5;



AddCloud(-0.5, -1.5, Color.PINK);

AddCloud( 1.5, 0.5, Color.LIGHT_GREEN);


short answer,

you say ' a candle hasn't crossed..'
but are using > and < , with 2 different sets of variables, so the formulas are not checking for a crossing.

----------------

your post1 didn't explain the rules, just a code, so it was confusing.

your post3 words actually explain what you are trying to do.
although it is confusing when reading rules as , 'having a value of 1 or -1'
the rules for each output should be written out separately, for 1 and -1.

----------------

computers only do what they are told.
they don't just make up different outputs.
this statement tells me you are stuck on your beliefs and have not looked at and evaluated the values in my test study, and reevaluated your code formulas.

'...when clearly the ranging conditions, which I have listed in the code, are not met.'

----------------

lets try to rewrite your rules

this is still confusing. what exactly has to happen for a 1 and for a -1 ?

' So the trending conditions are when the 9EMA is less than the 21MA and any one the last 3 candles haven't crossed over the 21MA or when the 9EMA hasn't crossed the 21MA within the last 3 bars (These would be values of 1 or -1). This should be shown in the lower study as the line being in the red (-1) for a downtrend or the green (1) for an uptrend.
The ranging conditions are defined in my code to show that the value should be 0 when there has been a change in the last 3 closing bars. '


you say ' a candle hasn't crossed..'
but are using > and < , with 2 different sets of variables, so the formulas are not checking for a crossing.


my guesses at what you mean

trending up = 1
. ema9 > ma21
. and (no price crossing of ma21 within 3 bars or ema9 has not crossed ma21 within 3 bars)

trending down = -1
. ema9 < ma21
. and (no price crossing of ma21 within 3 bars or ema9 has not crossed ma21 within 3 bars)


trending = 0 , = ranging ?
i don't know why you are adding a new word, ranging, to a condition of trending. it just complicates things.
(price crossed ma21 within 3 bars or ema9 crossed ma21 within 3 bars)


i created formulas for these rules and added them to the end of post3 code.
take a look and see if i have the formulas correct.


#--------------------------------


this is an upper test code , to help you verify the rules.


Code:
#ranging_trending_rules_p3_upper

#https://usethinkscript.com/threads/analyze-problem-with-code.14701/
#post3
#So the trending conditions are when the 9EMA is less than the 21MA and any one the last 3 candles haven't crossed over the 21MA or when the 9EMA hasn't crossed the 21MA within the last 3 bars (These would be values of 1 or -1). This should be shown in the lower study as the line being in the red (-1) for a downtrend or the green (1) for an uptrend. The ranging conditions are defined in my code to show that the value should be 0 when there has been a change in the last 3 closing bars.

#declare lower;

#Input Moving Average Lengths
input First_MA = 9;
input Second_MA = 21;

#Define Moving Averages and Price
def MA1 = MovAvgExponential(close, First_MA);
def MA2 = SimpleMovingAvg(close, Second_MA);

plot z1 = ma1;
plot z2 = ma2;

#def Close_Price = if isNaN(close) then open else close[1];

#Up Trend, Down Trend, & Ranging Conditions
def Up_Trend = MA1 > MA2 and close > MA2;
def Down_Trend = MA1 < MA2 and close < MA2;
def Ranging1 = MA1 > MA2 and close[1] < MA2;
def Ranging2 = MA1 > MA2 and close[2] < MA2;
def Ranging3 = MA1 > MA2 and close[3] < MA2;
def Ranging4 = MA1 < MA2 and close[1] > MA2;
def Ranging5 = MA1 < MA2 and close[2] > MA2;
def Ranging6 = MA1 < MA2 and close[3] > MA2;

def Ranging = Ranging1 or Ranging2 or Ranging3 or Ranging4 or Ranging5 or Ranging6;

#Plot Lower Study Line
def MyLine =  if Ranging then 0 else if Up_Trend then 1 else if Down_Trend then -1 else 0;
#plot MyLine = if Up_Trend then 1 else if Down_Trend then -1 else if Ranging then 0 else double.NaN;

#Define Upper and Lower Bounds
def UpperBound = 0.5;
def LowerBound = -0.5;

#AddCloud(-0.5, -1.5, Color.PINK);
#AddCloud( 1.5, 0.5, Color.LIGHT_GREEN);


#-----------------------------
input test1 = no;
AddChartBubble(test1, low,
Ranging 
#MyLine
, Color.YELLOW, no);

input test2 = no;
AddChartBubble(test2, low,
 Ranging1 + "\n" +
 Ranging2 + "\n" +
 Ranging3 + "\n" +
 Ranging4 + "\n" +
 Ranging5 + "\n" +
 Ranging6 + "\n" 
#+
# Ranging7 + "\n" +
# Ranging8  + "\n" +

# Ranging9  + "\n" +
# Ranging10  + "\n" +
# Ranging11  + "\n" +
# Ranging12 + "\n" +
# Ranging13 + "\n" +
# Ranging14
, (if  Ranging then Color.YELLOW else Color.GRAY), no);
#, color.yellow, no);
#

#--------------------------------

input crossing_bars = 3;

#def crossing = (price crossed ma21 within 3 bars  or  ema9 crossed ma21 within 3 bars)
def crossing1 = close crosses ma1;
def crossing2 = ma1 crosses ma2;
def cross1 = if sum(crossing1, crossing_bars) > 0 then 1 else 0;
def cross2 = if sum(crossing2, crossing_bars) > 0 then 1 else 0;

def trend = 
 if (ma1 > ma2 and cross1 == 0 and cross2 == 0) then 1
 else if (ma1 < ma2 and cross1 == 0 and cross2 == 0) then -1
 else if (cross1 or cross2) then 0
 else trend[1];


addchartbubble(1, low,
trend + "  T\n" +
crossing1  + " x1\n" +
cross1 + " x1cnt\n" +
crossing2 + " x2\n" +
cross2 + " x2cnt"
, (if trend > 0 then color.green else if trend < 0 then color.red else color.gray), no);
#
 
Solution

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
421 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