Nvidia Stock Analysis & Visualization with LightningChart Python Trader
Tutorial
Written by a Human
Learn how to build a trading application for NVIDIA stock analysis using the LightningChart Python Trading library.
Introduction
This article demonstrates how to create the Nvidia stock analysis using LightningChart Python Trader as a data visualization library. The data will be pre-processed using the Python library Pandas. The stock data has been sourced from a Kaggle dataset. The visualisation will go over historical closing prices, as well as OHLC values through candlestick and Heikin Ashi charts, volume, and trend directions.
Kaggle dataset
The data for this article was acquired from a Kaggle dataset by Oleh Onyshchak. The dataset contains data for both stocks and ETFs, but this analysis will focus on NVIDIA. However, more than 5000 companies are included in the original dataset. The data is first downloaded as a CSV file and then pre-processed before being imported to the trader. The data import and pre-processing are done mostly with Pandas.
Unfortunately, the dataset only goes until 2020, since Nvidia experienced rapid growth in the mid-2020s due to the rise of AI. Because of this, this growth will not really be taken into account, despite being crucial to the company’s state. It should be noted that the article serves mainly to demonstrate Python Traders’ visualisation features and is not intended to be taken as detailed stock analysis/financial advice.
Plotted variables
LightningChart 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 trading day, as well as what the price was at market open and close. Volume value is not required for the trader to work, but is needed for certain indicators.
Libraries used
LightningChart Python trader
LightningChart Python Trader must be installed unless it is already available in your environment. You can install it using the following command
pip install lightningchart_trader
- Pandas: Pandas also needs to be installed before it can be imported into the project.
- OS Library: Python’s built-in OS library will be used briefly. It comes preinstalled with Python, so no additional setup is required
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
This code snippet imports the necessary libraries and creates a new trader instance, which remains unchanged across charts.
from lightningchart_trader import TAChart
import pandas as pd
import os
# license key for python trader and create trader instance
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-17/'
# All file names in the filepath
csv_list = os.listdir(path_to_csv)
# 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[0])
This part of the code imports the necessary libraries and creates a new trader instance. It needs to be run every time, but won’t change between charts. I’ll provide only the code going forward that changes on a per-chart basis, but make sure to include the snippet above.
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. Example:
'C:/Users\name\Desktop\path\to\the\files/'
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.
NVIDIA Stock Analysis & Visualizations
Now, we will build the visualizations for NVIDIA stock analysis.
NVIDIA’s historical closing price
NVIDIA’s closing price between 1999 and 2020
from lightningchart_trader import TAChart
import pandas as pd
import os
# license key for python trader and create trader instance
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-17/'
# All file names in the filepath
csv_list = os.listdir(path_to_csv)
# 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[0])
# New dataframe for the adjusted close price.
stock_dataframe.rename(columns={
'Unnamed: 0' : 'date',
'Close' : 'Old close',
'Adj Close' : 'Close'}, inplace=True)
# Set chart title; I'm using the file name as a placeholder,
# type; CandleStick, Bar, Line, Mountain, Renko, HeikinAshi or Point&Figure
# and data source used; in this case the stock dataframe
chart.set_chart_title(csv_list[0])
chart.set_price_chart_type('Line')
chart.set_data(stock_dataframe)
# Optional appearence customization
chart.set_color_theme('darkGold')
chart.set_line_color("#FCD601")
chart.change_time_range(0)
chart.set_series_background_color('#2B2B1D', fillStyle=1, gradientColor="#000000", angle=-51.0, gradientSpeed=0.80)
chart.open()
OHLC data chart for 1 year
Candlestick and Heikin Ashi charts for analysing price movements on a day-by-day basis as well as intraday volatility.
from lightningchart_trader import TAChart
import pandas as pd
import os
# license key for python trader and create trader instance
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-17/'
# All file names in the filepath
csv_list = os.listdir(path_to_csv)
# 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[0])
# New dataframe for the adjusted close price.
stock_dataframe.rename(columns={
'Unnamed: 0' : 'date',
'Close' : 'Old close',
'Adj Close' : 'Close'}, inplace=True)
# Set chart title; I'm using the file name as a placeholder,
# type; CandleStick, Bar, Line, Mountain, Renko, HeikinAshi or Point&Figure
# and data source used; in this case the stock dataframe
chart.set_chart_title(csv_list[0])
chart.set_price_chart_type('CandleStick')
chart.set_data(stock_dataframe)
# Optional appearence customization
chart.set_color_theme('darkGold')
chart.set_line_color("#FCD601")
chart.change_time_range(0)
chart.set_series_background_color('#45673C', fillStyle=1, gradientColor="#673628", angle=-168, gradientSpeed=0.80)
chart.open()
Historical volume and volatility analysis
Combining volume and high-minus-low to visualize how intra-day volatility, volume, and (close) price might correlate. Additionall,y added stock splits via the drawing tools.
from lightningchart_trader import TAChart
import pandas as pd
import os
# license key for python trader and create trader instance
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-17/'
# All file names in the filepath
csv_list = os.listdir(path_to_csv)
# 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[0])
# New dataframe for the adjusted close price.
stock_dataframe.rename(columns={
'Unnamed: 0' : 'date',
'Close' : 'Old close',
'Adj Close' : 'Close'}, inplace=True)
# Set chart title; I'm using the file name as a placeholder,
# type; CandleStick, Bar, Line, Mountain, Renko, HeikinAshi or Point&Figure
# and data source used; in this case the stock dataframe
chart.set_chart_title(csv_list[0])
chart.set_price_chart_type('Line')
chart.set_data(stock_dataframe)
# Optional appearence customization
chart.set_color_theme('darkGold')
chart.set_line_color("#FCD601")
chart.change_time_range(4)
chart.set_series_background_color('#45673C', fillStyle=1, gradientColor="#673628", angle=-168, gradientSpeed=0.80)
chart.add_volume()
chart.add_high_minus_low()
chart.open()
Price trend direction analysis with Renko chart
Renko charts are used to isolate trend directions and price movements. These charts do not represent time in a linear fashion like line or candlestick charts. This is because new bricks/boxes are only added when the price moves a set amount in one direction. This means that the time gap between bricks can be a day, a week, a month, or more, depending on volatility.
A box size of 8 was used for the chart. This means that the stock price has to move $8 in either direction from the previous brick’s price before a new brick is added. The base type was “close.” These values can be changed from the chart settings (the top left icon).
Two things can be observed from the chart. One is the trend direction, and the other is volatility. Since the chart’s time horizon is 1999-2020, but dates before 2015 take only a fraction of the space, it can be concluded that the price has experienced more movement in the 2015-2020 time frame. Additionally, the exact periods can be identified; for example, 2018-2019 seems to be a rather big one. This is something you can spot from most chart types, of course, but a Renko chart allows the trend to be observed simultaneously.
from lightningchart_trader import TAChart
import pandas as pd
import os
# license key for python trader and create trader instance
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-17/'
# All file names in the filepath
csv_list = os.listdir(path_to_csv)
# 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[0])
# New dataframe for the adjusted close price.
stock_dataframe.rename(columns={
'Unnamed: 0' : 'date',
'Close' : 'Old close',
'Adj Close' : 'Close'}, inplace=True)
# Set chart title; I'm using the file name as a placeholder,
# type; CandleStick, Bar, Line, Mountain, Renko, HeikinAshi or Point&Figure
# and data source used; in this case the stock dataframe
chart.set_chart_title(csv_list[0])
chart.set_price_chart_type('Renko')
chart.set_data(stock_dataframe)
# Optional appearence customization
chart.set_color_theme('darkGold')
chart.set_line_color("#FCD601")
chart.change_time_range(4)
chart.set_series_background_color('#45673C', fillStyle=1, gradientColor="#673628", angle=-168, gradientSpeed=0.80)
chart.open()
Analysis
How has NVIDIA’s stock price evolved over the period covered by the dataset?
NVIDIA’s stock price had largely moved sideways over the long term (1999-2016), but after that, it experienced a rapid price growth over the next 2 years or so. In the following years, the stock’s volatility went up noticeably, partly due to the pandemic.
What patterns can be detected in short-term vs. long-term trends?
In the 1-year (2019-2020) period, the stock has experienced both up- and down trends, with these periods varying in length. When looking at longer periods (1999-2020), the stock has overall trended upwards. There have been shorter downward periods at regular intervals, but it wasn’t until October 2018 that NVIDIA would experience its first proper downtrend.
How does volume correlate with significant price movements?
It would require a more quantitative approach to confirm the correlation between volume and volatility, although higher volume tends to result in higher volatility. Purely from looking at a chart, there does appear to be a historical trend of the two values correlating.
Conclusion
The stock data has been imported from a CSV file to Python Trader using Pandas. The data frame has been modified to use the adjusted close value of close value. Several different chart types were used to analyse the stock course, over long- and short-term. From this NVIDIA stock analysis, multiple conclusions can be drawn, and one might be able to make educated guesses about the future. Of course, some of the changes were caused by the pandemic and were not by an event exclusive to Nvidia.
Continue learning with LightningChart
Debunking SciChart’s Performance
Learn about SciChart’s misleading benchmark performance metrics that distort how a real high-end chart library performs.
Swing index indicator: formula and implementation with LC JS Trader
Learn the Swing Index indicator formula and implementation with LightningChart JS Trader to detect trend direction and refine trading signals.
How to use the Supertrend indicator for Fintech app development
Learn about the Supertrend indicator in fintech app development to generate clear buy and sell signals, optimize ATR settings, and enhance trading strategies.
