OpenAI Library MT5 by StormWave User's Manual

OpenAI Library MT5 by StormWave User's Manual

9 February 2024, 23:38
VitalDefender Inc.
0
224

TECHNICAL MANUAL

This blog is intended as a brief technical guide to start using the OpenAI API and exploit the enormous potential offered by the use of artificial intelligence, in particular Large Language Models, applied to trading.

Link to purchase the Library:

https://www.mql5.com/en/market/product/112766?source=Unknown

First you need to proceed to include the attached file StormWaveOpenAI.mqh, which contains the classes and the header of the library, so that you don't have to worry about anything else.

Once the library has been included, we can start with a really simple example, which is the commented source code for the "OpenAI API" Expert Advisor, which you can download for free and test with my OpenAI API at the following link:
https://www.mql5.com/en/market/product/112756?source=Site+Market+Product+Page

Let's proceed!

 #include <StormWaveOpenAI.mqh>       //--- Include the custom OpenAI header file for API integration

input string OPENAI_API_KEY_ = "" ;  // YOUR OPENAI APIKEY
input string MESSAGE_ = "" ;         // SEND MESSAGE
input int MAX_TOKEN_ = 300 ;         // MAX TOKEN

COpenAI *client;                     //--- Declare a pointer to the OpenAI client
CMessages *_message_;               //--- Declare a pointer for handling messages

string __api_key__ = OPENAI_API_KEY_;

//+------------------------------------------------------------------+
//| OnInit()                                                         |
//+------------------------------------------------------------------+
int OnInit ()
  {
//---
   if ( MQLInfoInteger ( MQL_TESTER ))
       return ( INIT_FAILED );

   client = iOpenAI(__api_key__); //--- Initialize the OpenAI client with your API key
   
//--- I decide to do message parsing directly from the cache so as to fetch data to continue chats already started
   client.ParseCache();
   client.start_thread();             //--- Start a new thread for the OpenAI client to operate in
   string completion;

//+------------------------------------------------------------------+
//|                           MESSAGE 1                              |
//+------------------------------------------------------------------+
//--- BOT PREPARATION ---
//--- I create a message
   _message_ = iMessages();           //--- Initialize the message handler
   string system_content = "You are a technical and professional financial assistant specializing in forex chart analysis and respond in a maximum of 40 words in the language of the last message sent by the user" ; //--- Define the message content
   _message_.AddMessage(system_content, system); //--- Add the message to the handler with a system identifier

   string warning = "Warning. You are using API KEY, so you are limited to entering a shorter message!\n" +
                     "If you want to enter a longer message you can use your API KEY." +
                     "You can get them by going to the following URL : https://platform.openai.com/api-keys " ;

   string user_content = MESSAGE_;
//--- Checking whether it is within the maximum number of tokens
   int str_tokens = client.CalculateTokens(MESSAGE_);
   if (str_tokens > MAX_TOKEN_)
     {
      :: MessageBox (warning, "Error" , MB_OK | MB_ICONWARNING );
       return ( INIT_FAILED );
     }
   user_content = MESSAGE_;

   _message_.AddMessage(user_content, user);           //--- Add the message to the handler with a user identifier
   int token = MAX_TOKEN_;

//--- Call the API to generate a completion based on the provided messages
   completion = client.completions_create(
                   /*model =        */    "gpt-3.5-turbo-0125" , //--- Specify the model to use for the completion
                   /*messages =     */    _message_,           //--- Pass the messages to the API
                   /*max_tokens =   */    300 ,                 //--- Set the maximum number of tokens to generate
                   /*temperature =  */    1.0                    //--- Set the creativity level of the response
                );

   :: MessageBox (client.PrintResultMessage(), "Result" , MB_OK | MB_ICONINFORMATION ); //--- Print the result of the API call
   DeletePointer(_message_);
   DeletePointer(client);
//---
   return ( INIT_SUCCEEDED );
  }

//+------------------------------------------------------------------+
//| OnTick()                                                         |
//+------------------------------------------------------------------+
void OnTick ()
  {
   if ( MQLInfoInteger ( MQL_TESTER ))
       return ;
   ExpertRemove ();
  }

template < typename T>
void DeletePointer(T* &ptr)
  {
   if (:: CheckPointer (ptr) == POINTER_DYNAMIC && ptr != NULL )
     {
       delete ptr;
      ptr = NULL ;
     }
  }

Well in this example we have created a simple EA that responds to questions made through the input window.

Other examples will follow soon with the use of tools (i.e. functions) and with the use of GPT-Vision's Computer Vision.

Thanks for reading this article!



Files:
JAson.mqh  31 kb
Share it with friends: