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/

    target order with different price limit depends on buy or sell

    General Discussion
    target order
    3
    6
    456
    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.
    • Y
      Yi Fang last edited by Yi Fang

      Hi:

      Is it possible to create target orders with different price limit for buy or sell operations.
      So the code looks something like this:

      
      self.order_target_percent(perc = 0.05,
                                exectype = Order.Limit,
                                price = X if order.isbuy() or price = Y if order.issell())
      
      
      Y 1 Reply Last reply Reply Quote 0
      • Y
        Yi Fang @Yi Fang last edited by Yi Fang

        So I read order_target_value method and it seems a buy or sell order is returned

                if target > value:
                    size = comminfo.getsize(price, target - value)
                    return self.buy(data=data, size=size, price=price, **kwargs)
        
                elif target < value:
                    size = comminfo.getsize(price, value - target)
                    return self.sell(data=data, size=size, price=price, **kwargs)
        

        I will try with order cancellation and reissue the same buy/sell order with different price limit, currently still stuck with the original question.

        A new question raised, is it possible to specify a price range for order execution, say I only want to buy a stock between 9 to 11 dollars, nothing more nothing less. I understand this is not a standard order which most brokers support, only for backtesting purpose.

        A B 2 Replies Last reply Reply Quote 0
        • A
          ab_trader @Yi Fang last edited by

          @yi-fang said in target order with different price limit depends on buy or sell:

          currently still stuck with the original question

          Did you try to code it the way you shown or you are waiting for an answer? Can be quicker to code it by yourself.

          Y 1 Reply Last reply Reply Quote 1
          • Y
            Yi Fang @ab_trader last edited by

            Did you try to code it the way you shown or you are waiting for an answer? Can be quicker to code it by yourself.

            I did, currently I am working around the issue by manipulating my data in some way. Still stuck with strategy level code though.
            The snippet shown was merely a demonstration of what I hoped to achieve, not working code.

            1 Reply Last reply Reply Quote 0
            • B
              backtrader administrators @Yi Fang last edited by

              @yi-fang said in target order with different price limit depends on buy or sell:

              So I read order_target_value method and it seems a buy or sell order is returned

              Instead of reading the method, you may want to read the documentation: https://cookbook.backtrader.com/documentation/strategy/

              All methods that operate on the market return the generated orders.

              I probably don't understand what your goal is, but I believe that the usage of order_target_xxx is wrong in your case. Those methods are meant to do things automatically for the trader and you want to decide manually what to do, depending on the existing position.

              There is no way around, in that case, keeping track of your position size, deciding on the new position size and then decide which price you want to issue.

              Y 1 Reply Last reply Reply Quote 0
              • Y
                Yi Fang @backtrader last edited by Yi Fang

                @backtrader

                My goal is described in my old post:

                https://community.backtrader.com/topic/1076/daily-upper-and-lower-bound-of-price/3

                Only here I am trying to take into account daily upper and lower bound of price for target order.

                target order or not, I did try to follow your suggestion in old post, but:

                1. Having a custom Sizer

                This wont work for me since my order are with specific size, a global custom Sizer will be overwritten.

                2.Having a commission scheme

                I read the custom commission scheme, dont see how this will work. The

                def _getcommission(self, size, price, pseudoexec)
                

                only pass in the execution price for orders, not previous bar's (when the order is created) close price, I wont be able to compare these two prices thus impose an inf commission accordingly.

                Official name for such trading restriction is called daily maximum price fluctuation I think, and it is common in commodity futures as well, might be a good idea to implement such feature natively.

                1 Reply Last reply Reply Quote 0
                • 1 / 1
                • First post
                  Last post
                Copyright © 2016, 2017, 2018 NodeBB Forums | Contributors
                $(document).ready(function () { app.coldLoad(); }); }