Retail Stock Analysis & Performance with LightningChart Python Trader
Tutorial
Written by a Human
Learn how to create a retail stock analysis trading application in Python using LightningChart Python Trader.
Introduction
This article demonstrates how to create a retail stock analysis using LightningChart Python Trader. The stocks in question are Indian retail companies, and the datasets are four Kaggle datasets by Dr. Jagadish Tawade & Nitiraj Kulkarni. The data will be imported to the trader and pre-processed using the Python library Pandas.
Dataset
Four Kaggle datasets by Dr. Jagadish Tawade & Nitiraj Kulkarni will be used as the data source. V-Mart retail dataset is the primary data that is used for all the charts. The data will be provided in CSV files that contain open, high, low, close, adj close, volume, and date values, as well as the company ticker. The following are the additional charts used in multi-chart (dashboard) visualizations.
Plotted Variables
Python Trader requires Open, High, Low, Close (O, H, L, C) values to work (as well as a date). These values refer to the stock’s Highest and Lowest prices during the day, as well as what the price was at market open and close. The volume value is not required for the trader to work, but is needed for certain indicators. The dataset provides both adjusted and unadjusted close values. LightningChart Python Trader can read and process data automatically if the fields (or in this case columns) are labelled correctly.
For example, when importing the dataset, the ‘date’ column is labelled as ‘unknown:0’. This is because the column lacks an index label (sometimes due to that column being used as an index in the CSV file). This will be addressed during the preprocessing step.
Libraries used
- LightningChart Python
- Pandas
Pandas will be used to import data to the trader. This is done by reading the dataset values and importing them into a data frame, which can easily be worked with before visualization. Pandas might be preinstalled with the Python standard library. If this is not the case, simply run the following command in the terminal. Python Trader also needs to be installed.
pip install pandas
pip install lightningchart_trader
LightningChart Python Trader
LightningChart’s Python Trader (PT) provides extensive charting solutions with a high degree of customizability through its easy-to-use UI and built-in functionalities. Since the library is built for Python, it’s also possible to use other Python libraries if necessary.
As with all Lightningchart products, Performance has been a high priority when developing the library, even when working with large amounts of data, thus removing possible bottlenecks in this area.
Overview of the UI
Above is the PT user interface with stock data charted using the default candlestick chart. On the top left are controls used to customize the chart and work with datasets. Here is the explanation for all the UI controls:
Charting features
These are chart types of Python trader provides out of the box.
Chart types can be changed with the the UI or with the set_price_chart_type(chart_type) command. Available ones are ‘CandleStick’, ‘Bar’, ‘Line’, ‘Mountain’, ‘HeikinAshi’, ‘Renko’, ‘Kagi’, and ‘PointAndFigure’. For example, a mountain creating a mountain chart using the following methods:
trader.set_price_chart_type('Mountain')
trader.add_data_array(data)
trader.open()
Chart visuals can be customized using the Python trader UI. Below are available settings for chart and indicator colours, background colour/image etc.
Default chart colours.
Background and indicator colours changed
Chart customization
Customisation can also be done with code, using the built-in methods. The code below changes the background image and the chart colours to the ones seen in the right image. But why use code for customisation, since working with UI is easier?
trader.set_color_theme('cyberSpace')
# Changing the indicator colours for the candlesticks
trader.set_positive_body_color('#0fdf0c')
trader.set_positive_wick_color('#33d7a0')
trader.set_negative_body_color("#f3eb16")
trader.set_negative_wick_color('#b85900')
When the customization is applied on code level there’s no need to change any settings or load a template. Meaning you won’t have to make the changes every time when opening a chart. Python trader supports the use of templates for customization, and these files can be loaded in with code.
However, you still need to manually select the file, and in case you want to share the chart the template file would also need to be shared. When the changes are done in code, only it needs to be shared to get the same results.
Technical indicators
Python trader supports over 100 technical indicators. These include ones such as Relative Strength Index (RSI), Bollinger bands, Moving averages (SMA, EMA etc), Oscillators for money flow and price, Fibonacci retracements, as well as statistical ones like Standard deviation. Technical indicators can be added to a chart easily with the trader UI, their properties (period count, standard dev) can also be changed the same way.
These indicators can also be added and customized via code.
trader.add_simple_moving_average(period_count=14)
trader.add_bollinger_band(period_count=14)
Indicators such as the Fibonacci patterns can be added with the built-in draw tools. Other markers can also be added this way. To see all the indicators, refer to the documentation.
Setting Up Python Environment
The data set is downloaded as a .csv file. This is the code used to process data and import it into charts.
from lightningchart_trader import TAChart
import pandas as pd
import os
license_key_path = "Python-Trader_License_key.txt"
# license key for python trader and the stock API
license_key = open(license_key_path).read()
chart = TAChart(license_key)
path_to_csv = 'data/'
# List of the file names with the stock data
csv_list = os.listdir(path_to_csv)
print(csv_list)
This part of the code imports the necessary libraries and assigns the Python trader license key to a variable. I recommend providing a full file path to the CSV files, even if the data is saved in the same directory or a subdirectory in relation to your .py files. This since I’ve personally experienced issues with Python when only a partial path is provided. In the following example, note the different types of forward slashes used. Python might give an error if you input the path in the same format as it is in the file directory.
'C:/Users\name\Desktop\path\to\the\files/'
Retail Stock Analysis & Visualizations
We will now start creating the visualizations.
V-Mart Retail Limited historical stock data
A line chart for the stock price for V-mart between 2013 – 30.4.2024
from lightningchart_trader import TAChart
import pandas as pd
import os
license_key_path = "Python-Trader_License_key.txt"
# license key for python trader and the stock API
license_key = open(license_key_path).read()
chart = TAChart(license_key)
path_to_csv = 'data/'
# List of the file names with the stock data
csv_list = os.listdir(path_to_csv)
print(csv_list)
# Transform the CSV into a dataframe, combine the file path, with a files index
stock_dataframe = pd.read_csv(path_to_csv + csv_list[3])
# rename the date column
stock_dataframe = stock_dataframe.rename(columns={'Unnamed: 0' : 'date'})
# Set chart title; I'm using the file name as a placeholder,
# type; CandleStick, Bar, Line, Mountain, Renko, HeikinAshi, Kagi or Point&Figure
# and data source used; in this case the stock dataframe
chart.set_chart_title(csv_list[3])
chart.set_price_chart_type('Line')
chart.set_data(stock_dataframe)
# Optional appearence customization
chart.set_color_theme('darkGold')
chart.set_line_color("#FC6A01")
chart.change_time_range(0)
chart.open()
A dashboard for several Indian retail
A dashboard for comparing price trends for several stock prices. Unfortunately, one of the datasets, the DMART one, only goes back to 2017. Thus, the others have been limited to that time frame. The dashboard is an effective way to make comparisons, as it still supports all features (indicators, chart types, etc).
from lightningchart_trader import TAChart
import pandas as pd
import os
license_key_path = "Python-Trader_License_key.txt"
# license key for python trader and the stock API
license_key = open(license_key_path).read()
chart = TAChart(license_key)
path_to_csv = 'data/project-11/'
# All file names in the filepath
csv_list = os.listdir(path_to_csv)
print(csv_list)
# Create a new dashboard with two rows and a two column
new_dashboard = chart.create_dashboard(rows=2, cols=2)
# These values will be used to add charts to the dashboard
row_index = 0
col_index = 0
# Loop though files other than the chemical index and add them to the dashboard
for index in range(4):
# Transform the CSV into a dataframe, combine the file path, with the desired files name
stock_dataframe = pd.read_csv(path_to_csv + csv_list[index])
chart_title = csv_list[index].removesuffix('.csv')
# rename date column
stock_dataframe = stock_dataframe.rename(columns={'Unnamed: 0' : 'date'})
# Add a chart to the dashboard witht he following parameters
dash_chart = new_dashboard.add_chart(chart_type='Line',
row_index=row_index, column_index=col_index,
title=chart_title).set_data(stock_dataframe)
# Place charts to the dashboard based on the current column and row index
col_index += 1
if col_index >= 2:
col_index = 0
row_index += 1
# Optional appearence customization
dash_chart.set_line_color("#65FC01")
dash_chart.change_time_range(2)
dash_chart.add_volume()
chart.open()
Line chart with VMARTS yearly returns plotted using the price range drawing tools
The drawing tool can be accessed from the UI. Finally, set the magnetic mode active as selected by right-clicking on the range nodes. Now the price range tool will automatically lock to the chart.
from lightningchart_trader import TAChart
import pandas as pd
import os
license_key_path = "Python-Trader_License_key.txt"
# license key for python trader and the stock API
license_key = open(license_key_path).read()
chart = TAChart(license_key)
path_to_csv = 'data/'
# List of the file names with the stock data
csv_list = os.listdir(path_to_csv)
print(csv_list)
# Transform the CSV into a dataframe, combine the file path, with a files index
stock_dataframe = pd.read_csv(path_to_csv + csv_list[3])
# rename the date column
stock_dataframe = stock_dataframe.rename(columns={'Unnamed: 0' : 'date'})
# Set chart title; I'm using the file name as a placeholder,
# type; CandleStick, Bar, Line, Mountain, Renko, HeikinAshi, Kagi or Point&Figure
# and data source used; in this case the stock dataframe
chart.set_chart_title(csv_list[3])
chart.set_price_chart_type('Line')
chart.set_data(stock_dataframe)
# Optional appearence customization
chart.set_color_theme('darkGold')
chart.set_line_color("#FC6A01")
chart.change_time_range(0)
chart.open()
Dashboard with volume indicator
The same dashboard as above, but with an added volume indicator. The settings have been changed to show an overlay and two colored bars. The first one puts the indicator in its own window to improve visibility, while the second one colors the indicator, also helping the visibility. Additionally, the colors of the bars were changed. You can access the settings by clicking on the pen icon in the top left corner.
from lightningchart_trader import TAChart
import pandas as pd
import os
license_key_path = "Python-Trader_License_key.txt"
# license key for python trader and the stock API
license_key = open(license_key_path).read()
chart = TAChart(license_key)
path_to_csv = 'data/project-11/'
# All file names in the filepath
csv_list = os.listdir(path_to_csv)
print(csv_list)
# Create a new dashboard with two rows and a two column
new_dashboard = chart.create_dashboard(rows=2, cols=2)
# These values will be used to add charts to the dashboard
row_index = 0
col_index = 0
# Loop though files other than the chemical index and add them to the dashboard
for index in range(4):
# Transform the CSV into a dataframe, combine the file path, with the desired files name
stock_dataframe = pd.read_csv(path_to_csv + csv_list[index])
chart_title = csv_list[index].removesuffix('.csv')
# rename date column
stock_dataframe = stock_dataframe.rename(columns={'Unnamed: 0' : 'date'})
# Add a chart to the dashboard witht he following parameters
dash_chart = new_dashboard.add_chart(chart_type='Line',
row_index=row_index, column_index=col_index,
title=chart_title).set_data(stock_dataframe)
# Place charts to the dashboard based on the current column and row index
col_index += 1
if col_index >= 2:
col_index = 0
row_index += 1
# Optional appearence customization
dash_chart.set_line_color("#65FC01")
dash_chart.change_time_range(2)
dash_chart.add_volume()
chart.open()
Box Plot for isolating periods of increased volume
As increased volume can be an indicator to confirm trends, looking at these periods can expose and highlight different things. For example, if a sudden spike or drop in stock price is momentary, or if there is a larger consensus behind it.
How have retail company stock prices evolved?
All of the companies have experienced growth, some even a significant one. 3 have had noticeable volatility in price trends, whilst one has had a consistent upwards trend.
What differences exist between the stock performances of various retail companies?
Aside from the previously mentioned trend differences. Two of the companies have experienced a significant % change in price. While the other two were still respectable, less of a jump.
Is there a strong correlation between certain companies?
The companies don’t seem to have any correlation. There is a pattern that can be observed during the start of the COVID-19 pandemic: a price drop followed by a larger jump, for all of them. But even this varies by size.
How does trading volume influence price movement in retail stocks?
There are some patterns, such increase in volume during stronger price movements, but this does not present itself every time.
Conclusion
The stock data has been imported from a CSV file to LightningChart Python Trader. The Pandas library was used to preprocess the data. Otherwise, all the charting was done within LightningChart Python Trader. After the code was implemented, it was easy to change the indicators and chart properties for different situations to enhance this retail stock analysis.
Continue learning with LightningChart
Create a JavaScript Scatter Plot
Written by a human | Updated on April 9th, 2025LightningChart JS This is a quick technical look into some interesting features of LightningChart JS XY charts and how to create an embedded scatter chat and add custom interactions to it using LightningChart JS....
HTML
Written by a human | Updated on April 9th, 2025HTML Charts with JavaScript HTML charts are standard and suitable for all-level developers with a simple implementation. The issue with basic HTML 5 charts is their limited functionalities and performance...
Volumetric Data Visualization
This article provides an overview of Volume Data, and the techniques which can be used to visualize it.
