Best Python code snippet using avocado_python
RelativePerformance.py
Source:RelativePerformance.py
1#!/usr/bin/env python2# coding: utf-83# In[24]:4import gc5import os6import sys7module_path = os.path.abspath(os.path.join('../..'))8if module_path not in sys.path:9 sys.path.append(module_path)10import numpy as np11import pandas as pd12import vectorbt as vbt13from numba import njit14from math import e15from vectorbt.generic.nb import diff_nb16from lib.utils import directory_to_data_frame_list, ExtendedPortfolio, create_windows, get_best_pairs17from lib.utils import is_notebook18# In[ ]:19current_dir = os.path.dirname(os.path.realpath("__file__"))20directory = "/home/ec2-user/data/long/"21ohlcv_series_list = directory_to_data_frame_list(directory)22# concatenamos los dfs23names = list(map(lambda t: t[0], ohlcv_series_list))24dfs = list(map(lambda t: t[1].get(["Close", "Volume"]), ohlcv_series_list))25ohlc_dict = {26#'Open':'first',27#'High':'max',28#'Low':'min',29'Close': 'last',30'Volume': 'sum'31}32# Resampleamos la información a candles más chicas para reducir la memora necesaria33# además solo agarramos close y volumne34for i in range(len(dfs)):35 dfs[i] = dfs[i].resample('5T', closed='left', label='left').apply(ohlc_dict)36ov_df = pd.concat(dfs, axis=1, keys=names)37# borramos las filas que tengan nan(parece que algunos pueden estar desalineados)38ov_df.dropna(inplace=True)39ov_df.columns.set_names(["symbol", "value"], inplace=True)40del ohlcv_series_list, names, dfs41ov_df.head()42# In[ ]:43figure, windows = create_windows(ohlc=ov_df, n=10, window_len=0.6, right_set_len=0.3*0.9)44(in_windows, _), (out_windows, _) = windows45del _46print("Done creating windows")47# In[ ]:48if is_notebook():49 figure.show()50del figure, ov_df51# In[ ]:52portfolio_kwargs = dict(53 direction='longonly',54 freq='m',55)56# creamos el indicador para el lr y otro para el wlr57# lo hago por separado para poder calcular el mlr58# con data de varios activos #del mercado59# y luego solo utiliza lr con los que me interesa60@njit61def lr_nb(close):62 c_log = np.log(close)63 return diff_nb(c_log)64LR = vbt.IndicatorFactory(65 input_names=['close'],66 output_names=['lr']67).from_apply_func(lr_nb, use_ray=True)68@njit69def wlr_nb(volume, lr):70 mkt_vol = volume.sum(axis=1)71 mkt_ratio = (volume.T / mkt_vol).T72 return lr * mkt_ratio73WLR = vbt.IndicatorFactory(74 input_names=['volume', 'lr'],75 output_names=['wlr']76).from_apply_func(wlr_nb, use_ray=True)77 #creamos el indicador para las bandas78@njit79def mkt_band_nb(mkt_lr, upper_filter, lower_filter):80 filtered = np.where(mkt_lr >= upper_filter, mkt_lr, np.nan)81 filtered = np.where(mkt_lr <= -lower_filter, mkt_lr, filtered)82 return filtered83MKT_BANDS = vbt.IndicatorFactory(84 input_names=['mkt_lr'],85 param_names=['upper_filter', 'lower_filter'],86 output_names=['filtered']87).from_apply_func(mkt_band_nb, use_ray=True)88# In[ ]:89# lr = log_return90# wlr = weighted log_return = lr * (Vi / Vmercado)91# mkt_lr = sum(wlr)92in_close = in_windows.xs('Close', level='value', axis=1)93in_volume = in_windows.xs('Volume', level='value', axis=1)94lr_ind = LR.run(in_close)95wlr_ind = WLR.run(in_volume, lr_ind.lr)96mkt_lr = wlr_ind.wlr.sum(axis=1, level="split_idx", skipna=False)97print("Done calculating mkt_lr")98del in_volume, in_windows # esto no se usa más99lr_ind.lr.head()100# In[ ]:101mkt_lr.head()102# In[ ]:103if is_notebook():104 # Grafico un resultadoo arbitrario selecionando filtros arbitrarios para ver como ejemplo el funcionamiento de la estategia105 split_index = 5106 _mkt_lr_arb = mkt_lr[split_index] # agarro el mkt_lr de algúna ventana107 lr_ada = lr_ind.lr[(split_index, "ADA")] # agarro el lr de ADA en esa ventana108 # borramos el mkt cuando está entre 0.0005 y - 0.0005109 filtered = np.where(_mkt_lr_arb >= 0.0005, _mkt_lr_arb, np.nan)110 filtered = np.where(_mkt_lr_arb <= -0.0005, _mkt_lr_arb, filtered)111 fig = pd.DataFrame({112 "lr_ada" : lr_ada,113 "mkt_lr": _mkt_lr_arb,114 "mkt_lr_filtered" : filtered115 }).vbt.plot()116 pd.DataFrame({117 "entries": np.where(filtered >= lr_ada, _mkt_lr_arb, np.nan), # compramos cuando el mercado está por encima de ada118 "exits": np.where(filtered <= lr_ada, _mkt_lr_arb, np.nan)119 }).vbt.scatterplot(fig=fig).show()120 del _mkt_lr_arb, lr_ada, filtered, fig121 gc.collect()122# In[ ]:123# Acá filtramos los thresholds del mkt_lr a partir del cual compramos o vendemos.124upper_fltr = np.linspace(0.00001, 0.003, 50, endpoint=False)125lower_fltr = np.linspace(0.00001, 0.005, 50, endpoint=False)126mkt_bands_ind = MKT_BANDS.run(mkt_lr=mkt_lr, upper_filter=upper_fltr , lower_filter=lower_fltr,127 per_column=False,128 param_product=True,129 short_name="mkt")130del upper_fltr, lower_fltr, mkt_lr131gc.collect()132print("Done calculating mkt_bands")133# In[ ]:134# Ya generamos todos los datos necesarios, ahora vamos a correr todas las simulaciones para cada assets que nos135# interesa testear136# para que no muera por memoria a la mitad y perder todo lo porcesado hasta el momento, me aseguro de que todas137# las keys existan en el df138test_asset_list = ["ADA", "BTC"]139assert( set(test_asset_list).issubset(in_close.columns.get_level_values(level="symbol").unique()))140# In[ ]:141# Recolectamos el close y el lr de cada uno para poder borrar de memoria el df grande de todos los close y los lrs que no usamos142# puesto que close y lr son varias veces más grandes que el lr y close individual143_lrs = {}144_close = {}145for asset in test_asset_list:146 _lrs[asset] = lr_ind.lr.xs(asset, level='symbol', axis=1)147 _close[asset] = in_close.xs(asset, level='symbol', axis=1)148 print(f"Done separating close and lrs for {asset}")149del in_close, lr_ind150in_close = _close151in_lrs = _lrs152gc.collect()153# In[ ]:154# corremos la simulación para cada asset155def dropnan(s):156 return s[~np.isnan(s)]157in_best_fltr_pairs = {}158params_names = mkt_bands_ind.level_names159for asset in test_asset_list:160 lr = in_lrs[asset]161 close = in_close[asset]162 entries = mkt_bands_ind.filtered_above(lr, crossover=True)163 exits = mkt_bands_ind.filtered_below(lr, crossover=True)164 del lr, in_lrs[asset]165 gc.collect()166 print(f"Running optimizing for {asset}")167 port = ExtendedPortfolio.from_signals(close, entries, exits, **portfolio_kwargs, max_logs=0)168 del entries, exits, close, in_close[asset]169 gc.collect()170 print(f"Done optimizing {asset}")171 172 # buscamos la mejor combinación de filtros173 in_best_fltr_pairs[asset] = get_best_pairs(port.expected_log_returns(), *params_names)174 # ploteamos la performace de todas las combinanciones175 if is_notebook():176 elr_volume = dropnan(port.expected_log_returns()).vbt.volume(title=f"{asset}'s Expected Log Return").show()177 sharpe_volume = dropnan(port.sharpe_ratio()).vbt.volume(title=f"{asset}'s Sharpe Ratio").show()178 del port179 gc.collect()180 print(f"Done plotting {asset}")181del mkt_bands_ind182gc.collect()183# In[ ]:184# acá arranca la parte de correr las simulaciones con los datos del out y los parámetros ya optimizados185out_close = out_windows.xs('Close', level='value', axis=1)186out_volume = out_windows.xs('Volume', level='value', axis=1)187lr_ind = LR.run(out_close)188wlr_ind = WLR.run(out_volume, lr_ind.lr)189mkt_lr = wlr_ind.wlr.sum(axis=1, level="split_idx", skipna=False)190_lrs = {}191_close = {}192for asset in test_asset_list:193 _lrs[asset] = lr_ind.lr.xs(asset, level='symbol', axis=1)194 _close[asset] = out_close.xs(asset, level='symbol', axis=1)195 print(f"Done separating close and lrs for {asset}")196del out_close, lr_ind, out_windows, wlr_ind197out_close = _close198out_lrs = _lrs199gc.collect()200for asset in test_asset_list:201 # para cada activo de los que me interesa tradear simulo el resultado de ser corrido con los parámetros optimizados202 in_best_pairs = np.array(in_best_fltr_pairs[asset])203 upper_fltr = in_best_pairs[:,0]204 lower_fltr = in_best_pairs[:,1]205 mkt_bands_ind = MKT_BANDS.run(mkt_lr=mkt_lr, upper_filter=upper_fltr , lower_filter=lower_fltr,206 per_column=True,207 param_product=False,208 short_name="mkt")209 lr = out_lrs[asset]210 close = out_close[asset]211 entries = mkt_bands_ind.filtered_above(lr, crossover=True)212 exits = mkt_bands_ind.filtered_below(lr, crossover=True)213 del lr, out_lrs[asset], mkt_bands_ind214 port = ExtendedPortfolio.from_signals(close, entries, exits, **portfolio_kwargs, max_logs=0)215 exp_plot = port.expected_log_returns().vbt.plot(title=f"{asset}'s Expected Log Return")216 sharpe_plot = port.sharpe_ratio().vbt.plot(title=f"{asset}'s Sharpe ratio")217 if is_notebook():218 exp_plot.show()219 sharpe_plot.show()220 else:221 exp_plot.write_html(f"{current_dir}/{asset}_simulation_exp_log_ret.html")222 sharpe_plot.write_html(f"{current_dir}/{asset}_simulation_sharpe-ratio.html")223 print(f"Done simulating {asset}")224# In[ ]:225# un pequeño test para asegurarnos que todas las cuentas den226_py = pd.DataFrame({227 'Close': [1,e,e**2],228 'Volume': [1,2,1]229})230_thon = pd.DataFrame({231 'Close': [e**2,e,1],232 'Volume': [1,4,10]233})234_test_df = pd.concat([_py,_thon], axis=1, keys=["Py", "Thon"])235_test_df.columns.set_names(["asset", "value"], inplace=True)236close = _test_df.xs('Close', level='value', axis=1)237volume = _test_df.xs('Volume', level='value', axis=1)238_test_lrInd = LR.run(close)239_test_wlrInd = WLR.run(volume, _test_lrInd.lr)240exp_py_lr = np.array([np.nan, 1, 1])241exp_thon_lr = np.array([np.nan, -1, -1])242assert (np.allclose(exp_py_lr, _test_lrInd.lr["Py"], equal_nan=True))243assert (np.allclose(exp_thon_lr, _test_lrInd.lr["Thon"], equal_nan=True))244exp_py_vr = np.array([0.5, 1/3, 1/11])245exp_thon_vr = np.array([0.5, 2/3, 10/11])246exp_py_wlr = exp_py_lr * exp_py_vr247exp_thon_wlr = exp_thon_lr * exp_thon_vr248assert (np.allclose(exp_py_wlr, _test_wlrInd.wlr["Py"], equal_nan=True))249assert (np.allclose(exp_thon_wlr, _test_wlrInd.wlr["Thon"], equal_nan=True))250# falta testear el cálculo de mkt_lr251_test_mkt_lr = _test_wlrInd.wlr.sum(axis=1, skipna=False)252exp_mkt_lr = exp_py_wlr + exp_thon_wlr...
test_plugin.py
Source:test_plugin.py
1#2# Copyright (c) 2019 One Identity3#4# Permission is hereby granted, free of charge, to any person obtaining a copy5# of this software and associated documentation files (the "Software"), to6# deal in the Software without restriction, including without limitation the7# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or8# sell copies of the Software, and to permit persons to whom the Software is9# furnished to do so, subject to the following conditions:10#11# The above copyright notice and this permission notice shall be included in12# all copies or substantial portions of the Software.13#14# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR15# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,16# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE17# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER18# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING19# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS20# IN THE SOFTWARE.21#22from copy import deepcopy23import pytest24from textwrap import dedent25import unittest.mock26from ..plugin import SafeguardPlugin27from ..safeguard import SafeguardException28from safeguard.sessions.plugin_impl.test_utils.plugin import (29 assert_plugin_hook_result,30 check_that_data_is_serializable,31 minimal_parameters,32 update_cookies,33)34def test_checkout_password_with_gateway_credentials(gateway_config, safeguard_lock, generate_params):35 plugin = SafeguardPlugin(gateway_config)36 params = generate_params()37 checkout_result = plugin.get_password_list(**deepcopy(params))38 check_that_data_is_serializable(checkout_result)39 update_cookies(params, checkout_result)40 plugin.authentication_completed(**minimal_parameters(params))41 plugin.session_ended(**minimal_parameters(params))42 checkout_result_cookie = checkout_result["cookie"]43 assert "access_request_id" in checkout_result_cookie44 assert "access_token" in checkout_result_cookie45 assert checkout_result["passwords"] # not None and has at least 1 element46def test_checkout_password_with_explicit_credentials(explicit_config, safeguard_lock, generate_params):47 plugin = SafeguardPlugin(explicit_config)48 params = generate_params()49 checkout_result = plugin.get_password_list(**deepcopy(params))50 check_that_data_is_serializable(checkout_result)51 update_cookies(params, checkout_result)52 plugin.authentication_completed(**minimal_parameters(params))53 plugin.session_ended(**minimal_parameters(params))54 checkout_result_cookie = checkout_result["cookie"]55 assert "access_request_id" in checkout_result_cookie56 assert "access_token" in checkout_result_cookie57 assert checkout_result["passwords"] # not None and has at least 1 element58def test_checkout_password_with_token(token_config, safeguard_lock, safeguard_client, generate_params):59 plugin = SafeguardPlugin(token_config)60 safeguard_client.authenticate()61 params = generate_params(session_cookie={"token": safeguard_client.access_token})62 checkout_result = plugin.get_password_list(**deepcopy(params))63 check_that_data_is_serializable(checkout_result)64 update_cookies(params, checkout_result)65 plugin.authentication_completed(**minimal_parameters(params))66 plugin.session_ended(**minimal_parameters(params))67 checkout_result_cookie = checkout_result["cookie"]68 assert "access_request_id" in checkout_result_cookie69 assert "access_token" in checkout_result_cookie70 assert checkout_result["passwords"] # not None and has at least 1 element71def test_get_password_list_returns_the_correct_response(explicit_config, dummy_sg_client_factory, generate_params):72 plugin = SafeguardPlugin(explicit_config, safeguard_client_factory=dummy_sg_client_factory)73 params = generate_params()74 result = plugin.get_password_list(**deepcopy(params))75 assert_plugin_hook_result(76 result,77 {78 "cookie": {"access_token": "the_access_token", "access_request_id": "the_access_request_id"},79 "passwords": ["the_password"],80 },81 )82def test_raises_exception_if_access_request_id_is_not_presented(explicit_config, dummy_sg_client_factory):83 plugin = SafeguardPlugin(explicit_config, safeguard_client_factory=dummy_sg_client_factory)84 with pytest.raises(SafeguardException) as exc_info:85 plugin.session_ended(cookie={"account": "x"}, session_cookie={}, session_id="the_session_id")86 assert exc_info.match("Missing access_request_id")87class SaveAssets(SafeguardPlugin):88 def __init__(self, configuration, safeguard_client_factory):89 super().__init__(configuration, safeguard_client_factory)90 self.test_asset_list = []91 def do_get_password_list(self):92 self.test_asset_list.append(self.asset)93def test_assets_suffix(explicit_config, dummy_sg_client_factory, generate_params):94 config = dedent(95 """96 [domain_asset_mapping]97 bar.baz=acme.com98 [assets]99 domain_suffix=baz100 """101 )102 plugin = SaveAssets(config, dummy_sg_client_factory)103 params = generate_params(server_hostname="foo.bar", server_domain="bar")104 plugin.get_password_list(**deepcopy(params))...
tests.py
Source:tests.py
1from rest_framework import status2from rest_framework.test import APITestCase3class AccountTests(APITestCase):4 def test_asset_list(self):5 response = self.client.post('/api/v1/rest-auth/registration/', {6 'username': 'francisco213422',7 'password1': 'Francisco123',8 'password2': 'Francisco123',9 'email': 'fra123@gmail.com',10 'email2': 'fra123@gmail.com',11 'first_name': 'fanasdasd',12 'last_name': 'asddasdasj',13 'avatar': 1,14 }, format='json')15 response = self.client.post('/api/v1/rest-auth/login/',16 {'username': 'francisco213422',17 'password': 'Francisco123'},18 format='json')...
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!!