I have a working trading code. On KeyboardInterrupt I’d like that a custom function is executed. For example, in case of bale out I want that all positions are sold. My code structure is sth as follows:
def start_trading(self): conn = tradeapi.StreamConn(...) ... @conn.on(r'AM\..+$') #^AM$ async def on_minute_bars(conn, channel, bar): .... try: conn.run(self.channels) except KeyboardInterrupt as Key: self.my_custom_func() self.log_msg.close()
The problem is that self.my_cusom_func() does not execute on the KeyboardInterrupt, which I’d expect. So where shall I place the self.my_custom_func() in order to be executed on the KeyboardInterrupt?