Changing the calculation of an indicator at a point

Human

Member
I would like to change the formula used for an indicator at a certain point. Let's say I am using a moving average. That is the first indicator. I want to change the formula 19 days in the past from the current day. This would roll forward as time passes so the standard moving average would always end 20 days ago and the new formula would extend to the present bar. Visually, the past 19 days would be slightly different from the final shape we would see twenty days ago and before that. I understand how to shift the moving average back 20 days. That is the easy part. What I don't know is how to start the second indicator 19 days ago and extend to the current bar. The second formula would be similar so visually it would look like one seamless indicator with no break between the two formulas. Is this possible? Are there any examples of something like this? I have searched for this, but I might not know what terms to use in my search.
 
Solution
I would like to change the formula used for an indicator at a certain point. Let's say I am using a moving average. That is the first indicator. I want to change the formula 19 days in the past from the current day. This would roll forward as time passes so the standard moving average would always end 20 days ago and the new formula would extend to the present bar. Visually, the past 19 days would be slightly different from the final shape we would see twenty days ago and before that. I understand how to shift the moving average back 20 days. That is the easy part. What I don't know is how to start the second indicator 19 days ago and extend to the current bar. The second formula would be similar so visually it would look like one...
I would like to change the formula used for an indicator at a certain point. Let's say I am using a moving average. That is the first indicator. I want to change the formula 19 days in the past from the current day. This would roll forward as time passes so the standard moving average would always end 20 days ago and the new formula would extend to the present bar. Visually, the past 19 days would be slightly different from the final shape we would see twenty days ago and before that. I understand how to shift the moving average back 20 days. That is the easy part. What I don't know is how to start the second indicator 19 days ago and extend to the current bar. The second formula would be similar so visually it would look like one seamless indicator with no break between the two formulas. Is this possible? Are there any examples of something like this? I have searched for this, but I might not know what terms to use in my search.



this will plot 1 line, that has data from one signal, and the last part is from a different signal.
the transisition bar is x bars before the last bar on the chart.
set the chart time to desired time, like days.

this uses 2 averages for test data.

can draw the original dat lines and a vertical line on transition bar.


Code:
# chg_formula_0

#https://usethinkscript.com/threads/changing-the-calculation-of-an-indicator-at-a-point.15653/
#Changing the calculation of an indicator at a point

def na = double.nan;
def bn = barnumber();

input barsback = 19;

#def lastbar = !isnan(close[0]) and isnan(close[-1]);


#---------------------------
# test data , 2 averages

def price = close;

input avg1_len = 9;
input avg1_type =  AverageType.EXPONENTIAL;
def avg1 = MovingAverage(avg1_type, price, avg1_len);

input avg2_len = 21;
input avg2_type =  AverageType.simple;
def avg2 = MovingAverage(avg2_type, price, avg2_len);


input show_lines = no;

plot z1 = if show_lines then avg1 else na;
z1.setdefaultcolor(getcolor(2));
#z1.setlineweight(1);
z1.hidebubble();

plot z2 = if show_lines then avg2 else na;
z2.setdefaultcolor(getcolor(4));
#z2.setlineweight(1);
z2.hidebubble();

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

def data1 = avg1;
def data2 = avg2;

# define a bar that is x bars from last bar
def t = if !isnan(getvalue(close, -(barsback+0))) and isnan(getvalue(close, -(barsback+1))) then 1 else 0;


def x;
def state;
if bn == 1 then {
 x = data1;
 state = 1;
} else if isnan(close) then {
 x = na;
 state = 0;
} else if t or state[1] == 2 then {
 x = data2;
 state = 2;
} else {
 x = data1;
 state = 1;
}

input show_combined_signal = yes;
plot z = if show_combined_signal then x else na;
z.setdefaultcolor(color.cyan);
#z.setlineweight(1);
z.hidebubble();

input show_transistion_bar = no;
addverticalline(show_transistion_bar and t, "-", color.white);

#
 
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
523 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