def init_detection_process_for_automated_trade_update( self, mean: int, sma_number: int): self.config.detection_process = PDP.UPDATE_TRADE_DATA self.config.pattern_type_list = FT.get_long_trade_able_types() self.exchange_config.trade_strategy_dict = { BT.BREAKOUT: [ TSTR.LIMIT, TSTR.LIMIT_FIX, TSTR.TRAILING_STOP, TSTR.TRAILING_STEPPED_STOP ] } self.exchange_config.default_trade_strategy_dict = { BT.BREAKOUT: TSTR.LIMIT } self.config.detection_process = PDP.UPDATE_TRADE_DATA self.config.plot_data = False self.config.with_trading = True self.config.trading_last_price_mean_aggregation = mean self.config.simple_moving_average_number = sma_number # default = 8 self.config.save_pattern_data = False self.config.save_trade_data = True self.config.replace_existing_trade_data_on_db = False # default = False self.config.plot_only_pattern_with_fibonacci_waves = False self.config.plot_min_max = True self.config.plot_volume = False self.config.length_for_local_min_max = 2 self.config.length_for_local_min_max_fibonacci = 1 self.config.bound_upper_value = CN.CLOSE self.config.bound_lower_value = CN.CLOSE self.config.fibonacci_tolerance_pct = 0.1 # default is 0.20 self.sound_machine.is_active = False
def __adjust_sys_config__(self): print('TradeTest.__adjust_sys_config__') self.sys_config.runtime_config.actual_trade_process = self.trade_process self.sys_config.config.plot_data = False if self.trade_process == TP.BACK_TESTING: self.sys_config.config.pattern_type_list = FT.get_long_trade_able_types() self.sys_config.config.save_pattern_data = False self.sys_config.config.save_trade_data = False
def __get_optimal_pattern_type_dict_for_long_trading__(self): opt_dict = {} for pattern_type in FT.get_long_trade_able_types(): if self._pattern_type_pos_neg_result_dict[ pattern_type] >= self.expected_relation_pos_neg: opt_dict[ pattern_type] = self._pattern_type_pos_neg_result_dict[ pattern_type] return opt_dict
def update_trade_records(self, mean: int, sma_number: int, trade_strategies: dict = None, pattern_end_date=str): if pattern_end_date == '': pattern_end_date = str(MyDate.adjust_by_days( None, -90)) # only the ones which are 3 month back self.sys_config.init_detection_process_for_automated_trade_update( mean, sma_number) if trade_strategies is None: trade_strategies = { BT.BREAKOUT: [ TSTR.LIMIT, TSTR.LIMIT_FIX, TSTR.TRAILING_STOP, TSTR.TRAILING_STEPPED_STOP ] } for pattern_type in FT.get_long_trade_able_types(): where_clause = "pattern_type = '{}' and period = 'DAILY' and trade_type in ('', 'long')".format( pattern_type) if pattern_end_date != '': where_clause += " AND {} > '{}'".format( DC.PATTERN_END_DT, pattern_end_date) df_pattern_for_pattern_type = self.db_stock.get_pattern_records_as_dataframe( where_clause) pattern_id_dict = { row[DC.ID]: row[DC.TRADE_TYPE] for index, row in df_pattern_for_pattern_type.iterrows() } # pattern_id_list = ['20_1_1_LTCUSD_20_2016-11-02_00:00_2016-12-04_00:00'] counter = 0 for pattern_id, trade_type in pattern_id_dict.items(): counter += 1 run_detail = '{} ({:03d} of {:03d}): {}'.format( pattern_type, counter, len(pattern_id_dict), pattern_id) result_dict = self.db_stock.get_missing_trade_strategies_for_pattern_id( pattern_id, trade_strategies, mean, sma_number) for buy_trigger, trade_strategy_list in result_dict.items(): if len(trade_strategy_list) == 0: print('{}: OK'.format(run_detail)) else: print('{}: processing...'.format(run_detail)) self.sys_config.config.pattern_ids_to_find = [ pattern_id ] self.sys_config.exchange_config.trade_strategy_dict = { buy_trigger: trade_strategy_list } pattern_controller = PatternDetectionController( self.sys_config) pattern_controller.run_pattern_detector() if trade_type == '': self.db_stock.update_trade_type_for_pattern( pattern_id, TRT.LONG)
def get_pattern_records_for_replay_as_dataframe(self) -> pd.DataFrame: query = self._pattern_table.get_query_select_for_records( "Period = 'DAILY'") db_df = DatabaseDataFrame(self, query) return db_df.df[db_df.df[DC.PATTERN_TYPE].isin( FT.get_long_trade_able_types())]
def __perform_task__(self): self._db_updater.update_trade_policy_metric_for_today( FT.get_long_trade_able_types())
""" Description: This module contains the global system configuration classes for stock pattern detection Author: Josef Sertl Copyright: SERTL Analytics, https://sertl-analytics.com Date: 2018-05-14 """ from sertl_analytics.constants.pattern_constants import FT, BT, TSTR, TTC, TP, INDICES from sertl_analytics.datafetcher.web_data_fetcher import IndicesComponentFetcher from pattern_test.trade_test import TradeTest, TradeTestApi api = TradeTestApi() api.test_process = TP.BACK_TESTING back_testing = TradeTest(api) back_testing.sys_config.config.pattern_type_list = FT.get_long_trade_able_types( ) # back_testing.sys_config.config.pattern_type_list = [FT.TRIANGLE_DOWN] back_testing.sys_config.config.save_trade_data = True back_testing.sys_config.config.simple_moving_average_number = 20 back_testing.sys_config.config.trading_last_price_mean_aggregation = 8 # ******** START setup ********** api.buy_trigger = BT.BREAKOUT api.trade_strategy = TSTR.TRAILING_STOP # ******** END setup ********** ticker_dic = IndicesComponentFetcher.get_ticker_name_dic(INDICES.DOW_JONES) # ticker_dic = IndicesComponentFetcher.get_ticker_name_dic(INDICES.CRYPTO_CCY) # ticker_dic = {'CVX': 'a'} api.and_clause = "Date BETWEEN '2018-06-07' AND '2019-09-04'"