This tutorial is based on last tutorial, modified the program to show a list of stock closing prices that higher or lower than its own 20-day simple moving average (SMA), then sort the list.

These 2 programs are some easy warmup exercises, but not part of my trading system. It is good to know what are the strongest and weakest stocks in the list. Again, this is assume user already download the historical stock closing price in your pc. Otherwise, please refer to this tutorial to get the stock prices first.

YouTube video here when it is ready.

Python program source code:

'''
Source code is from ATradingSystem, https://atradingsystem.com/
All rights reserved.
'''


import pandas as pd


stock_list = ['0038.HK', '0116.HK', '0135.HK', '0148.HK', '0168.HK', '0175.HK', '0187.HK', '0200.HK',
              '0257.HK', '0268.HK', '0285.HK', '0303.HK', '0316.HK', '0322.HK', '0323.HK', '0347.HK',
              '0358.HK', '0384.HK', '0388.HK', '0392.HK', '0398.HK', '0451.HK', '0489.HK', '0512.HK',
              '0590.HK', '0669.HK', '0688.HK', '0698.HK', '0700.HK', '0716.HK', '0732.HK', '0746.HK',
              '0763.HK', '0823.HK', '0826.HK', '0836.HK', '0838.HK', '0855.HK', '0867.HK', '0868.HK',
              '0880.HK', '0883.HK', '0914.HK', '0921.HK', '0939.HK', '0960.HK', '1044.HK', '1053.HK',
              '1057.HK', '1066.HK', '1072.HK', '1083.HK', '1093.HK', '1109.HK', '1133.HK', '1171.HK',
              '1177.HK', '1193.HK', '1208.HK', '1211.HK', '1308.HK', '1378.HK', '1478.HK', '1600.HK',
              '1812.HK', '1818.HK', '1882.HK', '1919.HK', '1999.HK', '2002.HK', '2018.HK', '2020.HK',
              '2128.HK', '2208.HK', '2238.HK', '2313.HK', '2314.HK', '2318.HK', '2319.HK', '2331.HK',
              '2333.HK', '2338.HK', '2343.HK', '2357.HK', '2380.HK', '2382.HK', '2388.HK', '2600.HK',
              '2678.HK', '2688.HK', '2689.HK', '2883.HK', '2899.HK', '3311.HK', '3323.HK', '3339.HK',
              '3808.HK', '3888.HK', '3899.HK', '3900.HK', '8083.HK']


inputstock = input("Enter 1 stock code, e.g. 0323, or empty for all stocks: ")
if inputstock:
    stock_list = [inputstock]


dayfaraway = int(input("Enter 1 for current date, or any number for day that away from current date: "))

column_names = ["stockname", "date", "price"]
dfnew = pd.DataFrame(columns=column_names)
print("")


i = 0
for stock_num in stock_list:
    stockname = 'HK' + stock_num[0:4]
    filepath = 'C:/pythonprojects/stocks/' + stockname + '.csv'

    df = pd.read_csv(filepath, parse_dates=["Date"])
    # df = idf.loc['2014-09-02':'2021-09-01', :]
    df['SMA_10'] = df['Close'].rolling(10).mean()
    df['SMA_20'] = df['Close'].rolling(20).mean()
    df['SMA_50'] = df['Close'].rolling(50).mean()

    # print(df)

    totalrows = len(df.index)
    row1 = totalrows - dayfaraway

    date1 = (df['Date'][row1]).strftime('%Y-%m-%d')
    close1 = df['Close'][row1]

    dfnew.loc[i, "stockname"] = stockname
    dfnew.loc[i, "date"] = date1
    dfnew.loc[i, "price"] = close1
    dfnew.loc[i, "SMA_10"] = df.loc[row1, "SMA_10"]
    dfnew.loc[i, "SMA_20"] = df.loc[row1, "SMA_20"]
    dfnew.loc[i, "SMA_50"] = df.loc[row1, "SMA_50"]

    diff_perc = (dfnew.loc[i, 'price'] - df.loc[row1, 'SMA_20']) * 100 / df.loc[row1, 'SMA_20']
    dfnew.loc[i, "diff_perc"] = diff_perc

    i += 1


dfnew2 = dfnew.sort_values(by=['diff_perc'], ascending=True)
pd.set_option('display.max_rows', None)
print(dfnew2)


filename = input("Input file name for saving details, or empty input to skip saving: ")
if filename:
    writefilepath = 'C:/pythonprojects/stocks/' + filename + '.csv'
    dfnew2.to_csv(writefilepath)
  • Line 25, input a stock code number in console, or empty input to continue.
  • Line 25, 39, 40, if stock code is like “GOOG”, comment line 39 and change “stockname” on line 40 to “stock_num”.
  • Line 30, input a number in console. e.g. 1, if using latest closing price; 10, if using closing price 10 days ago.
  • Line 42, read csv file (historical stock price) here.
  • Line 44-46, create 10-day, 20-day and 50-day simple moving average (SMA) column here. Modified the number if user wanted. e.g. df[‘SMA_5’] = df[‘Close’].rolling(5).mean() for 5-day SMA.
  • Line 56-61, create a table row and copy the row details.
  • Line 63-64, create new column, calculate the difference between the closing price and its own 20-day sma in percentage. User can choose 10-day or 50-day if wanted.
  • Line 66, repeat with next row, until end of stock list.
  • Line 69, table created and sort on the column “diff_perc”
  • Line 74-77, input file name in console, to save the table into csv file. Modified the file path for your own pc.

 

Here is a sample result:

Enter 1 stock code, e.g. 0323, or empty for all stocks: 
Enter 1 for current date, or any number for day that away from current date: 1

    stockname        date  price    SMA_10     SMA_20     SMA_50   diff_perc
45     HK0960  2022-10-31   10.0   16.1360   18.72300   23.01820  -46.589756
99     HK3900  2022-10-31   7.47   10.9320   12.89600   14.59880  -42.075062
79     HK2331  2022-10-31   40.6   47.6300   54.36750   61.90800  -25.323033
26     HK0688  2022-10-31  14.98   18.0300   19.43500   20.63000  -22.922562
84     HK2380  2022-10-31   2.27    2.7820    2.91050    3.63760  -22.006528
73     HK2208  2022-10-31   6.81    8.3550    8.65200   10.15260  -21.289875
14     HK0323  2022-10-31   1.26    1.5660    1.60000    1.78660  -21.250000
100    HK8083  2022-10-31   0.07    0.0787    0.08820    0.10466  -20.634921
7      HK0200  2022-10-31    4.1    4.3650    5.13600    5.28380  -20.171340
4      HK0168  2022-10-31   55.0   63.0650   68.27250   71.31300  -19.440477
82     HK2343  2022-10-31    1.9    2.1780    2.35650    2.60700  -19.371950
89     HK2688  2022-10-31  78.05   91.8000   96.43750  105.06500  -19.066753
94     HK3323  2022-10-31   4.56    5.2930    5.63350    6.55140  -19.055649
53     HK1109  2022-10-31   24.6   28.5350   30.06000   31.29100  -18.163673
15     HK0347  2022-10-31   1.63    1.9000    1.97800    2.19940  -17.593529
17     HK0384  2022-10-31   6.96    8.0230    8.42550    9.84180  -17.393626
18     HK0388  2022-10-31  209.0  235.4000  251.96000  282.72400  -17.050325
42     HK0914  2022-10-31   20.2   22.9050   24.22500   27.05500  -16.615067
55     HK1171  2022-10-31   22.1   24.9650   26.49000   29.37300  -16.572291
30     HK0732  2022-10-31   0.96    1.0660    1.13300    1.34800  -15.269197
77     HK2318  2022-10-31   31.4   34.6800   36.87000   41.29100  -14.835910
28     HK0700  2022-10-31  205.6  220.7400  240.66000  277.80800  -14.568271
78     HK2319  2022-10-31  25.15   27.8950   29.43500   31.77900  -14.557500
2      HK0135  2022-10-31   4.69    5.3190    5.48850    5.99860  -14.548602
58     HK1208  2022-10-31   1.55    1.6560    1.80800    1.96000  -14.269912
36     HK0838  2022-10-31   1.03    1.1460    1.20000    1.38420  -14.166667
5      HK0175  2022-10-31   8.44    9.1180    9.79150   12.37340  -13.802788
57     HK1193  2022-10-31   20.1   22.3350   23.31000   26.63200  -13.770914
71     HK2020  2022-10-31   69.0   74.5300   79.85000   86.44100  -13.587977
3      HK0148  2022-10-31  19.36   21.8210   22.09050   23.25620  -12.360517
74     HK2238  2022-10-31   4.78    5.3450    5.44050    6.08200  -12.140428
87     HK2600  2022-10-31   2.24    2.4770    2.54950    2.77260  -12.139635
47     HK1053  2022-10-31   0.61    0.6800    0.69300    0.76680  -11.976912
68     HK1999  2022-10-31   4.38    4.8180    4.96850    5.45260  -11.844621
8      HK0257  2022-10-31   2.61    2.8450    2.95950    3.33080  -11.809427
60     HK1308  2022-10-31  12.86   14.2160   14.55300   16.74480  -11.633340
35     HK0836  2022-10-31  11.42   13.0740   12.91700   14.24960  -11.589378
22     HK0489  2022-10-31   3.55    3.8270    4.00600    4.49140  -11.382926
12     HK0316  2022-10-31  114.7  125.9600  129.31500  152.32600  -11.301860
39     HK0868  2022-10-31   10.1   11.0220   11.34700   12.82800  -10.989689
19     HK0392  2022-10-31  19.92   21.9920   22.34850   23.46140  -10.866501
61     HK1378  2022-10-31   5.57    5.9100    6.20800    6.98220  -10.277062
63     HK1600  2022-10-31   2.81    3.0470    3.12950    3.45860  -10.209299
75     HK2313  2022-10-31   54.4   58.6300   60.58000   68.92500  -10.201387
88     HK2678  2022-10-31   5.06    5.6300    5.62250    6.20960  -10.004446
51     HK1083  2022-10-31   2.75    3.0590    3.04400    3.17860   -9.658344
33     HK0823  2022-10-31   46.4   48.9600   51.21500   56.98200   -9.401543
72     HK2128  2022-10-31   6.37    6.8280    6.97350    8.11780   -8.654191
59     HK1211  2022-10-31  175.7  187.4200  191.58000  215.27600   -8.288965
6      HK0187  2022-10-31   2.44    2.6030    2.64650    3.12860   -7.802758
38     HK0867  2022-10-31   8.57    9.2420    9.28350   10.18340   -7.685679
27     HK0698  2022-10-31  0.074    0.0759    0.08005    0.10022   -7.557776
23     HK0512  2022-10-31   3.36    3.6720    3.62600    3.84700   -7.335907
85     HK2382  2022-10-31  68.05   71.8000   73.37750   88.07800   -7.260400
37     HK0855  2022-10-31   5.56    5.8620    5.98550    6.53520   -7.108846
1      HK0116  2022-10-31   7.24    7.5360    7.79150    8.21200   -7.078226
46     HK1044  2022-10-31  30.45   32.0350   32.75250   35.01200   -7.029998
93     HK3311  2022-10-31   7.06    7.7060    7.59250    8.39560   -7.013500
40     HK0880  2022-10-31   2.45    2.4860    2.63400    2.71900   -6.985573
13     HK0322  2022-10-31  12.28   12.9220   13.16000   13.47720   -6.686930
44     HK0939  2022-10-31   4.17    4.4090    4.45950    4.62360   -6.491759
67     HK1919  2022-10-31   8.47    8.9320    9.04750    9.83860   -6.382979
29     HK0716  2022-10-31    0.6    0.6420    0.63850    0.68080   -6.029757
20     HK0398  2022-10-31   3.36    3.4250    3.57500    4.06200   -6.013986
31     HK0746  2022-10-31   4.98    5.2260    5.29550    5.94940   -5.957889
24     HK0590  2022-10-31  17.06   17.7480   18.09900   19.23700   -5.740649
76     HK2314  2022-10-31   2.38    2.4830    2.52350    2.66640   -5.686546
98     HK3899  2022-10-31   7.65    7.9400    8.07050    8.46460   -5.210334
86     HK2388  2022-10-31   24.4   25.3100   25.72500   26.34700   -5.150632
11     HK0303  2022-10-31  41.75   43.6800   43.94250   48.35500   -4.989475
16     HK0358  2022-10-31   8.58    8.9230    9.02450    9.40520   -4.925481
49     HK1066  2022-10-31  10.82   11.9280   11.37900   10.65920   -4.912558
48     HK1057  2022-10-31   1.31    1.3720    1.37100    1.50740   -4.449307
41     HK0883  2022-10-31   9.36    9.7750    9.74500    9.97260   -3.950744
92     HK2899  2022-10-31    7.5    7.6910    7.79250    8.31580   -3.753609
25     HK0669  2022-10-31  74.35   76.3300   77.20000   85.21400   -3.691710
56     HK1177  2022-10-31   3.81    4.0840    3.93600    3.93000   -3.201220
95     HK3339  2022-10-31   1.08    1.1040    1.11000    1.20360   -2.702703
90     HK2689  2022-10-31   4.65    4.8090    4.77000    5.53960   -2.515723
43     HK0921  2022-10-31    6.5    6.5600    6.65800    7.66640   -2.373085
32     HK0763  2022-10-31   14.0   14.3860   14.29100   15.33680   -2.036247
96     HK3808  2022-10-31    7.0    7.2810    7.11050    7.32580   -1.554040
69     HK2002  2022-10-31   1.63    1.6390    1.65400    1.74520   -1.451028
81     HK2338  2022-10-31   7.52    7.6180    7.56600    8.84520   -0.607983
54     HK1133  2022-10-31   2.65    2.7480    2.65700    2.58000   -0.263455
62     HK1478  2022-10-31    3.1    3.1060    3.10700    3.68440   -0.225298
52     HK1093  2022-10-31   8.07    8.3250    8.03100    7.82320    0.485618
64     HK1812  2022-10-31   2.22    2.2040    2.20000    2.35340    0.909091
66     HK1882  2022-10-31  15.72   15.6060   15.17300   16.44200    3.605088
50     HK1072  2022-10-31  11.84   11.7880   11.28100   11.47960    4.955234
34     HK0826  2022-10-31   2.18    2.0760    2.06300    2.29100    5.671352
91     HK2883  2022-10-31   8.84    8.5310    8.30500    8.22200    6.441902
83     HK2357  2022-10-31   3.44    3.4070    3.23050    3.46680    6.485064
65     HK1818  2022-10-31   6.48    6.2800    6.04600    6.07200    7.178300
80     HK2333  2022-10-31   8.57    7.9240    7.93950    9.79700    7.941306
0      HK0038  2022-10-31   3.16    2.9820    2.90650    3.04480    8.721830
97     HK3888  2022-10-31  23.75   22.0450   20.83300   22.15320   14.001824
70     HK2018  2022-10-31  14.38   12.7260   12.35500   13.34080   16.390125
10     HK0285  2022-10-31   22.8   20.3290   19.32800   20.79400   17.963576
9      HK0268  2022-10-31  12.84   11.2240   10.50950   12.22380   22.175175
Input file name for saving details, or empty input to skip saving: 

Process finished with exit code 0
  • It can be drawn the conclusion that most Hong Kong stocks are far below 20-day simple moving average.