Navigation

    Backtrader Community

    • Register
    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    For code/output blocks: Use ``` (aka backtick or grave accent) in a single line before and after the block. See: http://commonmark.org/help/

    position problem:how to get order Index?(when I print position show “Opened: 0.01” )

    General Code/Help
    3
    4
    151
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • magieva
      magieva last edited by

      I'm a beginner,My English is not good.Please forgive me.
      I buy(size=0.01) 3 times in different price. such as: orderA,orderB,orderC
      in notify_order,print(self.getposition(self.data)) last time show:

      --- Position Begin

      • Size: 0.03
      • Price: 1.0755933333333332
      • Price orig: 1.0759599999999998
      • Closed: 0
      • Opened: 0.01
      • Adjbase: 1.07323
        --- Position End

      I think:
      Size: 0.03 = orderA.size+orderB.size+orderC.size
      Opened: 0.01=orderC.size

      problem:
      1.how can I get orderA,orderB,orderC Index? I want to see their cost(order.executed.value)
      2.how can I close orderB only? I want to close order by order.ref

      my English is not good, I use translation software, so the translation is not accurate.
      it's hard to understand the document.
      Can you write a demo for me?
      Thank you very much.

      B 1 Reply Last reply Reply Quote 0
      • A
        ab_trader last edited by

        @magieva said in position problem:how to get order Index?(when I print position show “Opened: 0.01” ):

        1.how can I get orderA,orderB,orderC Index? I want to see their cost(order.executed.value)

        In the notify_order() print order.executed.value. So in case of N order, the script will print order.executed.value N times.

        2.how can I close orderB only? I want to close order by order.ref

        Order can't be closed. It can be issued, accepted and executed. After the execution of the order the position size is increasing or decreasing.

        • If my answer helped, hit reputation up arrow at lower right corner of the post.
        • Python Debugging With Pdb
        • New to python and bt - check this out
        1 Reply Last reply Reply Quote 0
        • B
          backtrader administrators @magieva last edited by

          @magieva said in position problem:how to get order Index?(when I print position show “Opened: 0.01” ):

          1.how can I get orderA,orderB,orderC Index? I want to see their cost(order.executed.value)

          Use notify_order, method of Strategy to receive the notifications for each order.

          • Docs - Strategy - https://www.backtrader.com/docu/strategy/

          @magieva said in position problem:how to get order Index?(when I print position show “Opened: 0.01” ):

          2.how can I close orderB only? I want to close order by order.ref

          That's an impossible. Here and in real life. Let me say it loud and clear: ORDERS ARE NOT POSITIONS, because an order is not open or closed. An order can be pending execution, partially executed, cancelled, rejected, but IT IS NOT YOUR POSITION in the market. The exchange does not keep track of which orders opened your position. Your orders are instructions to do something.

          You can use tradeid to simulate trades which logically group orders. But you have to remember, that the EXCHANGE won't do that for you.

          1 Reply Last reply Reply Quote 1
          • magieva
            magieva last edited by

            Thank you for your answer.

            I am used to MT4. The operation habit of order affects my understanding.

            After understanding, I can think of an application: Calculate used margin

                    if order.status in [order.Completed]:  
            
                        # marginUsed
                        for bit in order.executed.exbits:
                            self.marginUsed += bit.openedvalue - bit.closedvalue
            

            If this is not right, please give me suggestions and thank you again for your reply

            1 Reply Last reply Reply Quote 0
            • 1 / 1
            • First post
              Last post
            Copyright © 2016, 2017, 2018, 2019, 2020, 2021 NodeBB Forums | Contributors