'Executing multiple python class instances simultaneously

I am working on a basic trading bot as an intro to classes project. It implements a class TradingBot and contains the method trade(self). I have multiple instances (t_account01, t_account02...)that take different API keys, symbol, interval and various other values. If I execute only one instance such as t_account01.trade(), I will get this output:

Time Program Started: 2022-03-15 12:17:02.143325

Initial Balance: 50000

Order Size: 18.68

Threading count: 23

Open Price: 39101

Last Price: 39122.0

Percentage Change: 0.0537 %

Threading count: 37

Open Price: 39101

Last Price: 39124.5

etc.

This trade method within the class is essentially outputting the balance, start time and order size based on balance for a specific API key at the start of the program. Additionally, it is constantly checking the opening price of a candle based on the given interval, of a given symbol, comparing it to the current price and calculating the percentage. If it attains a specific percentage then it executes a trade.

In theory, it works just fine when running one instance, but when running two or more, only the first instance appears in the run window and I am unsure if the others are also simultaneously running as only the first instance appears. I am trying to get multiple instances running at the same time (forever or until i stop it) however I am unsure as the way I have tried as shown below in the main of the code does not seem to work. Ideally I would like to see all the data being outputted for each instance but I have a feeling it is not possible. (I omitted a bunch of other methods and only included the trade method for shortness along with the sensitive api keys).

class TradingBot:

    def __init__(self, ENDPOINT_REAL_PUBLIC, ENDPOINT_REAL, HTTP_ADDRESS, MAX_ORDER_SIZE, API_KEY, API_SECRET, AVAIL_BAL_REQ,
                 LEVERAGE, SYMBOL, INTERVAL, PERC_CHANGE, PERC_EXIT, STOP_LOSS, SLEEP_OPEN):
        self.INTERVAL = INTERVAL
        self.ENDPOINT_REAL_PUBLIC = ENDPOINT_REAL_PUBLIC
        self.ENDPOINT_REAL = ENDPOINT_REAL
        self.SYMBOL = SYMBOL
        self.LEVERAGE = LEVERAGE
        self.MAX_ORDER_SIZE = MAX_ORDER_SIZE
        self.STOP_LOSS = STOP_LOSS
        self.AVAIL_BAL_REQ = AVAIL_BAL_REQ
        self.PERC_CHANGE = PERC_CHANGE
        self.PERC_EXIT = PERC_EXIT
        self.SLEEP_OPEN = SLEEP_OPEN
        self.API_KEY = API_KEY
        self.API_SECRET = API_SECRET
        self.HTTP_ADDRESS = HTTP_ADDRESS
        self.session = HTTP(self.HTTP_ADDRESS,  # for MAINNET: https://api.bybit.com
                       api_key=self.API_KEY,  # Testnet API
                       api_secret=self.API_SECRET)  # Testnet Secret
        self.ws = WebSocket(
            endpoint=self.ENDPOINT_REAL,  # for MAINNET: wss://stream.bybit.com/realtime
            subscriptions=['order', 'position'],
            api_key=self.API_KEY,  # Testnet API
            api_secret=self.API_SECRET,  # Testnet Secret
            ping_interval=30,
            ping_timeout=None,
            restart_on_error=True)


    def trade(self):
        self.set_leverage()

        print()
        print("Time Program Started: ", self.current_date_time())
        print("Initial Balance: ", self.avail_balance_calc())
        print("Order Size: ", self.order_size_calc())
        print()


        count = 0
        while count < 11:
            self.open_position_size()
            self.open_position_price()
            self.open_position_size_short()
            self.open_position_price_short()

            print("Threading count: ", threading.active_count())
            if threading.active_count() >= 1750:  # 2000 is approx max thread count
                subprocess.call([sys.executable, os.path.realpath(__file__)] + sys.argv[1:])

            if (self.open_position_size() == 0) and (self.open_position_size_short() == 0) and (self.avail_balance_calc() > self.AVAIL_BAL_REQ):
                print("Open Price: ", self.open_price_calc())
                print("Last Price: ", self.last_price_calc())
                print("Percentage Change: ", round((self.perc_change_calc() * 100), 4), "%")

                # LONG ORDER & POSITION
                if self.perc_change_calc() >= self.PERC_CHANGE:
                    self.market_long_order()

                    # Position Info
                    self.open_position_size()
                    self.open_position_price()
                    print("Open Position Size: ", self.open_position_size())
                    print("Open Position Entry Price: ", self.open_position_price())

                    # Limit long close position
                    limitPriceLong = round((self.open_position_price() * self.PERC_EXIT))
                    print(self.session.place_active_order(
                        symbol=self.SYMBOL,
                        side="Sell",
                        order_type="Limit",
                        qty=self.open_position_size(),
                        price=limitPriceLong,
                        time_in_force="GoodTillCancel",
                        reduce_only=True,  # true means close order, false means open position
                        close_on_trigger=True
                    ))
                    count += 1
                    print("Current trade count: ", count)
                    time.sleep(self.SLEEP_OPEN)  # sleep 2 minutes (120 seconds)

                # SHORT ORDER & POSITION
                if self.perc_change_calc() <= (-self.PERC_CHANGE):
                    self.market_short_order()

                    # Position Info
                    self.open_position_size_short()
                    self.open_position_price_short()
                    print("Open Position Size: ", self.open_position_size_short())
                    print("Open Position Entry Price: ", self.open_position_price_short())

                    # Limit close position
                    limitPriceShort = round((self.open_position_price_short() / self.PERC_EXIT))
                    print(self.session.place_active_order(
                        symbol=self.SYMBOL,
                        side="Buy",
                        order_type="Limit",
                        qty=self.open_position_size_short(),
                        price=limitPriceShort,
                        time_in_force="GoodTillCancel",
                        reduce_only=True,  # true means close order, false means open position
                        close_on_trigger=True
                    ))
                    count += 1
                    print("Current trade count: ", count)
                    time.sleep(self.SLEEP_OPEN)  # sleep 2 minutes (120 seconds)

        print()
        print("Total trade count: ", count)
        print("Program has reached maximum amount of trades")
        print()
        print("Time Program Ended: ", self.current_date_time())
        print("Final Balance: ", self.avail_balance_calc())
        print()

if __name__ == "__main__":

    t_account01 = TradingBot("wss:...",'wss:...',"https:...",20,'API','API',2500,15,"BTCUSDT","candle.5.BTCUSDT",0.0025,1.0005,1.0005,300)
    t_account02 = TradingBot("wss:...",'wss:...',"https:...",20,'API','API',2500,15,"BTCUSDT","candle.5.BTCUSDT",0.0025,1.0010,1.0010,300)
    t_account03 = TradingBot("wss:...",'wss:...',"https:...",20,'API','API',2500,15,"BTCUSDT","candle.5.BTCUSDT",0.0030,1.0010,1.0010,300)

    
    t_account01.trade()
    t_account02.trade()
    t_account03.trade()


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source