ChatGPT, BARD, Other AI Scripts Which Can't Be Used In ThinkOrSwim

MerryDay

Administrative
Staff member
Staff
VIP
Lifetime
90% of the members attempting chatGPT scripting; do not provide good detailed specifications.

Members know what end result that they want but lack the ability to tell chatGPT, the step-by-step logic required to code a script to achieve that end result.
This results in chatGPT creating non-working garbage code.

Here is a video on how to successfully have chatGPT create functional scripts.

The above video, explains the basics of providing good specifications to AI which results in better scripts.
AOzhl05.png
 
Last edited:
Hello all,

I want to create an indicator that

1-Plots EMA13 (yellow, EMA 48 (purple), and EMA 200 (Red).
2-Displays Previous Day High, low, open and close level
3-Plots pre-market high and low
4-Adds label indicating whether price is above or below these key level
5-Suggest trade bias (call/puts) based on EMA positioning.

What I'm missing it is not plotting on TOS. THX
Code:
declare upper;
declare once_per_bar;

# Define EMA values
input EMA1 = 13;
input EMA2 = 48;
input EMA3 = 200;

def EMA_13 = ExpAverage(close, EMA1);
def EMA_48 = ExpAverage(close, EMA2);
def EMA_200 = ExpAverage(close, EMA3);

# Previous Day Levels
def prevHigh = high(period = "DAY" )[1];
def prevLow = low(period = "DAY" )[1];
def prevClose = close(period = "DAY" )[1];
def prevOpen = open(period = "DAY" )[1];

# Pre-Market High and Low (assumes extended hours enabled)
def preMarketHigh = high(period = AggregationPeriod.DAY, priceType = PriceType.LAST, extended = yes);
def preMarketLow = low(period = AggregationPeriod.DAY, priceType = PriceType.LAST, extended = yes);

# Define Colors
AssignPriceColor(if close > EMA_13 then Color.GREEN else Color.RED);

# Plot EMAs
plot pEMA_13 = EMA_13;
pEMA_13.SetDefaultColor(Color.YELLOW);
pEMA_13.SetLineWeight(2);

plot pEMA_48 = EMA_48;
pEMA_48.SetDefaultColor(Color.PURPLE);
pEMA_48.SetLineWeight(2);

plot pEMA_200 = EMA_200;
pEMA_200.SetDefaultColor(Color.RED);
pEMA_200.SetLineWeight(2);

# Plot previous day levels
plot pdHigh = prevHigh;
pdHigh.SetDefaultColor(Color.DARK_ORANGE);
pdHigh.SetStyle(Curve.SHORT_DASH);

plot pdLow = prevLow;
pdLow.SetDefaultColor(Color.DARK_ORANGE);
pdLow.SetStyle(Curve.SHORT_DASH);

plot pdOpen = prevOpen;
pdOpen.SetDefaultColor(Color.DARK_ORANGE);
pdOpen.SetStyle(Curve.SHORT_DASH);

plot pdClose = prevClose;
pdClose.SetDefaultColor(Color.DARK_ORANGE);
pdClose.SetStyle(Curve.SHORT_DASH);

# Plot pre-market levels
plot pmHigh = preMarketHigh;
pmHigh.SetDefaultColor(Color.LIGHT_RED);
pmHigh.SetStyle(Curve.SHORT_DASH);

plot pmLow = preMarketLow;
pmLow.SetDefaultColor(Color.LIGHT_RED);
pmLow.SetStyle(Curve.SHORT_DASH);

# Define signal conditions
def aboveEMA_13 = close > EMA_13;
def aboveEMA_48 = close > EMA_48;
def aboveEMA_200 = close > EMA_200;
def abovePDH = close > prevHigh;
def abovePDL = close > prevLow;
def abovePMH = close > preMarketHigh;
def abovePML = close > preMarketLow;

# Labels for Trade Direction
AddLabel(yes, "EMA 13: " + EMA_13, if aboveEMA_13 then Color.GREEN else Color.RED);
AddLabel(yes, "EMA 48: " + EMA_48, if aboveEMA_48 then Color.GREEN else Color.RED);
AddLabel(yes, "EMA 200: " + EMA_200, if aboveEMA_200 then Color.GREEN else Color.RED);
AddLabel(yes, "Prev High: " + prevHigh, if abovePDH then Color.GREEN else Color.RED);
AddLabel(yes, "Prev Low: " + prevLow, if abovePDL then Color.GREEN else Color.RED);
AddLabel(yes, "Pre-Mkt High: " + preMarketHigh, if abovePMH then Color.GREEN else Color.RED);
AddLabel(yes, "Pre-Mkt Low: " + preMarketLow, if abovePML then Color.GREEN else Color.RED);

# Trade Bias
AddLabel(yes, if aboveEMA_13 then "Focus on Calls" else "Focus on Puts", if aboveEMA_13 then Color.GREEN else Color.RED);

reply to #399

learn to look up functions in the online manual
you are trying to use function parameters that dont exist


there is no parameter extended for price function
def preMarketHigh = high(period = AggregationPeriod.DAY, priceType = PriceType.LAST, extended = yes);
https://toslc.thinkorswim.com/center/reference/thinkScript/Functions/Fundamentals/close


if you want premarket data, you have to use these to define a period of time
https://toslc.thinkorswim.com/center/reference/thinkScript/Functions/Date---Time/SecondsTillTime
https://toslc.thinkorswim.com/center/reference/thinkScript/Functions/Date---Time/SecondsFromTime
like 4am to 9:30
https://www.tradinghours.com/markets/nasdaq


no purple color, use magenta
https://toslc.thinkorswim.com/center/reference/thinkScript/Constants/Color

add new code to find premarket hi and lo


Code:
#chat399_fix_ema_prehilo

#https://usethinkscript.com/threads/chatgpt-bard-other-ai-scripts-which-cant-be-used-in-thinkorswim.13822/page-20#post-152957
#MrCrude
#399

#I want to create an indicator that

#1-Plots EMA13 (yellow, EMA 48 (purple), and EMA 200 (Red).
#2-Displays Previous Day High, low, open and close level
#3-Plots pre-market high and low
#4-Adds label indicating whether price is above or below these key level
#5-Suggest trade bias (call/puts) based on EMA positioning.

#What I'm missing it is not plotting on TOS. THX


declare upper;
declare once_per_bar;

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

# Define EMA values
input EMA1 = 13;
input EMA2 = 48;
input EMA3 = 200;

def EMA_13 = ExpAverage(close, EMA1);
def EMA_48 = ExpAverage(close, EMA2);
def EMA_200 = ExpAverage(close, EMA3);

# Previous Day Levels
def prevHigh = high(period = "DAY" )[1];
def prevLow = low(period = "DAY" )[1];
def prevClose = close(period = "DAY" )[1];
def prevOpen = open(period = "DAY" )[1];


# Pre-Market High and Low (assumes extended hours enabled)
#def preMarketHigh = high(period = AggregationPeriod.DAY, priceType = PriceType.LAST, extended = yes);
#def preMarketLow = low(period = AggregationPeriod.DAY, priceType = PriceType.LAST, extended = yes);

input prestart = 0400;
input preend = 0930;
def pre = if secondsfromTime(prestart) >= 0 and secondstillTime(preend) > 0 then 1 else 0;

def n = 400;
def big = 99999;

def preMarketHigh;
def preMarketlow;
if bn == 1 then {
 preMarketHigh = na;
 preMarketlow = na;
} else if (!pre[1] and pre) then {
# on the first bar of pre, look for hi/lo
 preMarketHigh = fold a = 0 to n
  with b
  while getvalue(pre, -a)
  do max(b, getvalue(high,-a));

 preMarketlow = fold c = 0 to n
  with d = big
  while getvalue(pre, -c)
  do min(d, getvalue(low,-c));
} else {
 preMarketHigh = preMarketHigh[1];
 preMarketlow = preMarketlow[1];
}


# Define Colors
AssignPriceColor(if close > EMA_13 then Color.GREEN else Color.RED);

# Plot EMAs
plot pEMA_13 = EMA_13;
pEMA_13.SetDefaultColor(Color.YELLOW);
pEMA_13.SetLineWeight(2);

plot pEMA_48 = EMA_48;
#pEMA_48.SetDefaultColor(Color.PURPLE);
pEMA_48.SetDefaultColor(Color.magenta);
pEMA_48.SetLineWeight(2);




plot pEMA_200 = EMA_200;
pEMA_200.SetDefaultColor(Color.RED);
pEMA_200.SetLineWeight(2);

# Plot previous day levels
plot pdHigh = prevHigh;
pdHigh.SetDefaultColor(Color.DARK_ORANGE);
pdHigh.SetStyle(Curve.SHORT_DASH);

plot pdLow = prevLow;
pdLow.SetDefaultColor(Color.DARK_ORANGE);
pdLow.SetStyle(Curve.SHORT_DASH);

plot pdOpen = prevOpen;
pdOpen.SetDefaultColor(Color.DARK_ORANGE);
pdOpen.SetStyle(Curve.SHORT_DASH);

plot pdClose = prevClose;
pdClose.SetDefaultColor(Color.DARK_ORANGE);
pdClose.SetStyle(Curve.SHORT_DASH);

# Plot pre-market levels
plot pmHigh = preMarketHigh;
pmHigh.SetDefaultColor(Color.LIGHT_RED);
pmHigh.SetStyle(Curve.SHORT_DASH);

plot pmLow = preMarketLow;
pmLow.SetDefaultColor(Color.LIGHT_RED);
pmLow.SetStyle(Curve.SHORT_DASH);


addchartbubble(0,low,
preMarketHigh + "\n" +
preMarketlow + "\n" +
#pema_13 + "\n" +
#pema_48 + "\n" +
#pema_200 + "\n" +
#pdhigh + "\n" +
#pdlow + "\n" +
#pdopen + "\n" +
#pdclose + "\n" +
pmhigh + "\n" +
pmlow + "\n" 
, color.yellow, no);



# Define signal conditions
def aboveEMA_13 = close > EMA_13;
def aboveEMA_48 = close > EMA_48;
def aboveEMA_200 = close > EMA_200;
def abovePDH = close > prevHigh;
def abovePDL = close > prevLow;
def abovePMH = close > preMarketHigh;
def abovePML = close > preMarketLow;

# Labels for Trade Direction
AddLabel(yes, "EMA 13: " + EMA_13, if aboveEMA_13 then Color.GREEN else Color.RED);
AddLabel(yes, "EMA 48: " + EMA_48, if aboveEMA_48 then Color.GREEN else Color.RED);
AddLabel(yes, "EMA 200: " + EMA_200, if aboveEMA_200 then Color.GREEN else Color.RED);
AddLabel(yes, "Prev High: " + prevHigh, if abovePDH then Color.GREEN else Color.RED);
AddLabel(yes, "Prev Low: " + prevLow, if abovePDL then Color.GREEN else Color.RED);
AddLabel(yes, "Pre-Mkt High: " + preMarketHigh, if abovePMH then Color.GREEN else Color.RED);
AddLabel(yes, "Pre-Mkt Low: " + preMarketLow, if abovePML then Color.GREEN else Color.RED);

# Trade Bias
AddLabel(yes, if aboveEMA_13 then "Focus on Calls" else "Focus on Puts", if aboveEMA_13 then Color.GREEN else Color.RED);

#
 
@halcyonguy I have this peak and valley script that I've been working on but its showing the line peak line and arrow before it confirms. i want to show the line and arrow only when price gives a confirmation of the peaks or valleys. For example when price makes a high and the next candle moves 1 penny below the low of that high that confirms a peak to me, then the arrow and line would then be placed at the highest point. if price never trades 1 penny below no arrow or line should appear until it breaks. same thing for valley price have to trade 1 penny above the wick high of the candle with the lowest point and then the arrow and line would be placed at the low of the lowest point. as soon as price breaks only then an arrow will appear confirming the valley. This is the current script... i hope you or someone can help!!!
Code:
#
# Peak/Valley Break Confirmation Script with Configurable Alerts + Custom Sounds

declare upper;

input len = 10;
input useClosePrice = no; # Use Close for break confirmation (yes) or wick (no)
input alertMode = {default "Both", "Only Peaks", "Only Valleys"}; # Alert Type

input peakSound = {default "Ring", "Chimes", "Bells", "Ding", "NoSound"};
input valleySound = {default "Bell", "Chimes", "Bells", "Ding", "NoSound"};

def na = Double.NaN;
def bn = BarNumber();
def lastbn = HighestAll(if !IsNaN(close) then bn else 0);
def offset = Min(len - 1, lastbn - bn);

# --- Find Peaks and Valleys ---
def isPeak = high > Highest(high[1], len - 1) and high == GetValue(Highest(high, len), -offset);
def isValley = low < Lowest(low[1], len - 1) and low == GetValue(Lowest(low, len), -offset);

def peakPrice = if isPeak then high else peakPrice[1];
def valleyPrice = if isValley then low else valleyPrice[1];

# --- Break Logic ---
def peakBroken = if useClosePrice
then close < peakPrice
else low < peakPrice;

def valleyBroken = if useClosePrice
then close > valleyPrice
else high > valleyPrice;

def peakBrokenConfirmed = if isPeak then 0 else if peakBroken then 1 else peakBrokenConfirmed[1];
def valleyBrokenConfirmed = if isValley then 0 else if valleyBroken then 1 else valleyBrokenConfirmed[1];

# --- Arrows ---
plot SellArrow = if peakBrokenConfirmed and !peakBrokenConfirmed[1] then peakPrice else na;
SellArrow.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);
SellArrow.SetDefaultColor(Color.RED);
SellArrow.SetLineWeight(3);

plot BuyArrow = if valleyBrokenConfirmed and !valleyBrokenConfirmed[1] then valleyPrice else na;
BuyArrow.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
BuyArrow.SetDefaultColor(Color.GREEN);
BuyArrow.SetLineWeight(3);

# --- Horizontal Range Lines ---
plot PeakLine = if !IsNaN(peakPrice) then peakPrice else na;
PeakLine.SetDefaultColor(Color.RED);
PeakLine.SetStyle(Curve.LONG_DASH);
PeakLine.SetLineWeight(1);

plot ValleyLine = if !IsNaN(valleyPrice) then valleyPrice else na;
ValleyLine.SetDefaultColor(Color.GREEN);
ValleyLine.SetStyle(Curve.LONG_DASH);
ValleyLine.SetLineWeight(1);

# --- Alerts with Custom Sounds ---
def doSellAlert = (alertMode == alertMode."Both" or alertMode == alertMode."Only Peaks") and SellArrow;
def doBuyAlert = (alertMode == alertMode."Both" or alertMode == alertMode."Only Valleys") and BuyArrow;

Alert(doSellAlert, "Peak Broken: Sell Signal", Alert.BAR, peakSound);
Alert(doBuyAlert, "Valley Broken: Buy Signal", Alert.BAR, valleySound);

reply to #400

i changed the peak / valley code

Code:
#chat400_peaks

#https://usethinkscript.com/threads/chatgpt-bard-other-ai-scripts-which-cant-be-used-in-thinkorswim.13822/page-20#post-152956
#Takentrade1
#400
#@halcyonguy I have this peak and valley script that I've been working on
# but its showing the line peak line and arrow before it confirms.
# i want to show the line and arrow only when price gives a confirmation of the peaks or valleys.

# For example when price makes a high
#  and the next candle moves 1 penny below the low of that high,
#   that confirms a peak to me,
# then the arrow and line would then be placed at the highest point. 
#if price never trades 1 penny below no arrow or line should appear until it breaks. 

#same thing for valley price have to trade 1 penny above the wick high of the candle with the lowest point and then the arrow and line would be placed at the low of the lowest point. as soon as price breaks only then an arrow will appear confirming the valley. This is the current script... i hope you or someone can help!!!


#
# Peak/Valley Break Confirmation Script with Configurable Alerts + Custom Sounds

declare upper;

input len = 10;
input useClosePrice = no; # Use Close for break confirmation (yes) or wick (no)
input alertMode = {default "Both", "Only Peaks", "Only Valleys"}; # Alert Type

input peakSound = {default "Ring", "Chimes", "Bells", "Ding", "NoSound"};
input valleySound = {default "Bell", "Chimes", "Bells", "Ding", "NoSound"};

def na = Double.NaN;
def bn = BarNumber();
#def lastbn = HighestAll(if !IsNaN(close) then bn else 0);
#def offset = Min(len - 1, lastbn - bn);

# --- Find Peaks and Valleys ---
#def isPeak = high > Highest(high[1], len - 1) and high == GetValue(Highest(high, len), -offset);
#def isValley = low < Lowest(low[1], len - 1) and low == GetValue(Lowest(low, len), -offset);

def ispeak = low > high[1] and low > high[-1];
def isvalley = high < low[1] and high < low[-1];


def peakPrice = if isPeak then high else peakPrice[1];
def valleyPrice = if isValley then low else valleyPrice[1];

# --- Break Logic ---
def peakBroken = if useClosePrice then close < peakPrice
 else low < peakPrice;

def valleyBroken = if useClosePrice then close > valleyPrice
 else high > valleyPrice;

def peakBrokenConfirmed = if isPeak then 0 else if peakBroken then 1 else peakBrokenConfirmed[1];
def valleyBrokenConfirmed = if isValley then 0 else if valleyBroken then 1 else valleyBrokenConfirmed[1];

# --- Arrows ---
plot SellArrow = if peakBrokenConfirmed and !peakBrokenConfirmed[1] then peakPrice else na;
SellArrow.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);
SellArrow.SetDefaultColor(Color.RED);
SellArrow.SetLineWeight(3);

plot BuyArrow = if valleyBrokenConfirmed and !valleyBrokenConfirmed[1] then valleyPrice else na;
BuyArrow.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
BuyArrow.SetDefaultColor(Color.GREEN);
BuyArrow.SetLineWeight(3);

# --- Horizontal Range Lines ---
plot PeakLine = if peakPrice > 0 and !IsNaN(peakPrice) then peakPrice else na;
peakline.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
PeakLine.SetDefaultColor(Color.RED);
PeakLine.SetStyle(Curve.LONG_DASH);
PeakLine.SetLineWeight(1);

plot ValleyLine = if valleyPrice > 0 and !IsNaN(valleyPrice) then valleyPrice else na;
valleyline.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
ValleyLine.SetDefaultColor(Color.GREEN);
ValleyLine.SetStyle(Curve.LONG_DASH);
ValleyLine.SetLineWeight(1);

# --- Alerts with Custom Sounds ---
def doSellAlert = (alertMode == alertMode."Both" or alertMode == alertMode."Only Peaks") and SellArrow;
def doBuyAlert = (alertMode == alertMode."Both" or alertMode == alertMode."Only Valleys") and BuyArrow;

Alert(doSellAlert, "Peak Broken: Sell Signal", Alert.BAR, peakSound);
Alert(doBuyAlert, "Valley Broken: Buy Signal", Alert.BAR, valleySound);
#
 

Attachments

  • img1.JPG
    img1.JPG
    53.4 KB · Views: 17

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
292 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