// This work is licensed under a Attribution-NonCommercial-ShareAlike 4.0 International (CC BY-NC-SA 4.0) https://creativecommons.org/licenses/by-nc-sa/4.0/
// © LuxAlgo
//@version=5
indicator("Signal Moving Average [LuxAlgo]", overlay = true)
//------------------------------------------------------------------------------
//Settings
//-----------------------------------------------------------------------------{
length = input(50)
src = input(close)
//------------------------------------------------------------------------------
//Signal moving average
//-----------------------------------------------------------------------------{
var ma = 0.
var os = 0.
target = ta.sma(src, length)
abs_diff = math.abs(target - target[1])
r2 = math.pow(ta.correlation(close, bar_index, length), 2)
os := r2 > 0.5 ? math.sign(src[1] - target[1]) : os
ma := r2 > 0.5 ? r2 * target + (1 - r2) * nz(ma[1], target)
: ma[1] - abs_diff * os
//-----------------------------------------------------------------------------}
//Plots
//-----------------------------------------------------------------------------{
plot0 = plot(src, display = display.none, editable = false)
css = os == 1 ? #0cb51a : #ff1100
plot1 = plot(ma, 'Signal MA'
, css)
fill_css = src > ma ? color.new(#0cb51a, 80) : color.new(#ff1100, 80)
fill(plot0, plot1, fill_css, 'Fill')
//-----------------------------------------------------------------------------}
input length = 50;
input src = close;
def ma;
def os;
def target = SimpleMovingAvg(src, length);
def abs_diff = AbsValue(target - target[1]);
def bar = BarNumber();
def r2 = Power(Correlation(src, bar, length), 2);
def osCond = if r2 > 0.5 then Sign(src[1] - target[1]) else os;
def oc = osCond;
oc.AssignValueColor(if osCond == 1 then osCond else Double.NaN, color.green, color.red);
def nz = if IsNaN(ma[1]) then target else ma[1];
def maCond = if r2 > 0.5 then r2 * target + (1 - r2) * nz else ma[1] - abs_diff * os;
plot srcPlot = src;
plot signalMA = maCond;
AddCloud(if src > ma then src else Double.NaN, color.light_green, color.light_red);
Join useThinkScript to post your question to a community of 21,000+ developers and traders.
the link is in post1
You "seem" to have lots of functional input.I asked ChatGPT to write a code that could identify what core sector etf is out performing and what one is underperforming. This is what I got. The code loaded without errors, but there was no functional input. Can anyone make this code work?
Thanks for the reply, I was looking for something that could give me what sector is out performing, and under performing in real time, on any timeframe. I went to the links you gave me and copied the scripts. I’ll try them out this week. I was also hoping that ChatGPT could write a functional script for me, since I can’t code myself. My hats off to all of you on this site who can. Your much appreciatedYou "seem" to have lots of functional input.
Perhaps what you are trying to say, is that you don't have any functional output.
Output in ToS is defined by PLOT statements.
You are correct, you have no PLOT statements.
You did not say what you want your functional output to look like:
1. Are you trying to create chart output? If so what do you want to plot on your chart?
Something like this:
or perhaps labels:2. Perhaps you are attempting to create a scanner.
What are the definitions of your filters?
Something like this:
input startDate = "2010-01-01";
def start = startDate;
def end = GetYYYYMMDD();
def days = DaysBetween(start, end);
def years = days / 365.0;
def lowPrice = low(start, start + 1);
def closePrice = close;
def cagr = if (closePrice / lowPrice > 0) then (closePrice / lowPrice)^(1/years) - 1 else 0;
def lastCAGR = if IsNaN(cagr[1]) then 0 else cagr[1];
def color = if lastCAGR >= 0 then Color.GREEN else Color.RED;
AddLabel(yes, "CAGR: " + Round(lastCAGR, 10), color = color);
def CalculateCAGR = lastCAGR;
RTD("tos.rtd", "LastCAGR", CalculateCAGR);
Hello kind forum. I am trying to write my first thinkscript and got the code from ChatGPT after describing what I wanted. Yet pasting it into the thinkScript editor gives me an error I don't know how to fix. My wife is a software engineer who is trying to assist and she's a little stumped as well. She suggested finding a forum of thinkScript experts so here I am.
I'm trying to write a study which I want to add as a column to a watchlist. The objective is to calculate the CAGR from a particular date that I can change. Here is the code:
Python:input startDate = "2010-01-01"; def start = startDate; def end = GetYYYYMMDD(); def days = DaysBetween(start, end); def years = days / 365.0; def lowPrice = low(start, start + 1); def closePrice = close; def cagr = if (closePrice / lowPrice > 0) then (closePrice / lowPrice)^(1/years) - 1 else 0; def lastCAGR = if IsNaN(cagr[1]) then 0 else cagr[1]; def color = if lastCAGR >= 0 then Color.GREEN else Color.RED; AddLabel(yes, "CAGR: " + Round(lastCAGR, 10), color = color); def CalculateCAGR = lastCAGR; RTD("tos.rtd", "LastCAGR", CalculateCAGR);
The error message in the editor is "Invalid statement: def at 10:1". That's the def cagr . . . line
My wife has tried rewriting this various ways but we're stuck! Any pointers would be so much appreciated. Thanks!
input startTime = 1015;
input endTime = 1600;
input color = createColor(255, 165, 0);
def dayHigh = high(period = "day");
def dayLow = low(period = "day");
def barTime = secondsFromTime(timeOfDay());
if (barTime >= secondsFromTime(startTime) and barTime <= secondsFromTime(endTime)) {
plot ArrowAbove = if close > dayHigh and barNumber() != 0 then dayHigh else double.nan;
ArrowAbove.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
ArrowAbove.SetLineWeight(2);
ArrowAbove.SetDefaultColor(color);
plot ArrowBelow = if close < dayLow and barNumber() != 0 then dayLow else double.nan;
ArrowBelow.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);
ArrowBelow.SetLineWeight(2);
ArrowBelow.SetDefaultColor(color);
}
i started typing my response on my cell after a quick glance....
i should have waited and loaded this code, there are several things wrong...
please make another post and explain what a cagr is.
here are some fixes, but there are too many things wrong to spend any more time on this before i understand the task.
thinkscript won't highlight every error at first. it may find one and highlight it red. after you fix that, another could pop up. then another and....
-----
this is wrong. dashed date text won't be recognized.
input startDate = "2010-01-01";
should be a number , in yyyymmdd format.
input startDate = 20100101;
and make sure it is a date that has data... not a weekend.
---------
this is wrong
def lowPrice = low(start, start + 1);
do you want to find the lowest of 2 values?
could use min(a,b)
to read a value on a specific date, will require an if then to compare dates then assign low to a variable.
--------
the color formula is wrong.
generally only numbers can be assigned with def. there is an exception with bracketed data. def x = { default a, b, c};
def color = if lastCAGR >= 0 then Color.GREEN else Color.RED;
have to put the color formula inside the label
AddLabel(yes, "CAGR: " + Round(lastCAGR, 10), (if lastCAGR >= 0 then Color.GREEN else Color.RED));
---------
the last line is not thinkscript.
rtd() is an excel function.
here is a link to a function in the online manual. click on 3 lines top left and search for others
https://tlc.thinkorswim.com/center/reference/thinkScript/Functions/Date---Time/GetYYYYMMDD
in a column, use this to change the color of the cell
https://tlc.thinkorswim.com/center/reference/thinkScript/Functions/Look---Feel/AssignBackgroundColor
^ isn't valid, use power()
https://tlc.thinkorswim.com/center/reference/thinkScript/Functions/Math---Trig/Power
next time, search this site for your topic. good chance someone already thought of it
https://usethinkscript.com/threads/how-to-use-ts-to-get-cagr-compounded-annual-growth-rates.5849/
https://www.investopedia.com/terms/c/cagr.asp
--------------------
oh, and hello and welcome.
as you will find out, chatgpt is not all it's hyped up to be. it may be able to create small code snippets , but the user should have knowledge of the code.
i started typing my response on my cell after a quick glance....
i should have waited and loaded this code, there are several things wrong...
please make another post and explain what a cagr is.
here are some fixes, but there are too many things wrong to spend any more time on this before i understand the task.
thinkscript won't highlight every error at first. it may find one and highlight it red. after you fix that, another could pop up. then another and....
-----
this is wrong. dashed date text won't be recognized.
input startDate = "2010-01-01";
should be a number , in yyyymmdd format.
input startDate = 20100101;
and make sure it is a date that has data... not a weekend.
---------
this is wrong
def lowPrice = low(start, start + 1);
do you want to find the lowest of 2 values?
could use min(a,b)
to read a value on a specific date, will require an if then to compare dates then assign low to a variable.
--------
the color formula is wrong.
generally only numbers can be assigned with def. there is an exception with bracketed data. def x = { default a, b, c};
def color = if lastCAGR >= 0 then Color.GREEN else Color.RED;
have to put the color formula inside the label
AddLabel(yes, "CAGR: " + Round(lastCAGR, 10), (if lastCAGR >= 0 then Color.GREEN else Color.RED));
---------
the last line is not thinkscript.
rtd() is an excel function.
here is a link to a function in the online manual. click on 3 lines top left and search for others
https://tlc.thinkorswim.com/center/reference/thinkScript/Functions/Date---Time/GetYYYYMMDD
in a column, use this to change the color of the cell
https://tlc.thinkorswim.com/center/reference/thinkScript/Functions/Look---Feel/AssignBackgroundColor
^ isn't valid, use power()
https://tlc.thinkorswim.com/center/reference/thinkScript/Functions/Math---Trig/Power
next time, search this site for your topic. good chance someone already thought of it
https://usethinkscript.com/threads/how-to-use-ts-to-get-cagr-compounded-annual-growth-rates.5849/
https://www.investopedia.com/terms/c/cagr.asp
--------------------
oh, and hello and welcome.
as you will find out, chatgpt is not all it's hyped up to be. it may be able to create small code snippets , but the user should have knowledge of the code.
I appreciate any help, Im just trying to create a super simple script that counts 1 day back and finds any 1 minute candles that have a range of more than $1.99 (for example, say there were 16 one minute candles with a range of more than $1.99, then the result SHOULD show as "16"). But instead its only showing the result as "YES" in red, and thats only if I hover my mouse over it. If I move my mouse away, it just says "NO".
It's just not working how I want. Please help!
]Im super new to thinkscript and am hoping there is a veteran who can help me point out what im doing wrong, cause as far as i know, the syntax and everything should be fine right?
# min_bar_ht_00
#https://usethinkscript.com/threads/chatgpt.13822/#post-119061
def na = double.nan;
def bn = barnumber();
def newday = getday() != getday()[1];
def dayend = getday() != getday()[-1];
def rng = (high - low);
def avg = average(close, 10);
input min_rng_dollar = 1.99;
input min_rng_percent = 1.3;
def minrng;
input range_type = { default dollar , percent};
switch (range_type) {
case dollar:
minrng = min_rng_dollar;
case percent:
minrng = avg * (min_rng_percent/100);
}
def rngx = (rng > minrng);
def rngxcnt = if bn == 1 then 0
else if newday and rngx then 1
else if newday then 0
else if rngx then rngxcnt[1] + 1
else rngxcnt[1];
plot z = if rngx then low*0.999 else na;
z.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
z.SetDefaultColor(Color.cyan);
z.setlineweight(2);
z.hidebubble();
input rng_cnt = yes;
addchartbubble(rng_cnt and dayend, high*1.001,
rngxcnt
, (if rngxcnt > 0 then color.yellow else color.gray), yes);
input test2 = no;
addchartbubble(test2, low*0.99,
(100 * rng / close)
, color.yellow, no);
#
https://usethinkscript.com/threads/home-for-chatgpt.13822/#post-115062Is there a way to get A.I. to write custom code using thinkscript language?
Is there a way to get A.I. to write custom code using thinkscript language?
Code for Rsi divergence. Please help me to correct it
input price = close;
input length = 14;
input overbought = 70;
input oversold = 30;
def diff = price - price[1];
def up = if diff > 0 then diff else 0;
def down = if diff < 0 then -diff else 0;
def avgUp = ExpAverage(up, length);
def avgDown = ExpAverage(down, length);
def rsi = 100 - (100 / (1 + avgUp / avgDown));
plot rsiLine = rsi;
rsiLine.SetDefaultColor(GetColor(5));
rsiLine.SetLineWeight(2);
def bullishDivergence = Cross(rsi, oversold) and price > price[1];
def bearishDivergence = Cross(overbought, rsi) and price < price[1];
plot bullish = bullishDivergence;
bullish.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
bullish.SetDefaultColor(GetColor(6));
bullish.SetLineWeight(3);
plot bearish = bearishDivergence;
bearish.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
bearish.SetDefaultColor(GetColor(1));
bearish.SetLineWeight(3);
# chat_rsi_diverg_00
#https://usethinkscript.com/threads/chatgpt.13822/#post-119427
# post15
declare lower;
def na = double.nan;
#songvicobac
# #15
#Code for Rsi divergence. Please help me to correct it
input price = close;
input length = 14;
input overbought = 70;
input oversold = 30;
def diff = price - price[1];
def up = if diff > 0 then diff else 0;
def down = if diff < 0 then -diff else 0;
def avgUp = ExpAverage(up, length);
def avgDown = ExpAverage(down, length);
def rsi = 100 - (100 / (1 + avgUp / avgDown));
plot rsiLine = rsi;
rsiLine.SetDefaultColor(GetColor(5));
rsiLine.SetLineWeight(2);
def bullishDivergence = Crosses(rsi, oversold) and price > price[1];
def bearishDivergence = Crosses(overbought, rsi) and price < price[1];
#plot bullish = bullishDivergence;
#bullish.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
plot bullish = if bullishDivergence then rsi else na;
bullish.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
bullish.SetDefaultColor(GetColor(6));
bullish.SetLineWeight(3);
#plot bearish = bearishDivergence;
#bearish.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
plot bearish = if bearishDivergence then rsi else na;
bearish.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);
bearish.SetDefaultColor(GetColor(1));
bearish.SetLineWeight(3);
#
Start a new thread and receive assistance from our community.
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.
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.