TradingView

Generating Pine Script in ChatGPT

Generating Pine Script in ChatGPT

People are incredibly excited about the increasing popularity of ChatGPT. It has become a phenomenon and is being talked about everywhere. People are eager to experience the potential of this innovative technology for themselves. ChatGPT is also capable of coding, allowing users to create their scripts and programs using different programming languages. I wanted to test it for Pine Script to see how it looks.

I tried with a simple request to provide me any Pine Script indicator:

It generated a simple moving average cross indicator:

study("Simple Moving Average Cross", overlay=true)

// Define the moving average lengths
fastLength = input(title="Fast Length", type=integer, defval=12)
slowLength = input(title="Slow Length", type=integer, defval=26)

// Calculate the moving averages
fastMA = sma(close, fastLength)
slowMA = sma(close, slowLength)

// Plot the moving averages
plot(fastMA, title="Fast MA", color=red, linewidth=2)
plot(slowMA, title="Slow MA", color=blue, linewidth=2)

// Show buy/sell signals
longCondition = crossover(fastMA, slowMA)
shortCondition = crossunder(fastMA, slowMA)

// Plot the signals
plotshape(longCondition, style=shape.arrowup, color=green, location=location.belowbar)
plotshape(shortCondition, style=shape.arrowdown, color=red, location=location.abovebar)

I added this to TradingView, and this seems to work fine. The code is in version 1 of Pine Script, but it seems to work at least. Then I asked it to write me a RSI strategy:

It outputted me the following code:

study("RSI Strategy", overlay=true)

// Define the RSI length and thresholds
length = input(title="Length", type=integer, defval=14)
overbought = input(title="Overbought Threshold", type=integer, defval=70)
oversold = input(title="Oversold Threshold", type=integer, defval=30)

// Calculate the RSI
rsi = rsi(close, length)

// Plot the RSI
plot(rsi, title="RSI", color=blue, linewidth=2)

// Define the overbought and oversold regions
plot(overbought, title="Overbought", style=plot.style_line, color=green, linewidth=2)
plot(oversold, title="Oversold", style=plot.style_line, color=red, linewidth=2)
fill(rsi >= overbought, overbought, rsi <= oversold, oversold, color=gray, transp=70)

// Buy/Sell Signals
buySignal = cross(rsi, oversold)
sellSignal = cross(overbought, rsi)

// Plot the signals
plotshape(buySignal, style=shape.arrowup, color=green, location=location.abovebar)
plotshape(sellSignal, style=shape.arrowdown, color=red, location=location.belowbar)

This, unfortunately, doesn’t work. When adding to TradingView, I received the following errors:

It seems like ChatGPT doesn’t understand versions of Pine Script and tries in old versions to use new variables. In the next request, I asked it to turn it into a strategy and explicitly asked to use v5:

Here is the output:

strategy("RSI Strategy", overlay=true)

// Define the RSI length and thresholds
length = input(title="Length", type=integer, defval=14)
overbought = input(title="Overbought Threshold", type=integer, defval=70)
oversold = input(title="Oversold Threshold", type=integer, defval=30)

// Calculate the RSI
rsi = rsi(close, length)

// Plot the RSI
plot(rsi, title="RSI", color=blue, linewidth=2)

// Define the overbought and oversold regions
plot(overbought, title="Overbought", style=plot.style_line, color=green, linewidth=2)
plot(oversold, title="Oversold", style=plot.style_line, color=red, linewidth=2)
fill(rsi >= overbought, overbought, rsi <= oversold, oversold, color=gray, transp=70)

// Buy/Sell Signals
buySignal = cross(rsi, oversold)
sellSignal = cross(overbought, rsi)

// Enter positions
if (buySignal)
    strategy.entry("Long", strategy.long)

if (sellSignal)
    strategy.close("Long")

// Plot the signals
plotshape(buySignal, style=shape.arrowup, color=green, location=location.abovebar)
plotshape(sellSignal, style=shape.arrowdown, color=red, location=location.belowbar)

It translated the study into a strategy, but still, it’s not version 5, and it won’t work for you in TradingView. For sure, you can go through the errors in TRadingView and make this code work. But the worrying part is that it generated the logic wrong:

buySignal = cross(rsi, oversold)
sellSignal = cross(overbought, rsi)

These two conditions for buying and selling are identical, and of course, the strategy won’t work for sure. After that I decided to ask a general question about TradinView functionality:

It outputted me a script with an example of stop loss implemented in Pine Script:

strategy("Stop Loss Strategy", overlay=true)

// Define the stop loss percentage
stopLossPercent = input(title="Stop Loss Percentage", type=float, defval=2)

// Calculate the stop loss price
stopLossPrice = close * (1 - stopLossPercent/100)

// Enter a long position
if (strategy.position_size == 0)
    strategy.entry("Long", strategy.long)

// Set the stop loss
strategy.exit("Stop Loss", "Long", stop_price=stopLossPrice)

With some description

This strategy uses the strategy.exit() function with the stop_price argument to set a stop loss. The stop loss percentage is defined as an input and the stop loss price is calculated as a percentage of the close price. If a long position is not already open, the strategy enters a long position with strategy.entry(). If the close price falls to the stop loss price, the position will be closed.

As you can guess, this code didn’t work as well from the beginning, and you need to fix the code for it to work. But at least we got here a pretty useful example.

Conclusion

So what is my take on ChayGPT for generating Pine Script code? Well, I don’t think it’s there yet to be super helpful in writing the code. Pine Script is not so popular, and it has a pretty complicated history of versions and compatibility, so it’s hard for ChatGPT to get it. However, I think it can be interesting for people learning Pine Script. There are not many places where you can get free advice, and there is a chance it will be able to answer better / quicker for your Pine Script-related questions.

Am I worried about my programming job going forward? Not really. I think it will become, at some point, a nice tool that will be integrated into your IDE and will help you avoid the most tedious tasks programmers have to do. So I’m pretty optimistic about it, and you?


Follow me on TradingView and YouTube.

This image has an empty alt attribute; its file name is wide.png

Leave a Comment

Your email address will not be published. Required fields are marked *

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

Pine Script Programming Courses
Pine Script Programming Courses
Learn to build your own TradingView Indicators and Strategies
Sidebar Signup Form
If you want to be the first in this business, subscribe to the latest news