1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
| from enum import Enum
import numpy as np import pandas as pd
from Preprocessing import StockDataPreprocessor from Features import FeatureType
class StockStatusCheck(Enum): OK = 0 ISOLATED_NAN = 1 INIT_NAN = 2 ERROR = 3
class StockTracker:
def __init__(self, stock_id): self.stock_id = stock_id self.cum_adj_factor = 1.0 self.last_open = None self.last_high = None self.last_low = None self.last_close = None
def check_price_data(self, row):
assert row["SecuritiesCode"] == self.stock_id, "SecuritiesCode does not match the tracked stock"
status_code = StockStatusCheck.OK
if np.isnan(row["Open"]) and np.isnan(row["High"]) and np.isnan(row["Low"]) and np.isnan(row["Close"]): if self.last_open is None: status_code = StockStatusCheck.INIT_NAN else: row["Open"] = self.last_open row["Close"] = self.last_close row["Low"] = self.last_low row["High"] = self.last_high status_code = StockStatusCheck.ISOLATED_NAN else: date = row["Date"] if np.isnan(row["Open"]): print(f"Warning: OPEN on {date} for {self.stock_id} is NaN") status_code = StockStatusCheck.ERROR if np.isnan(row["High"]): print(f"Warning: HIGH on {date} for {self.stock_id} is NaN") status_code = StockStatusCheck.ERROR if np.isnan(row["Low"]): print(f"Warning: LOW on {date} for {self.stock_id} is NaN") status_code = StockStatusCheck.ERROR if np.isnan(row["Close"]): print(f"Warning: CLOSE on {date} for {self.stock_id} is NaN") status_code = StockStatusCheck.ERROR
if status_code == StockStatusCheck.OK: self.last_open = row["Open"] self.last_high = row["High"] self.last_low = row["Low"] self.last_close = row["Close"]
return row, status_code
def adjust_prices(self, row): row[["Open", "High", "Low", "Close"]] /= self.cum_adj_factor row["Volume"] *= self.cum_adj_factor return row
def update(self, row): row, status_code = self.check_price_data(row) is_okay = True if status_code == StockStatusCheck.INIT_NAN or status_code == StockStatusCheck.ERROR: is_okay = False if np.isnan(row["ExpectedDividend"]): row["ExpectedDividend"] = 0.0 row = self.adjust_prices(row) self.cum_adj_factor *= row["AdjustmentFactor"]
if is_okay: pass
return row, status_code
class StateTracker:
def __init__(self, features): self.stock_ids = [1301, 1332, 1333, 1376, 1377, 1379, 1381, 1407, 1414, 1417, 1419, 1429, 1435, 1515, 1518, 1605, 1662, 1663, 1712, 1716, 1719, 1720, 1721, 1723, 1726, 1762, 1766, 1775, 1780, 1787, 1793, 1799, 1801, 1802, 1803, 1805, 1808, 1810, 1811, 1812, 1813, 1814, 1815, 1820, 1821, 1822, 1833, 1835, 1852, 1860, 1861, 1870, 1871, 1873, 1878, 1879, 1882, 1884, 1885, 1888, 1890, 1893, 1898, 1899, 1911, 1914, 1921, 1925, 1926, 1928, 1929, 1930, 1934, 1938, 1939, 1941, 1942, 1944, 1945, 1946, 1949, 1950, 1951, 1952, 1954, 1959, 1961, 1963, 1965, 1967, 1968, 1969, 1973, 1975, 1976, 1979, 1980, 1981, 1982, 2001, 2002, 2003, 2004, 2009, 2053, 2060, 2108, 2109, 2114, 2117, 2120, 2121, 2124, 2127, 2130, 2146, 2148, 2150, 2153, 2154, 2157, 2158, 2160, 2168, 2170, 2175, 2181, 2183, 2185, 2193, 2198, 2201, 2204, 2206, 2207, 2208, 2209, 2211, 2212, 2217, 2220, 2221, 2222, 2226, 2229, 2264, 2266, 2267, 2268, 2269, 2270, 2281, 2282, 2288, 2292, 2294, 2296, 2301, 2305, 2307, 2309, 2315, 2317, 2325, 2326, 2327, 2329, 2331, 2335, 2337, 2349, 2353, 2359, 2371, 2372, 2374, 2378, 2379, 2384, 2389, 2393, 2395, 2412, 2413, 2418, 2427, 2429, 2432, 2433, 2440, 2445, 2453, 2461, 2462, 2469, 2471, 2475, 2477, 2484, 2489, 2491, 2492, 2497, 2498, 2501, 2502, 2503, 2531, 2533, 2540, 2573, 2579, 2587, 2588, 2590, 2593, 2594, 2602, 2607, 2612, 2613, 2651, 2653, 2659, 2664, 2669, 2670, 2676, 2678, 2681, 2685, 2686, 2692, 2694, 2695, 2698, 2702, 2705, 2715, 2726, 2729, 2730, 2733, 2734, 2737, 2742, 2749, 2751, 2752, 2753, 2760, 2761, 2763, 2767, 2768, 2780, 2782, 2784, 2790, 2791, 2792, 2801, 2802, 2804, 2805, 2806, 2809, 2810, 2811, 2814, 2815, 2819, 2830, 2831, 2871, 2874, 2875, 2882, 2884, 2897, 2899, 2904, 2908, 2910, 2914, 2915, 2918, 2922, 2923, 2925, 2929, 2930, 2931, 3001, 3002, 3003, 3028, 3031, 3034, 3036, 3038, 3040, 3046, 3048, 3050, 3064, 3075, 3076, 3085, 3086, 3087, 3088, 3091, 3092, 3097, 3099, 3101, 3103, 3104, 3105, 3106, 3107, 3110, 3116, 3132, 3134, 3139, 3141, 3148, 3150, 3151, 3153, 3154, 3156, 3157, 3159, 3166, 3167, 3176, 3178, 3179, 3180, 3182, 3183, 3186, 3191, 3193, 3196, 3197, 3198, 3199, 3201, 3221, 3222, 3228, 3231, 3232, 3244, 3245, 3252, 3254, 3264, 3276, 3284, 3288, 3289, 3291, 3302, 3315, 3319, 3328, 3333, 3341, 3349, 3355, 3360, 3361, 3371, 3377, 3382, 3387, 3388, 3391, 3395, 3397, 3401, 3402, 3405, 3407, 3415, 3421, 3431, 3433, 3436, 3443, 3445, 3457, 3458, 3465, 3475, 3539, 3543, 3546, 3547, 3548, 3549, 3553, 3569, 3580, 3591, 3593, 3597, 3608, 3626, 3628, 3632, 3635, 3636, 3648, 3649, 3656, 3657, 3659, 3660, 3662, 3663, 3665, 3668, 3673, 3675, 3676, 3677, 3678, 3679, 3681, 3687, 3688, 3694, 3696, 3697, 3708, 3733, 3738, 3762, 3763, 3765, 3769, 3771, 3772, 3774, 3778, 3784, 3788, 3798, 3800, 3817, 3825, 3834, 3835, 3836, 3837, 3843, 3844, 3853, 3854, 3856, 3857, 3861, 3863, 3865, 3880, 3891, 3900, 3901, 3902, 3903, 3906, 3914, 3915, 3916, 3919, 3921, 3922, 3923, 3925, 3926, 3932, 3934, 3937, 3939, 3941, 3946, 3950, 3951, 3962, 3966, 3969, 4004, 4005, 4008, 4021, 4023, 4025, 4026, 4027, 4028, 4041, 4042, 4043, 4044, 4045, 4046, 4047, 4061, 4062, 4063, 4078, 4080, 4082, 4088, 4091, 4092, 4094, 4095, 4097, 4099, 4100, 4107, 4109, 4112, 4113, 4114, 4116, 4118, 4151, 4182, 4183, 4185, 4186, 4187, 4188, 4189, 4202, 4203, 4204, 4205, 4206, 4208, 4212, 4215, 4216, 4218, 4220, 4221, 4228, 4229, 4235, 4238, 4246, 4272, 4275, 4286, 4290, 4293, 4298, 4301, 4307, 4308, 4310, 4312, 4318, 4323, 4324, 4326, 4327, 4337, 4343, 4344, 4345, 4348, 4350, 4362, 4365, 4368, 4369, 4401, 4403, 4410, 4452, 4461, 4462, 4463, 4464, 4471, 4502, 4503, 4506, 4507, 4516, 4519, 4521, 4523, 4526, 4527, 4528, 4530, 4534, 4536, 4538, 4540, 4541, 4543, 4544, 4547, 4548, 4549, 4550, 4551, 4552, 4553, 4554, 4559, 4563, 4565, 4568, 4569, 4571, 4572, 4574, 4577, 4578, 4579, 4581, 4582, 4584, 4587, 4592, 4593, 4595, 4611, 4612, 4613, 4617, 4619, 4620, 4621, 4626, 4628, 4631, 4633, 4634, 4636, 4641, 4658, 4659, 4661, 4662, 4665, 4666, 4668, 4671, 4674, 4676, 4680, 4681, 4684, 4686, 4687, 4689, 4694, 4699, 4704, 4708, 4709, 4714, 4716, 4718, 4719, 4722, 4725, 4726, 4732, 4733, 4739, 4743, 4745, 4746, 4751, 4755, 4763, 4765, 4767, 4768, 4771, 4772, 4776, 4781, 4792, 4800, 4801, 4809, 4812, 4813, 4816, 4819, 4820, 4825, 4826, 4828, 4832, 4837, 4839, 4847, 4848, 4849, 4901, 4902, 4911, 4912, 4914, 4917, 4919, 4921, 4922, 4923, 4927, 4928, 4951, 4955, 4956, 4958, 4963, 4966, 4967, 4968, 4970, 4971, 4973, 4974, 4975, 4978, 4980, 4985, 4992, 4994, 4996, 4997, 4998, 5008, 5011, 5013, 5015, 5017, 5019, 5020, 5021, 5101, 5105, 5108, 5110, 5121, 5122, 5142, 5161, 5184, 5185, 5186, 5191, 5192, 5195, 5201, 5202, 5208, 5214, 5217, 5218, 5232, 5233, 5261, 5262, 5269, 5273, 5288, 5301, 5302, 5304, 5310, 5331, 5332, 5333, 5334, 5344, 5351, 5352, 5357, 5384, 5388, 5393, 5401, 5406, 5408, 5410, 5411, 5423, 5440, 5444, 5449, 5451, 5463, 5464, 5471, 5480, 5481, 5482, 5486, 5541, 5563, 5602, 5631, 5632, 5659, 5698, 5702, 5703, 5706, 5707, 5711, 5713, 5714, 5715, 5726, 5727, 5741, 5801, 5802, 5803, 5805, 5807, 5809, 5821, 5851, 5857, 5901, 5902, 5909, 5911, 5918, 5929, 5930, 5932, 5933, 5938, 5943, 5945, 5946, 5947, 5949, 5951, 5957, 5959, 5970, 5975, 5976, 5982, 5985, 5988, 5989, 5991, 5992, 5999, 6005, 6013, 6023, 6027, 6028, 6030, 6035, 6036, 6047, 6050, 6055, 6058, 6062, 6067, 6070, 6071, 6073, 6078, 6080, 6082, 6088, 6089, 6094, 6095, 6098, 6099, 6101, 6103, 6104, 6113, 6118, 6125, 6134, 6135, 6136, 6140, 6141, 6143, 6144, 6145, 6146, 6149, 6151, 6157, 6178, 6182, 6183, 6184, 6191, 6194, 6196, 6197, 6199, 6200, 6201, 6222, 6237, 6238, 6240, 6245, 6246, 6247, 6250, 6254, 6257, 6258, 6264, 6266, 6268, 6269, 6272, 6273, 6277, 6278, 6279, 6282, 6284, 6287, 6289, 6293, 6301, 6302, 6305, 6306, 6309, 6310, 6315, 6323, 6324, 6326, 6328, 6330, 6331, 6332, 6333, 6339, 6340, 6345, 6349, 6351, 6357, 6361, 6363, 6364, 6365, 6366, 6367, 6368, 6369, 6370, 6371, 6376, 6378, 6379, 6381, 6382, 6383, 6387, 6395, 6406, 6407, 6409, 6411, 6412, 6413, 6417, 6418, 6419, 6420, 6425, 6430, 6432, 6436, 6440, 6444, 6448, 6454, 6455, 6457, 6458, 6459, 6460, 6462, 6463, 6464, 6465, 6470, 6471, 6472, 6473, 6474, 6479, 6480, 6481, 6482, 6484, 6485, 6486, 6490, 6498, 6501, 6502, 6503, 6504, 6506, 6507, 6508, 6516, 6517, 6532, 6533, 6535, 6538, 6539, 6584, 6586, 6588, 6590, 6592, 6594, 6616, 6617, 6619, 6620, 6622, 6625, 6626, 6627, 6629, 6630, 6632, 6637, 6638, 6640, 6641, 6644, 6645, 6651, 6652, 6668, 6670, 6674, 6676, 6701, 6702, 6703, 6706, 6707, 6718, 6723, 6724, 6727, 6728, 6736, 6737, 6740, 6741, 6742, 6744, 6745, 6750, 6752, 6753, 6754, 6755, 6758, 6762, 6768, 6769, 6770, 6777, 6779, 6787, 6788, 6789, 6794, 6798, 6800, 6804, 6806, 6807, 6809, 6810, 6814, 6815, 6817, 6820, 6823, 6824, 6832, 6834, 6841, 6844, 6845, 6848, 6849, 6850, 6855, 6856, 6857, 6859, 6861, 6866, 6869, 6871, 6875, 6877, 6879, 6881, 6882, 6890, 6902, 6904, 6905, 6908, 6912, 6914, 6915, 6918, 6920, 6923, 6924, 6925, 6929, 6932, 6937, 6938, 6941, 6947, 6951, 6952, 6954, 6955, 6957, 6958, 6960, 6961, 6962, 6963, 6965, 6966, 6967, 6971, 6976, 6981, 6986, 6988, 6994, 6995, 6996, 6997, 6999, 7003, 7004, 7011, 7012, 7013, 7102, 7105, 7148, 7157, 7164, 7167, 7169, 7172, 7173, 7177, 7180, 7181, 7182, 7184, 7186, 7187, 7189, 7191, 7192, 7201, 7202, 7203, 7205, 7211, 7220, 7222, 7224, 7226, 7229, 7231, 7236, 7238, 7239, 7240, 7241, 7242, 7244, 7245, 7246, 7250, 7254, 7259, 7261, 7267, 7269, 7270, 7272, 7276, 7278, 7279, 7280, 7282, 7283, 7287, 7292, 7294, 7296, 7298, 7309, 7313, 7315, 7408, 7412, 7414, 7419, 7420, 7421, 7433, 7438, 7445, 7447, 7451, 7453, 7456, 7458, 7459, 7463, 7466, 7467, 7475, 7476, 7480, 7482, 7483, 7487, 7500, 7504, 7508, 7510, 7512, 7513, 7516, 7518, 7520, 7522, 7532, 7537, 7545, 7550, 7552, 7554, 7564, 7570, 7575, 7581, 7593, 7595, 7596, 7599, 7600, 7605, 7606, 7607, 7609, 7611, 7613, 7616, 7618, 7621, 7628, 7630, 7636, 7637, 7638, 7649, 7701, 7702, 7705, 7715, 7716, 7717, 7718, 7721, 7723, 7725, 7726, 7729, 7730, 7731, 7732, 7733, 7734, 7735, 7739, 7740, 7741, 7744, 7745, 7747, 7749, 7751, 7752, 7762, 7774, 7775, 7777, 7779, 7780, 7814, 7816, 7817, 7818, 7820, 7821, 7823, 7826, 7832, 7839, 7840, 7844, 7846, 7856, 7860, 7864, 7867, 7868, 7874, 7879, 7893, 7905, 7906, 7911, 7912, 7914, 7915, 7917, 7921, 7925, 7936, 7937, 7942, 7943, 7947, 7949, 7951, 7952, 7955, 7956, 7958, 7962, 7965, 7966, 7970, 7972, 7974, 7976, 7979, 7981, 7984, 7987, 7988, 7989, 7990, 7994, 7995, 8001, 8002, 8005, 8008, 8012, 8014, 8015, 8016, 8018, 8020, 8022, 8031, 8032, 8035, 8037, 8038, 8041, 8043, 8050, 8051, 8052, 8053, 8056, 8057, 8058, 8059, 8060, 8061, 8065, 8066, 8068, 8070, 8074, 8075, 8078, 8079, 8081, 8084, 8086, 8088, 8089, 8093, 8095, 8096, 8097, 8098, 8101, 8103, 8111, 8113, 8114, 8117, 8125, 8129, 8130, 8131, 8132, 8133, 8136, 8137, 8140, 8141, 8150, 8151, 8153, 8154, 8155, 8157, 8158, 8159, 8160, 8163, 8165, 8167, 8168, 8173, 8174, 8179, 8182, 8185, 8194, 8198, 8200, 8202, 8203, 8214, 8217, 8218, 8219, 8227, 8233, 8237, 8242, 8244, 8249, 8252, 8253, 8255, 8267, 8273, 8275, 8276, 8278, 8279, 8281, 8282, 8283, 8285, 8289, 8291, 8303, 8304, 8306, 8308, 8309, 8316, 8331, 8334, 8336, 8337, 8338, 8341, 8343, 8344, 8345, 8346, 8354, 8355, 8358, 8359, 8360, 8361, 8362, 8364, 8366, 8367, 8368, 8369, 8370, 8377, 8381, 8382, 8385, 8386, 8387, 8388, 8392, 8393, 8395, 8399, 8410, 8411, 8418, 8424, 8425, 8439, 8473, 8508, 8511, 8515, 8522, 8524, 8527, 8530, 8541, 8544, 8550, 8558, 8566, 8570, 8572, 8584, 8585, 8591, 8593, 8595, 8596, 8600, 8601, 8604, 8609, 8613, 8616, 8622, 8624, 8628, 8630, 8697, 8698, 8699, 8706, 8707, 8708, 8713, 8714, 8715, 8725, 8739, 8750, 8766, 8771, 8772, 8793, 8795, 8798, 8801, 8802, 8803, 8804, 8806, 8818, 8830, 8841, 8842, 8844, 8848, 8850, 8860, 8864, 8869, 8871, 8876, 8877, 8881, 8890, 8892, 8897, 8905, 8909, 8914, 8917, 8920, 8923, 8925, 8928, 8929, 8934, 8935, 8999, 9001, 9003, 9005, 9006, 9007, 9008, 9009, 9010, 9014, 9020, 9021, 9022, 9024, 9025, 9028, 9031, 9033, 9037, 9039, 9041, 9042, 9044, 9045, 9046, 9048, 9052, 9055, 9057, 9058, 9064, 9065, 9066, 9068, 9069, 9070, 9072, 9075, 9076, 9081, 9083, 9086, 9090, 9099, 9101, 9104, 9107, 9110, 9115, 9119, 9142, 9201, 9202, 9232, 9233, 9301, 9302, 9303, 9304, 9305, 9308, 9310, 9319, 9324, 9364, 9368, 9369, 9375, 9381, 9384, 9386, 9401, 9404, 9405, 9409, 9412, 9413, 9414, 9416, 9418, 9422, 9424, 9432, 9433, 9435, 9436, 9438, 9441, 9449, 9467, 9468, 9470, 9474, 9501, 9502, 9503, 9504, 9505, 9506, 9507, 9508, 9509, 9511, 9513, 9517, 9531, 9532, 9533, 9534, 9535, 9536, 9537, 9543, 9551, 9600, 9601, 9602, 9603, 9605, 9612, 9613, 9616, 9619, 9621, 9622, 9627, 9628, 9629, 9631, 9632, 9639, 9640, 9641, 9658, 9661, 9663, 9672, 9678, 9682, 9684, 9687, 9692, 9697, 9699, 9706, 9708, 9715, 9716, 9717, 9719, 9722, 9726, 9728, 9729, 9733, 9735, 9739, 9740, 9742, 9743, 9744, 9746, 9749, 9755, 9757, 9759, 9766, 9769, 9783, 9787, 9788, 9790, 9793, 9795, 9810, 9823, 9824, 9828, 9830, 9831, 9832, 9837, 9842, 9843, 9850, 9856, 9861, 9869, 9873, 9880, 9882, 9887, 9889, 9896, 9900, 9902, 9903, 9906, 9919, 9928, 9932, 9934, 9936, 9945, 9946, 9948, 9955, 9956, 9960, 9962, 9974, 9977, 9979, 9982, 9983, 9984, 9987, 9989, 9990, 9991, 9993, 9994, 9997, 9539, 9519, 3558, 6544, 5757, 1413, 3561, 3978, 3983, 3479, 3964, 3563, 3984, 3480, 3990, 3993, 7809, 3482, 3994, 9260, 6556, 3484, 6653, 8919, 9143, 7198, 3540, 4249, 6235, 7199, 9267, 6565, 6566, 6569, 9270, 6571, 9450, 6572, 7322, 4382, 4384, 4385, 9273, 6580, 9274, 4390, 7030, 7806, 7033, 3491, 3496, 7036, 7326, 3612, 5290, 7327, 9278, 9279, 3498, 4423, 7931, 9434, 4425, 6232, 6564, 7047, 7048, 4431, 4433, 1887, 4434, 4435, 4436, 7060, 7061, 2975, 7065, 1431, 4443, 4931, 4446, 7803, 4599, 7679, 4449, 4448, 4475, 7071, 4477, 4880, 4251, 7683, 3449, 4479, 4480, 4481, 4483, 4478, 4482, 4485, 7685, 2980, 4488, 7082, 7085, 7088, 4490, 7089, 4493, 7094, 7095, 7351, 4499, 4051, 4495, 4053, 4054, 4883, 4056, 1375, 2932, 4058, 4933, 7337, 7339, 7354, 2987, 4934, 6612, 7092, 7944, 4165, 4167, 7358, 4168, 7342, 4169] self.stock_trackers = {} for s_id in self.stock_ids: self.stock_trackers[s_id] = StockTracker(s_id) self.global_features = []
self.local_features = {}
for feature in features:
if feature.feature_type == FeatureType.GLOBAL: self.global_features.append(feature) elif feature.feature_type == FeatureType.LOCAL: for stock_id in self.stock_ids: if stock_id not in self.local_features: self.local_features[stock_id] = [] self.local_features[stock_id].append(feature.copy())
def prepare_data_for_training(self, df):
df = StockDataPreprocessor.preprocess_for_training(df)
train_dfs = []
for stock_id, subdf in df.groupby("SecuritiesCode"): for feature in self.local_features[stock_id]: subdf = feature.add_feature_pandas(subdf) train_dfs.append(subdf)
return pd.concat(train_dfs).sort_index()
def update_single_row(self, row): stock_id = row["SecuritiesCode"] row, status_code = self.stock_trackers[stock_id].update(row) row["StatusCode"] = status_code for feature in self.local_features[stock_id]: row = feature.update_row(row) return row
def online_update_apply(self, prices): return prices.apply(lambda row: self.update_single_row(row), axis=1)
def online_update(self, prices): updated_prices = [] for row_id, row in prices.iterrows(): stock_id = row["SecuritiesCode"] row, status_code = self.stock_trackers[stock_id].update(row) row["StatusCode"] = status_code for feature in self.local_features[stock_id]: row = feature.update_row(row) updated_prices.append(row.to_frame().T) return pd.concat(updated_prices)
def set_priors(self): pass
|