ChatGPT is utterly amazing and scary at the same time

If you haven't tried the latest craze in AI world, I encourage you to try it. It is called ChatGPT and it is available for free (for time being) at https://chat.openai.com/chat

Among its many capabilities is writing the code. It is amazingly good in writing in popular languages like Python, Javascript. But apparently it knows something about AFL too.
It makes mistakes and sometimes hallucinates and makes up functions that don't exist, but remember it is just beginning of the revolution.

In a few posts below I will share some of the prompts I used and AI responses.

18 Likes

Me: What is AFL?

ChatGPT:

AFL (AmiBroker Formula Language) is a programming language developed by AmiBroker, a software company that provides technical analysis and charting tools for the financial markets. AFL is used to write formulas and indicators for use with the AmiBroker software.

AFL is a high-level, interpreted language that is optimized for financial data analysis. It provides a wide range of built-in functions and operators for accessing and manipulating financial data, such as stock prices, volumes, and indicators. It also includes a graphic drawing API that allows you to draw lines, shapes, and other graphical elements on charts.

AFL is often used by traders and investors to create custom indicators, backtest trading strategies, and analyze market data. It is also used by developers to create custom applications and plugins for the AmiBroker software.

I hope this helps! Let me know if you have any other questions about AFL.

Me: Can you write AFL formulas?

ChatGPT:

Yes, I can write AFL (AmiBroker Formula Language) formulas. Here are some examples of simple AFL formulas:

  1. Moving Average:
// Declare a variable "length" and set it to the length of the moving average
length = Param("Length", 10, 2, 100, 1);

// Calculate the moving average
ma = MA(Close, length);

// Plot the moving average on the chart
Plot(ma, "MA", colorRed, styleLine);
  1. Bollinger Bands:
// Declare variables "length" and "stdDev" and set them to the length and standard deviation of the Bollinger Bands
length = Param("Length", 20, 2, 100, 1);
stdDev = Param("Std Dev", 2, 0.1, 10, 0.1);

// Calculate the Bollinger Bands
upper = MA(Close, length) + stdDev * StdDev(Close, length);
lower = MA(Close, length) - stdDev * StdDev(Close, length);

// Plot the Bollinger Bands on the chart
Plot(upper, "Upper Band", colorGreen, styleLine);
Plot(lower, "Lower Band", colorGreen, styleLine);
  1. RSI (Relative Strength Index):
// Declare a variable "length" and set it to the length of the RSI
length = Param("Length", 14, 2, 100, 1);

// Calculate the RSI
rsi = RSI(Close, length);

// Plot the RSI on the chart
Plot(rsi, "RSI", colorBlue, styleLine);

// Add horizontal lines to show the overbought and oversold levels
AddHorizontalLine(70, "Overbought", colorRed);
AddHorizontalLine(30, "Oversold", colorGreen);

I hope these examples are helpful! Let me know if you have any questions or need further assistance with writing AFL formulas

5 Likes

As you can see it made some mistakes. It made up the function that doesn't exist (AddHorizontalLine) and it mistyped StDev as StdDev. It also does not understand that AFL is case insensitive and that therefore he can't use rsi (lowercase) as variable name because the name is reserved for RSI function. But other than that the formulas were good.

It is also able to "explain" the formulas, for example when I fed it with Relative Performance formula saying:

Explain the code:

NumBars = 20;
fvb = Status("firstvisiblebar");
Plot( 100 * ( C - C[ fvb ] ) / C[ fvb ], Name(), colorBlue );
for( i = 0; ( symbol = StrExtract( TickerList, i ) ) != ""; i++ )
{
 fc = Foreign( symbol, "C" );

 if( ! IsNull( fc[ 0 ] ) )
 {
   Plot( 100 * ( fc - fc[ fvb ] )/ fc[ fvb ], symbol, colorLightOrange + ( (2*i) % 15 ), styleLine );
 }
}
PlotGrid( 0, colorYellow );
_N( Title = "{{NAME}} - Relative Performance [%]: {{VALUES}}" );

ChatGPT responded with:

Sure! Here is an explanation of the code you provided:

_N( TickerList = ParamStr("Tickers", "^DJI,MSFT,GE") );

This line of code initializes the TickerList variable with a string containing a list of ticker symbols separated by commas. The ParamStr() function is used to retrieve the value of the "Tickers" parameter from the formula's input. If the "Tickers" parameter is not specified, the default value "^DJI,MSFT,GE" is used. The _N() function is used to suppress any error messages that may be generated by the formula.

NumBars = 20;

This line of code sets the value of the NumBars variable to 20.

fvb = Status("firstvisiblebar");

This line of code retrieves the index of the first visible bar on the chart and assigns it to the fvb variable. The Status() function is used to retrieve the value of the "firstvisiblebar" status variable, which contains the index of the first visible bar on the chart.

Plot( 100 * ( C - C[ fvb ] ) / C[ fvb ], Name(), colorBlue );

This line of code plots the relative performance of the current symbol as a percentage on the chart. The Plot() function is used to draw a line on the chart, and it takes three arguments:

  1. The data to be plotted (in this case, the relative performance calculated using the current close price and the close price of the first visible bar)
  2. The label for the line (in this case, the name of the current symbol)
  3. The color of the line
for( i = 0; ( symbol = StrExtract( TickerList, i ) ) != ""; i++ )
{
 fc = Foreign( symbol, "C" );

 if( ! IsNull( fc[ 0 ] ) )
 {
   Plot( 100 * ( fc - fc[ fvb ] )/ fc[ fvb ], symbol, colorLightOrange + ( (2*i) % 15 ), styleLine );
 }
}

This loop iterates over the list of ticker symbols in the TickerList variable. The StrExtract() function is used to extract each symbol from the list, and the Foreign() function is used to retrieve the close prices for the symbol. If the close prices are not null, the relative performance of the symbol is plotted on the chart using the Plot() function. The colorLightOrange + ( (2*i) % 15 ) expression is used to assign a different color to each line plotted on the chart.

PlotGrid( 0, colorYellow );

This line of code plots a horizontal gridline at the price 0 on the chart, using the color yellow.

_N( Title = "{{NAME}} - Relative Performance [%]: {{VALUES}}" );

This line of code sets the title for the chart using the Title formula parameter. The {{NAME}} and {{VALUES}} placeholder variables are replaced with the name of the current symbol and the values of the plotted data, respectively. The _N() function is used to suppress


Me: It is quite scary to realize that this explanation was generated by computer and how precise it is.

5 Likes

Here is another example:

Me: Use array processing to calculate median from midprice in AFL

ChatGPT:

Sure! Here is an example of how you can use array processing in AFL (AmiBroker Formula Language) to calculate the median of the midprice for the last 10 bars on the chart:

// Declare an array to store the midprice for the last 10 bars
midprice = (High + Low) / 2;

// Calculate the median of the midprice
median = Median(midprice, 10);

// Print the median to the Output window
Printf("Median of midprice: %f", median);

In the example above, the High[] and Low[] arrays contain the high and low prices for all the bars on the chart, respectively. The midprice array is created by averaging the high and low prices for each bar. The Median() function is then used to calculate the median of the midprice array for the last 10 bars on the chart. Finally, the median is printed to the Output window using the Printf() function.

I hope this helps! Let me know if you have any questions or need further assistance.


Again it does not understand that it can't use median as variable name, so let us correct him:

Me: AFL is case insensitive, fix above code

Certainly! Here is the revised version of the code that is case insensitive:

// Declare an array to store the midprice for the last 10 bars
midprice = (high + low) / 2;

// Calculate the median of the midprice
median = Median(midprice, 10);

// Print the median to the Output window
Printf("Median of midprice: %f", median);

In this revised version of the code, the high[] and low[] arrays are used instead of the High[] and Low[] arrays. Since AFL is case insensitive, these variables can be written in either uppercase or lowercase. It is generally considered good practice to use lowercase letters for variables and function names, and to use uppercase letters for constants.

I hope this helps! Let me know if you have any other questions.


It got it wrong all the way, but let us show him more precisely what we mean:

Me: That is incorrect. Since reserved function names are case insensitive you can't use them for variable names. Fix it

ChatGPT:

You are correct that reserved function names in AFL (AmiBroker Formula Language) are case insensitive, and therefore cannot be used as variable names. I apologize for the error in my previous response.

Here is a revised version of the code that uses different variable names:

// Declare an array to store the midprice for the last 10 bars
mid_price = (High + Low) / 2;

// Calculate the median of the midprice
median_price = Median(mid_price, 10);

// Print the median to the Output window
Printf("Median of midprice: %f", median_price);

In this revised version of the code, the mid_price array is used instead of the midprice array, and the median_price variable is used instead of the median variable. These names do not conflict with any reserved function names in AFL and are therefore valid variable names.

I hope this helps! Let me know if you have any other questions.


And now as you can see it understood.

5 Likes

TJ, Can we post the ChatGPT conversion code here? I mean it has converted Pinescript to AFL but with lots of error.

1 Like

As I wrote: ChatGPT makes lots of mistakes for the time being. It is not really usable as code conversion tool just yet. It does not seem to grasp array processing too well and does not work properly with longer code. But as I said, that's just the beginning of a new era.

5 Likes

TJ , I think the more conversation that you have with it the more it will learn to correct it's mistakes. Glad to know that you have this conversation and interaction with it.

I doubt it learns "permanently". The GPT algorithm is trained for conversation and it keeps context within the current conversation, so it appears to correct itself within current conversation but I doubt if any "corrections" it receives are stored/learnt permanently unless the entire model is re-trained.

Anyway why not just ask ChatGPT:

Me: Do you permanently store corrections that you have learnt during conversation

ChatGPT:

As an artificial intelligence, I do not have the ability to store information or learn in the same way that humans do. I am programmed to provide responses based on the information that I have been trained on, and I do not have the ability to retain information or change my responses based on individual conversations.

However, the developers at OpenAI may use the feedback and corrections provided by users to improve the quality and accuracy of my responses in the future. If you notice any errors or inaccuracies in my responses, please let me know so that I can provide the most accurate and helpful information to all users.

3 Likes

Yes TJ , you are right and it seems that the data it processes are up to 2021 so if we do interactions with it now then in the next update it may consider the data which is in 2022.

i asked it to write a profitable trading system in Amibroker AFL and it comes up with excuses :smiley:

8 Likes

I think the programmers may launch a paid version for judgemental responses.

1 Like

Thank you for the information Mr. Tomasz, I hope the development and progress of programming will be accelerated and sophisticated. Much like the development of Google Translate, and especially AFL is getting more advanced and easy to use. Thanks

The biggest problem with ChatGPT is that it is not able to say "I don't know". If it does not have slightest idea, it makes things up, produces nonsense, yet it is very confident in saying things that are simply not true. When writing code it can make up non-existing function names, libraries, etc and use them as they were available.

For example it produced the code below when it was asked to "Use AFL formula to send requests to REST API that controls coffee maker"

// Set the endpoint URL and the required parameters
string endpoint = "https://api.example.com/coffee-maker/status";

// Send a GET request to the endpoint
string response = WebRequest("GET", endpoint, "");

// Print the response
Print(response);

It got everything wrong, including syntax and non-existing function names (although you CAN talk to REST API endpoints from AFL, but you should use InternetOpenURL, not made-up WebRequest function).

4 Likes

That reminds me of a lot of real people! :smiley:

5 Likes

I like that it comments the code!

1 Like

I've just been playing around with ChatGPT for an hour and tried AFL and it sorta worked!!!! I ran over to the site to share it all with you but you're already there!

Its amazing. Using ChatGPT to explain AFL code pulled from the repos is really helpful particularly since we all code AFL differently.

OpenAI is valued at $29B in private market. Among investors Microsoft with a $1B investment.

There are some innovative prompts that one could try.

Yesterday I played a bit with ChatGPT acting as linux terminal, and within that terminal, not only all Linux commands worked properly, but I was able to use gcc to compile simple C code and compiled "program" showed up in that virtual environment and it could be run and worked OK. The level of abstraction that ChatGPT is able to handle is insane.

Still it makes up things and hallucinates quite often.

This thing is going to take over many jobs, and surprisingly, not the easiest ones.
GPT-4 is coming in spring 2023 and is going to pose even bigger threat to job market

1 Like

Have you tried creating a dll (data import or converting afl to c++) for Amibroker?

No, at the moment ChatGPT is not able to produce useful C/C++ code beyond simple functions. If you try to ask it for anything beyond what is already available in thousands of copies on StackOverflow/GitHub it fails miserably. It is good only with stuff that is present in thousands of iterations already.

Example from "embedded" world: while it easily writes a "blinking LED for Arduino", it fails if you ask ChatGPT to display a circle on NT35510 based display connected to ESP32-S3. The reason for that is that "blinking LED" code is everywhere on the web, while the other thing is niche.

If you ask it to write data plugin for AmiBroker it will produce made-up, non-working nonsense.

Overall, ChatGPT is most likely to take over "popular" jobs (like junior Javascript programmers, because learning data is available in absurd amounts on the web). Highly specialized, niche skills are not going to be replaced anytime soon because of lack of data on the web.