def setUpClass(cls) -> None: cls.window = Window(window_start=date(2008, 1, 3), trading_win_len=timedelta(days=90), repository=DataRepository()) cls.empty_portfolio: Portfolio = Portfolio(100_000, cls.window) cls.portfolio_to_be_augmented = Portfolio(100_000, cls.window) cls.decision_maker = SignalGenerator(cls.empty_portfolio, entry_z=0.5, exit_z=0.1, emergency_z=0.8) cls.pair1 = CointegratedPair(pair=(SnpTickers.AAPL, SnpTickers.MSFT), mu_x_ann=0.0, sigma_x_ann=0.0, scaled_beta=1.2, hl=0.0, ou_mean=0.0, ou_std=0.0, ou_diffusion_v=0.0, recent_dev=0.0, recent_dev_scaled=0.1) cls.pair2 = CointegratedPair(pair=(SnpTickers.AAPL, SnpTickers.TDG), mu_x_ann=0.0, sigma_x_ann=0.0, scaled_beta=0.5, hl=0.0, ou_mean=0.0, ou_std=0.0, ou_diffusion_v=0.0, recent_dev=0.0, recent_dev_scaled=0.2)
class Application: def __init__(self): self.running = True self.window = Window(self, 900, 600, 'Boxes') self.game_state = GameState(self) def run(self): """ Main function of the game :return: None """ self.window.init() self.game_state.preupdate() while self.running: self.game_state.update() self.window.display()
def goToPlay(): witdthofacell = 5 numberOfLine = 100 numberOfColumn = 100 mondamier = Damier(witdthofacell, numberOfLine, numberOfColumn) regles = Rule('classic') myWindow = Window(mondamier, regles)
def __init__(self, logger: logging.Logger, backtest_start: date = date(2008, 1, 2), backtest_end: Optional[date] = None, target_number_of_coint_pairs: int = 100, max_active_pairs: int = 10, clust_and_coint_frequency_per_window=8, window_length: timedelta = timedelta(days=120), trading_freq: timedelta = timedelta(days=1), adf_confidence_level: AdfPrecisions = AdfPrecisions.ONE_PCT, max_mean_rev_time: int = 30, hurst_exp_threshold: float = 0.15, entry_z_lower_bound: float = 2, entry_z_upper_bound: float = 3, emergency_delta_z: float = 2, exit_delta_z: float = 1): # If end_date is None, run for the entirety of the dataset # Window is the lookback period (from t=window_length to t=0 (today) over which we analyse data # to inform us on trades to make on t=0 (today). self.logger: logging.Logger = logger self.backtest_start: date = backtest_start self.max_active_pairs: int = max_active_pairs self.clust_and_coint_frequency_per_window: int = clust_and_coint_frequency_per_window self.window_length: timedelta = window_length self.trading_freq: timedelta = trading_freq self.adf_confidence_level: AdfPrecisions = adf_confidence_level # e.g. "5%" or "1%" self.max_mean_rev_time: int = max_mean_rev_time self.hurst_exp_threshold: float = hurst_exp_threshold self.entry_z_lower_bound: float = entry_z_lower_bound self.entry_z_upper_bound: float = entry_z_upper_bound self.emergency_delta_z: float = emergency_delta_z self.exit_delta_z: float = exit_delta_z # Last SNP date, hard coded for now... self.backtest_end = date(year=2019, month=12, day=31) if backtest_end is None else backtest_end self.repository = DataRepository(self.window_length) self.current_window: Window = Window(window_start=backtest_start, win_len=window_length, repository=self.repository) self.today = self.current_window.window_business_days[-1] self.day_count: int = 0 self.last_traded_date: Optional[date] = None self.clusterer = Clusterer() self.cointegrator = Cointegrator(self.repository, target_number_of_coint_pairs, self.adf_confidence_level, self.max_mean_rev_time, previous_cointegrated_pairs=[]) self.filters = Filters() self.portfolio: Portfolio = Portfolio(100_000, self.current_window) self.signalgenerator = SignalGenerator(self.portfolio, entry_z_lower_bound, entry_z_upper_bound, exit_delta_z, emergency_delta_z, max_active_pairs)
def __init__(self, backtest_start: date = date(2008, 1, 2), trading_window_length: timedelta = timedelta(days=90), backtest_end: Optional[date] = None, adf_confidence_level: AdfPrecisions = AdfPrecisions.FIVE_PCT, max_mean_rev_time: int = 15, hurst_exp_threshold: float = 0.35, entry_z: float = 1.5, emergency_z: float = 3, exit_z: float = 0.5): # If end_date is None, run for the entirety of the dataset # Window is the lookback period (from t=window_length to t=0 (today) over which we analyse data # to inform us on trades to make on t=0 (today). # We assume an expanding window for now. self.backtest_start: date = backtest_start self.window_length: timedelta = trading_window_length self.adf_confidence_level: AdfPrecisions = adf_confidence_level # e.g. "5%" or "1%" self.max_mean_rev_time: int = max_mean_rev_time self.entry_z: float = entry_z self.exit_z: float = exit_z self.hurst_exp_threshold: float = hurst_exp_threshold # if the pair crosses this boundary, we don't believe their cointegrated anymore # - close the position at a loss # require: emergency_z > entry_z > exit_z self.emergency_z: float = emergency_z # Last SNP date, hard coded for now... self.backtest_end = date( year=2020, month=12, day=31) if backtest_end is None else backtest_end self.repository = DataRepository() initial_window = Window(window_start=backtest_start, trading_win_len=trading_window_length, repository=self.repository) self.current_window: Window = initial_window self.history: List[Window] = [initial_window] self.trading_days = initial_window.window_trading_days self.today = self.trading_days[-1] self.days_alive: int = (self.today - self.backtest_start).days self.clusterer = Clusterer() self.cointegrator = Cointegrator(self.repository, self.adf_confidence_level, self.max_mean_rev_time, self.entry_z, self.exit_z, initial_window, self.history[-1]) self.risk_manager = RiskManager(self.entry_z, self.exit_z) self.filters = Filters() self.portfolio: Portfolio = Portfolio(100_000, self.current_window) self.dm = SignalGenerator(self.portfolio, entry_z, emergency_z, exit_z)
def run_one_window(win_info): window = Window(win_info) window.run_window_call_variant() return window
def run_one_window(win_info): window = Window(win_info, tech=get_value("paras")["tech"]) window.run_window_benchmark() return window
def test_window_initialised_with_non_trading_day(self): # 1st of Jan isn't a trading day; the production code should raise a KeyError with self.assertRaises(KeyError): Window(date(2008, 1, 1), timedelta(days=10), DataRepository())
def setUpClass(cls) -> None: cls.initial = Window(date(2008, 1, 2), timedelta(days=10), DataRepository())
def run_GUI_demo_2(): app = QtWidgets.QApplication(sys.argv) wTitle: str = progName + "run_GUI_demo_2 (version : " + version + ")" GUI = Window() GUI.show() sys.exit(app.exec_())
def __init__(self): self.running = True self.window = Window(self, 900, 600, 'Boxes') self.game_state = GameState(self)
class UI: def __init__(self, initial, final, level, pivot): self.simulator = Simulator(initial, final, level, pivot) self.background = Window(*(0, 0), *os.get_terminal_size(), *os.get_terminal_size(), '01 ') def render_background_with_form(self, form): x = self.background.x + (self.background.width - form.width) // 2 y = self.background.y + (self.background.height - form.height) // 2 form.set_x_y(x, y) for i in range(100000): self.background.render_window(form) time.sleep(0.3) self.background.push_new_line_on_window() def one_game(self, form): tick = 0.3 form.set_string('Enter size:', *(1, 1)) time.sleep(tick) size = form.get_and_set_string_from_input(2, 1) data = self.simulator.get_data(int(size)) initial = self.simulator.get_initial(data) form.set_string(initial, *(3, 1)) time.sleep(tick) form.set_string('Enter string:', *(4 + 2, 1)) time.sleep(tick) user_input = form.get_and_set_string_from_input(5 + 2, 1) form.set_string('Result:', *(6 + 2, 1)) time.sleep(tick) result = self.simulator.validate_input(user_input, data) if result['status'] == 'SUCCESS': i = 7 form.set_string('SUCCESS', *(7 + 2, 1)) time.sleep(tick) else: for i, message in enumerate(result['messages'], start=7 + 2): result = '' if message[0] == 'incompatible': result = f'#{message[1]}, expected {message[3]} but was {message[2]}' else: result = f'Incorrect size' form.set_string(result, *(i, 1)) time.sleep(tick) form.set_string('pause', *(i + 1 + 2, 1)) time.sleep(tick) user_input = form.get_and_set_string_from_input(i + 2 + 2, 1) form.clean() def run(self): width, height = os.get_terminal_size() form = Form(width // 2, height // 2) thread = threading.Thread(target=self.render_background_with_form, args=([form])) thread.start() while True: self.one_game(form)
def __init__(self, initial, final, level, pivot): self.simulator = Simulator(initial, final, level, pivot) self.background = Window(*(0, 0), *os.get_terminal_size(), *os.get_terminal_size(), '01 ')
import pygame from src.Game import Game from src.Window import Window from src.Menu import Menu from src.State import State if __name__ == '__main__': pygame.init() window = Window() current_state = Menu(window) while window.is_open(): window.fill_base() for event in pygame.event.get(): if event.type == pygame.QUIT or State.is_key_pressed( pygame.K_ESCAPE): window.close() pygame.quit() print("UPDATE: Game window closed") break state_name = current_state.state current_state.keys_pressed_reaction() if state_name != current_state.state: if current_state.state == State.GAME_STATE or current_state.state == State.RESTART_STATE: # Create new game current_state = Game(window) elif current_state.state == State.AI_GAME_STATE or current_state.state == State.AI_RESTART_STATE: current_state = Game(window, State.AI_GAME_STATE) elif current_state.state == State.MENU_STATE: current_state = Menu(window)
def __init__(self): Window.Window()
return pair_expected_return def __mean_variance_optimizer(self, cov_matrix, expected_return): cov_matrix_inv = np.linalg.inv(cov_matrix.values) weight = cov_matrix_inv.dot(expected_return) scale = weight.sum() weight = weight / scale return weight[:,0] from datetime import date, timedelta from src.DataRepository import DataRepository if __name__ == '__main__': win = Window(window_start=date(2008, 1, 2), trading_win_len=timedelta(days=90), repository=DataRepository()) test_input = [ CointegratedPair(pair = (SnpTickers.CRM, SnpTickers.WU), mu_x_ann = 0.1, sigma_x_ann = 0.3, scaled_beta = 1.2, hl = 7.5, ou_mean = -0.017, ou_std = 0.043, ou_diffusion_v = 0.70, recent_dev= 0.071, recent_dev_scaled = 2.05), CointegratedPair(pair=(SnpTickers.ABC, SnpTickers.ABT),
def generate_pairs(self, clustering_results: Dict[int, Tuple[Tuple[Tickers]]], hurst_exp_threshold: float, current_window: Window): # run cointegration_analysis on all poss combinations of pairs within the same cluster current_cointegrated_pairs = [] n_cointegrated = 0 tickers_per_cluster = [i for i in clustering_results.values()] for cluster in tickers_per_cluster: for pair in itertools.combinations(list(cluster), 2): t1 = current_window.get_data(tickers=[pair[0]], features=[Features.CLOSE]) t2 = current_window.get_data(tickers=[pair[1]], features=[Features.CLOSE]) try: # sometimes there are no price data, in which case, skip residuals, beta, reg_output = self.__logged_lin_reg(t1, t2) except ValueError: continue adf_test_statistic, adf_critical_values = self.__adf( residuals.flatten()) hl_test = self.__hl(residuals) he_test = self.__hurst_exponent_test(residuals, current_window) ou_mean, ou_std, ou_diffusion_v, \ recent_dev, recent_dev_scaled = self.__ou_params(residuals) ols_stdzed_residuals = (residuals - ou_mean) / ou_std is_cointegrated = self.__acceptance_rule( adf_test_statistic, adf_critical_values, self.adf_confidence_level, hl_test, self.max_mean_rev_time, he_test, hurst_exp_threshold, ols_stdzed_residuals, at_least=int(current_window.window_length.days / 6)) if is_cointegrated: #a = pd.concat([t1, t2], axis=1).iplot(asFigure=True) #b = pd.concat([np.log(t1), np.log(t2)], axis=1).iplot(asFigure=True) #a.show() #b.show() n_cointegrated += 1 t1_most_recent = float(t1.iloc[-1, :]) t2_most_recent = float(t2.iloc[-1, :]) hedge_ratio = beta * t1_most_recent / t2_most_recent scaled_beta = hedge_ratio / (hedge_ratio - 1) recent_dev_scaled_hist = [recent_dev_scaled] cointegration_rank = self.__score_coint( adf_test_statistic, self.adf_confidence_level, adf_critical_values, he_test, hurst_exp_threshold, 10) #a = pd.DataFrame(ols_stdzed_residuals).iplot(asFigure=True) #a.show() position = Position(pair[0], pair[1]) current_cointegrated_pairs.append( CointegratedPair(pair, reg_output, scaled_beta, hl_test, ou_mean, ou_std, ou_diffusion_v, recent_dev, recent_dev_scaled, recent_dev_scaled_hist, cointegration_rank, ols_stdzed_residuals, position)) if n_cointegrated == self.target_number_of_coint_pairs: current_cointegrated_pairs = sorted( current_cointegrated_pairs, key=lambda coint_pair: coint_pair. cointegration_rank, reverse=True) self.previous_cointegrated_pairs = current_cointegrated_pairs return current_cointegrated_pairs self.previous_cointegrated_pairs = current_cointegrated_pairs return current_cointegrated_pairs
def generate_pairs(self, clustering_results: Dict[int, Tuple[Tuple[Tickers]]], hurst_exp_threshold: float, current_window: Window): # run cointegration_analysis on all poss combinations of pairs current_cointegrated_pairs = [] n_cointegrated = 0 list_of_lists = [i for i in clustering_results.values()] flattened = [pair for x in list_of_lists for pair in x] sorted_cluster_results = sorted(flattened, key=lambda x: x[0].value) for pair in sorted_cluster_results: t1 = current_window.get_data(universe=Universes.SNP, tickers=[pair[0]], features=[Features.CLOSE]) t2 = current_window.get_data(universe=Universes.SNP, tickers=[pair[1]], features=[Features.CLOSE]) try: # sometimes there are no price data residuals, beta, reg_output = self.__logged_lin_reg(t1, t2) except: continue # for some reason residuals is a (60,1) array not (60,) array when i run the code so have changed input to residuals.flatten adf_test_statistic, adf_critical_values = self.__adf( residuals.flatten()) hl_test = self.__hl(residuals) he_test = self.__hurst_exponent_test(residuals, current_window) is_cointegrated = self.__acceptance_rule( adf_test_statistic, adf_critical_values, self.adf_confidence_level, hl_test, self.max_mean_rev_time, he_test, hurst_exp_threshold) target_coint = 300 if is_cointegrated: n_cointegrated += 1 print(n_cointegrated, " / ", target_coint, "cointegrated") r_x = self.__log_returner(t1) mu_x_ann = float(250 * np.mean(r_x)) sigma_x_ann = float(250**0.5 * np.std(r_x)) ou_mean, ou_std, ou_diffusion_v, recent_dev, recent_dev_scaled = self.__ou_params( residuals) scaled_beta = beta / (beta - 1) recent_dev_scaled_hist = [recent_dev_scaled] cointegration_rank = self.__score_coint( adf_test_statistic, self.adf_confidence_level, adf_critical_values) current_cointegrated_pairs.append( CointegratedPair(pair, mu_x_ann, sigma_x_ann, reg_output, scaled_beta, hl_test, ou_mean, ou_std, ou_diffusion_v, recent_dev, recent_dev_scaled, recent_dev_scaled_hist, cointegration_rank)) if n_cointegrated == target_coint: # logic to be fixed and made more efficient by: 1) having proper # clustering algorithm; 2) not running clustering and cointegration # everyday 3) taking best 10 pairs according to some score current_cointegrated_pairs = sorted( current_cointegrated_pairs, key=lambda coint_pair: coint_pair.cointegration_rank, reverse=True) self.previous_cointegrated_pairs = current_cointegrated_pairs return current_cointegrated_pairs self.previous_cointegrated_pairs = current_cointegrated_pairs return current_cointegrated_pairs