Deploying Algorithmic Trading strategy Part-2 (Logging Files)

Ganesh N
6 min readMay 20, 2022

--

My last articles covered how to deploy strategy for algorithmic trading. From the process of auto login to entering and exiting market everything was automated. Check it out here

In this article I’ll be talking about how to log events that occur during the process and inspect them later for better understanding of the events.

CONTENTS

1. Introduction to Logging

2. Use of Logging in AlgoTrading

3. Logging & its levels of severity of events

4. Saving events in log file

5. Integrating logging with our previous code

6. Conclusion

7. Further Improvements

1. Introduction to Logging

Storing information and saving it is very important in any field. Analyzing data after the events gives insights on what actually happened during proces and can help us understand what could happen further.

Saving data into files to analyze them later is called Logging Files. Logging is used to tracking events that occur when the software runs. We can face many problems while running applications such as suppose input has to be integer, and we gave a float, in this case error may occur.

Logging is beneficial in such cases to track where the error occurred. Suppose there is no logging record, and the program is interrupted during its execution, we will be unable to find the actual cause of the problem.

Luckily we have a library in python which allows us to create log files so that we can store information. Python’s logging module can be used to log events.

2. Use of Logging in AlgoTrading

Suppose we ran our script of AlgoTrading and trade was entered and exited that day and we need to find when did we entered the trade and when there was exit. Without storing these information in real time, it would be difficult to find out what happened.

Logging hence has important part in our code to understand the events. Let’s say we logged all this information in to a file. We logged the ltp (last traded price) of the stock every 2 seconds, when did trade entry happened and when did trade exited, it would look like this…

Here we can see the ltp for every 2 seconds is being recorded and date & time of trade entry is also shown.

Let’s say there was an error in connection and kite couldn’t fetch ltp from it’s API, in such case HTTPSConnectionPool error occurs. It can also be logged in files as shown below…we will see further how to log these events.

3. Logging & its levels of severity of events

Levels of Severity and their equivalent Numerical values

Logging is a python module in the standard library that provides the facility to work with the framework for releasing log messages from the python programs. The logging module offers the five levels that specify the severity of events.

  1. DEBUG : It is used to provide detailed information and only use it when there is diagnosing problems.
  2. INFO : It provides the information regarding that things are working as we want.
  3. WARNING : It is used to warn that something happened unexpectedly, or we will face the problem in the upcoming time.
  4. ERROR : It is used to inform when we are in some serious trouble, the software hasn’t executed some programs.
  5. CRITICAL : It specifies the serious error, the program itself may be incapable of remaining executing.

These level can be used according to our needs and log events. Let’s see an example…

Notice that debug() and info() message didn’t display messages because, by default, log module logs messages with a severity level of WARNING, ERROR and CRITICAL. Although we can set the level of severity and log events as we wish.

4. Saving events in log file

Above example gives output only in console. If we want to save those in a file, we must use basicConfic() function. The logging module provides the basicConfig(**kwarg), used to configure the logging.

The basicConfig accepts four basic arguments…

  • filename : It specifies a name of the file.
  • filemode : It opens a file in a specific mode. The default mode of the opening file is ‘a’, which means we can append the content. ‘w’ is for writing.
  • format : The format defines the format of the log message.
  • level : The specified severity level is set by the root level.

The file will be saved with name test.log and contents would look like…

We can set the severity level in basicConfig() by passing in level argument. By setting severity to INFO, every message will be logged in to the file.

5. Integrating logging with our previous code

Now that we know how to log events in a file, we come to the main part where we will integrate this logging with our strategy. To maintain simplicity in our code, we will only use one level of severity i.e. INFO.

First we create logging object and set severity and file name.

Above code will log events in a file named e.g. 13_5_2022_BANKNIFTYMAYFUT.log

  1. Let’s say we want to log interval high, low, ltp of stock every few seconds in a file. As soon as entry condition satisfies, it will be logged in file that trade is entered and at what price.

If entry condition is satisfied but few errors occurred during the process. Log file would look like this…

2. Now let’s say after entering the trade, we wanna see when was exit condition satisfied and what was target and stop loss. The code would be…

If exit condition is satisfied, log file would look like…

6. Conclusion

  • We saw what Logging module is in python.
  • How it can be useful to record series of events in AlgoTrading.
  • Its levels of severity (debug, info, warning, error, & critical).
  • How to create log file, change severity level and define its format.
  • And finally how to integrate with our code to log every event that occurs during the process.

7. Further Improvements

Obviously logging events in a file is useful to understand what’s happening but one cannot keep an eye on events all the time. If there could be a way to get updates in real time, it would be great…

So in my next article I’ll be talking about how to get real time updates (messages) on your Telegram App. We will be creating a Telegram bot that will give us updates about when was trade entered and exited in real time. check out the blog here

See you in my follow up article…in case of any doubts and suggestions, reach out to me here…

LinkedIn GitHub HackerRank

Happy Learning and Trading!!!

--

--

Ganesh N

I am a data science enthusiast and an Algo Trader. I talk about automation and how to make process of Algorithmic Trading more efficient.