Steps to install python and python IDE (PyCharm):

  1. Download and install 64-bit latest version of python from https://www.python.org/downloads/
  2. Download and install 64-bit latest Community version of PyCharm from https://www.jetbrains.com/pycharm/download/

That is. You can now start a new python project with PyCharm IDE!

Why using PyCharm? There are a few free python integrated development environment (IDE), such as Jupyter Notebook, Microsoft Visual Studio Code, Spyder, etc. I choose PyCharm because its user interface is very similar to Android Studio, which I am familiar with. If you already using one of these python ide, it is not necessary to install another one.

 

Here is the YouTube video demo:

 

Here is the source code of the demo project. Simply copy and paste into your new project and click run to see the result. To learn more, please watch the above video. It contains bonus more than the source code below.

import pandas as pd
import mplfinance as mpf

stockname = 'HK0388'
filepath = 'C:/pythonprojects/stocks/' + stockname + '.csv'

df = pd.read_csv(filepath, index_col=0, parse_dates=True)
# df = df.loc['2010-01-02':, :]

totalrows = len(df.index)
print('total rows = ' + str(totalrows))

mystyle = mpf.make_mpf_style(base_mpf_style='yahoo', mavcolors=["red", "blue", "fuchsia"])

mpf.plot(df, type='candle', style=mystyle, figscale=1.5, figratio=(18, 9), mav=(10, 20, 100), volume=True,
         title=stockname)

 

  • PyCharm project default uses main.py as the first file name, where .py is the python file extension.
  • *After copy paste the source code into your new project, line 1 and 2 is shown red with error. We need to actually online download the 2 modules (pandas and mplfinance) into the project. Each python IDE has its own method. Some IDE require using command prompt to type command, but PyCharm is easier. Please watch the video above.
  • Line 1, pandas is excel like module, which is use to handle large amount of rows and columns data. Coming next project will need to install this module again, since all python projects are independent.
  • Line 2, “mplfinance” module is use to plot graph. Many online tutorial uses “matplotlib” or “plotly” module. User can choose any one of them, but plotly requires more coding to fix an issue.
  • Line 1 and 2, pd and mpf is now a short name of module “pandas” and “mplfinance” respectively. Python developer usually use short name instead of full name in program.
  • *Line 5, user should update your path and file name HK0388.csv before running the program.
  • Line 7, assume you already downloaded historical stock data in csv format in the path. I will show how to use another python program to download historical stock data in next demo.
  • Line 7, read the csv file from the location, using parameters index_col=0, parse_dates=True
  • Line 8, # is a comment which python will not execute this line of code. If remove the #, data will only uses after the date 2010-01-02
  • Line 10, count the number of rows (data).
  • Line 11, display a message in output console, which shows the total number of rows.
  • Line 13, chart style to use. It is yahoo finance chart like style. “mavcolors” are the color of lines to use that match the mav specify in line 15.
  • Line 15, plot the candlestick chart as shown below. mav=(10, 20, 100) are the simple moving average of 10, 20 and 100 days. This is a built-in function for “mplfinance” module. volume=True (or False) shows (or hide) the volume transaction in lower part of the chart. “figscale” and “figratio” are the size of chart, which can adjust by yourself. “title” is now HK0388.

candlestick chart of HKEX, 0388.HK