Indicate Change of Polarity on Chart


Category:
0
0

I’ve calculated a variable called “slope”, which is a number that can be positive or negative. For each new day I’d like the script to test if the polarity flips (+ to – or – to +), and if so, to indicate that on the chart. I’m not sure how best to do this. Any help would be appreciated. Thx.

Marked as spam
Posted by (Questions: 4, Answers: 6)
Asked on September 20, 2019 8:25 am
157 views
1
Private answer

Since your request is relating to a chart study, I have moved this post out of the FAQ's and into the Chart Studies topic.

Without your code we really have nothing to work with.

From the built-in studies included with Thinkorswim we have an example of how to do this. Here is the source code for a study named HullMovingAvg:

https://tlc.thinkorswim.com/center/reference/Tech-Indicators/studies-library/G-L/HullMovingAvg.html

input price = close;
input length = 20;
input displace = 0;
plot HMA = MovingAverage(AverageType.HULL, price, length)[-displace];
HMA.DefineColor("Up", GetColor(1));
HMA.DefineColor("Down", GetColor(0));
HMA.AssignValueColor(if HMA > HMA[1] then HMA.color("Up") else HMA.color("Down"));

Marked as spam
Posted by (Questions: 37, Answers: 4087)
Answered on September 20, 2019 10:18 am
0
Thank you. I am calculating the slope of a parabola using quadratic regression methodology. The slope is calculated as the derivative of the 2nd order polynomial: plot parabola = a * Power(x, 2) + b * x + c; def slope = (2 * a * x) + b; The slope could be + or -. I am trying to figure out how to write a script for an indicator on the chart that alerts when the slope goes from + to - or vice versa, - to +. Thank you again.
( at September 20, 2019 2:41 pm)
0

Ok. In your original request you did not ask for an alert. You stated that you wanted the chart to indicate when the direction has changed. In the example code of the HullMovingAvg you can replace the plot variable ”HMA” with your variable ”parabola”. Then you have to replace every other occurrence of ”HMA” with ”slope”.

( at September 20, 2019 2:49 pm)
0
I tried the suggestion but was not successful. Nothing was painted on the chart at least. I’ll try to restate the problem more clearly. The script plots a parabola through a dataset of closing prices of length L, using least squares regression. The slope of the parabola at close is calculated as the derivative of the 2nd order polynomial. On any given close the slope will be >0 or =0 or <0. Requirement: if the slope changes from + to - or - to + at close, then an indicator is painted on the chart. An alert would also be generated, if possible. If this is less than clear please let me know. Thanks again for your help.
( at September 22, 2019 5:45 pm)
0
You have provided none of the details required to provide a complete solution to your request. "...least squares regression... blah, blah, blah..." Sorry to be so blunt, but the only thing this conveys is that you are using some fancy math. Without your actual code, the best I can offer is the example of the Hull moving average I gave in my original answer. If you would like to provide your full code then we can provide a more exact solution. If you don't want to make your code public, you can submit a custom project request and pay for the solution.
( at September 22, 2019 9:02 pm)
0
I apologize for the lack of information. I'm using a premium script that I've modified, and I don't have permission from the author to share it. But your HMA suggestion got me thinking about the "Crosses" function. thinkScript needs something to compare the slope of the parabola to in the Crosses function, so I defined zero = 0. I used the code from thinkScript's RSICrossover script. Its seems to work. plot parabola = a * Power(x, 2) + b * x + c; def slope = (2 * a * x) + b; def zero = 0; plot Crossing = Crosses(slope, zero, crossingType == CrossingType.above); Crossing.SetPaintingStrategy(if crossingType == CrossingType.above then PaintingStrategy.BOOLEAN_ARROW_UP else PaintingStrategy.BOOLEAN_ARROW_DOWN); AddLabel(yes, slope, Color.BLUE); Again my apologies and thanks for your help, its what I needed and is much appreciated.
( at September 24, 2019 4:58 pm)