Sometimes, stock trader may want to find out the strongest and weakest in a list of stocks. Here is the python source code:

#YouTube video here when it is ready:

'''
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]


# compare_first = 90  # 90 days ago to compare
# compare_last = 1

a, b = input("Enter day1 and last day to compare, e.g. 90 1: ").split()
compare_first = int(a)
compare_last = int(b)


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

i = 0
posChgPerc = 0
negChgPerc = 0
posChgNum = 0
negChgNum = 0
for stock_num in stock_list:
    stockname = 'HK' + stock_num[0:4]
    # stockname = 'HKHSI'
    # print('--> ' + stockname + ': <--')

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

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

    totalrows = len(df.index)
    row1 = totalrows - compare_first
    rowx = totalrows - compare_last
    # print('total rows = ' + str(totalrows))

    date1 = df['Date'][row1]
    close1 = df['Close'][row1]

    datex = df['Date'][rowx]
    closex = df['Close'][rowx]
    pricediff = ((closex - close1) / close1) * 100
    # print(pricediff)

    dfnew.loc[i, "stockname"] = stockname
    dfnew.loc[i, "start date"] = date1
    dfnew.loc[i, "start price"] = close1
    dfnew.loc[i, "end date"] = datex
    dfnew.loc[i, "end price"] = closex
    dfnew.loc[i, "% changed"] = pricediff

    if pricediff > 0:
        posChgNum += 1
        posChgPerc += pricediff
    else:
        negChgNum += 1
        negChgPerc += pricediff

    i += 1


dfnew2 = dfnew.sort_values(by=['% changed'], ascending=False)
pd.set_option('display.max_rows', None)
print(dfnew2)

if posChgNum > 0:
    posChgAvg = posChgPerc / posChgNum
else:
    posChgAvg = 0
if negChgNum > 0:
    negChgAvg = negChgPerc / negChgNum
else:
    negChgAvg = 0
print("Number of gain: " + str(posChgNum) + ", average gain %: " + f"{posChgAvg:.2f}")
print("Number of loss: " + str(negChgNum) + ", average loss %: " + f"{negChgAvg:.2f}")
print("Total stocks counted: " + str(len(dfnew2.index)))


print("")
filename = input('Enter file name (without extension) to save file, empty input to skip saving file: ')
if filename:
    writefilepath = 'C:/pythonprojects/stocks/' + filename + '.csv'
    dfnew2.to_csv(writefilepath)
  • Line 10 (and line 47), change and input your own stock file name. This is assume you already have download the historical data in your PC. If not, go back to previous post to download historical data by python.
  • Line 24, python program waiting for user to input in console. If using a list of stocks in line 10, simply press enter key for empty input.
  • Line 32, input 2 numbers in console. First one is x days ago, second one is latest day to compare. If using today latest closing price, second input is 1. The second number should be smaller than the first number, but larger than 0.
  • Line 47, change the stock code name for your own. E.g. if stock file name is “AAPL”, simply remove “HK” or remove this line.
  • Line 51, change to your own file path location.
  • Line 104, input file name to save if necessary. Enter empty input to skip saving file and end this python program.

 

The input and result should be similar to the following:

Enter 1 stock code, e.g. 0323, or empty for all stocks: 
Enter day1 and last day to compare, e.g. 90 1: 191 1

    stockname  start date start price    end date end price  % changed
55     HK1171  2022-01-03       16.26  2022-10-12     27.45  68.819188
99     HK3900  2022-01-03       11.88  2022-10-12      14.4  21.212121
41     HK0883  2022-01-03        8.09  2022-10-12      9.58    18.4178
91     HK2883  2022-01-03        6.84  2022-10-12      7.84  14.619883
26     HK0688  2022-01-03       18.42  2022-10-12     20.45   11.02063
49     HK1066  2022-01-03        9.91  2022-10-12     10.58   6.760848
4      HK0168  2022-01-03   70.849998  2022-10-12     70.65  -0.282284
86     HK2388  2022-01-03   25.950001  2022-10-12      25.8  -0.578039
53     HK1109  2022-01-03   32.599998  2022-10-12     30.55  -6.288338
82     HK2343  2022-01-03         3.0  2022-10-12       2.7      -10.0
52     HK1093  2022-01-03        8.47  2022-10-12      7.59  -10.38961
65     HK1818  2022-01-03        6.56  2022-10-12      5.81 -11.432927
19     HK0392  2022-01-03        27.0  2022-10-12     22.85  -15.37037
92     HK2899  2022-01-03        9.28  2022-10-12      7.79 -16.056034
13     HK0322  2022-01-03        15.8  2022-10-12     13.18 -16.582278
24     HK0590  2022-01-03       21.25  2022-10-12     17.58 -17.270588
44     HK0939  2022-01-03        5.44  2022-10-12      4.46 -18.014706
58     HK1208  2022-01-03        2.55  2022-10-12      2.04      -20.0
46     HK1044  2022-01-03   40.400002  2022-10-12      32.3 -20.049509
50     HK1072  2022-01-03       13.92  2022-10-12      10.8 -22.413793
61     HK1378  2022-01-03        8.47  2022-10-12      6.55 -22.668241
33     HK0823  2022-01-03   68.650002  2022-10-12     52.25 -23.889296
20     HK0398  2021-12-31        4.88  2022-10-12      3.63 -25.614754
2      HK0135  2022-01-03        7.41  2022-10-12      5.44 -26.585695
59     HK1211  2021-12-31  266.600006  2022-10-12     195.0 -26.856716
71     HK2020  2022-01-03  114.699997  2022-10-12      82.9 -27.724497
43     HK0921  2022-01-03         9.0  2022-10-12      6.48      -28.0
69     HK2002  2022-01-03        2.28  2022-10-12      1.64 -28.070175
93     HK3311  2022-01-03        9.81  2022-10-12      7.05 -28.134557
1      HK0116  2022-01-03        11.1  2022-10-12      7.96 -28.288288
16     HK0358  2022-01-03        12.7  2022-10-12      9.08 -28.503937
0      HK0038  2022-01-03        4.01  2022-10-12      2.85 -28.927681
6      HK0187  2022-01-03        3.84  2022-10-12      2.71 -29.427083
11     HK0303  2022-01-03   61.650002  2022-10-12      43.2  -29.92701
77     HK2318  2022-01-03       55.75  2022-10-12     38.85 -30.313901
74     HK2238  2022-01-03        7.83  2022-10-12      5.45 -30.395913
79     HK2331  2022-01-03        85.5  2022-10-12      59.1 -30.877193
78     HK2319  2022-01-03   44.849998  2022-10-12      30.2 -32.664434
54     HK1133  2022-01-03        3.82  2022-10-12      2.55 -33.246073
66     HK1882  2022-01-03   21.799999  2022-10-12      14.5 -33.486235
38     HK0867  2022-01-03       13.64  2022-10-12      8.97 -34.237537
56     HK1177  2022-01-03        5.62  2022-10-12      3.69 -34.341637
89     HK2688  2022-01-03  150.800003  2022-10-12      98.0 -35.013264
32     HK0763  2022-01-03        21.5  2022-10-12     13.96 -35.069767
12     HK0316  2022-01-03       201.0  2022-10-12     129.2 -35.721393
98     HK3899  2022-01-03       12.26  2022-10-12      7.85 -35.970636
10     HK0285  2022-01-03        28.1  2022-10-12     17.64 -37.224199
42     HK0914  2022-01-03   39.450001  2022-10-12     24.65 -37.515844
22     HK0489  2022-01-03        6.62  2022-10-12      4.11 -37.915408
36     HK0838  2022-01-03        1.96  2022-10-12       1.2  -38.77551
31     HK0746  2022-01-03        8.59  2022-10-12      5.25 -38.882421
72     HK2128  2022-01-03        11.3  2022-10-12      6.82 -39.646018
18     HK0388  2022-01-03  454.600006  2022-10-12     270.2 -40.563133
39     HK0868  2022-01-03       19.34  2022-10-12      11.4 -41.054809
7      HK0200  2022-01-03        9.47  2022-10-12      5.58 -41.077086
87     HK2600  2022-01-03        4.41  2022-10-12      2.59 -41.269841
48     HK1057  2022-01-03        2.45  2022-10-12      1.43 -41.632653
96     HK3808  2022-01-03        11.9  2022-10-12      6.94 -41.680672
84     HK2380  2021-12-31        5.25  2022-10-12      3.06 -41.714286
94     HK3323  2022-01-03        9.61  2022-10-12       5.6 -41.727367
15     HK0347  2022-01-03         3.5  2022-10-12      2.02 -42.285714
14     HK0323  2021-11-26        2.82  2022-10-12      1.61 -42.907801
3      HK0148  2022-01-03   38.450001  2022-10-12      21.8 -43.302992
67     HK1919  2022-01-03       16.08  2022-10-12      9.04 -43.781095
73     HK2208  2022-01-03       15.86  2022-10-12      8.87  -44.07314
64     HK1812  2022-01-03         3.9  2022-10-12      2.18 -44.102564
28     HK0700  2022-01-03  453.799988  2022-10-12     253.6 -44.116349
90     HK2689  2022-01-03        8.41  2022-10-12      4.66 -44.589774
45     HK0960  2022-01-03   36.099998  2022-10-12     19.88 -44.930745
23     HK0512  2022-01-03        6.55  2022-10-12      3.59  -45.19084
88     HK2678  2022-01-03       10.22  2022-10-12      5.57 -45.499022
83     HK2357  2022-01-03        5.53  2022-10-12       3.0 -45.750452
47     HK1053  2022-01-03         1.3  2022-10-12       0.7 -46.153846
97     HK3888  2022-01-03   34.650002  2022-10-12     18.44  -46.78211
57     HK1193  2022-01-03   44.099998  2022-10-12      23.2 -47.392288
37     HK0855  2022-01-03       11.44  2022-10-12      5.92 -48.251748
17     HK0384  2021-12-31   16.200001  2022-10-12      8.36 -48.395065
95     HK3339  2022-01-03        2.19  2022-10-12      1.12 -48.858447
60     HK1308  2022-01-03       29.65  2022-10-12     14.98 -49.477234
40     HK0880  2022-01-03        5.11  2022-10-12      2.57 -49.706458
29     HK0716  2022-01-03        1.26  2022-10-12      0.62 -50.793651
25     HK0669  2022-01-03  157.600006  2022-10-12      76.9 -51.205586
5      HK0175  2022-01-03   21.299999  2022-10-12     10.18 -52.206571
76     HK2314  2022-01-03        5.44  2022-10-12      2.58 -52.573529
35     HK0836  2022-01-03   27.450001  2022-10-12      12.8 -53.369765
81     HK2338  2022-01-03       15.62  2022-10-12      7.28 -53.393086
8      HK0257  2022-01-03        6.63  2022-10-12      2.99 -54.901961
68     HK1999  2022-01-03       11.54  2022-10-12       5.2 -54.939341
75     HK2313  2022-01-03  138.199997  2022-10-12     61.25 -55.680173
34     HK0826  2022-01-03         4.7  2022-10-12       2.0 -57.446809
51     HK1083  2022-01-03        6.93  2022-10-12      2.94 -57.575758
9      HK0268  2022-01-03        23.9  2022-10-12      9.18 -61.589958
70     HK2018  2022-01-03       30.75  2022-10-12     11.56 -62.406504
63     HK1600  2022-01-03        8.24  2022-10-12       3.0 -63.592233
27     HK0698  2022-01-03       0.255  2022-10-12     0.081 -68.235294
30     HK0732  2022-01-03        3.56  2022-10-12      1.13 -68.258427
62     HK1478  2022-01-03        9.94  2022-10-12      3.02 -69.617706
85     HK2382  2022-01-03       250.0  2022-10-12      73.7     -70.52
80     HK2333  2022-01-03   26.799999  2022-10-12      7.67 -71.380596
100    HK8083  2022-01-03        0.51  2022-10-12     0.097 -80.980392
21     HK0451  2022-01-03       0.249  2022-10-12     0.037 -85.140562
Number of gain: 6, average gain %: 23.48
Number of loss: 95, average loss %: -39.38
Total stocks counted: 101

Enter file name (without extension) to save file, empty input to skip saving file: 

Process finished with exit code 0

 

– Line 1, enter empty input, to use the list of stocks on line 10.

– Line 2, enter input 191 1. This is 191 days ago, and today latest price to compare.

– Line 110, enter empty input to skip saving file. This python program will end here.

– It can be draw conclusion that most Hong Kong stocks are getting lower and lower (hit more than 25-year low).

Bonus Reminder:

Sometimes, investor may wish to find out what stocks go up more, before or after a holiday/event. E.g. 1st trading day after new year – simply run this program with parameters:

First parameter: empty input to use all stocks

Second parameter: x x-1 , where x is the first day and only 1 day after new year. E..g: 150 149

Third parameter:  file name to save, optional. Empty input to skip saving file.

Repeat the above steps for past several years to find out what stocks get high possibility to go up after new year!