Custom Index code problem

MLlalala

Member
I am trying to make a custom index that follow QQQ, here is my code:

declare lower;
input cutoff=4;
input sym1 = "aapl";
input sym2 = "msft";
input sym3 = "amzn";
input sym4 = "tsla";
input sym5 = "goog";
def v1=close(sym1)+open(sym1)+low(sym1)+high(sym1);
def v2=close(sym2)+open(sym2)+low(sym2)+high(sym2);
def v3=close(sym3)+open(sym3)+low(sym3)+high(sym3);
def v4=close(sym4)+open(sym4)+low(sym4)+high(sym4);
def v5=close(sym5)+open(sym5)+low(sym5)+high(sym5);

def curve=power((v1/4*v2/4*v3/4*v4/4*v5/4),1/5);
plot c= reference EhlersSuperSmootherFilter(curve, cutoff);

The code is a little messy, but basically i use OHLC/4 as price, and then take geometric average, raise to power 1/n, in this case, n=5 stocks. However, when i plot it on 1 min chart, there is some value missing as shown in below chart, i dunn really know why this happen. Appreciate if anyone can shed some light.

CAHp5H6.png
 
Solution
You are right, I found the problem: it is goog missing data. In this case, does the syntax should be written something like, if price(goog) =0 then price(goog)[1]? but what if the quote is missing for several bars, may i know how should i keep looking back until i find a value?

that makes sense. more expensive stocks , like goog, won't be as actively traded, and will have periods of no trades, no data.

if you reference the previous value within the original formula, then there won't be any gaps of missing data. every past bar of v1 will have a value.
def v1 = if isnan(ohlc4(sym1)) then v1[1] else ohlc4(sym1);

i used ohlc4() to simplify the formulas. added a variable to control 'fixing' the data, so along with the test plots...
I am trying to make a custom index that follow QQQ, here is my code:

declare lower;
input cutoff=4;
input sym1 = "aapl";
input sym2 = "msft";
input sym3 = "amzn";
input sym4 = "tsla";
input sym5 = "goog";
def v1=close(sym1)+open(sym1)+low(sym1)+high(sym1);
def v2=close(sym2)+open(sym2)+low(sym2)+high(sym2);
def v3=close(sym3)+open(sym3)+low(sym3)+high(sym3);
def v4=close(sym4)+open(sym4)+low(sym4)+high(sym4);
def v5=close(sym5)+open(sym5)+low(sym5)+high(sym5);

def curve=power((v1/4*v2/4*v3/4*v4/4*v5/4),1/5);
plot c= reference EhlersSuperSmootherFilter(curve, cutoff);

The code is a little messy, but basically i use OHLC/4 as price, and then take geometric average, raise to power 1/n, in this case, n=5 stocks. However, when i plot it on 1 min chart, there is some value missing as shown in below chart, i dunn really know why this happen. Appreciate if anyone can shed some light.

CAHp5H6.png

you could simply things by using the built in function, ohlc/4
https://tlc.thinkorswim.com/center/reference/thinkScript/Functions/Fundamentals/ohlc4

maybe 1 of the values is 0? maybe 1 symbol wasn't traded when the others were?
plot lines for all the ohlc/4 symbols to check your input data. then determine how to deal with it. if a 0, use a previous value?
 

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

you could simply things by using the built in function, ohlc/4
https://tlc.thinkorswim.com/center/reference/thinkScript/Functions/Fundamentals/ohlc4

maybe 1 of the values is 0? maybe 1 symbol wasn't traded when the others were?
plot lines for all the ohlc/4 symbols to check your input data. then determine how to deal with it. if a 0, use a previous value?
You are right, I found the problem: it is goog missing data. In this case, does the syntax should be written something like, if price(goog) =0 then price(goog)[1]? but what if the quote is missing for several bars, may i know how should i keep looking back until i find a value?
 
You are right, I found the problem: it is goog missing data. In this case, does the syntax should be written something like, if price(goog) =0 then price(goog)[1]? but what if the quote is missing for several bars, may i know how should i keep looking back until i find a value?

that makes sense. more expensive stocks , like goog, won't be as actively traded, and will have periods of no trades, no data.

if you reference the previous value within the original formula, then there won't be any gaps of missing data. every past bar of v1 will have a value.
def v1 = if isnan(ohlc4(sym1)) then v1[1] else ohlc4(sym1);

i used ohlc4() to simplify the formulas. added a variable to control 'fixing' the data, so along with the test plots, you can see a before and after.
added this to the plot,
if isnan(close) then double.nan else
so it doesn't plot a horizontal line after the last bar.

Ruby:
# symbols_ohlc4

declare lower;
input cutoff = 4;
input sym1 = "aapl";
input sym2 = "msft";
input sym3 = "amzn";
input sym4 = "tsla";
input sym5 = "goog";

#def v1=close(sym1)+open(sym1)+low(sym1)+high(sym1);
#def v2=close(sym2)+open(sym2)+low(sym2)+high(sym2);
#def v3=close(sym3)+open(sym3)+low(sym3)+high(sym3);
#def v4=close(sym4)+open(sym4)+low(sym4)+high(sym4);
#def v5=close(sym5)+open(sym5)+low(sym5)+high(sym5);

input fix_data = yes;
def v1 = if fix_data and isnan(ohlc4(sym1)) then v1[1] else ohlc4(sym1);
def v2 = if fix_data and isnan(ohlc4(sym2)) then v2[1] else ohlc4(sym2);
def v3 = if fix_data and isnan(ohlc4(sym3)) then v3[1] else ohlc4(sym3);
def v4 = if fix_data and isnan(ohlc4(sym4)) then v4[1] else ohlc4(sym4);
def v5 = if fix_data and isnan(ohlc4(sym5)) then v5[1] else ohlc4(sym5);

# def curve=power((v1/4*v2/4*v3/4*v4/4*v5/4),1/5);
input power_factor = 5;
def curve = power((v1*v2*v3*v4*v5),1/power_factor);
plot c = if isnan(close) then double.nan else reference EhlersSuperSmootherFilter(curve, cutoff);

#-------------------------------------------
input test_show_lines = no;
plot z1 = v1;
plot z2 = v2;
plot z3 = v3;
plot z4 = v4;
plot z5 = v5;
z1.SetHiding(!test_show_lines);
z2.SetHiding(!test_show_lines);
z3.SetHiding(!test_show_lines);
z4.SetHiding(!test_show_lines);
z5.SetHiding(!test_show_lines);

#
 
Last edited:
Solution
that makes sense. more expensive stocks , like goog, won't be as actively traded, and will have periods of no trades, no data.

if you reference the previous value within the original formula, then there won't be any gaps of missing data. every past bar of v1 will have a value.
def v1 = if isnan(ohlc4(sym1)) then v1[1] else ohlc4(sym1);

i used ohlc4() to simplify the formulas. added a variable to control 'fixing' the data, so along with the test plots, you can see a before and after.
added this to the plot,
if isnan(close) then double.nan else
so it doesn't plot a horizontal line after the last bar.

Ruby:
# symbols_ohlc4

declare lower;
input cutoff = 4;
input sym1 = "aapl";
input sym2 = "msft";
input sym3 = "amzn";
input sym4 = "tsla";
input sym5 = "goog";

#def v1=close(sym1)+open(sym1)+low(sym1)+high(sym1);
#def v2=close(sym2)+open(sym2)+low(sym2)+high(sym2);
#def v3=close(sym3)+open(sym3)+low(sym3)+high(sym3);
#def v4=close(sym4)+open(sym4)+low(sym4)+high(sym4);
#def v5=close(sym5)+open(sym5)+low(sym5)+high(sym5);

input fix_data = yes;
def v1 = if fix_data and isnan(ohlc4(sym1)) then v1[1] else ohlc4(sym1);
def v2 = if fix_data and isnan(ohlc4(sym2)) then v2[1] else ohlc4(sym2);
def v3 = if fix_data and isnan(ohlc4(sym3)) then v3[1] else ohlc4(sym3);
def v4 = if fix_data and isnan(ohlc4(sym4)) then v4[1] else ohlc4(sym4);
def v5 = if fix_data and isnan(ohlc4(sym5)) then v5[1] else ohlc4(sym5);

# def curve=power((v1/4*v2/4*v3/4*v4/4*v5/4),1/5);
input power_factor = 5;
def curve = power((v1*v2*v3*v4*v5),1/power_factor);
plot c = if isnan(close) then double.nan else reference EhlersSuperSmootherFilter(curve, cutoff);

#-------------------------------------------
input test_show_lines = no;
plot z1 = v1;
plot z2 = v2;
plot z3 = v3;
plot z4 = v4;
plot z5 = v5;
z1.SetHiding(!test_show_lines);
z2.SetHiding(!test_show_lines);
z3.SetHiding(!test_show_lines);
z4.SetHiding(!test_show_lines);
z5.SetHiding(!test_show_lines);

#
I find another problem, how should i stop plotting the marked area?

G7oPDYg.png
 

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

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