For code/output blocks: Use ``` (aka backtick or grave accent) in a single line before and after the block. See:

Handling CTRL-C when trading live

  • Hi Everybody, dear @backtrader,

    I’d like to handle CTRL-C (SIGINT) when trading Live. In this case I’d like to trigger method runstop(), and in the stop() method I want to save the statistics of my trading to a DB. I googled this and added the recommended code:

        def next(self, frompre=False):
            try: logic here...
            except KeyboardInterrupt:
                self.log('CTRL-C detected - stopping')

    However, this does not work apparently:

    2017-05-16T12:27:25, Portfolio value: 99776.2408, cash: 97511.7685
    2017-05-16T12:27:25, Position: USD_CZK, size: 4093.0000, price: 23.8976, value: 97812.9177
    2017-05-16T12:27:25, Position: USD_MXN, size: 10487.0000, price: 18.7057, value: 196167.0954
    2017-05-16T12:27:25, Position: GBP_ZAR, size: 5780.0000, price: 16.9514, value: 97978.9764
    2017-05-16T12:27:25, Position: EUR_TRY, size: 24923.0000, price: 3.9304, value: 97956.6115
    forrtl: error (200): program aborting due to control-C event
    Image              PC                Routine            Line        Source
    libifcoremd.dll    00007FFDA97643E4  Unknown               Unknown  Unknown
    KERNELBASE.dll     00007FFDD2EE717D  Unknown               Unknown  Unknown
    KERNEL32.DLL       00007FFDD5DB2774  Unknown               Unknown  Unknown
    ntdll.dll          00007FFDD5FC0D61  Unknown               Unknown  Unknown

    How can I achieve the goal to be able to stop a live trading and in that moment save everything to the DB?

    Thanks and best regards,

  • administrators

Log in to reply