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

ib paper live trade error



  • After I used bt.sizers.PercentSizer
    cerebro.addsizer(bt.sizers.PercentSizer, percents=90)

    I got an error

    ***** DATA NOTIF: LIVE
    1442,2020-06-09T20:25:25,107.75,107.75,107.75,107.75,0.00
    Traceback (most recent call last):
      File "/Users/me/anaconda3/envs/backtr/lib/python3.5/site-packages/ib/ext/EClientSocket.py", line 976, in placeOrder
        self.send(order.m_outsideRth)
      File "/Users/me/anaconda3/envs/backtr/lib/python3.5/site-packages/ib/lib/overloading.py", line 82, in __call__
        return func(*args)
      File "/Users/me/anaconda3/envs/backtr/lib/python3.5/site-packages/ib/ext/EClientSocket.py", line 1786, in send_4
        self.send(1 if val else 0)
      File "/Users/me/anaconda3/envs/backtr/lib/python3.5/site-packages/ib/lib/overloading.py", line 82, in __call__
        return func(*args)
      File "/Users/me/anaconda3/envs/backtr/lib/python3.5/site-packages/ib/ext/EClientSocket.py", line 1765, in send_3
        self.send(str(val))
      File "/Users/me/anaconda3/envs/backtr/lib/python3.5/site-packages/ib/lib/overloading.py", line 82, in __call__
        return func(*args)
      File "/Users/me/anaconda3/envs/backtr/lib/python3.5/site-packages/ib/ext/EClientSocket.py", line 1755, in send_1
        self.sendEOL()
      File "/Users/me/anaconda3/envs/backtr/lib/python3.5/site-packages/ib/ext/EClientSocket.py", line 1744, in sendEOL
        self.m_dos.write(self.EOL)
      File "/Users/me/anaconda3/envs/backtr/lib/python3.5/site-packages/ib/lib/__init__.py", line 147, in write
        send(eol)
    BrokenPipeError: [Errno 32] Broken pipe
    
    During handling of the above exception, another exception occurred:
    
    Traceback (most recent call last):
      File "/Users/me/quan/bt/ibtutorial.py", line 86, in <module>
        run()
      File "/Users/me/quan/bt/ibtutorial.py", line 82, in run
        cerebro.run()
      File "/Users/me/anaconda3/envs/backtr/lib/python3.5/site-packages/backtrader/cerebro.py", line 1127, in run
        runstrat = self.runstrategies(iterstrat)
      File "/Users/me/anaconda3/envs/backtr/lib/python3.5/site-packages/backtrader/cerebro.py", line 1298, in runstrategies
        self._runnext(runstrats)
      File "/Users/me/anaconda3/envs/backtr/lib/python3.5/site-packages/backtrader/cerebro.py", line 1630, in _runnext
        strat._next()
      File "/Users/me/anaconda3/envs/backtr/lib/python3.5/site-packages/backtrader/strategy.py", line 347, in _next
        super(Strategy, self)._next()
      File "/Users/me/anaconda3/envs/backtr/lib/python3.5/site-packages/backtrader/lineiterator.py", line 271, in _next
        self.next()
      File "/Users/me/quan/bt/ibtutorial.py", line 64, in next
        self.buy()
      File "/Users/me/anaconda3/envs/backtr/lib/python3.5/site-packages/backtrader/strategy.py", line 939, in buy
        **kwargs)
      File "/Users/me/anaconda3/envs/backtr/lib/python3.5/site-packages/backtrader/brokers/ibbroker.py", line 379, in buy
        return self.submit(order)
      File "/Users/me/anaconda3/envs/backtr/lib/python3.5/site-packages/backtrader/brokers/ibbroker.py", line 337, in submit
        self.ib.placeOrder(order.m_orderId, order.data.tradecontract, order)
      File "/Users/me/anaconda3/envs/backtr/lib/python3.5/site-packages/backtrader/stores/ibstore.py", line 1286, in placeOrder
        self.conn.placeOrder(orderid, contract, order)
      File "/Users/me/anaconda3/envs/backtr/lib/python3.5/site-packages/ib/lib/__init__.py", line 60, in inner
        return func(*args, **kwds)
      File "/Users/me/anaconda3/envs/backtr/lib/python3.5/site-packages/ib/ext/EClientSocket.py", line 1155, in placeOrder
        self.error_0(id, EClientErrors.FAIL_SEND_ORDER, str(e))
      File "/Users/me/anaconda3/envs/backtr/lib/python3.5/site-packages/ib/lib/__init__.py", line 60, in inner
        return func(*args, **kwds)
      File "/Users/me/anaconda3/envs/backtr/lib/python3.5/site-packages/ib/ext/EClientSocket.py", line 1709, in error_0
        self.m_anyWrapper.error(id, errorCode, errorMsg)
      File "/Users/me/anaconda3/envs/backtr/lib/python3.5/site-packages/ib/lib/overloading.py", line 82, in __call__
        return func(*args)
    TypeError: error() takes 2 positional arguments but 4 were given
    

    Below are full code:

    
    from __future__ import unicode_literals
    # -*- coding: utf-8; py-indent-offset:4 -*-
    ###############################################################################
    #
    # Copyright (C) 2018 Daniel Rodriguez
    #
    # This program is free software: you can redistribute it and/or modify
    # it under the terms of the GNU General Public License as published by
    # the Free Software Foundation, either version 3 of the License, or
    # (at your option) any later version.
    #
    # This program is distributed in the hope that it will be useful,
    # but WITHOUT ANY WARRANTY; without even the implied warranty of
    # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    # GNU General Public License for more details.
    #
    # You should have received a copy of the GNU General Public License
    # along with this program.  If not, see <http://www.gnu.org/licenses/>.
    #
    ###############################################################################
    from __future__ import (absolute_import, division, print_function,
                            unicode_literals)
    
    import backtrader as bt
    
    
    class St(bt.Strategy):
        def logdata(self):
            txt = []
            txt.append('{}'.format(len(self)))
            txt.append('{}'.format(self.data.datetime.datetime(0).isoformat()))
            txt.append('{:.2f}'.format(self.data.open[0]))
            txt.append('{:.2f}'.format(self.data.high[0]))
            txt.append('{:.2f}'.format(self.data.low[0]))
            txt.append('{:.2f}'.format(self.data.close[0]))
            txt.append('{:.2f}'.format(self.data.volume[0]))
            print(','.join(txt))
    
        data_live = False
    
        def notify_data(self, data, status, *args, **kwargs):
            print('*' * 5, 'DATA NOTIF:', data._getstatusname(status), *args)
            if status == data.LIVE:
                self.data_live = True
    
        def notify_order(self, order):
            if order.status == order.Completed:
                buysell = 'BUY ' if order.isbuy() else 'SELL'
                txt = '{} {}@{}'.format(buysell, order.executed.size,
                                        order.executed.price)
                print(txt)
    
        bought = 0
        sold = 0
    
        def next(self):
            self.logdata()
            if not self.data_live:
                return
    
            if not self.bought:
                self.bought = len(self)  # keep entry bar
                self.buy()
            elif not self.sold:
                if len(self) == (self.bought + 3):
                    self.sell()
    
    
    def run(args=None):
        cerebro = bt.Cerebro(stdstats=False)
        store = bt.stores.IBStore(port=7497)
    
        data = store.getdata(dataname='USD.JPY-CASH-IDEALPRO', timeframe=bt.TimeFrame.Ticks)
        cerebro.resampledata(data, timeframe=bt.TimeFrame.Seconds, compression=5)
    
        cerebro.broker = store.getbroker()
    
        cerebro.addstrategy(St)
        cerebro.addsizer(bt.sizers.PercentSizer, percents=90)
    #    cerebro.addsizer(bt.sizers.SizerFix, stake=3000)
        cerebro.run()
    
    
    if __name__ == '__main__':
        run()
    
    
    


  • It seems the connection to IB broker was terminated from the server side. You may want to take a look at the TWS API Logs to see what was the cause (it could be a lot of different reasons for connection drop)


Log in to reply
 

});