backtesting.py icon indicating copy to clipboard operation
backtesting.py copied to clipboard

Trades are not execute, although conditions are fulfilled?

Open PaulForInvent opened this issue 4 years ago • 5 comments

Hey,

I feel this might not be a bug but rather a lack of knowledge of usage. Maybe I miss some point. Now, I already saw a quite few times on the chart with the trades, that there should be some buys because condition is fullfilled but there are no buys...

I simply do something like:

        if (price < tenkan_sen
           and price < kijun_sen
            and price < senkou_span_b
            and price < senkou_span_a 
            and self.daily_rsi[-1] < self.lower_level
        ):


            self.buy()

    
        elif (price > tenkan_sen
            and price > kijun_sen
            and price > senkou_span_b
            and price > senkou_span_a 
            and self.daily_rsi[-1] > self.upper_level
        ):

            self.position.close()

I see visullay few occations where this is true but no trades are executed...

What do I oversee?

I thought that you can only have 1 single active trade, but then I saw 2 active trades at once...

I know this not a minimal example. But I think every user might have come over this issue, as it might be typical as an misusage...Otherwise if this is not the case, I really have something seriously... :-)

Edit.

Found out that it works, as it really BUYs it, but it somehow gets lost afterwards. So have to g line by line of the Broker/Trades Classes. Seems heavy...This could be mentioned in the docs how trades/orders are processed...

PaulForInvent avatar Aug 31 '21 15:08 PaulForInvent

Please don't let it be such foolish thing, that it bought 100 positions, but when you do position.close() you just sell a single stock?! So maybe just the last one? Thats why the trades are not withhin the charts because you have all the buys but no sells to the buys...

#mindfuck.

PaulForInvent avatar Aug 31 '21 20:08 PaulForInvent

Calling self.position.close() should close all active trades on the next bar.

kernc avatar Sep 01 '21 10:09 kernc

@kernc No it just closed size=1 positions?

I used now self.sell() to close all. Maybe I miss something? Now there are strange things that only the beginning and last trade is connected on the chart somehow...

PaulForInvent avatar Sep 03 '21 17:09 PaulForInvent

Unfortunately, I really have no idea what you're doing. Construct a reproducible minimal working example that shows off the issue, and I can investigate.

kernc avatar Sep 03 '21 17:09 kernc

Thanks for your deep interest.

Isn't this basically related to basic usage of this library and your meaning of what your broker is doing and how are the trades processed? I mean I just use a simple example from your tutorial. The conditions are actually not important as it could be anything!

So before I go deep into your code, I ask here as it is really simple for one who used it before...

PaulForInvent avatar Sep 03 '21 21:09 PaulForInvent