def send_trading_signals(self, timestamp, long_selected, short_selected): # current position account = self.account_service.latest_account current_holdings = [position['security_id'] for position in account['positions'] if position['available_long'] > 0] if long_selected: # just long the security not in the positions longed = long_selected - set(current_holdings) if longed: position_pct = 1.0 / len(longed) order_money = account['cash'] * position_pct for security_id in longed: trading_signal = TradingSignal(security_id=security_id, the_timestamp=timestamp, trading_signal_type=TradingSignalType.trading_signal_open_long, trading_level=self.level, order_money=order_money) for listener in self.trading_signal_listeners: listener.on_trading_signal(trading_signal) # just short the security in current_holdings and short_selected if short_selected: shorted = set(current_holdings) & short_selected for security_id in shorted: trading_signal = TradingSignal(security_id=security_id, the_timestamp=timestamp, trading_signal_type=TradingSignalType.trading_signal_close_long, position_pct=1.0, trading_level=self.level) for listener in self.trading_signal_listeners: listener.on_trading_signal(trading_signal)
def run(self): # now we just support day level for timestamp in pd.date_range(start=self.start_timestamp, end=self.end_timestamp, freq='B').tolist(): self.account_service.on_trading_open(timestamp) account = self.account_service.latest_account current_holdings = [ position['security_id'] for position in account['positions'] ] df = self.selectors_comparator.make_decision(timestamp=timestamp) selected = set() if not df.empty: selected = set(df['security_id'].to_list()) if selected: # just long the security not in the positions longed = selected - set(current_holdings) if longed: position_pct = 1.0 / len(longed) order_money = account['cash'] * position_pct for security_id in longed: trading_signal = TradingSignal( security_id=security_id, the_timestamp=timestamp, trading_signal_type=TradingSignalType. trading_signal_open_long, trading_level=TradingLevel.LEVEL_1DAY, order_money=order_money) for listener in self.trading_signal_listeners: listener.on_trading_signal(trading_signal) shorted = set(current_holdings) - selected for security_id in shorted: trading_signal = TradingSignal( security_id=security_id, the_timestamp=timestamp, trading_signal_type=TradingSignalType. trading_signal_close_long, position_pct=1.0, trading_level=TradingLevel.LEVEL_1DAY) for listener in self.trading_signal_listeners: listener.on_trading_signal(trading_signal) self.account_service.on_trading_close(timestamp)
def run(self): # now we just support day level for timestamp in pd.date_range(start=self.start_timestamp, end=self.end_timestamp, freq='B').tolist(): account = self.account_service.get_account_at_time(timestamp) positions = [ position.security_id for position in account.positions ] # select the targets from the selectors selected = set() for selector in self.selectors: df = selector.get_targets(timestamp) if not df.empty: targets = set(df['security_id'].to_list()) if not selected: selected = targets else: selected = selected & targets if selected: # just long the security not in the positions longed = selected - set(positions) position_pct = 1.0 / len(longed) for security_id in longed: trading_signal = TradingSignal( security_id=security_id, the_timestamp=timestamp, trading_signal_type=TradingSignalType. trading_signal_open_long, trading_level=None, position_pct=position_pct) for listener in self.trading_signal_listeners: listener.on_trading_signal(trading_signal) shorted = set(positions) - selected for security_id in shorted: trading_signal = TradingSignal( security_id=security_id, the_timestamp=timestamp, trading_signal_type=TradingSignalType. trading_signal_close_long, trading_level=None) for listener in self.trading_signal_listeners: listener.on_trading_signal(trading_signal) self.account_service.save_closing_account(timestamp)
def sell(self, due_timestamp, happen_timestamp, entity_ids, position_pct=1.0): # current position account = self.get_current_account() current_holdings = [] if account.positions: current_holdings = [ position.entity_id for position in account.positions if position != None and position.available_long > 0 ] shorted = set(current_holdings) & entity_ids for entity_id in shorted: trading_signal = TradingSignal( entity_id=entity_id, due_timestamp=due_timestamp, happen_timestamp=happen_timestamp, trading_signal_type=TradingSignalType.close_long, trading_level=self.level, position_pct=position_pct) self.send_trading_signal(trading_signal)
def buy(self, due_timestamp, happen_timestamp, entity_ids, position_pct=1.0, ignore_in_position=True): if ignore_in_position: account = self.get_current_account() current_holdings = [] if account.positions: current_holdings = [ position.entity_id for position in account.positions if position != None and position.available_long > 0 ] entity_ids = set(entity_ids) - set(current_holdings) if entity_ids: position_pct = (1.0 / len(entity_ids)) * position_pct for entity_id in entity_ids: trading_signal = TradingSignal( entity_id=entity_id, due_timestamp=due_timestamp, happen_timestamp=happen_timestamp, trading_signal_type=TradingSignalType.open_long, trading_level=self.level, position_pct=position_pct) self.send_trading_signal(trading_signal)
def sell(self, due_timestamp, happen_timestamp, entity_ids): # current position account = self.get_current_account() current_holdings = [] if account.positions: current_holdings = [ position.entity_id for position in account.positions if position is not None and position.available_long > 0 ] shorted = set(current_holdings) & set(entity_ids) if shorted: position_pct = self.short_position_control() for entity_id in shorted: trading_signal = TradingSignal( entity_id=entity_id, due_timestamp=due_timestamp, happen_timestamp=happen_timestamp, trading_signal_type=TradingSignalType.close_long, trading_level=self.level, position_pct=position_pct) self.trading_signals.append(trading_signal)
def make_decision(self): self.current_trading_signal = None if len(self.history_data) < 10: return ma_short = SMA(self.history_data, self.short_period)[-1] ma_long = SMA(self.history_data, self.long_period)[-1] if ma_short > ma_long: if self.last_status == ShortLongStatus.SHORT_ON_LONG: start, end = self.signal_timestamp_interval() self.send_trading_signal( TradingSignal(security_id=self.security_id, current_price=self.current_data['close'], start_timestamp=start, end_timestamp=end, trading_signal_type=TradingSignalType. trading_signal_keep_long)) else: # self.keep_status.append((self.current_timestamp, ShortLongStatus.SHORT_ON_LONG)) start, end = self.signal_timestamp_interval() self.send_trading_signal( TradingSignal(security_id=self.security_id, current_price=self.current_data['close'], start_timestamp=start, end_timestamp=end, trading_signal_type=TradingSignalType. trading_signal_close_short)) self.send_trading_signal( TradingSignal(security_id=self.security_id, current_price=self.current_data['close'], start_timestamp=start, end_timestamp=end, trading_signal_type=TradingSignalType. trading_signal_open_long)) self.last_status = ShortLongStatus.SHORT_ON_LONG if ma_short < ma_long: if self.last_status == ShortLongStatus.LONG_ON_SHORT: start, end = self.signal_timestamp_interval() self.send_trading_signal( TradingSignal(security_id=self.security_id, current_price=self.current_data['close'], start_timestamp=start, end_timestamp=end, trading_signal_type=TradingSignalType. trading_signal_keep_short)) else: # self.keep_status.append((self.current_timestamp, ShortLongStatus.LONG_ON_SHORT)) start, end = self.signal_timestamp_interval() self.send_trading_signal( TradingSignal(security_id=self.security_id, current_price=self.current_data['close'], start_timestamp=start, end_timestamp=end, trading_signal_type=TradingSignalType. trading_signal_close_long)) self.send_trading_signal( TradingSignal(security_id=self.security_id, current_price=self.current_data['close'], start_timestamp=start, end_timestamp=end, trading_signal_type=TradingSignalType. trading_signal_open_short)) self.last_status = ShortLongStatus.LONG_ON_SHORT