def init_tick_key_list_for_retrospection(self, retrospective_ticks: int): offset_time_stamp = MyDate.get_offset_timestamp(days=-retrospective_ticks) self._tick_key_list_for_retrospection = [] while offset_time_stamp < MyDate.time_stamp_now(): offset_time_stamp += self._seconds_unit date_str = str(MyDate.get_date_from_epoch_seconds(offset_time_stamp)) self._tick_key_list_for_retrospection.append(date_str)
def add_buy_order_status_data_to_pattern_data_dict(self, order_status: OrderStatus, trade_strategy: str): self._data_dict[DC.BUY_ORDER_ID] = order_status.order_id self._data_dict[DC.BUY_ORDER_TPYE] = order_status.type self._data_dict[DC.BUY_ORDER_TPYE_ID] = OT.get_id(order_status.type) self._data_dict[DC.BUY_TIME_STAMP] = int(order_status.time_stamp) self._data_dict[DC.BUY_DT] = MyDate.get_date_from_epoch_seconds(order_status.time_stamp) self._data_dict[DC.BUY_TIME] = str(MyDate.get_time_from_epoch_seconds(order_status.time_stamp)) self._data_dict[DC.BUY_AMOUNT] = order_status.original_amount self._data_dict[DC.BUY_PRICE] = order_status.avg_execution_price self._data_dict[DC.BUY_TOTAL_COSTS] = order_status.value_total self._data_dict[DC.BUY_TRIGGER] = order_status.order_trigger self._data_dict[DC.BUY_TRIGGER_ID] = BT.get_id(order_status.order_trigger) self._data_dict[DC.BUY_COMMENT] = order_status.order_comment self._data_dict[DC.TRADE_STRATEGY] = trade_strategy self._data_dict[DC.TRADE_STRATEGY_ID] = TSTR.get_id(trade_strategy)
def init_tick_key_list_for_retrospection(self, retrospective_ticks: int, period: str, aggregation=1): self._tick_key_list_for_retrospection = [] self._period_for_retrospection = period self._aggregation_for_retrospection = aggregation offset_ts = self.__get_offset_time_stamp__(retrospective_ticks, period, aggregation) seconds_aggregation = MyDate.get_seconds_for_period_aggregation( period, aggregation) while offset_ts < MyDate.time_stamp_now(): offset_ts += seconds_aggregation key = str(MyDate.get_date_from_epoch_seconds( offset_ts)) if period == PRD.DAILY else offset_ts self._tick_key_list_for_retrospection.append(key)
def get_tick_list_as_data_frame_for_replay(self): tick_table = [] for tick in self.tick_list: date = MyDate.get_date_from_epoch_seconds(tick.time_stamp) time = MyDate.get_time_from_epoch_seconds(tick.time_stamp) date_time = str( MyDate.get_date_time_from_epoch_seconds(tick.time_stamp)) row = [ tick.position, tick.open, tick.high, tick.low, tick.close, tick.volume, date, time, date_time, tick.time_stamp ] tick_table.append(row) v_array = np.array(tick_table).reshape([len(self.tick_list), 10]) return pd.DataFrame(v_array, columns=[ CN.POSITION, CN.OPEN, CN.HIGH, CN.LOW, CN.CLOSE, CN.VOL, CN.DATE, CN.TIME, CN.DATETIME, CN.TIMESTAMP ])
def add_sell_order_status_data_to_pattern_data_dict(self, order_status: OrderStatus): self._data_dict[DC.SELL_ORDER_ID] = order_status.order_id self._data_dict[DC.SELL_ORDER_TPYE] = order_status.type self._data_dict[DC.SELL_ORDER_TPYE_ID] = OT.get_id(order_status.type) self._data_dict[DC.SELL_TIME_STAMP] = int(order_status.time_stamp) self._data_dict[DC.SELL_DT] = MyDate.get_date_from_epoch_seconds(order_status.time_stamp) self._data_dict[DC.SELL_TIME] = str(MyDate.get_time_from_epoch_seconds(order_status.time_stamp)) self._data_dict[DC.SELL_AMOUNT] = order_status.original_amount self._data_dict[DC.SELL_PRICE] = order_status.avg_execution_price self._data_dict[DC.SELL_TOTAL_VALUE] = order_status.value_total self._data_dict[DC.SELL_TRIGGER] = order_status.order_trigger self._data_dict[DC.SELL_TRIGGER_ID] = ST.get_id(order_status.order_trigger) self._data_dict[DC.SELL_COMMENT] = order_status.order_comment win_min = round(self._data_dict[DC.BUY_PRICE] * 1.005, 2) # at least 0.5% if self._data_dict[DC.SELL_PRICE] > win_min: self._data_dict[DC.TRADE_RESULT] = TR.WINNER elif self._data_dict[DC.SELL_PRICE] > self._data_dict[DC.BUY_PRICE]: self._data_dict[DC.TRADE_RESULT] = TR.NEUTRAL else: self._data_dict[DC.TRADE_RESULT] = TR.LOSER self._data_dict[DC.TRADE_RESULT_ID] = TR.get_id(self._data_dict[DC.TRADE_RESULT])
def get_xy_from_timestamp_to_date_str(xy): if type(xy) == list: return [(str(MyDate.get_date_from_epoch_seconds(t_val[0])), t_val[1]) for t_val in xy] return str(MyDate.get_date_from_epoch_seconds(xy[0])), xy[1]
def date(self): return self.tick[ CN. DATE] if CN.DATE in self.tick else MyDate.get_date_from_epoch_seconds( self.tick[CN.TIMESTAMP])
def date_str_for_f_var(self): return str(MyDate.get_date_from_epoch_seconds(self.f_var))
def add_wave_end_data_to_wave_records(self, symbol='', ts_start=0, ts_end=0, scheduled_job=False) -> int: """ Some attributes have to be calculated AFTER the waves completes: DC.WAVE_END_FLAG, DC.WAVE_MAX_RETR_PCT, DC.WAVE_MAX_RETR_TS_PCT """ print('Add wave end data to wave records..') access_layer_wave = AccessLayer4Wave(self.db_stock) access_layer_stocks = AccessLayer4Stock(self.db_stock) df_wave_to_process = access_layer_wave.get_wave_data_frame_without_end_data( symbol, ts_start, ts_end) ts_start_stocks = ts_start if ts_end == 0 else ts_end df_stocks = access_layer_stocks.get_stocks_data_frame_for_wave_completing( symbol=symbol, ts_start=ts_start_stocks) tolerance = 0.01 update_counter = 0 ts_now = MyDate.time_stamp_now() for wave_index, wave_row in df_wave_to_process.iterrows(): wave_entity = WaveEntity(wave_row) ts_start, ts_end = wave_entity.get_ts_start_end_for_check_period() ts_start_dt = MyDate.get_date_from_epoch_seconds(ts_start) ts_end_dt = MyDate.get_date_from_epoch_seconds(ts_end) wave_end_value, wave_value_range = wave_entity.wave_end_value, wave_entity.wave_value_range if ts_end < ts_now: wave_end_reached = 1 max_retracement = 0 max_retracement_ts = ts_start max_retracement_dt = ts_start_dt df_filtered_stocks = df_stocks[np.logical_and( df_stocks[DC.SYMBOL] == wave_entity.symbol, np.logical_and(df_stocks[DC.TIMESTAMP] > ts_start, df_stocks[DC.TIMESTAMP] <= ts_end))] for index_stocks, row_stocks in df_filtered_stocks.iterrows(): row_low, row_high, row_ts = row_stocks[DC.LOW], row_stocks[ DC.HIGH], row_stocks[DC.TIMESTAMP] if wave_entity.wave_type == FD.ASC: if wave_end_value < row_high * (1 - tolerance): wave_end_reached = 0 break else: retracement = wave_end_value - row_low else: if wave_end_value > row_low * (1 + tolerance): wave_end_reached = 0 break else: retracement = row_high - wave_end_value if retracement > max_retracement: max_retracement = round(retracement, 2) max_retracement_ts = row_ts max_retracement_dt = row_stocks[DC.DATE] max_retracement_pct = round( max_retracement / wave_value_range * 100, 2) max_retracement_ts_pct = round( (max_retracement_ts - ts_start) / wave_entity.wave_ts_range * 100, 2) data_dict = { DC.WAVE_END_FLAG: wave_end_reached, DC.WAVE_MAX_RETR_PCT: max_retracement_pct, DC.WAVE_MAX_RETR_TS_PCT: max_retracement_ts_pct } access_layer_wave.update_record(wave_entity.row_id, data_dict) update_counter += 1 if scheduled_job: self.sys_config.file_log.log_scheduler_process( log_message='Updated: {}/{}'.format( update_counter, df_wave_to_process.shape[0]), process='Update wave records', process_step='add_wave_end_data') return update_counter
def get_date_time_for_retracement_pct(self, retracement_pct: float) -> str: ts = self.get_timestamp_for_retracement_pct(retracement_pct) if self.period == PRD.DAILY: return str(MyDate.get_date_from_epoch_seconds(ts)) return MyDate.get_date_time_from_epoch_seconds_as_string(ts)