solving equation with multiple embeded ExpAverage

Newbie_789456

New member
Hi Everyone,

have this code for DSS below (N.B it is a lower study):

def divisor = ExpAverage(ExpAverage(Highest(high, length) - Lowest(low, length), s_length), r_length);

def DSS = if divisor == 0 then 0 else 100 * ExpAverage(ExpAverage(close - Lowest(low , length), s_length), r_length) / divisor;

for simplicity, we assume divisor !=0

I am trying to leverage that code to determine the value for "close" that will make the following equation equal to 80 for example?

100 * ExpAverage(ExpAverage(close - Lowest(low , length), s_length), r_length) / divisor = 80

It is an algebra problem as much as it is coding, but because of the multiple/embedded ExpAverage() I am really struggling.


given:

DSS= 100 * ExpAverage(ExpAverage(close - Lowest(low , length), s_length), r_length) / divisor;

where:

input s_length = 5;
input length = 10;
input bottom_line = 20;
input middle_line = 50;
input top_line = 80;
def r_alpha = 2 / (r_length + 1);
def s_alpha = 2 / (s_length + 1);

I am trying to solve the equation DSS = top_line

i.e. what value of variable "close" will make the above true.

Here is the ToS definition of ExpAverage for reference:

The formula for the calculation of the exponential moving average is recursively defined as follows:
EMA1 = price1;
EMA2 = α*price2 + (1 - α)*EMA1;
EMA3 = α*price3 + (1 - α)*EMA2;
EMAN = α*priceN + (1 - α)*EMAN-1;
where α is a smoothing coefficient equal to 2/(length + 1).


So I attempted to break down the ExpAverages formula in order to isolate "close"..

Here are the first steps for reference:

100 * ExpAverage(ExpAverage(close - Lowest(low , length), s_length), r_length) / divisor = top_line
ExpAverage(ExpAverage(close - Lowest(low , length), s_length), r_length) / divisor = (top_line/100)
ExpAverage(ExpAverage(close - Lowest(low , length), s_length), r_length) = (top_line/100)*divisor
(r_alpha)* (ExpAverage(close - Lowest(low , length), s_length))+ (1-r_alpha)* ExpAverage(ExpAverage(close - Lowest(low , length), s_length), r_length) [1] = (top_line/100)*divisor
.......

The closest I got was the code below, but it is still not correct.

price at which DSS is equal to Top_Line = ((((((Top_Line / 100) * divisor) - ((1 - r_alpha) * ExpAverage(ExpAverage(close - Lowest(low , length), s_length), r_length)[1])) / r_alpha) - ( 1 - s_alpha) * ExpAverage(close - Lowest(low , length), s_length)) / s_alpha) + Lowest(low , length);

At the end of the day, I am trying to determine historically on the chart what was the price value in each bar in the top graph, that would have made DSS = top_line in the lower graph.. so that if the high of a candle in the top graph was equal or above that price value, I know that DSS would have touched the top_line in the lower graph..

Any help would be much appreciated. Even if it is just spotting an error I don't see...

Many thanks in advance!
 
Solution
Hi Everyone,

have this code for DSS below (N.B it is a lower study):

def divisor = ExpAverage(ExpAverage(Highest(high, length) - Lowest(low, length), s_length), r_length);

def DSS = if divisor == 0 then 0 else 100 * ExpAverage(ExpAverage(close - Lowest(low , length), s_length), r_length) / divisor;

for simplicity, we assume divisor !=0

I am trying to leverage that code to determine the value for "close" that will make the following equation equal to 80 for example?

100 * ExpAverage(ExpAverage(close - Lowest(low , length), s_length), r_length) / divisor = 80

It is an algebra problem as much as it is coding, but because of the multiple/embedded ExpAverage() I am really struggling.


given:

DSS= 100 *...
Hi Everyone,

have this code for DSS below (N.B it is a lower study):

def divisor = ExpAverage(ExpAverage(Highest(high, length) - Lowest(low, length), s_length), r_length);

def DSS = if divisor == 0 then 0 else 100 * ExpAverage(ExpAverage(close - Lowest(low , length), s_length), r_length) / divisor;

for simplicity, we assume divisor !=0

I am trying to leverage that code to determine the value for "close" that will make the following equation equal to 80 for example?

100 * ExpAverage(ExpAverage(close - Lowest(low , length), s_length), r_length) / divisor = 80

It is an algebra problem as much as it is coding, but because of the multiple/embedded ExpAverage() I am really struggling.


given:

DSS= 100 * ExpAverage(ExpAverage(close - Lowest(low , length), s_length), r_length) / divisor;

where:

input s_length = 5;
input length = 10;
input bottom_line = 20;
input middle_line = 50;
input top_line = 80;
def r_alpha = 2 / (r_length + 1);
def s_alpha = 2 / (s_length + 1);

I am trying to solve the equation DSS = top_line

i.e. what value of variable "close" will make the above true.

Here is the ToS definition of ExpAverage for reference:

The formula for the calculation of the exponential moving average is recursively defined as follows:
EMA1 = price1;
EMA2 = α*price2 + (1 - α)*EMA1;
EMA3 = α*price3 + (1 - α)*EMA2;
EMAN = α*priceN + (1 - α)*EMAN-1;
where α is a smoothing coefficient equal to 2/(length + 1).


So I attempted to break down the ExpAverages formula in order to isolate "close"..

Here are the first steps for reference:

100 * ExpAverage(ExpAverage(close - Lowest(low , length), s_length), r_length) / divisor = top_line
ExpAverage(ExpAverage(close - Lowest(low , length), s_length), r_length) / divisor = (top_line/100)
ExpAverage(ExpAverage(close - Lowest(low , length), s_length), r_length) = (top_line/100)*divisor
(r_alpha)* (ExpAverage(close - Lowest(low , length), s_length))+ (1-r_alpha)* ExpAverage(ExpAverage(close - Lowest(low , length), s_length), r_length) [1] = (top_line/100)*divisor
.......

The closest I got was the code below, but it is still not correct.

price at which DSS is equal to Top_Line = ((((((Top_Line / 100) * divisor) - ((1 - r_alpha) * ExpAverage(ExpAverage(close - Lowest(low , length), s_length), r_length)[1])) / r_alpha) - ( 1 - s_alpha) * ExpAverage(close - Lowest(low , length), s_length)) / s_alpha) + Lowest(low , length);

At the end of the day, I am trying to determine historically on the chart what was the price value in each bar in the top graph, that would have made DSS = top_line in the lower graph.. so that if the high of a candle in the top graph was equal or above that price value, I know that DSS would have touched the top_line in the lower graph..

Any help would be much appreciated. Even if it is just spotting an error I don't see...

Many thanks in advance!
I finally solved it.. :D Please find the solution below if ever you need to leverage this approach to solve for an ExpAverage function...
Actually, the initial approach was correct, I had just gotten lost in all the parenthesis and misplaced some of them...

((((((Top_Line/100*divisor)-((1-r_alpha)ExpAverage(ExpAverage(close - Lowest(low , length), s_length), r_length)[1]))/r_alpha)-((1-s_alpha) ExpAverage(close - Lowest(low , length), s_length)[1])))/(s_alpha))+lowest(low,length);

So to conclude, breaking down the EMAN into α*priceN + (1 - α)*EMAN-1;works...

Cheers!
 
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
455 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