Backtrader Community

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

    Posts made by T H

    • positionFill option doesn't work when I call close().

      Hi, everyone.
      I'm trying to specify the positionFill option when I call close().
      But it does not work properly.
      I use OANDA live trading.

      Thank you very much for your help.

      My code:

          kwargs = dict(
              positionFill="REDUCE_ONLY"
          )
          self.close(**kwargs)
      

      notify_store:('positionFill': 'DEFAULT' Why???)

      *****, STORE NOTIF:, {'id': '902', 'time': '1605605285.328517100', 'userID': 12345678, 'accountID': '101-009-12345678-001', 'batchID': '902', 'requestID': '78791990707426496', 'type': 'MARKET_ORDER', 'instrument': 'GBP_JPY', 'units': '-20.0', 'timeInForce': 'FOK', 'positionFill': 'DEFAULT', 'reason': 'CLIENT_ORDER', 'clientExtensions': {'id': '1605604583.042589-37'}}
      

      FYI: Oanda V20 API Reference
      https://developer.oanda.com/rest-live-v20/order-df/#MarketOrderRequest

      posted in General Code/Help
      T H
      T H
    • Connection to v20 REST server at Oanda live server failed.

      Hi!
      My program using bt works both PRACTICE and LIVE on my local, but on the server it only works PRACTICE.
      The connection to the Oanda server fails in LIVE case.
      Of course, I know it's not bt fault.
      I would like to know how to check detail of this connection error.
      Or, if anyone knows of this event, please advise.

      Thanks.

      2020-10-05 00:56:29,713 [    INFO] setting up API-client for environment live
      2020-10-05 00:56:29,716 [    INFO] *****, STORE NOTIF:, Connection to v20 REST server at https://api-fxtrade.oanda.com:443/v3/accounts/001-001-1234567-001/summary failed
      2020-10-05 00:56:29,716 [    INFO] *****, STORE NOTIF:, Connection to v20 REST server at https://stream-fxtrade.oanda.com:443/v3/accounts/001-001-1234567-001/transactions/stream failed
      2020-10-05 00:56:29,716 [    INFO] *****, STORE NOTIF:, Connection to v20 REST server at https://api-fxtrade.oanda.com:443/v3/accounts/001-001-1234567-001/openPositions failed
      2020-10-05 00:56:29,716 [    INFO] *****, STORE NOTIF:, Trying to reconnect streaming events (1 of -1)
      2020-10-05 00:56:29,717 [    INFO] *****, STORE NOTIF:, Connection to v20 REST server at https://api-fxtrade.oanda.com:443/v3/accounts/001-001-1234567-001/instruments failed
      2020-10-05 00:56:29,717 [    INFO] *****, STORE NOTIF:, Connection to v20 REST server at https://api-fxtrade.oanda.com:443/v3/accounts/001-001-1234567-001/summary failed
      2020-10-05 00:56:29,717 [    INFO] *****, STORE NOTIF:, Connection to v20 REST server at https://stream-fxtrade.oanda.com:443/v3/accounts/001-001-1234567-001/transactions/stream failed
      
      posted in General Code/Help
      T H
      T H
    • RE: Order Synchronization

      @dasch Got it. Thanks. Please take care of the rest.

      posted in General Code/Help
      T H
      T H
    • RE: Order Synchronization

      @dasch Thank you for understanding it.
      But I’m not sure how to open an issue on Github.
      Anyways, I tried to open this issue.
      This is my first time.
      Is this Okay?

      https://github.com/happydasch/btoandav20/issues/60

      posted in General Code/Help
      T H
      T H
    • RE: Order Synchronization

      @dasch Yes, That's right.
      But this msg_type is current stream's msg_type.
      What this code wants to handle is old transactions.

      posted in General Code/Help
      T H
      T H
    • RE: Order Synchronization

      only this "if".

      if msg_type == "transaction.Transaction":  # Is this code necessary? *** HERE ***
      
      posted in General Code/Help
      T H
      T H
    • RE: Order Synchronization

      @dasch Hi. It's been a long time.
      Please tell me your opinion.
      I think the below code is not necessary.(That is "*** HERE ***" in code below)
      Because if msg_type that got from a stream is "transaction.TransactionHeartbeat", this fetching code doesn't work.

      oandav20store.py

                      response = self.oapi_stream.transaction.stream(
                          self.p.account
                      )
                      # process response
                      for msg_type, msg in response.parts():
                          # if a connection occurred
                          if reconnections > 0:
                              if reconnections > 0 and last_id:
                                  # get all transactions between the last seen and first from
                                  # reconnected stream
                                  old_transactions = self.get_transactions_since(
                                      last_id)
                                  for t in old_transactions:
                                      if msg_type == "transaction.Transaction":  # Is this code necessary? *** HERE ***
                                      
                                          if t.id > last_id:
                                              self._transaction(t.dict())
                                              last_id = t.id
                              reconnections = 0
                          if msg_type == "transaction.Transaction":
                              if not last_id or msg.id > last_id:
                                  self._transaction(msg.dict())
                                  last_id = msg.id
      

      What do you think about this?
      (I'm sorry this isn't about backtrader.)

      Thanks.

      posted in General Code/Help
      T H
      T H
    • RE: Order Synchronization

      @dasch I might have solved this problem.
      Sometimes, I had created a new order and had canceled an other order in the one Next() iteration until now.
      I tried to delay the timing of canceling order.
      I have been running it all day, but now this problem isn't occurring.

      Thank you for everything you've done for me!

      posted in General Code/Help
      T H
      T H
    • RE: Order Synchronization

      @dasch Oh... thank you for your information. Anyway, I'll check all of my code once.
      By the way, In general, does this happen frequently with btoandav20? Or Is this rare in my case?

      posted in General Code/Help
      T H
      T H
    • RE: Order Synchronization

      I tried several ways.
      Unfortunately, it didn't improve.

      Then I changed create_new_order() method in the following.

      def create_new_order(self, price, exec_type):
          stopprice = price - (self.order_type_sign * self.max_range_price)
          limitprice = price + (self.order_type_sign * self.interval_price)
      
          self.tradeid_seq +=1
          
          orders = None
          if self.order_type == bt.Order.Buy:
              # orders = self.buy_bracket(size=self.order_size, price=price, exectype=exec_type, 
              #                     # tradeid=self.tradeid_seq, 
              #                     stopprice=stopprice, stopexec=bt.Order.Stop, 
              #                     limitprice=limitprice, limitexec=bt.Order.Limit)
      
              main_order = self.buy(size=self.order_size, price=price, exectype=exec_type, transmit=False)
              stop_order  = self.sell(price=stopprice, size=main_order.size, exectype=bt.Order.Stop,
                                  transmit=False, parent=main_order)
              limit_order = self.sell(price=limitprice, size=main_order.size, exectype=bt.Order.Limit,
                                  transmit=True, parent=main_order)
              
              orders = [main_order, stop_order, limit_order]
          elif self.order_type == bt.Order.Sell:
              # orders = self.sell_bracket(size=self.order_size, price=price, exectype=exec_type, 
              #                     # tradeid=self.tradeid_seq, 
              #                     stopprice=stopprice, stopexec=bt.Order.Stop, 
              #                     limitprice=limitprice, limitexec=bt.Order.Limit)
      
              main_order = self.sell(size=self.order_size, price=price, exectype=exec_type, transmit=False)
              stop_order  = self.buy(price=stopprice, size=main_order.size, exectype=bt.Order.Stop,
                                  transmit=False, parent=main_order)
              limit_order = self.buy(price=limitprice, size=main_order.size, exectype=bt.Order.Limit,
                                  transmit=True, parent=main_order)
              
              orders = [main_order, stop_order, limit_order]
          
          return orders
      

      But, it still didn't improve.
      There is cases where the order object differs from the actual state.

      But then I realized that there is no id of transactions from 1845 to 1847.

      Do you have any good solutions in regards to that?

      NEW ORDER
      ( type:Sell, size:300, price:136.18999999999988, exectype:2(Limit), stopprice:146.18999999999988, limitprice:136.13999999999987 )

      2020-09-11 19:36:37,706 [    INFO] *****, STORE NOTIF:, {'id': '1844', 'time': '1599820561.598982864', 'userID': 10733067, 'accountID': '101-009-10733067-005', 'batchID': '1844', 'requestID': '24724532290588330', 'type': 'LIMIT_ORDER', 'instrument': 'GBP_JPY', 'units': '-300.0', 'price': 136.19, 'timeInForce': 'GTC', 'positionFill': 'DEFAULT', 'triggerCondition': 'DEFAULT', 'reason': 'CLIENT_ORDER', 'clientExtensions': {'id': '1599755079.86117-565'}, 'takeProfitOnFill': {'price': 136.14, 'timeInForce': 'GTC', 'clientExtensions': {'id': '1599755079.86117-567'}}, 'stopLossOnFill': {'price': 146.19, 'timeInForce': 'GTC', 'clientExtensions': {'id': '1599755079.86117-566'}}}
      
      2020-09-11 19:37:20,039 [    INFO] *****, STORE NOTIF:, Connection to v20 REST server at https://stream-fxpractice.oanda.com:443/v3/accounts/101-009-10733067-005/transactions/stream failed
      2020-09-11 19:37:25,790 [    INFO] *****, STORE NOTIF:, Trying to reconnect streaming events (1 of -1)
      
      
      2020-09-11 19:48:21,639 [    INFO] *****, STORE NOTIF:, {'id': '1848', 'time': '1599821264.782127550', 'userID': 10733067, 'accountID': '101-009-10733067-005', 'batchID': '1848', 'type': 'ORDER_FILL', 'orderID': '1846', 'clientOrderID': '1599755079.86117-567', 'instrument': 'GBP_JPY', 'units': '300.0', 'gainQuoteHomeConversionFactor': '1.0', 'lossQuoteHomeConversionFactor': '1.0', 'price': 136.14, 'fullVWAP': 136.14, 'fullPrice': {'type': 'PRICE', 'bids': [{'price': 136.126, 'liquidity': '250000'}], 'asks': [{'price': 136.14, 'liquidity': '250000'}], 'closeoutBid': 136.117, 'closeoutAsk': 136.149}, 'reason': 'TAKE_PROFIT_ORDER', 'pl': '15.0', 'financing': '0.0', 'commission': '0.0', 'guaranteedExecutionFee': '0.0', 'accountBalance': '3001745.978', 'tradesClosed': [{'tradeID': '1845', 'units': '300.0', 'price': 136.14, 'realizedPL': '15.0', 'financing': '0.0', 'guaranteedExecutionFee': '0.0', 'halfSpreadCost': '2.1'}], 'halfSpreadCost': '2.1'}
      2020-09-11 19:48:21,640 [    INFO] *****, STORE NOTIF:, {'id': '1849', 'time': '1599821264.782127550', 'userID': 10733067, 'accountID': '101-009-10733067-005', 'batchID': '1848', 'type': 'ORDER_CANCEL', 'orderID': '1847', 'clientOrderID': '1599755079.86117-566', 'reason': 'LINKED_TRADE_CLOSED'}
      2020-09-11 19:48:21,640 [    INFO] *****, STORE NOTIF:, Received external transaction ORDER_CANCEL with id 1849. Skipping transaction.
      

      After this, the status of the previous new order(s) in the following.

      Main Order: Accepted
      TP Order: Submitted
      SL Order: Submitted

      Naturally, the main order have already been executed in the market.

      posted in General Code/Help
      T H
      T H
    • RE: Order Synchronization

      @dasch I see. Thanks!

      posted in General Code/Help
      T H
      T H
    • RE: Order Synchronization

      @dasch OMG! Thanks, I really appreciate it!!!!!

      posted in General Code/Help
      T H
      T H
    • RE: Order Synchronization

      @dasch if you know, please tell me.
      Is there an ID that identifies a set of brackets instead of a tradeid?
      Like the BatchID in the transaction log earlier.

      posted in General Code/Help
      T H
      T H
    • RE: Order Synchronization

      OMG!! Thank you for your information.
      Anyway, I'll try it.

      posted in General Code/Help
      T H
      T H
    • RE: Order Synchronization

      @dasch That's hard…
      But, I can provide only my order method.
      This is in next().
      This is called after the status of the orders checked.
      Nothing in particular. Maybe...

      def create_new_order(self, price, exec_type):
          stopprice = price - (self.order_type_sign * self.max_range_price)
          limitprice = price + (self.order_type_sign * self.interval_price)
      
          self.tradeid_seq +=1
          
          orders = None
          if self.order_type == bt.Order.Buy:
              orders = self.buy_bracket(size=self.order_size, price=price, exectype=exec_type, 
                                  tradeid=self.tradeid_seq, 
                                  stopprice=stopprice, stopexec=bt.Order.Stop, 
                                  limitprice=limitprice, limitexec=bt.Order.Limit)
          elif self.order_type == bt.Order.Sell:
              orders = self.sell_bracket(size=self.order_size, price=price, exectype=exec_type, 
                                  tradeid=self.tradeid_seq, 
                                  stopprice=stopprice, stopexec=bt.Order.Stop, 
                                  limitprice=limitprice, limitexec=bt.Order.Limit)
          
          logger.info("$$$ ORDER $$$ type:{}, size:{}, price:{}, exectype:{}({}), tradeid:{}, stopprice:{}, limitprice:{}".format(
              bt.Order.OrdTypes[self.order_type], self.order_size, price, exec_type, bt.Order.ExecTypes[exec_type], self.tradeid_seq, stopprice, limitprice)
          )
          return orders
      
      
      posted in General Code/Help
      T H
      T H
    • RE: Order Synchronization

      By the way, the order (tradid:55) has already been successfully filled. (according to "OANDA Web Trading Platform")
      But this orders status also as follows.

      Main Order: Accepted <-- Why is it "Accepted" and not "Completed"?
      TP Order: Submitted
      SL Order: Submitted

      (I'm not very good at English, so I don't know what verbs to use.(Filled? Opened?, Executed?) I'm sorry about that...)

      posted in General Code/Help
      T H
      T H
    • RE: Order Synchronization

      Finally, I succeeded in getting the api transactions.
      The order I made (tradeid:54) should originally be in Accepted status after filled, but that status is still "Submitted".
      In fact, When I check the order (tradeid:54) in "OANDA Web Trading Platform” , the order has been successfully filled.

      • NEW BRACKET ORDER[tradeid:54]
        ( type:Sell, size:300, price:137.92799999999986, exectype:2(Limit), tradeid:54, stopprice:147.92799999999986, limitprice:137.87799999999984 )
      2020-09-10 14:03:07,051 [    INFO] *****, STORE NOTIF:, {'id': '368', 'time': '1599714154.801444989', 'userID': 10733067, 'accountID': '101-009-10733067-005', 'batchID': '368', 'requestID': '24724085988296074', 'type': 'LIMIT_ORDER', 'instrument': 'GBP_JPY', 'units': '-300.0', 'price': 137.928, 'timeInForce': 'GTC', 'positionFill': 'DEFAULT', 'triggerCondition': 'DEFAULT', 'reason': 'CLIENT_ORDER', 'clientExtensions': {'id': '1599667859.922813-160'}, 'takeProfitOnFill': {'price': 137.878, 'timeInForce': 'GTC', 'clientExtensions': {'id': '1599667859.922813-162'}}, 'stopLossOnFill': {'price': 147.928, 'timeInForce': 'GTC', 'clientExtensions': {'id': '1599667859.922813-161'}}}
          :
          :
      2020-09-10 14:18:46,834 [    INFO] *****, STORE NOTIF:, {'id': '369', 'time': '1599715094.455659870', 'userID': 10733067, 'accountID': '101-009-10733067-005', 'batchID': '369', 'requestID': '42738488438139833', 'type': 'ORDER_CANCEL', 'orderID': '368', 'clientOrderID': '1599667859.922813-160', 'reason': 'CLIENT_REQUEST_REPLACED', 'replacedByOrderID': '370'}
      2020-09-10 14:18:46,836 [    INFO] *****, STORE NOTIF:, {'id': '370', 'time': '1599715094.455659870', 'userID': 10733067, 'accountID': '101-009-10733067-005', 'batchID': '369', 'requestID': '42738488438139833', 'type': 'LIMIT_ORDER', 'instrument': 'GBP_JPY', 'units': '-300.0', 'price': 137.928, 'timeInForce': 'GTC', 'positionFill': 'DEFAULT', 'triggerCondition': 'DEFAULT', 'reason': 'REPLACEMENT', 'clientExtensions': {'id': '1599667859.922813-160'}, 'takeProfitOnFill': {'price': 137.876, 'timeInForce': 'GTC'}, 'stopLossOnFill': {'price': 147.928, 'timeInForce': 'GTC'}, 'replacesOrderID': '368'}
      
      
      • ORDER STATUS[tradeid:54] (2020-09-10 14:03:06 - 2020-09-10 14:21:51)
        Main Order: Accepted
        TP Order: Submitted
        SL Order: Submitted

      • MAIN ORDER FILLED[tradeid:54] and NEW BRACKET ORDER[tradeid:55]
        (type:Sell, size:300, price:137.97799999999987, exectype:2(Limit), tradeid:55, stopprice:147.97799999999987, limitprice:137.92799999999986)

      2020-09-10 14:21:51,437 [    INFO] *****, STORE NOTIF:, {'id': '371', 'time': '1599715278.907071425', 'userID': 12345678, 'accountID': '101-009-12345678-005', 'batchID': '371', 'type': 'ORDER_FILL', 'orderID': '370', 'clientOrderID': '1599667859.922813-160', 'instrument': 'GBP_JPY', 'units': '-300.0', 'gainQuoteHomeConversionFactor': '1.0', 'lossQuoteHomeConversionFactor': '1.0', 'price': 137.928, 'fullVWAP': 137.928, 'fullPrice': {'type': 'PRICE', 'bids': [{'price': 137.928, 'liquidity': '250000'}], 'asks': [{'price': 137.942, 'liquidity': '250000'}], 'closeoutBid': 137.921, 'closeoutAsk': 137.948}, 'reason': 'LIMIT_ORDER', 'pl': '0.0', 'financing': '0.0', 'commission': '0.0', 'guaranteedExecutionFee': '0.0', 'accountBalance': '2999633.0916', 'tradeOpened': {'tradeID': '371', 'units': '-300.0', 'price': 137.928, 'guaranteedExecutionFee': '0.0', 'halfSpreadCost': '2.1', 'initialMarginRequired': '2069.01'}, 'halfSpreadCost': '2.1'}
      2020-09-10 14:21:51,437 [    INFO] *****, STORE NOTIF:, {'id': '372', 'time': '1599715278.907071425', 'userID': 12345678, 'accountID': '101-009-12345678-005', 'batchID': '371', 'type': 'TAKE_PROFIT_ORDER', 'tradeID': '371', 'price': 137.876, 'timeInForce': 'GTC', 'triggerCondition': 'DEFAULT', 'reason': 'ON_FILL'}
      2020-09-10 14:21:51,439 [    INFO] *****, STORE NOTIF:, Received external transaction TAKE_PROFIT_ORDER with id 372. Skipping transaction.
      2020-09-10 14:21:51,439 [    INFO] *****, STORE NOTIF:, {'id': '373', 'time': '1599715278.907071425', 'userID': 12345678, 'accountID': '101-009-12345678-005', 'batchID': '371', 'type': 'STOP_LOSS_ORDER', 'tradeID': '371', 'price': 147.928, 'timeInForce': 'GTC', 'triggerCondition': 'DEFAULT', 'reason': 'ON_FILL'}
      2020-09-10 14:21:51,439 [    INFO] *****, STORE NOTIF:, Received external transaction STOP_LOSS_ORDER with id 373. Skipping transaction.
      2020-09-10 14:22:00,987 [    INFO] *****, STORE NOTIF:, v20 REST request to https://stream-fxpractice.oanda.com:443/v3/accounts/101-009-12345678-005/transactions/stream has timed out (stream)
      2020-09-10 14:22:02,451 [    INFO] *****, STORE NOTIF:, v20 REST request to https://stream-fxpractice.oanda.com:443/v3/accounts/101-009-12345678-005/pricing/stream?instruments=GBP_JPY has timed out (stream)
          :
      2020-09-10 14:22:06,300 [    INFO] *****, STORE NOTIF:, Trying to reconnect streaming events (1 of -1)
      
      
      • ORDER STATUS[tradeid:54] (2020-09-10 14:22:03 - )
        Main Order: Completed
        TP Order: Submitted <-- Why is it "Submitted" and not "Accepted"?
        SL Order: Submitted <-- Why is it "Submitted" and not "Accepted"?
      posted in General Code/Help
      T H
      T H
    • RE: Order Synchronization

      @dasch I didn't know that!!!!
      I’m so happy! Thank you so so much!
      I'll try it, after that I’ll wait for the problem to be reproduced.

      posted in General Code/Help
      T H
      T H
    • RE: Order Synchronization

      @dasch Thank you for your advice.
      I already set notif_transactions=True, but I don’t understand how to use it.
      Please tell me how to get those transactions.

      posted in General Code/Help
      T H
      T H
    • RE: Order Synchronization

      @dasch said in Order Synchronization:

      could you provide more details on how you use oanda.

      • Set the sell_bracket at 5 pips intervals. (tp:-5pips, s/l:+1000pips)
      • Holds the return value of the order in the List.
      • The state of the order is monitored in next().

      When I check the order that the status of main order is "Accepted" in "OANDA Web Trading Platform", it is already executed.
      Moreover, when I close all orders with self.close(), it doesn't close just that one order.

      Am I making sense?

      also do you open positions on the same account outside of backtrader?

      Never.

      posted in General Code/Help
      T H
      T H
    • 1
    • 2
    • 3
    • 1 / 3