Stream - need a way to manually change "state" while running the stream

I am building my first trading bot, and I have the pseudo code below, my issue is in run_connection() I think. Basically I need a way for the user to change the state of the system somehow while the stream is running, so that I can go from state 1 (buying/selling) to 2 (just selling). Essentially, I need a way to gracefully get the bot to exit after a while. The only way I can think of to accept user input while the stream is running is to catch KeyboardInterrupt exceptions, and change the state based on the current state, but I cannot get it to do that properly. Any help with this or other ways of accomplishing the same thing are much appreciated!

async def trade_bars(bars):
    # if the state is 1:
        # do BOTH buying (add buys to a holdings dictionary) and selling
    # if the state is 2:
        # do ONLY selling of items in the holdings dictionary
        # if all holdings are sold:
            # raise a keyboard interrupt programatically to stop running the stream

def run_connection(stream):
    except KeyboardInterrupt:
        # if the state is 1:
            # change the state to 2 and then CONTINUE (not stop), running the stream
        # if the state is 2:
            # stop running the stream 
    except Exception as e:
        print(f'Exception from websocket connection: {e}')
        # restart the stream

if __name__ == "__main__":
    # set the "state" to 1
    stream = Stream(
        d_globals['ALPACA_API_KEY'], d_globals['ALPACA_SECRET_KEY'],
        base_url=d_globals['API_URL'], raw_data=False,
        data_feed='iex', crypto_exchanges = ['CBSE']
    stream.subscribe_crypto_bars(trade_bars, d_globals['symbol'])