How to use test_close method in autotest

Best Python code snippet using autotest_python

datacontainer.py

Source:datacontainer.py Github

copy

Full Screen

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...

Full Screen

Full Screen

readcode.py

Source:readcode.py Github

copy

Full Screen

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:...

Full Screen

Full Screen

test.is_close.test_close.py

Source:test.is_close.test_close.py Github

copy

Full Screen

...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)...

Full Screen

Full Screen

Automation Testing Tutorials

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.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run autotest automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful