get slope of a function that uses enableApproximation()

tatl

Member
i'm trying to get the slope of a derivative of RSI, which is a line that traces the high pivots:
Code:
def rsiHighPeak = if rsi[1] < rsi and rsi[-1] < rsi then 1 else 0;

plot rsiHighPeakPlot = if rsiHighPeak == 1 then rsi else Double.NaN;

rsiHighPeakPlot.enableApproximation();

i looked here to find code to get the slope of a line:
https://tlc.thinkorswim.com/center/reference/thinkScript/Functions/Math---Trig/ATan

so i wrote this:
Code:
input length = 1;
def height = rsiHighPeakPlot - rsiHighPeakPlot[length];
plot "Angle, deg" = ATan(height/lengthh) * 180 / Double.Pi;

the trouble is, it returns N/A , because there are non-number values in rsiHighPeakPlot

i guess enableApproximation() doesn't actually change the function in the code...it just changes how it plots on the chart...
i mean...it plots a bunch of specific nodes with specific values so they're totally there. i just dont know how to reference or access that function in the code.
any tips?
 
Solution
The only way I know of to do this is to use lots of variables and counters:

NOTE: you're using future bars x[-1] which will repaint. I'll give you code that works the way you wrote it, but you may want to reconsider using future bars in your calculations.

You define the peak well:
Code:
def rsiHighPeak = if rsi[1] < rsi and rsi[-1] < rsi then 1 else 0;
so that you have a nice array of 1 and 0. Let's use that:
Code:
def value_at_RSI_High_Peak = if rsiHighPeak == 1 then RSI else value_at_RSI_High_Peak[1];
Then we need (for calculating slope) some length (x axis value) in bars:
Code:
def bars_since_RSI_High_Peak = if rsiHighPeak == 1 then 1 else bars_since_RSI_High_Peak[1] + 1;
then you can find some sort of "angle"...
The only way I know of to do this is to use lots of variables and counters:

NOTE: you're using future bars x[-1] which will repaint. I'll give you code that works the way you wrote it, but you may want to reconsider using future bars in your calculations.

You define the peak well:
Code:
def rsiHighPeak = if rsi[1] < rsi and rsi[-1] < rsi then 1 else 0;
so that you have a nice array of 1 and 0. Let's use that:
Code:
def value_at_RSI_High_Peak = if rsiHighPeak == 1 then RSI else value_at_RSI_High_Peak[1];
Then we need (for calculating slope) some length (x axis value) in bars:
Code:
def bars_since_RSI_High_Peak = if rsiHighPeak == 1 then 1 else bars_since_RSI_High_Peak[1] + 1;
then you can find some sort of "angle" using trig:
Code:
def height = rsiHighPeakPlot - value_at_RSI_High_Peak;
plot "Angle, deg" = ATan( height / bars_since_RSI_High_Peak ) * 180 / Double.Pi;
Remember though, that when people think of angles, they usually think of planes with x and y coordinates (or your choice of labels) where x and y are in the same unit, e.g. cm or tenths of an inch or light years or whatever. The charts will give you unequal units of dollars and minutes (or hours or days or whatever). Your slope will necessarily change drastically depending on the scales of the two (price changes drastically from one instrument to another as well, look at /6E and BRK.A for a good example).

You can easily enough repeat the process outlined above for the low swing as well.

There have been many discussions about future bars and how to rework indicators not to use them. You end up with one bar extra delay in locating your peak, but it is a stable point rather than open to repainting. Just be aware.

Have fun and come back if this doesn't make sense.
-mashume
 
Solution

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

i'm trying to get the slope of a derivative of RSI, which is a line that traces the high pivots:
Code:
def rsiHighPeak = if rsi[1] < rsi and rsi[-1] < rsi then 1 else 0;

plot rsiHighPeakPlot = if rsiHighPeak == 1 then rsi else Double.NaN;

rsiHighPeakPlot.enableApproximation();

i looked here to find code to get the slope of a line:
https://tlc.thinkorswim.com/center/reference/thinkScript/Functions/Math---Trig/ATan

so i wrote this:
Code:
input length = 1;
def height = rsiHighPeakPlot - rsiHighPeakPlot[length];
plot "Angle, deg" = ATan(height/lengthh) * 180 / Double.Pi;

the trouble is, it returns N/A , because there are non-number values in rsiHighPeakPlot

i guess enableApproximation() doesn't actually change the function in the code...it just changes how it plots on the chart...
i mean...it plots a bunch of specific nodes with specific values so they're totally there. i just dont know how to reference or access that function in the code. hal_slope
any tips?



this will find the slope between rsi peaks, the change in RSI per bar.
on each rsi peak, a loop runs, to find the next peak, and count the bars to it.
then it uses that number as an offset to read the next peak rsi value.
it calculate the rsi difference, then divides it by the bar quantity, to get the slope, the change in rsi per bar.


i don't believe angles are valid on stock charts, because there are different units in the x axis and y axis, so i don't use them.


Code:
# rsi_peak_slopes_00

#https://usethinkscript.com/threads/get-slope-of-a-function-that-uses-enableapproximation.14972/
# get slope of a function that uses enableApproximation()
# tatl  9/8  #1
#i'm trying to get the slope of a derivative of RSI, which is a line that traces the high pivots:


# RSI
# TD Ameritrade IP Company, Inc. (c) 2007-2023

declare lower;

input length = 14;
input over_Bought = 70;
input over_Sold = 30;
input price = close;
input averageType = AverageType.WILDERS;
input showBreakoutSignals = no;

def NetChgAvg = MovingAverage(averageType, price - price[1], length);
def TotChgAvg = MovingAverage(averageType, AbsValue(price - price[1]), length);
def ChgRatio = if TotChgAvg != 0 then NetChgAvg / TotChgAvg else 0;

plot RSI = 50 * (ChgRatio + 1);
plot OverSold = over_Sold;
plot OverBought = over_Bought;
#plot UpSignal = if RSI crosses above OverSold then OverSold else Double.NaN;
#plot DownSignal = if RSI crosses below OverBought then OverBought else Double.NaN;

#UpSignal.SetHiding(!showBreakoutSignals);
#DownSignal.SetHiding(!showBreakoutSignals);

RSI.DefineColor("OverBought", GetColor(5));
RSI.DefineColor("Normal", GetColor(7));
RSI.DefineColor("OverSold", GetColor(1));
RSI.AssignValueColor(if RSI > over_Bought then RSI.color("OverBought") else if RSI < over_Sold then RSI.color("OverSold") else RSI.color("Normal"));
OverSold.SetDefaultColor(GetColor(8));
OverBought.SetDefaultColor(GetColor(8));
#UpSignal.SetDefaultColor(Color.UPTICK);
#UpSignal.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
#DownSignal.SetDefaultColor(Color.DOWNTICK);
#DownSignal.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);

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


# peaks
def na = double.nan;
def bn = barnumber();
# https://usethinkscript.com/threads/zigzag-high-low-with-supply-demand-zones-for-thinkorswim.172/#post-7048
# robert payne code
input peak_length = 2;
def lastBar = HighestAll(if IsNaN(close) then 0 else bn);
def offset = Min(peak_length - 1, lastBar - bn);
#def valley = low < Lowest(low[1], peak_length - 1) and low == GetValue(Lowest(low, peak_length), -offset);
#def peak = high > highest(high[1], peak_length - 1) and high == GetValue(highest(high, peak_length), -offset);
def valley = rsi < Lowest(rsi[1], peak_length - 1) and rsi == GetValue(Lowest(rsi, peak_length), -offset);
def peak = rsi > highest(rsi[1], peak_length - 1) and rsi == GetValue(highest(rsi, peak_length), -offset);


#def rsiPeak = if rsi[1] < rsi and rsi[-1] < rsi then 1 else 0;
#plot zrsiPeak = if rsiPeak == 1 then rsi else Double.NaN;

plot zrsiPeak = if Peak == 1 then rsi else Double.NaN;
zrsiPeak.enableApproximation();
zrsipeak.SetDefaultColor(Color.cyan);
#x.setlineweight(1);


def n = 200;

def peakbars;
def diff;
def peakslope;
if peak then {
 peakbars = fold i = 1 to n
  with p=1
  while !getvalue(peak, -i) and !isnan(close)
  do p + 1;
 diff = (getvalue(rsi, -peakbars) - rsi);
 peakslope = diff/peakbars;
} else {
 peakbars = peakbars[1];
 diff = diff[1];
 peakslope = peakslope[1];
}


plot z1 = if peak then rsi else na;
z1.SetPaintingStrategy(PaintingStrategy.POINTS);
z1.SetDefaultColor(Color.cyan);
z1.setlineweight(3);
z1.hidebubble();


addchartbubble(peak, rsi*1.4,
rsi + "  rsi\n" +
getvalue(rsi,-peakbars) + "  next peak\n" +
diff + "  diff\n" +
peakbars + "  bars\n" +
peakslope + "  slope/bar"
, color.yellow, yes);

#

HPqMTrr.jpg
 
Last edited:
@mashume Very intrigued by the simplicity of your TickDelta script. Suggest Utilizing Fisher Transform : essentially renders a combined slope with a non linear indicator. We all are essentially looking to
profit from Inflection points. ( it sets me to pondering: Concept: What is thinking? as opposed to coding. Sometimes I need to remind myself that thinking is a prerequisite of coding. Duh! We are all looking at up down, expand contract. Steve Jobs always reminded us when stuck in the mud to simplify simplify ) Best wishes and grateful for your coding expertise. MagicQuotes. :)
 

this will find the slope between rsi peaks, the change in RSI per bar.
on each rsi peak, a loop runs, to find the next peak, and count the bars to it.
then it uses that number as an offset to read the next peak rsi value.
it calculate the rsi difference, then divides it by the bar quantity, to get the slope, the change in rsi per bar.


i don't believe angles are valid on stock charts, because there are different units in the x axis and y axis, so i don't use them.


Code:
# rsi_peak_slopes_00

#https://usethinkscript.com/threads/get-slope-of-a-function-that-uses-enableapproximation.14972/
# get slope of a function that uses enableApproximation()
# tatl  9/8  #1
#i'm trying to get the slope of a derivative of RSI, which is a line that traces the high pivots:


# RSI
# TD Ameritrade IP Company, Inc. (c) 2007-2023

declare lower;

input length = 14;
input over_Bought = 70;
input over_Sold = 30;
input price = close;
input averageType = AverageType.WILDERS;
input showBreakoutSignals = no;

def NetChgAvg = MovingAverage(averageType, price - price[1], length);
def TotChgAvg = MovingAverage(averageType, AbsValue(price - price[1]), length);
def ChgRatio = if TotChgAvg != 0 then NetChgAvg / TotChgAvg else 0;

plot RSI = 50 * (ChgRatio + 1);
plot OverSold = over_Sold;
plot OverBought = over_Bought;
#plot UpSignal = if RSI crosses above OverSold then OverSold else Double.NaN;
#plot DownSignal = if RSI crosses below OverBought then OverBought else Double.NaN;

#UpSignal.SetHiding(!showBreakoutSignals);
#DownSignal.SetHiding(!showBreakoutSignals);

RSI.DefineColor("OverBought", GetColor(5));
RSI.DefineColor("Normal", GetColor(7));
RSI.DefineColor("OverSold", GetColor(1));
RSI.AssignValueColor(if RSI > over_Bought then RSI.color("OverBought") else if RSI < over_Sold then RSI.color("OverSold") else RSI.color("Normal"));
OverSold.SetDefaultColor(GetColor(8));
OverBought.SetDefaultColor(GetColor(8));
#UpSignal.SetDefaultColor(Color.UPTICK);
#UpSignal.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
#DownSignal.SetDefaultColor(Color.DOWNTICK);
#DownSignal.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);

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


# peaks
def na = double.nan;
def bn = barnumber();
# https://usethinkscript.com/threads/zigzag-high-low-with-supply-demand-zones-for-thinkorswim.172/#post-7048
# robert payne code
input peak_length = 2;
def lastBar = HighestAll(if IsNaN(close) then 0 else bn);
def offset = Min(peak_length - 1, lastBar - bn);
#def valley = low < Lowest(low[1], peak_length - 1) and low == GetValue(Lowest(low, peak_length), -offset);
#def peak = high > highest(high[1], peak_length - 1) and high == GetValue(highest(high, peak_length), -offset);
def valley = rsi < Lowest(rsi[1], peak_length - 1) and rsi == GetValue(Lowest(rsi, peak_length), -offset);
def peak = rsi > highest(rsi[1], peak_length - 1) and rsi == GetValue(highest(rsi, peak_length), -offset);


#def rsiPeak = if rsi[1] < rsi and rsi[-1] < rsi then 1 else 0;
#plot zrsiPeak = if rsiPeak == 1 then rsi else Double.NaN;

plot zrsiPeak = if Peak == 1 then rsi else Double.NaN;
zrsiPeak.enableApproximation();
zrsipeak.SetDefaultColor(Color.cyan);
#x.setlineweight(1);


def n = 200;

def peakbars;
def diff;
def peakslope;
if peak then {
 peakbars = fold i = 1 to n
  with p=1
  while !getvalue(peak, -i) and !isnan(close)
  do p + 1;
 diff = (getvalue(rsi, -peakbars) - rsi);
 peakslope = diff/peakbars;
} else {
 peakbars = peakbars[1];
 diff = diff[1];
 peakslope = peakslope[1];
}


plot z1 = if peak then rsi else na;
z1.SetPaintingStrategy(PaintingStrategy.POINTS);
z1.SetDefaultColor(Color.cyan);
z1.setlineweight(3);
z1.hidebubble();


addchartbubble(peak, rsi*1.4,
rsi + "  rsi\n" +
getvalue(rsi,-peakbars) + "  next peak\n" +
diff + "  diff\n" +
peakbars + "  bars\n" +
peakslope + "  slope/bar"
, color.yellow, yes);

#

View attachment 18310

Quick Question, Why not do this for Close instead of the derivative=RSI with a tangent angle? or a Recursive Median Filter with an alpha component>
 
Last edited by a moderator:

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
264 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