Best Python code snippet using autotest_python
datacontainer.py
Source:datacontainer.py
1import matplotlib2matplotlib.use('Agg')3import csv4import matplotlib.pyplot as plt5import numpy as np6import pandas as pd7import random8import talib9from sklearn import metrics, preprocessing10from talib.abstract import *11class ContainerException(Exception):12 pass13class Container():14 """15 Container class for loading and providing data to the TradingStateModel.16 The container class assumes that data is of the form [num_assets, num_periods, num_asset_features].17 It assumes that close data is of the form [num_assets, num_periods]18 An instance should have members self.train_data, self.test_data, self.train_close, and self.test_close19 """20 def __init__(self):21 pass22 @property23 def num_assets(self):24 return self.train_data.shape[0]25 @property26 def train_length(self):27 return self.train_data.shape[1]28 @property29 def test_length(self):30 return self.test_data.shape[1]31 @property32 def num_asset_features(self):33 return self.train_data.shape[2]34 @property35 def num_flattened_features(self):36 return self.num_assets * self.num_asset_features + self.num_assets37 @staticmethod38 def split(closes, data, split_level):39 train_close = closes[:, 0:split_level]40 test_close = closes[:, split_level:]41 train_data = data[:, 0:split_level, :]42 test_data = data[:, split_level:, :]43 return train_data, test_data, train_close, test_close44 def get_data(self, train=True):45 if train:46 return self.train_data47 else:48 return self.test_data49 def get_all_prices(self, train=True):50 if train:51 return self.train_close 52 else:53 return self.test_close 54 def initial_time(self, train=True, episode_length=None, history_length=0):55 if train:56 if history_length > self.train_length:57 raise ValueError('History length should be less than or equal to length of training set')58 init_time = np.random.randint(low=history_length,59 high=self.train_length - episode_length)60 else:61 if history_length > self.test_length:62 raise ValueError('History length should be less than or equal to length of test set')63 init_time = history_length64 end_time = init_time + episode_length65 return init_time, end_time 66 def get_asset_features(self, train, time, history_length=None):67 data = self.get_data(train=train)68 if history_length is None:69 return data[:, time, :] # [num_assets, num_asset_features]70 else:71 return data[:, time-history_length+1:time+1, :] # [num_assets, history_length, num_asset_features]72 def get_prices(self, train, time, history_length=None):73 prices = self.get_all_prices(train=train)74 if history_length is None:75 return prices[:, time] # [num_assets]76 else:77 return prices[:, time-history_length+1:time+1] # [num_assets, history_length]78 def get_price_returns(self, train, time):79 curr_prices = self.get_prices(train=train, time=time)80 old_prices = self.get_prices(train=train, time=time-1)81 returns = (curr_prices - old_prices) / old_prices82 return returns83 def plot_prices(self, train):84 prices = self.get_all_prices(train=train)85 for ind in range(prices.shape[0]):86 plt.plot(prices[ind, :])87 plt.show()88 def plot_returns(self, train):89 returns = self.get_data(train=train)[:, :, 0]90 for ind in range(returns.shape[0]):91 plt.plot(returns[ind, :])92 plt.show()93 def featurize(self, closes, conf):94 """95 param closes is of the form [num_assets, num_periods]96 returns array of form [num_assets, num_periods, num_features]97 The first feature should always be the returns X_t^i = change(price_asset_(t-1 to t))/price_asset_(t-1)98 """99 num_assets = closes.shape[0]100 num_periods = closes.shape[1]101 features = []102 if conf['returns'] is True:103 diff = np.diff(closes)104 returns = diff / closes[:, 0:num_periods-1]105 returns = np.concatenate((np.zeros((num_assets, 1)), returns),106 axis=1) # [num_assets, num_periods]107 #returns = np.log(1 + returns)108 features.append(returns)109 if len(features) == 0:110 raise ValueError('No features')111 elif len(features) == 1:112 feature = features[0]113 return np.expand_dims(feature, axis=2)114 else:115 return np.stack(features, axis=2)116class TestContainer(Container):117 def __init__(self, shape='sine', num_assets=3, num_samples=200, train_split=0.7):118 super().__init__()119 if shape is 'sine':120 closes = [np.sin(2*np.pi*np.linspace(start=0, # [num_assets, num_samples]121 stop=8,122 num=num_samples)+(5*np.pi/8)*asset) for asset in range(num_assets)]123 closes = np.array(closes)124 closes = closes+5125 # closes = np.concatenate((np.ones((1, num_samples)), closes),126 # axis=0)127 data = self.featurize(closes,128 conf={'returns': True}) # [num_assets, num_samples, num_asset_features]129 split_level = int(num_samples * train_split)130 self.train_data = data[:, 0:split_level, :]131 self.train_close = closes[:, 0:split_level] 132 self.test_data = data[:, split_level:, :]133 self.test_close = closes[:, split_level:]134class EasyContainer(Container):135 def __init__(self, num_samples=200, train_split=0.7):136 super().__init__()137 closes = [10+np.arange(1, num_samples+1)*10, np.linspace(10000, 0, num_samples)+0.01, np.linspace(1000, 0, num_samples)+0.01]138 closes = np.array(closes)139 print("Closes:", closes) 140 data = self.featurize(closes,141 conf={'returns': True})142 split_level = int(num_samples * train_split)143 self.train_data, self.test_data, self.train_close, self.test_close = \144 Container.split(closes=closes, data=data, split_level=split_level)145class BitcoinTestContainer(Container):146 def __init__(self, csv_file_name=None, train_split=0.7):147 assert csv_file_name is not None148 file = open(csv_file_name)149 reader = csv.DictReader(file)150 self.data = {151 'open': [],152 'high': [],153 'low': [],154 'close': [],155 'volume': []156 }157 times = []158 for line in reader:159 for key in self.data:160 self.data[key].append(float(line[key]))161 times.append(int(line['time']))162 self.df = pd.DataFrame(data=self.data,163 index=times)164 split_level = int(len(times) * train_split)165 self.train_df = self.df.iloc[:split_level, :]166 self.test_df = self.df.iloc[split_level:, :]167 self.process(train_df=self.train_df,168 test_df=self.test_df)169 def featurize(self, df):170 close = df['close'].values171 diff = np.diff(close)172 diff = np.insert(diff, 0, 0)173 sma15 = SMA(df, timeperiod=15)174 sma60 = SMA(df, timeperiod=60)175 rsi = RSI(df, timeperiod=14)176 atr = ATR(df, timeperiod=14)177 data = np.column_stack((diff, sma15, close-sma15, sma15-sma60, rsi, atr))178 data = np.nan_to_num(data) 179 return np.array(data), np.expand_dims(close, 1) # [num_periods, num_features], [num_periods, 1]180 def process(self, train_df, test_df):181 self.pre_train_data, self.pre_train_close = self.featurize(train_df)182 self.pre_test_data, self.pre_test_close = self.featurize(test_df)183 self.feature_scaler = preprocessing.MinMaxScaler()184 self.train_data = self.feature_scaler.fit_transform(self.pre_train_data) # [num_periods, features]185 self.test_data = self.feature_scaler.transform(self.pre_test_data)186 self.pre_train_data, self.pre_test_data, self.train_data, self.test_data = \187 [np.array([arr]) for arr in [self.pre_train_data, self.pre_test_data, self.train_data, self.test_data]]188 # [1, num_periods, num_features]189 self.price_scaler = preprocessing.MinMaxScaler()190 self.train_close = self.feature_scaler.fit_transform(self.pre_train_close)191 self.test_close = self.feature_scaler.transform(self.pre_test_close)192 self.pre_train_close, self.pre_test_close, self.train_close, self.test_close = \193 [np.array([arr]) for arr in [self.pre_train_close, self.pre_test_close, self.train_close, self.test_close]]194 self.train_close = self.pre_train_close195 self.test_close = self.pre_test_close196 # [1, num_periods, 1]197class DataContainer(Container):198 def __init__(self, csv_file_name=None, hdf_file_name=None):199 if hdf_file_name is not None:200 key = 'train'201 pd_data = pd.read_hdf(hdf_file_name, key=key)202 asset_names = list(pd_data.columns.levels[0])203 train_closing_prices = [pd_data[asset_name, 'close'].values for asset_name in asset_names]204 key = 'test'205 pd_data = pd.read_hdf(hdf_file_name, key=key)206 asset_names = list(pd_data.columns.levels[0])207 test_closing_prices = [pd_data[asset_name, 'close'].values for asset_name in asset_names]208 self.train_close = np.array(train_closing_prices)209 self.test_close = np.array(test_closing_prices)210 self.sma15_train, self.sma15_test = [talib.SMA(arr, timeperiod=15) for arr in 211 [self.train_close, self.test_close]]212 self.train_data, self.test_data = [self.featurize(closes, {'returns': True}) for closes in213 [self.sma15_train, self.sma15_test]]214 self.train_data, self.test_data = [np.nan_to_num(arr) for arr in...
readcode.py
Source:readcode.py
1import urllib.parse2from selenium import webdriver3import time4import pandas as pd5import copy6from datetime import datetime, timedelta7import FinanceDataReader as fdr8from matplotlib import pyplot as plt9import numpy as np10MARKET_CODE_DICT = {11 'kospi': 'stockMkt',12 'kosdaq': 'kosdaqMkt',13 'konex': 'konexMkt'14}15DOWNLOAD_URL = 'kind.krx.co.kr/corpgeneral/corpList.do'16def download_stock_codes(market=None, delisted=False):17 params = {'method': 'download'}18 if market.lower() in MARKET_CODE_DICT:19 params['marketType'] = MARKET_CODE_DICT[market]20 if not delisted:21 params['searchType'] = 1322 params_string = urllib.parse.urlencode(params)23 request_url = urllib.parse.urlunsplit(['http', DOWNLOAD_URL, '', params_string, ''])24 df = pd.read_html(request_url, header=0)[0]25 df.ì¢
목ì½ë = df.ì¢
목ì½ë.map('{:06d}'.format)26 return df27def stock_li_crawling(stock):28 all_stock_li = []29 cnt = 030 browser = webdriver.Chrome('C:/Users/KWON/Desktop/TeamProject/data/chromedriver.exe')31 for code in stock['ì¢
목ì½ë']:32 url = 'https://navercomp.wisereport.co.kr/v2/company/c1030001.aspx?cmp_cd=' + code + '&cn='33 browser.get(url)34 browser.implicitly_wait(5)35 a = browser.find_elements_by_class_name('lvl1')36 b = [i.find_elements_by_tag_name('td') for i in a]37 stock_li = [x.text for i in range(len(b)) for x in b[i] if b[i][0].text == 'í¼ì¹ê¸° ì´í¬ê´ì´ìµ' if x.text != 'í¼ì¹ê¸° ì´í¬ê´ì´ìµ']38 all_stock_li.append([code, stock_li])39 cnt += 140 if cnt % 50 == 0:41 time.sleep(1)42 browser.close()43 return all_stock_li44def re_list(stock_data):45 a = copy.deepcopy(stock_data)46 for i in range(len(a)):47 del a[i][1][-3::2]48 a = pd.DataFrame([(i[1]) for i in a], index=[i[0] for i in a])49 a.columns=[['2014','2015','2016','2017','2018','ì ë
ëë¹']]50 return a51def bluechip(data):52 cnt = 053 dellist = []54 for x in range(len(data)):55 for y in data.iloc[x,:-1]:56 if y == None:57 dellist.append(data.iloc[x].name)58 break59 elif (y == ' ') | ('-' in y):60 dellist.append(data.iloc[x].name)61 cnt += 162 break63 result_data = data.drop(dellist)64 print(cnt,' ê° ì¢
목 ìì ë¨')65 return result_data66def spell_check(data):67 a = copy.deepcopy(data)68 for x in range(len(a)):69 for i,y in enumerate(a.iloc[x]):70 if ',' in y:71 a.iloc[x,i] = y.replace(',','')72 return a73def surplus(data, y_cnt, r_rate):74 dellist = []75 for i in range(len(data)):76 cnt = 377 for a in data.iloc[i,5:5-y_cnt:-1]:78 if a / data.iloc[i,6-cnt] * 100 - 100 <= r_rate:79 dellist.append(data.iloc[i].name)80 break81 else:82 cnt += 183 continue84 print(len(dellist),' ê° ì¢
ëª©ì´ ìì ë©ëë¤.')85 result_data = data.drop(dellist)86 return result_data87# ì¤í ìì88## ì¢
목ì½ë ì¶ì¶89kospi_code = download_stock_codes('kospi')90kospi_code.head()91kospi_code_list = kospi_code[:][['íì¬ëª
','ì¢
목ì½ë']]92kosdaq_code = download_stock_codes('kosdaq')93kosdaq_code.head()94kosdaq_code_list = kosdaq_code[:][['íì¬ëª
','ì¢
목ì½ë']]95## ë
ëë³ ì´ í¬ê´ì´ìµ ì¤í¬ëí96kospi_stock_list = stock_li_crawling(kospi_code_list) # kospi ì¢
목ì ë°ë¥¸ ì´í¬ê´ì´ìµì¶ì¶97kosdaq_stock_list = stock_li_crawling(kosdaq_code_list) # kosdac ì¢
목ì ë°ë¥¸ ì´í¬ê´ì´ìµì¶ì¶98len(kospi_stock_list)99len(kosdaq_stock_list)100## ë°ì´í° ì ì 101kospi_stock_data = re_list(kospi_stock_list)102kosdaq_stock_data = re_list(kosdaq_stock_list)103## ì°ë주 ëª©ë¡ ì ë³, ì ì기ì
빼기104kospi_blue = bluechip(kospi_stock_data)105kosdaq_blue = bluechip(kosdaq_stock_data)106## ë°ì´í°ë¤ì ì«ìíì¼ë¡ ì ì í, ì±ì¥í ì°ë주 ì ë³107s_kospi = spell_check(kospi_blue)108s_kosdaq = spell_check(kosdaq_blue)109s_kospi2 = s_kospi.iloc[:,:5].astype('float')110s_kosdaq2 = s_kosdaq.iloc[:,:5].astype('float')111s_kospi2.iloc[0][4] - s_kospi2.iloc[0][3] <= 0112s_kosdaq2113### 3ë
ê° íìì ì±ì¥, íìí ì±ì¥ë¥ 50% ì´ì114kospi_3_sp = surplus(s_kospi2,3,50)115kosdaq_3_sp = surplus(s_kosdaq2,3,50)116len(kospi_3_sp)117len(kosdaq_3_sp)118kospi_3_sp.index119kosdaq_3_sp.index120### 4ë
ê° íìì ì±ì¥, íìí ì±ì¥ë¥ 50% ì´ì121kospi_4_sp = surplus(s_kospi2,4,50)122kosdaq_4_sp = surplus(s_kosdaq2,4,50)123len(kospi_4_sp)124len(kosdaq_4_sp)125## ì¬ë¦¬ì ì ì êµ¬ê° ê·¸ëí íì¸126kospi_3_sp.index127kosdaq_3_sp.index128kospi_4_sp.index129kosdaq_4_sp.index130code_name = '000060'131test = fdr.DataReader(code_name,'2016')132test_close = test['Close'][:-1]133max_point = test_close[test_close == max(test_close)] # ìµëì , indexë ë ì§134l_point = test_close[test_close == min(test_close)] # ìµì ì 135for i in range(len(test_close)):136 if test_close.index[i] < l_point.index[0]:137 continue138 elif test_close.index[i] == l_point.index[0]:139 v_gradient = -1; sw = 0; c_point = l_point[0]140 continue141 if (sw == 0) & (c_point < test_close[i]):142 sw = 1143 elif (sw == 1) & (c_point > test_close[i]):144 sw = 0145 tmp = (c_point-l_point[0]) / ((test_close.index[i-1]-l_point.index[0]).days)146 if v_gradient == -1:147 v_gradient = tmp148 min_point = test_close[test_close==c_point]149 elif (v_gradient > 0) & (v_gradient >= tmp):150 v_gradient = tmp151 min_point = test_close[test_close==c_point]152 else:153 pass154 c_point = test_close[i]155v_gradient # ì§ì§ì 기ì¸ê¸°156min_point # ì§ì§ì ì í ì index = ë ì§, ê° = 주ê°157l_point # ì§ì§ì ì ìµ ì ì index = ë ì§, ê° = 주ê°158sw # ì¤ìì¹ 0 ì¼ê²½ì° ì£¼ê° ìì¹, 1 ì¼ ê²½ì° ì£¼ê° íë½159# ê·¸ëí 그리기160start_p = l_point[0] - ((l_point.index[0]-test_close[:1].index[0]).days) * v_gradient # ì§ì§ì ììì ì¤ì 161end_p = ((test_close[-1:].index[0]-l_point.index[0]).days) * v_gradient + l_point[0] # ì§ì§ì ëì ì¤ì 162x_sup = [test_close[:1].index[0], test_close[-1:].index[0]] # ì§ì§ì xì¶163y_sup = [start_p, end_p] # ì§ì§ì yì¶164max_plt = [max_point[0] for i in range(len(test_close.index))] # ìµê³ ì ì§ì ì¤ì 165start_30_sup = round(np.percentile(np.arange(start_p ,max_point[0]),30))166end_30_sup = round(np.percentile(np.arange(end_p ,max_point[0]),30))167y_30_sup = [start_30_sup, end_30_sup]168test_close.plot() # ì£¼ê° ê·¸ëí169plt.plot(x_sup,y_sup) # ì§ì§ì ê·¸ëí170plt.plot(x_sup,y_30_sup) # ì§ì§ì ê³¼ ìµê³ ì ì§ì ì¬ì´ì ë°±ë¶ì¨ 30% ì§ì , ì´ ìì ê³¼ ì§ì§ì ì¬ì´ê° ì¬ë¦¬ì ì ì 구ê°171plt.plot(test_close.index,max_plt) # ìµê³ ì ì§ì ê·¸ëí172# ë´ì¼173## ê·¸ëì 매ì íì´ë°ì¸ê°? (back_test를 ìí ê¸°ê° ì¤ì )174test_date = datetime(2018,12,3)175test_data = test_close[test_close.index == test_date]176com_low = ((test_data.index[0]-l_point.index[0]).days) * v_gradient + l_point[0] # í´ë¹ ë ì§ì ìµì ê° ì§ì§ì 구ê°177com_30 = ((test_data.index[0]-l_point.index[0]).days) * gradient_30_sup + l_30_sup # í´ë¹ ë ì§ì ë°±ë¶ì¨ 30% 구ê°178### ì¬ë¦¬ì ì ì 구ê°ì ìì ê²½ì° ë§¤ìíë¤. ì¤ì ììë ë°ì´í°ë¥¼ 모ì ì£¼ê° ê·¸ëí를 ë³´ë©° todayë¡ ì¤íí´ë³¼ê².179if (test_data[0] >= com_low) & (test_data[0] <= com_30):180 print('í´ë¹ ì¢
목 ì½ë : ',code_name,' ì¢
목 매ì ì¶ì²')181else:...
test.is_close.test_close.py
Source:test.is_close.test_close.py
...20 return (abs(a-b)<eps).all()21 if isinstance(a, (Iterable,Generator)) or isinstance(b, (Iterable,Generator)):22 return is_close(np.array(a), np.array(b), eps=eps)23 return abs(a-b)<eps24def test_close(a,b,eps=1e-5):25 """26 "`test` that `a` is within `eps` of `b`"27 purpose:28 1. We want unified names like `test_close`29 2. We often do `test(a, b, partial(is_close, eps=...), "close")`30 3. we want to be lazy by just typing `test_close(a, b, eps=...)`31 4. so we use 3 to wrap 232 """33 test(a,b,partial(is_close,eps=eps),'close')34test_close(1,1.001,eps=1e-2)35test_fail(lambda: test_close(1,1.001))36test_close([-0.001,1.001], [0.,1.], eps=1e-2)37test_close(np.array([-0.001,1.001]), np.array([0.,1.]), eps=1e-2)...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!