For code/output blocks: Use ``` (aka backtick or grave accent) in a single line before and after the block. See: http://commonmark.org/help/

Continuous working of limit orders



  • Hi there BT community.

    Been using Backtrader for a few months now, and love it. Such a great resource.

    However I'm not a great coder, and am struggling with a concept for a mean reversion strategy. Any guidance would be much appreciated. To explain, I have created a stationary time series, and would like to continually work limit orders at the calculated top and bottom Bollinger Bands.

    So for buys, say the lower Bollinger is at a price of 10, I would like to be always working a bid to buy at 10, so that I know if I see a close at 9 in the self.data.close[0], I would have a fill in the real world.

    Up to now, I've been using something like:

    if self.data.close[0] < self.bband.lines.bot: 
        self.log('BUY, %.4f' % (self.data.close[0]))
        self.order = self.buy()
    

    But this (as expected) only generating an order after the close is through the lower Bollinger Band.

    I have tried playing with cheat on close, and also to make it a limit order and set the price to be the self.bband.lines.bot[-1]. But I've not managed to get it to work. And it feels like I'm looking for the solution in the wrong place.

    Could anyone help with setting up the strategy? To be clear, I would like to have orders in the market already, so that if the price series trades through my limit orders, I will have a fill at the calculated bollinger band [-1] levels, not the most recent close.

    Many thanks,
    Chris.


  • administrators

    Really, I have tried to decode the information you tried to paste into this message and I couldn't.

    See

    1. The title is "Continous working of limit orders", but the code shown issues a Market order
    2. cheat-on-close is meant only for Market orders, yet it is mentoned.

    Then
    @chrismo said in Continuous working of limit orders:

    So for buys, say the lower Bollinger is at a price of 10, I would like to be always working a bid to buy at 10, so that I know if I see a close at 9 in the self.data.close[0], I would have a fill in the real world.

    You seem to try to let us know that if you see a value of 10 in the lower Bollinger Band, you want to place a buy order with (I guess) Limit price 10.

    @chrismo said in Continuous working of limit orders:

    But this (as expected) only generating an order after the close is through the lower Bollinger Band.

    All orders are generated after the close. Were the orders generated before, you could buy what's not available.

    @chrismo said in Continuous working of limit orders:

    and also to make it a limit order and set the price to be the self.bband.lines.bot[-1]. But I've not managed to get it to work. And it feels like I'm looking for the solution in the wrong place.

    And that's why you show a snippet with Market orders?

    @chrismo said in Continuous working of limit orders:

    so that if the price series trades through my limit orders, I will have a fill at the calculated bollinger band [-1] levels, not the most recent close.

    self.buy(exectype=bt.Order.Limit, price=self.bollinger_band.bot[-1])
    

    ????

    Execution is of course NOT guaranteed. Because it is a Limit order.



  • Hi @backtrader Thanks for the response.

    I should have been more careful with the code snippet I included. Limit order should have been in there, yes.

    The point I wanted to make with it was that I can see what I am doing with respect to code order is not correct.

    I can see that I am waiting for the initial IF statemant to True before submitting the buy order. I just don't know how else to set it up.

    To try and explain what I'm trying to do more clearly:

    The data I am using is 30 minute intervals. I am only interested in the closing prices. Again just focussing on the buy order.

    Running through the data, say the current self.data.close[0] is 12, and the self.bband.lines.bot is 10.

    For the next 30 minutes (i.e. until the next price update), I wish to work a limit buy order at a price of 10.

    Once we have the next() update, if the close is now 9 (or lower), I can be 100% sure that I have a fill at a price of 10 (it had to have traded through my 10 bid for the 9's to have printed). And for any close of >= 10, I will assume I have no fill.

    so for each half hour, the self.bband.lines.top and self.bband.lines.bot levels are updated to work limit orders for the subsequent 30 minutes.

    Does that clear it up? Is that possible to implement?


  • administrators

    @chrismo said in Continuous working of limit orders:

    For the next 30 minutes (i.e. until the next price update), I wish to work a limit buy order at a price of 10.

    When you issue a Limit order and unless the price is a match, it is active until the valid.

    In any case there seems to be some misunderstanding, because there are no "next 30 minutes", because we work with discrete bars, 30-minutes in your case. So the order is evaluated from bar to bar.

    @chrismo said in Continuous working of limit orders:

    Once we have the next() update, if the close is now 9 (or lower), I can be 100% sure that I have a fill at a price of 10

    If that's the case, you will already have been notified of order execution. There is no need to be sure of anything. The fill price can be better than 10 if the opening price formed a gap in your favor.

    @chrismo said in Continuous working of limit orders:

    And for any close of >= 10, I will assume I have no fill.

    There is nothing to assume. There will have been no notification.

    @chrismo said in Continuous working of limit orders:

    so for each half hour, the self.bband.lines.top and self.bband.lines.bot levels are updated to work limit orders for the subsequent 30 minutes.

    The indicators are continuously updated, but the orders are inmutable. You will have to cancel the existing order and issue a new one to match your current requirements.

    @chrismo said in Continuous working of limit orders:

    Is that possible to implement?

    Of course, as pointed out above. Cancel and reissue.


Log in to reply
 

});