def __init_loop_list_for_ticker__(self): self._loop_list_ticker = LoopList4Dictionaries() if self.sys_config.from_db and self._excel_file_with_test_data is not None: for ind, rows in self._df_test_data.iterrows(): if self._loop_list_ticker.counter >= self._excel_file_with_test_data.row_start: self.sys_config.config.ticker_dic[rows[PSC.TICKER]] = rows[ PSC.NAME] start_date = MyDate.get_date_from_datetime( rows[PSC.BEGIN_PREVIOUS]) date_end = MyDate.get_date_from_datetime( rows[PSC.END] + timedelta(days=rows[PSC.T_NEEDED] + 20)) and_clause = "Date BETWEEN '{}' AND '{}'".format( start_date, date_end) self._loop_list_ticker.append({ LL.TICKER: rows[PSC.TICKER], LL.AND_CLAUSE: and_clause }) if self._loop_list_ticker.counter >= self._excel_file_with_test_data.row_end: break else: for ticker in self.sys_config.ticker_dict: and_clause = self.sys_config.data_provider.and_clause self._loop_list_ticker.append({ LL.TICKER: ticker, LL.AND_CLAUSE: and_clause })
def init_and_clause(self): if self.period == PRD.INTRADAY: minutes = self.aggregation * self.limit days = int(minutes / (60 * 24)) + 1 dt_start = MyDate.get_date_from_datetime() dt_start = MyDate.adjust_by_days(dt_start, -days) dt_end = MyDate.get_date_from_datetime() dt_end = MyDate.adjust_by_days(dt_end, 1) self._and_clause = self.get_and_clause(dt_start, dt_end)
def get_and_clause(dt_start=None, dt_end=None): if dt_start is None: date_start = MyDate.get_date_from_datetime() date_start = MyDate.adjust_by_days(date_start, -1) dt_end = MyDate.get_date_from_datetime() dt_end = MyDate.adjust_by_days(dt_end, 1) else: date_start = MyDate.get_date_from_datetime(dt_start) if dt_end: date_end = MyDate.get_date_from_datetime(dt_end) return "Date BETWEEN '{}' AND '{}'".format(date_start, date_end) return "Date >= '{}'".format(date_start)
def update_equity_records(self) -> SalesmanDatabaseUpdateJobResult: result_obj = SalesmanDatabaseUpdateJobResult() access_layer = AccessLayer4Sale(self.db_salesman) dt_today = MyDate.get_date_from_datetime() # dt_today = MyDate.adjust_by_days(dt_today, 40) dt_valid_until = MyDate.adjust_by_days(dt_today, 30) dt_today_str = str(dt_today) dt_valid_until_str = str(dt_valid_until) return result_obj
def update_trade_policy_metric_for_today(self, pattern_type_list: list): print("\nSTARTING 'update_trade_policy_metric_for_today' for {}...". format(pattern_type_list)) access_layer = AccessLayer4TradePolicyMetric(self.db_stock) dt_today_str = str(MyDate.get_date_from_datetime()) check_dict = {TPMDC.VALID_DT: dt_today_str} if access_layer.are_any_records_available(check_dict): print( "END 'update_trade_policy_metric_for_today': No updates for today\n" ) # return policy_list = TradePolicyFactory.get_trade_policies_for_metric_calculation( ) period_list = [PRD.DAILY] mean_aggregation_list = [4, 8, 16] for pattern_type in pattern_type_list: for period in period_list: for mean_aggregation in mean_aggregation_list: policy_handler = TradePolicyHandler( pattern_type=pattern_type, period=period, mean_aggregation=mean_aggregation) if policy_handler.are_enough_data_available(): for policy in policy_list: rewards, entity_counter = policy_handler.run_policy( policy, False) insert_dict = { TPMDC.VALID_DT: dt_today_str, TPMDC.POLICY: policy.policy_name, TPMDC.EPISODES: 1, TPMDC.PERIOD: period, TPMDC.AGGREGATION: 1, TPMDC.TRADE_MEAN_AGGREGATION: mean_aggregation, TPMDC.PATTERN_TYPE: pattern_type, TPMDC.NUMBER_TRADES: entity_counter, TPMDC.REWARD_PCT_TOTAL: round(rewards, 2), TPMDC.REWARD_PCT_AVG: round(rewards / entity_counter, 2), } access_layer.insert_data([insert_dict]) print("END 'update_trade_policy_metric_for_today'\n")
def init_detection_process_for_automated_pattern_update(self): date_now = MyDate.get_date_from_datetime() date_from = MyDate.adjust_by_days(date_now, -180) self.data_provider.and_clause = "Date BETWEEN '{}' AND '{}'".format( str(date_from), str(date_now)) self.data_provider.from_db = True self.data_provider.period = PRD.DAILY self.config.pattern_type_list = FT.get_all() self.config.detection_process = PDP.UPDATE_PATTERN_DATA self.config.plot_data = False self.config.with_trading = False self.config.save_pattern_data = True self.config.save_trade_data = False self.config.save_wave_data = False self.config.plot_only_pattern_with_fibonacci_waves = False self.config.plot_min_max = False 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
def update_equity_records(self) -> StockDatabaseUpdateJobResult: result_obj = StockDatabaseUpdateJobResult() access_layer = AccessLayer4Equity(self.db_stock) dt_today = MyDate.get_date_from_datetime() # dt_today = MyDate.adjust_by_days(dt_today, 40) dt_valid_until = MyDate.adjust_by_days(dt_today, 30) dt_today_str = str(dt_today) dt_valid_until_str = str(dt_valid_until) exchange_equity_type_dict = self.__get_equity_dict__() result_obj.number_processed_records = len(exchange_equity_type_dict) for exchange, equity_type in exchange_equity_type_dict.items(): value_dict = access_layer.get_index_dict(exchange) if access_layer.are_any_records_available_for_date( dt_today, exchange, equity_type): result_obj.number_deleted_records += access_layer.delete_existing_equities( equity_type, exchange) sleep(2) self.__update_equity_records_for_equity_type__( access_layer, dt_today_str, dt_valid_until_str, exchange, equity_type) result_obj.number_saved_records += 1 return result_obj
def add_entry(self, pattern: Pattern): self.__init_dic__() self.dic[PSC.C_BOUND_UPPER_VALUE] = self.sys_config.config.bound_upper_value self.dic[PSC.C_BOUND_LOWER_VALUE] = self.sys_config.config.bound_lower_value self.dic[PSC.C_CHECK_PREVIOUS_PERIOD] = self.sys_config.config.check_previous_period self.dic[PSC.C_BREAKOUT_OVER_CONGESTION] = self.sys_config.config.breakout_over_congestion_range self.dic[PSC.C_TOLERANCE_PCT] = pattern.tolerance_pct self.dic[PSC.C_BREAKOUT_RANGE_PCT] = self.sys_config.config.breakout_range_pct self.dic[PSC.C_AND_CLAUSE] = self.sys_config.data_provider.and_clause self.dic[PSC.CON_PREVIOUS_PERIOD_CHECK_OK] = pattern.condition_handler.previous_period_check_ok self.dic[PSC.CON_COMBINED_PARTS_APPLICABLE] = pattern.condition_handler.combined_parts_applicable self.dic[PSC.CON_BREAKOUT_WITH_BUY_SIGNAL] = pattern.condition_handler.breakout_with_buy_signal self.dic[PSC.STATUS] = 'Finished' if pattern.was_breakout_done() else 'Open' self.dic[PSC.TICKER] = self.sys_config.runtime_config.actual_ticker self.dic[PSC.NAME] = self.sys_config.runtime_config.actual_ticker_name self.dic[PSC.PATTERN] = pattern.pattern_type self.dic[PSC.BEGIN_PREVIOUS] = 'TODO' self.dic[PSC.BEGIN] = MyDate.get_date_from_datetime(pattern.date_first) self.dic[PSC.END] = MyDate.get_date_from_datetime(pattern.date_last) self.dic[PSC.LOWER] = round(0, 2) self.dic[PSC.UPPER] = round(0, 2) # TODO Lower & Upper bound for statistics slope_upper, slope_lower, slow_regression = pattern.part_entry.get_slope_values() self.dic[PSC.SLOPE_UPPER] = slope_upper self.dic[PSC.SLOPE_LOWER] = slope_lower self.dic[PSC.SLOPE_RELATION] = 0 self.dic[PSC.TICKS] = pattern.part_entry.ticks if pattern.was_breakout_done(): self.dic[PSC.BREAKOUT_DATE] = MyDate.get_date_from_datetime(pattern.breakout.breakout_date) self.dic[PSC.BREAKOUT_DIRECTION] = pattern.breakout.breakout_direction self.dic[PSC.VOLUME_CHANGE] = pattern.breakout.volume_change_pct if pattern.is_part_trade_available(): self.dic[PSC.EXPECTED] = pattern.trade_result.expected_win self.dic[PSC.RESULT] = pattern.trade_result.actual_win self.dic[PSC.VAL] = pattern.trade_result.formation_consistent self.dic[PSC.EXT] = pattern.trade_result.limit_extended_counter self.dic[PSC.BOUGHT_AT] = round(pattern.trade_result.bought_at, 2) self.dic[PSC.SOLD_AT] = round(pattern.trade_result.sold_at, 2) self.dic[PSC.BOUGHT_ON] = MyDate.get_date_from_datetime(pattern.trade_result.bought_on) self.dic[PSC.SOLD_ON] = MyDate.get_date_from_datetime(pattern.trade_result.sold_on) self.dic[PSC.T_NEEDED] = pattern.trade_result.actual_ticks self.dic[PSC.LIMIT] = round(pattern.trade_result.limit, 2) self.dic[PSC.STOP_LOSS_AT] = round(pattern.trade_result.stop_loss_at, 2) self.dic[PSC.STOP_LOSS_TRIGGERED] = pattern.trade_result.stop_loss_reached if pattern.part_trade is not None: self.dic[PSC.RESULT_DF_MAX] = pattern.part_trade.max self.dic[PSC.RESULT_DF_MIN] = pattern.part_trade.min self.dic[PSC.FIRST_LIMIT_REACHED] = False # default self.dic[PSC.STOP_LOSS_MAX_REACHED] = False # default if pattern.breakout_direction == FD.ASC \ and (pattern.part_entry.bound_upper + pattern.part_entry.height < self.dic[PSC.RESULT_DF_MAX]): self.dic[PSC.FIRST_LIMIT_REACHED] = True if pattern.breakout_direction == FD.DESC \ and (pattern.part_entry.bound_lower - pattern.part_entry.height > self.dic[PSC.RESULT_DF_MIN]): self.dic[PSC.FIRST_LIMIT_REACHED] = True if pattern.breakout_direction == FD.ASC \ and (pattern.part_entry.bound_lower > self.dic[PSC.RESULT_DF_MIN]): self.dic[PSC.STOP_LOSS_MAX_REACHED] = True if pattern.breakout_direction == FD.DESC \ and (pattern.part_entry.bound_upper < self.dic[PSC.RESULT_DF_MAX]): self.dic[PSC.STOP_LOSS_MAX_REACHED] = True new_entry = [self.dic[column] for column in self.column_list] self.list.append(new_entry)