Navigation

    Backtrader Community

    • Register
    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    1. Home
    2. dongiulio
    For code/output blocks: Use ``` (aka backtick or grave accent) in a single line before and after the block. See: http://commonmark.org/help/
    D
    • Profile
    • Following 0
    • Followers 0
    • Topics 8
    • Posts 14
    • Best 2
    • Groups 0

    dongiulio

    @dongiulio

    3
    Reputation
    443
    Profile views
    14
    Posts
    0
    Followers
    0
    Following
    Joined Last Online

    dongiulio Unfollow Follow

    Best posts made by dongiulio

    • RE: TypeError: error() takes 2 positional arguments but 4 were given

      found my issue:

      A StopLimit order requires the plimit parameter, to specify at which price to activate the limit order, somehow I managed to get through backtesting without setting it, but interactive broker didn't like it.

      apologies for the noise.

      posted in General Code/Help
      D
      dongiulio
    • all data always available when backtesting with a pandas dataframe feed

      hello,

      I'm not sure what I'm doing wrong, I'm implementing an indicator and loading data from a pandas dataframe,

      here's how I load the data feed:

      data = bt.feeds.PandasData(dataname=pandas, **fxcm.candles_to_backtrader(pandas))
      
          # Add the Data Feed to Cerebro
          cerebro.adddata(data)
      
          # Run over everything
          cerebro.run()
      
      

      where fxcm.candles_to_backtrader(pandas) is defined as:

        def candles_to_backtrader(self, candles, ask=True):
              aob = "ask"
              if not ask:
                  aob = "bid"
      
              return {"name": aob,
                      "open": "%sopen" % aob,
                      "high": "%shigh" % aob,
                      "low": "%slow" % aob,
                      "close": "%sclose" % aob,
                      "volume": "tickqty"}
      

      my indicator is:

      import backtrader as bt
      
      
      class Density(bt.Indicator):
          lines = ('my_indicator',)
          params = ()
      
          def next(self):
              print("new iteration")
              for i in self.datas[0].close:
                  print("available data %f" % i)
      

      next gets called many times, and everytime the for loop prints all the close values as they were produced by candles_to_backtrader method.

      I'm quite confused, I expected that when backtesting I were to find in datas[0] only an incremental fraction of the input data at each call to next.

      So first call I'd see the oldest values, the second call I'd see the oldest and the following ones, and get the full dataset only at the latest call.

      What am I missing here?

      thanks.

      posted in Indicators/Strategies/Analyzers
      D
      dongiulio

    Latest posts made by dongiulio

    • unit testing strategies

      Hello,

      I'm trying to unit test my strategy, but I'm finding it hard to obtain an instantiated object for my Strategy class,

      these classes are supposed to be instantiated directly by Cerebro, so if I try instantiating them directly I get:

       *** AttributeError: 'NoneType' object has no attribute '_next_stid'
      

      on the other side, cerebro seems to hold only an uninstantiated version of the class, how could I get an instantiated Strategy Object so that I could unit test its methods?

      Thanks,

      posted in General Code/Help
      D
      dongiulio
    • existing order info from ibbroker

      Hello,

      Because TWS sometimes disconnects and sometimes requires re-login, I need to include an occasional restart of my trading system and TWS dockers.

      Because my TS is based on limit orders (and not market orders) when I restart there might be orders still pending from a previous session, I would need to extract from interactive brokers the info about the existing pending orders, so that I can manipulate them (i.e. cancel them)

      I'm struggling to get this info, I've explored:

      self.broker.orders which don't seem to be available for an IBBroker,
      self.broker.ib which has lots of info, but don't seem to see anything order related

      how could I get this info?

      Thanks,
      Giulio

      posted in General Code/Help
      D
      dongiulio
    • RE: TypeError: error() takes 2 positional arguments but 4 were given

      found my issue:

      A StopLimit order requires the plimit parameter, to specify at which price to activate the limit order, somehow I managed to get through backtesting without setting it, but interactive broker didn't like it.

      apologies for the noise.

      posted in General Code/Help
      D
      dongiulio
    • RE: TypeError: error() takes 2 positional arguments but 4 were given

      Apologies for the late response, somehow I ran out of free time for this project, and was busy with something else.

      @backtrader said in TypeError: error() takes 2 positional arguments but 4 were given:

      You could let us know that exectype=4 is StopLimit which differs from the standard Limit which is shown by Interactive Brokers for bracket oerder.

      exectype=4 is the result of exectype=bt.Order.StopLimit, I got that value by copy pasting the params for the method call.

      You could then look at this, which means the order wasn't sent and Interactive Brokers is probably giving you a huge warning and the TWS console is showing something, including orders which may have been created but not sent.
      Since you try again, TWS interprets you are ok with the huge ignored warning.

      I checked on TWS log, it complains about number format,

      2019-03-15 17:00:59.175 [HR] ERROR [JTS-EServerSocket-372] - [35:62:76:1:0:0:0:ERR] Client socket broken - 
      java.lang.NumberFormatException: For input string: "1.132"
        at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
        at java.lang.Integer.parseInt(Integer.java:580)
        at java.lang.Integer.parseInt(Integer.java:615)
        at jextend.bb.p(bb.java:908)
        at jextend.dI.aZ(dI.java:2517)
        at jextend.dI.run(dI.java:2114)
        at java.lang.Thread.run(Thread.java:748)
      

      obviously my entry price (1.132) is not an Int, I'm not sure how should I represent my entry price as an int.

      posted in General Code/Help
      D
      dongiulio
    • TypeError: error() takes 2 positional arguments but 4 were given

      Hello,

      I'm switching my strategy to paper trading on interactivebrokers, the strangest thing is happening, this is my error as logged:

      --------------------
      
      ---> pavement.run
      Traceback (most recent call last):
        File "/Users/me/Documents/me/trading/trader/trader_venv/lib/python3.7/site-packages/ib/ext/EClientSocket.py", line 958, in placeOrder
          self.send(order.m_tif)
        File "/Users/me/Documents/me/trading/trader/trader_venv/lib/python3.7/site-packages/ib/lib/overloading.py", line 82, in __call__
          return func(*args)
        File "/Users/me/Documents/me/trading/trader/trader_venv/lib/python3.7/site-packages/ib/ext/EClientSocket.py", line 1754, in send_1
          self.m_dos.write(val)
        File "/Users/me/Documents/me/trading/trader/trader_venv/lib/python3.7/site-packages/ib/lib/__init__.py", line 152, in write
          send(pack('!c', char))
      BrokenPipeError: [Errno 32] Broken pipe
      
      During handling of the above exception, another exception occurred:
      
      Traceback (most recent call last):
        File "/Users/me/Documents/me/trading/trader/trader_venv/lib/python3.7/site-packages/paver/tasks.py", line 201, in _run_task
          return do_task()
        File "/Users/me/Documents/me/trading/trader/trader_venv/lib/python3.7/site-packages/paver/tasks.py", line 198, in do_task
          return func(**kw)
        File "pavement.py", line 133, in run
          raise SystemExit(main([CODE_DIRECTORY] + args))
        File "/Users/me/Documents/me/trading/trader/trader/main.py", line 79, in main
          raise error
        File "/Users/me/Documents/me/trading/trader/trader/main.py", line 76, in main
          action(options)
        File "/Users/me/Documents/me/trading/trader/trader/main.py", line 40, in _live
          live.run()
        File "/Users/me/Documents/me/trading/trader/trader/live.py", line 59, in run
          self.stats = self.cerebro.run()
        File "/Users/me/Documents/me/trading/trader/trader_venv/lib/python3.7/site-packages/backtrader/cerebro.py", line 1127, in run
          runstrat = self.runstrategies(iterstrat)
        File "/Users/me/Documents/me/trading/trader/trader_venv/lib/python3.7/site-packages/backtrader/cerebro.py", line 1298, in runstrategies
          self._runnext(runstrats)
        File "/Users/me/Documents/me/trading/trader/trader_venv/lib/python3.7/site-packages/backtrader/cerebro.py", line 1630, in _runnext
          strat._next()
        File "/Users/me/Documents/me/trading/trader/trader_venv/lib/python3.7/site-packages/backtrader/strategy.py", line 347, in _next
          super(Strategy, self)._next()
        File "/Users/me/Documents/me/trading/trader/trader_venv/lib/python3.7/site-packages/backtrader/lineiterator.py", line 271, in _next
          self.next()
        File "/Users/me/Documents/me/trading/trader/trader/strategies/BankTradingStrategy.py", line 190, in next
          self.short_trade = self.place_short()
        File "/Users/me/Documents/me/trading/trader/trader/strategies/BankTradingStrategy.py", line 115, in place_short
          limitprice=price - limitdistance)
        File "/Users/me/Documents/me/trading/trader/trader/SupportStrategy.py", line 43, in sell_bracket
          orders = super(SupportStrategy, self).sell_bracket(**kargs)
        File "/Users/me/Documents/me/trading/trader/trader_venv/lib/python3.7/site-packages/backtrader/strategy.py", line 1217, in sell_bracket
          o = self.sell(**kargs)
        File "/Users/me/Documents/me/trading/trader/trader_venv/lib/python3.7/site-packages/backtrader/strategy.py", line 969, in sell
          **kwargs)
        File "/Users/me/Documents/me/trading/trader/trader_venv/lib/python3.7/site-packages/backtrader/brokers/ibbroker.py", line 391, in sell
          return self.submit(order)
        File "/Users/me/Documents/me/trading/trader/trader_venv/lib/python3.7/site-packages/backtrader/brokers/ibbroker.py", line 337, in submit
          self.ib.placeOrder(order.m_orderId, order.data.tradecontract, order)
        File "/Users/me/Documents/me/trading/trader/trader_venv/lib/python3.7/site-packages/backtrader/stores/ibstore.py", line 1286, in placeOrder
          self.conn.placeOrder(orderid, contract, order)
        File "/Users/me/Documents/me/trading/trader/trader_venv/lib/python3.7/site-packages/ib/lib/__init__.py", line 60, in inner
          return func(*args, **kwds)
        File "/Users/me/Documents/me/trading/trader/trader_venv/lib/python3.7/site-packages/ib/ext/EClientSocket.py", line 1155, in placeOrder
          self.error_0(id, EClientErrors.FAIL_SEND_ORDER, str(e))
        File "/Users/me/Documents/me/trading/trader/trader_venv/lib/python3.7/site-packages/ib/lib/__init__.py", line 60, in inner
          return func(*args, **kwds)
        File "/Users/me/Documents/me/trading/trader/trader_venv/lib/python3.7/site-packages/ib/ext/EClientSocket.py", line 1709, in error_0
          self.m_anyWrapper.error(id, errorCode, errorMsg)
        File "/Users/me/Documents/me/trading/trader/trader_venv/lib/python3.7/site-packages/ib/lib/overloading.py", line 82, in __call__
          return func(*args)
      TypeError: error() takes 2 positional arguments but 4 were given
      

      I can reproduce this with:

              (Pdb) self.sell_bracket(price= 1.1350936200039081, exectype= 4, stopprice= 1.1373796835411358, limitprice= 1.1298980210556635)
              *** TypeError: error() takes 2 positional arguments but 4 were given
              (Pdb) self.sell_bracket(price= 1.1350936200039081, exectype= 4, stopprice= 1.1373796835411358, limitprice= 1.1298980210556635)
              [<backtrader.brokers.ibbroker.IBOrder object at 0x12b0b9668>, <backtrader.brokers.ibbroker.IBOrder object at 0x12b0b9908>, <backtrader.brokers.ibbroker.IBOrder object at 0x12b0b9ac8>]
              (Pdb)
      

      basically sell_bracket fails only at the first call. the following ones see no problems

      any ideas?

      posted in General Code/Help
      D
      dongiulio
    • broker cash and value on separate scales in the plot

      Hello,

      When plotting the results of a backtest the broker cash and value in the top portion are on the same scale. like in the screenshotbroker cash and value

      With forex broker cash varies on a way greater scale than the value, so the value is usually shown as a semi flat line, making it difficult to appreciate its variations, I think it would be nice to have the two lines on separate scales, so that both can be appreciated visually.

      What do you think?

      posted in Indicators/Strategies/Analyzers
      D
      dongiulio
    • RE: problem with bracket orders

      @backtrader thanks for the link,

      this:

      • Issue a Market order
      • Upon confirmation of execution you have the actual execution price and the actual market price (close)
      • You can then issue two OCO orders which will bracket your existing positions. You calculate the stop-loss and take-profit prices from the execution price or from the market price

      this was actually my initial implementation, I'm trying to move away from it because while it would work fine with real time trading, it introduces a bias in backtesting.
      I explain: I understand that, in backtesting, events happen only at the end of full candles, it might happen that the bracket orders would have been executed in the same candle of the entry,
      but obviously won't, because in backtrading they will be submitted only at the end of the candle.

      maybe I could work it around (actually just diminish the bias) by backtesting with minute candles and resampling it to hour?

      posted in General Code/Help
      D
      dongiulio
    • RE: problem with bracket orders

      @backtrader said in problem with bracket orders:

      I would even ask myself if a StopLimit order for the mainside is possible in a real broker, because that order type is in many occasions simulated.

      fair point, stoplimit might be simulated, but for all intents I don't think it would make a massive difference in strategies that use it, as long as one knows what's going on.

      Though I get the same issue if I change to use simple stop orders, rather than stoplimit:

      self.long_trade = self.buy_bracket(
                      price=entry_long,
                      exectype=bt.Order.Stop,
                      stopprice=stop_long,
                      limitprice=limit_long
                      )
      
                  self.short_trade = self.sell_bracket(
                      price=entry_short,
                      exectype=bt.Order.Stop,
                      stopprice=stop_short,
                      limitprice=limit_short,
                      oco=self.long_trade[0]
                      )
      

      It doesn't look like the issue comes from the execution type.

      posted in General Code/Help
      D
      dongiulio
    • RE: problem with bracket orders

      hello,

      thanks for your reply,

      I don't understand what you mean: do you reckon that it wouldn't be possible to place two orders like these with a broker?

      Imgur

      basically this structure:
      Imgur

      posted in General Code/Help
      D
      dongiulio
    • problem with bracket orders

      Hello,

      I found something that I don't understand on how bracket orders work, I'll explain:

      my strategy tries to catch runaways using the bollinger bands, so what I do is to place:

      two stoplimit bracket orders: buy above current price, and a sell below current price,

      These should open respectively a long and short position.
      They are also OCO, so that if a long is open then the short will not, and vice versa.

      This is my code to place such orders:

                  self.long_trade = self.buy_bracket(
                      price=entry_long,
                      exectype=bt.Order.StopLimit,
                      plimit=entry_long,
                      stopprice=stop_long,
                      limitprice=limit_long
                      )
      
                  self.short_trade = self.sell_bracket(
                      price=entry_short,
                      exectype=bt.Order.StopLimit,
                      plimit=entry_short,
                      stopprice=stop_short,
                      limitprice=limit_short,
                      oco=self.long_trade[0]
                      )
      

      With this I expect that both stoplimit orders get accepted, if price allows one of them to be opened (say the long/buy one), the other order (the short/sell and its respective bracked orders ) gets canceled , at the same time the two bracket orders (at stopprice and limitprice) become active.

      With these orders my strategy can open as many long positions as it wants and everything works as expected. As soon as the price hits a short/sell order, something weird happens, here's my logs to show it:

      ORDER 151,              main status:  Accepted -  Buy,  price: 1.04395
      ORDER 152, (parent 151) stop status:  Accepted -  Sell, price: 1.03978
      ORDER 153, (parent 151) limit status: Accepted -  Sell, price: 1.04645
      ORDER 154,              main status:  Accepted -  Sell, price: 1.03895
      ORDER 155, (parent 154) stop status:  Accepted -  Buy,  price: 1.04312
      ORDER 156, (parent 154) limit status: Accepted -  Buy,  price: 1.03645
      ORDER 154,              main status:  Completed - Sell, price: 1.03895 actual price: 1.03895
      ORDER 151,              main status:  Canceled -  Buy,  price: 1.04395
      ORDER 156, (parent 154) limit status: Canceled -  Buy,  price: 1.03645
      ORDER 155, (parent 154) stop status:  Canceled -  Buy,  price: 1.04312
      
      • orders [151, 152, 153] are respectively the main, stop, limit orders for the buy_bracket,
      • orders [154, 155, 156] are respectively the main, stop, limit orders for the sell_bracket,
      • order 154 is completed, so the main buy order is canceled as expected,
      • orders [152, 153] should be canceled too but they are not.
      • orders [155, 156] should NOT be canceled, but they are

      this leaves my strategy with an open position (which prevents from creating further orders) that has no orders to close it, effectively hanging the whole strategy.

      I tried reverting the order creation in the code, first creating the short_trade, and lastly creating the long_trade:

                  self.short_trade = self.sell_bracket(
                      price=self.sma - distance,
                      exectype=bt.Order.StopLimit,
                      plimit=self.sma - distance,
                      stopprice=self.sma + stopdistance,
                      limitprice=self.sma - limitdistance)
      
                  self.long_trade = self.buy_bracket(
                      price=self.sma + distance,
                      exectype=bt.Order.StopLimit,
                      plimit=self.sma + distance,
                      stopprice=self.sma - stopdistance,
                      limitprice=self.sma + limitdistance,
                      oco=self.short_trade[0])
      

      and exactly the reverse happens, I can open and close short trades no problems, but as the first long/buy trade is opened its bracket orders are canceled and the whole strategy stops working.

      I'm not sure what I'm doing wrong here, any ideas?

      posted in General Code/Help
      D
      dongiulio