Exemplo n.º 1
0
    def execute(self, epoch_s: float, register: SymbolDayRegister) -> None:

        for _ in range(self.N_ACTIONS):
            action = random.randint(0, 1)

            # buy
            if action == 0:
                symbols = list(register.keys())
                symbol = random.choice(symbols)
                price_per_share = register.get_close(symbol, epoch_s)
                if price_per_share is None:
                    continue
                if self.cash >= self.TRADE_SIZE:
                    shares = self.TRADE_SIZE / price_per_share
                    self.buy(symbol, price_per_share, shares)

            # sell
            if action == 1:
                symbols = list(self.holdings.keys())
                if len(symbols) == 0:
                    continue
                symbol = random.choice(symbols)
                price_per_share = register.get_close(symbol, epoch_s)
                if price_per_share is None:
                    continue
                shares = self.holdings[symbol]
                self.sell(symbol, price_per_share, shares)
Exemplo n.º 2
0
    def determine_buys(self, epoch_s: float,
                       register: SymbolDayRegister) -> List[str]:
        """
        Apply rules that trigger buy order
        """
        stocks = list(register.keys())
        random.shuffle(stocks)
        to_buy = list()
        for stock in stocks:

            buy_flag = True
            prices = [None] * (self.n_crash + 1)

            for i in range(self.n_crash):

                close_today = register.get_close(stock, epoch_s, -i)
                if close_today is None:
                    buy_flag = False
                    break

                close_yesterday = register.get_close(stock, epoch_s, -i - 1)
                if close_yesterday is None:
                    buy_flag = False
                    break

                gain = 100 * (close_today / close_yesterday - 1)
                if gain > self.gain_crash:
                    buy_flag = False
                    break

                prices[i] = close_today
                prices[i + 1] = close_yesterday

            if buy_flag:
                to_buy.append(stock)
                date = IntuitiveDateConverter.to_day_str(epoch_s)
                print(
                    f'Identified BUY flag: {date} : {stock.to_str()} : {prices}'
                )
                if self.logger is not None:
                    message = f'Identified BUY flag: {date} : {stock.to_str()} : {prices}'
                    self.logger.log_message(message)

            if len(to_buy) >= self.n_buy_per_day:
                break

        return to_buy
Exemplo n.º 3
0
    def execute(self, epoch_s: float, register: SymbolDayRegister) -> None:

        to_sell = self.determine_sells(epoch_s, register)
        for stock in to_sell:
            price_per_share = register.get_close(stock, epoch_s)
            shares = self.holdings[stock]
            self.sell(stock, price_per_share, shares)

        to_buy = self.determine_buys(epoch_s, register)
        for stock in to_buy:

            if stock in self.holdings:
                continue

            price_per_share = register.get_close(stock, epoch_s)

            if self.cash >= self.trade_size:
                shares = self.trade_size / price_per_share
                shares = shares // 0.01 / 100
                self.buy(stock, price_per_share, shares)
Exemplo n.º 4
0
    def determine_sells(self, epoch_s: float,
                        register: SymbolDayRegister) -> List[str]:
        """
        Apply rules that trigger sell order
        """
        stocks = self.holdings.keys()
        to_sell = list()
        for stock in stocks:

            sell_flag = True
            prices = [None] * (self.n_down + 1)

            for i in range(self.n_down):

                close_today = register.get_close(stock, epoch_s, -i)
                if close_today is None:
                    sell_flag = False
                    break

                close_yesterday = register.get_close(stock, epoch_s, -i - 1)
                if close_yesterday is None:
                    sell_flag = False
                    break

                gain = 100 * (close_today / close_yesterday - 1)
                if gain > self.gain_down:
                    sell_flag = False
                    break

                prices[i] = close_today
                prices[i + 1] = close_yesterday

            if sell_flag:
                to_sell.append(stock)
                date = IntuitiveDateConverter.to_day_str(epoch_s)
                if self.logger is not None:
                    message = f'Identified SELL flag: {date} : {stock.to_str()} : {prices}'
                    self.logger.log_message(message)

        return to_sell
    def execute(self, epoch_s: float, register: SymbolDayRegister) -> None:

        if self.cash <= 10:
            return

        available_symbols = register.symbols_available(epoch_s)
        allocated_per_trade = self.cash / len(available_symbols)

        for symbol in available_symbols:
            price_per_share = register.get_close(symbol, epoch_s)
            shares = allocated_per_trade / price_per_share
            shares = shares // 0.01 / 100
            self.buy(symbol, price_per_share, shares)