Navigation

    Backtrader Community

    • Register
    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    1. Home
    2. tw00000
    T
    • Flag Profile
    • Block User
    • Profile
    • Following
    • Followers
    • Topics
    • Posts
    • Best
    • Groups
    Save
    Saving

    tw00000

    @tw00000

    0
    Reputation
    52
    Posts
    454
    Profile views
    0
    Followers
    0
    Following
    Joined Last Online

    tw00000 Follow

    Posts made by tw00000

    • Trade "opened"

      In: https://www.backtrader.com/docu/trade.html?highlight=trade

      It reads:

      isopen (bool): records if any update has opened the trade

      I'm wondering which of these options this refers:

      1. Trade was filled
      2. Trade was sent to broker
      3. Trade was accepted

      Here's what I'm trying to achieve:

      At the end of a backtest, I'm saving all the trades to a pickle file so that I can examine them

      I would like to know, for each order (buy / sell / close): when the order was sent to the broker and when the order was filled.

      Here's what I have so far, but I'm not sure what I'm getting from trade.dtopen:

      for index, data in enumerate(strategy.datas):
              dataname = data._name
      
              data_trades = strategy._trades[data][0]
              trades = []
              for trade in data_trades:
                  try: 
                      trades.append({'ticker' : trade.data._name,
                                      'bar_closed' : len(trade.data),
                                      'trade_ref' : trade.ref,
                                      'pnl' : round(trade.pnl, 2),
                                      'bar_opened' : trade.baropen,
                                      'date_opened' : trade.data.num2date(trade.dtopen),
                                      'bar_closed' : trade.barclose,
                                      'date_closed' : trade.data.num2date(trade.dtclose),
                                      'n_bars_open_for' : trade.barlen,
                                      'entry_trade' : trade.history[0]['event']['order'].info,
                                      'exit_trade' : trade.history[1]['event']['order'].info,
                                      'entry_price' : trade.history[0]['event']['price'],
                                      'exit_price' : trade.history[1]['event']['price']
                                      })
                  except Exception as e:
                      pass
      

      Thanks for any assistance!

      posted in General Code/Help
      T
      tw00000
    • RE: Opening bracket limit buy and bracket limit sell order at same time

      I figured it out, I think!

      Since I was placing limit orders in both directions, and limit orders executed at your defined price or better, both long and short brackets were trying to execute simultaneously. I changed these to 'Stop' orders (which is kind of like market-buy-if-touched, for this purpose, right?) I get normal cancellation behavior, or like I would expect:

      *SUBMITTED* 185.56 LL_style open long trade (184.62, 184.95) 2018-09-14 2018-09-14 14:45:00
      *SUBMITTED* 182.78 LL_style close long trade SL (184.62, 184.95) 2018-09-14 2018-09-14 14:45:00
      *SUBMITTED* 186.95 LL_style close long trade TP (184.62, 184.95) 2018-09-14 2018-09-14 14:45:00
      *SUBMITTED* 184.01 LL_style open short trade (184.62, 184.95) 2018-09-14 2018-09-14 14:45:00
      *SUBMITTED* 182.63 LL_style close short trade TP (184.62, 184.95) 2018-09-14 2018-09-14 14:45:00
      *SUBMITTED* 186.77 LL_style close short trade SL  (184.62, 184.95) 2018-09-14 2018-09-14 14:45:00
      *ACCEPTED* 185.56 LL_style open long trade (184.62, 184.95) 2018-09-14 2018-09-14 14:45:00
      *ACCEPTED* 182.78 LL_style close long trade SL (184.62, 184.95) 2018-09-14 2018-09-14 14:45:00
      *ACCEPTED* 186.95 LL_style close long trade TP (184.62, 184.95) 2018-09-14 2018-09-14 14:45:00
      *ACCEPTED* 184.01 LL_style open short trade (184.62, 184.95) 2018-09-14 2018-09-14 14:45:00
      *ACCEPTED* 182.63 LL_style close short trade TP (184.62, 184.95) 2018-09-14 2018-09-14 14:45:00
      *ACCEPTED* 186.77 LL_style close short trade SL  (184.62, 184.95) 2018-09-14 2018-09-14 14:45:00
      *COMPLETED* 184.01 LL_style open short trade (184.62, 184.95) 2018-09-14 2018-09-14 16:00:00
      *CANCELING* LL_style open long trade LONG 2018-09-14 16:00:00
      *COMPLETED* 183.27 LL_style open short trade (184.05, 184.45) 2018-09-13 2018-09-14 17:00:00
      

      Until I find out later that something else mysteriously is going wrong, I think that solves it :) Hopefully this trail of tears helps someone in the future

      posted in General Code/Help
      T
      tw00000
    • RE: Opening bracket limit buy and bracket limit sell order at same time

      @backtrader said in Opening bracket limit buy and bracket limit sell order at same time:

      Well, you call addinfo with named arguments

      Could you show a quick example?

      posted in General Code/Help
      T
      tw00000
    • RE: Opening bracket limit buy and bracket limit sell order at same time

      Another version I'm attempting (also not working):

      In my init:

      self.bracket_orders = {}
      

      After my bracket orders in next:

      self.bracket_orders[self.shortside.ref] = self.longside
      self.bracket_orders[self.longside.ref] = self.shortside
      

      In my notify_order:

      if order.status in [order.Completed]:
          self.cancel(self.bracket_orders[order.ref])
      

      Trying to get creative here!

      This one ends up with a strange dance:

      *ACCEPTED* 144.67 open long trade 2017-09-06
      *ACCEPTED* 143.22 close long trade SL 2017-09-06
      *ACCEPTED* 147.2 close long trade TP 2017-09-06
      *ACCEPTED* 144.21 open short trade 2017-09-06
      *ACCEPTED* 141.69 close short trade TP 2017-09-06
      *ACCEPTED* 145.65 close short trade SL  2017-09-06
      *COMPLETED* 144.67 open long trade 2017-09-06
      *CANCELING* open short trade SHORT
      *COMPLETED* 144.21 open short trade 2017-09-06
      *CANCELING* open long trade LONG
      

      Where you can see that both orders end up canceled for some reason

      posted in General Code/Help
      T
      tw00000
    • RE: Opening bracket limit buy and bracket limit sell order at same time

      @backtrader said in Opening bracket limit buy and bracket limit sell order at same time:

      You probably want to check the order reference of the main order of a bracket as the key for a dictionary entry which contains a list with the orders of the other bracket (you would only need to cancel the main opposite order though)

      Here's what I have so far:

      In my next, after all of the code to create the brackets:

      self.shortside.addinfo(bracket_orders = {self.shortside.ref: self.longside, self.longside.ref: self.shortside})
      self.longside.addinfo(bracket_orders = {self.shortside.ref: self.longside, self.longside.ref: self.shortside})
      

      And then in notify_order:

              if order.status in [order.Completed]:
                  print(dict(order.info)['info']['bracket_orders'][order.ref])
                  self.cancel(dict(order.info)['info']['bracket_orders'][order.ref])
      

      This returns the error KeyError: 'bracket_orders'.

      I'm attempting to use this method I found in the Orders docs:

      info: custom information passed over method addinfo(). It is kept in the form of an OrderedDict which has been subclassed, so that keys can also be specified using ‘.’ notation

      But I haven't yet figured out how to use it, which I'm hoping allows you to add information to an order object.

      Even then, I think I could be a ways off, as "self.shortside" will be overwritten every time a new order comes into the pipeline

      posted in General Code/Help
      T
      tw00000
    • RE: Opening bracket limit buy and bracket limit sell order at same time

      @backtrader said in Opening bracket limit buy and bracket limit sell order at same time:

      Yes, but when one of brackets is active (because its middle order has executed) the other is still in the system awaiting activation. You probably want to manage that.

      I'm pretty sure this is my current problem. Could just be 3:20am brain, but I can't figure out how to manage it. I thought that by checking in notify_order if an order is (for example) a buy, and then checking if it's an "open" order, then canceling the other (sell) bracket, I would be avoiding this issue?

      posted in General Code/Help
      T
      tw00000
    • RE: Opening bracket limit buy and bracket limit sell order at same time

      Figured it out!

      If anyone tries this in the future -- don't forget your Sizer has to be able to handle placing those trades all at once :)

      posted in General Code/Help
      T
      tw00000
    • RE: Opening bracket limit buy and bracket limit sell order at same time

      Between this post: https://community.backtrader.com/topic/523/simultaneous-bracket-order-cancel-one-when-another-has-been-submitted-accepted/2

      and this post: https://community.backtrader.com/topic/1546/problem-with-bracket-orders

      I'm getting somewhere...

      My order placing is like this (truncated to not take up too much space):

                              self.longside = self.buy(data=dataname,
      
                              self.long_lowside  = self.sell(data=dataname, 
      
                              self.long_highside = self.sell(data=dataname, 
      
                              self.shortside = self.sell(data=dataname, 
      
                              self.short_lowside  = self.buy(data=dataname, 
      
                              self.short_highside = self.buy(data=dataname, 
      
      

      My notify_order is like this:

             def notify_order(self, order):
          
                  # if order.status in [order.Submitted]:
                  #     print('*SUBMITTED*', dict(order.info)['info'])
          
                  # if order.status in [order.Accepted]:
                      # print('*ACCEPTED*', dict(order.info)['info']['setup'],  dict(order.info)['info']['datetime'])
          
                  date = self.data.datetime.datetime()
                  if order.status in [order.Completed]:
                      # print('*COMPLETED*', dict(order.info)['info']['setup'], dict(order.info)['info']['datetime']) 
                      if order.isbuy():
                          if ('open trade' in dict(order.info)['info']['setup']) & ('LONG' in dict(order.info)['info']['direction']):
                              # print('*CANCELING*', dict(self.shortside.info)['info']['setup'], dict(self.shortside.info)['info']['direction'])
                              self.cancel(self.shortside)
                              # print('*CANCELING*', dict(self.short_lowside.info)['info']['setup'], dict(self.shortside.info)['info']['direction'])
                              self.cancel(self.short_lowside)
                              # print('*CANCELING*', dict(self.short_highside.info)['info']['setup'], dict(self.shortside.info)['info']['direction'])
                              self.cancel(self.short_highside)
                      elif order.issell():
                          if ('open trade' in dict(order.info)['info']['setup']) & ('SHORT' in dict(order.info)['info']['direction']):
                              # print('*CANCELING*', dict(self.longside.info)['info']['setup'], dict(self.longside.info)['info']['direction'])
                              self.cancel(self.longside)
                              # print('*CANCELING*', dict(self.long_lowside.info)['info']['setup'], dict(self.long_lowside.info)['info']['direction'])
                              self.cancel(self.long_lowside)
                              # print('*CANCELING*', dict(self.long_highside.info)['info']['setup'], dict(self.long_highside.info)['info']['direction'])
                              self.cancel(self.long_highside)
      

      I know it's not quite working yet because over 950+ trading days it only ends up placing 5-10 trades, where normally if I did longs only or shorts only it would be placing more like 300-500. Still troubleshooting...

      PS: I am ending each block that makes up a bracket order with a transmit=True

      posted in General Code/Help
      T
      tw00000
    • Opening bracket limit buy and bracket limit sell order at same time

      Is this possible?

      Here's the scenario: when a given signal occurs, I want to place a limit buy and a limit sell order, say 5% away in each direction from current price. Depending on which is entered, I would like an associated Stop Loss and Take Profit that gets attached to the main side order as well.

      Thanks for any help.

      (I think I saw a post once about a similar question, but wasn't able to find it)

      EDIT: I found the post: https://community.backtrader.com/topic/1546/problem-with-bracket-orders ... seems like it might be possible.

      Could I do something like this?

      mainside = self.buy(price=14.50, exectype=bt.Order.Limit, transmit=False)
      lowside  = self.sell(price=12.00, size=mainside.size, exectype=bt.Order.Stop,
                           transmit=False, parent=mainside)
      highside = self.sell(price=15.00, size=mainside.size, exectype=bt.Order.Limit,
                           transmit=False, parent=mainside)
      shortside = self.sell(price=12.50, exectype=bt.Order.Limit, transmit=False)
      shortstop  = self.buy(price=14.00, size= shortside.size, exectype=bt.Order.Stop,
                                                transmit=False, parent=shortside)
      shortprofit = self.buy(price=10.00, size= shortside.size, exectype=bt.Order.Limit,
                                                transmit=True, parent=shortside)
      

      Note: I'm leaving 'transmit' to False until the very last order, is that right?

      posted in General Code/Help
      T
      tw00000
    • RE: Using VWR

      @backtrader said in Using VWR:

      That blog link was used as the reference for the implementation. You may have a look at the results from the implementation here:

      • https://www.backtrader.com/blog/posts/2016-09-06-vwr/vwr.html

      Yes, I also saw those results, I guess you're referencing:

              TimeFrame.Years: 1.5368
              TimeFrame.Months: 1.5163
              TimeFrame.Weeks: 1.5383
              TimeFrame.Days: 1.5221
      

      These are hugely different results than what I'm seeing. Should the VWR be multiplied by some factor before getting those results?

      posted in Indicators/Strategies/Analyzers
      T
      tw00000
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 1 / 6