kernc / backtesting.py

:mag_right: :chart_with_upwards_trend: :snake: :moneybag: Backtest trading strategies in Python.
https://kernc.github.io/backtesting.py/
GNU Affero General Public License v3.0
5.6k stars 1.08k forks source link

How to take partials using close() or sell() methods #1180

Open danielwang730 opened 1 month ago

danielwang730 commented 1 month ago

Hi,

Apologies in advance if I'm not following convention for submitting this issue, but I had a question about taking partials: specifically, is it possible to do so at an exact price specified?

For example (using made-up numbers), if I buy 20 shares of MSFT at 9:40 AM at $100/share, I want to partial (take profit) 50% if it hits exactly $102.31. Let's say at 9:41 AM the OHLC values are {O: $101, H: $104, L: $100.5, C: $102.8}, which means the $102.31 figure is met. How would I now sell 10 shares of MSFT at $102.31 exactly?

I noticed that when using SL or TP in Strategy.buy(), it would get the exact price, but I only want to take a partial, and not the whole thing. For comparison, when using TradingView's platform, their Strategy.exit() function gives options for partial amount, when to partial, etc., and it hits precisely at the specified price. Is there a way to do it here as well?

Thanks, Daniel

ferromariano commented 1 month ago

ES

La funcionalidad de configurar tu TP parcial por defecto, No existe.

Pero me parece que podes simularlo haciendo un seguimiento de la orden

Ejemplo:

EN

The functionality to configure your partial TP by default does not exist.

But I think you can simulate it by tracking the order

Example:

class MSFT102(Strategy):

    def init(self):
      ....

    def buyLimitPartialTP(self, price: float, tp_price: float, tp_partial_price: float, size: float, tp_partial_size_perce: float):
        tp_partial_size = size*tp_partial_size_perce
        tp_size         = size-tp_partial_size

        self.buy(limit=price, tp=tp_price,         size=tp_size)
        self.buy(limit=price, tp=tp_partial_price, size=tp_partial_size)

    def next(self):
        self.buyLimitPartialTP(price=100, tp_price=105, tp_partial_price=102.5, size=20, tp_partial_size_perce=0.5)
soumenhalder commented 2 weeks ago

Hey Daniel, Just checking in—have you been able to resolve the issue? I’m encountering something similar on my end and would appreciate any updates or insights you might have!

danielwang730 commented 2 weeks ago

I found that using the _broker._reduce_trade method in the Broker class actually works. It's essentially overriding the buy or sell functions, and it manually places in the order you want. This is what the code looks like:

def _reduce_trade(self, trade: Trade, price: float, size: float, time_index: int):
    assert trade.size * size < 0
    assert abs(trade.size) >= abs(size)

    size_left = trade.size + size
    assert size_left * trade.size >= 0
    if not size_left:
        close_trade = trade
    else:
        # Reduce existing trade ...
        trade._replace(size=size_left)
        if trade._sl_order:
            trade._sl_order._replace(size=-trade.size)
        if trade._tp_order:
            trade._tp_order._replace(size=-trade.size)

        # ... by closing a reduced copy of it
        close_trade = trade._copy(size=-size, sl_order=None, tp_order=None)
        self.trades.append(close_trade)

    self._close_trade(close_trade, price, time_index)

And here's the link to the documentation: https://github.com/kernc/backtesting.py/blob/master/backtesting/backtesting.py

This has been working for me when I'm trying to take partials, and it also trades when you want it to. It does have its limitations of course, but I feel it more accurately simulates what might happen on a "real" trade. I've been trying to look into other packages that might do something similar, however, so I'm not sure how much longer I will be using this function. More specifically, I'm trying to look for a vectorized way to go through the data - rather than iterating through numpy arrays - to try taking full advantage of the pandas capabilities.

soumenhalder commented 1 week ago

Ah, I thought you meant to take a partial profit with the current price. If you partially close with some given condition, that will be a market order, not a limit order, and you would not take full advantage of price movement. The current scenario does not allow to a contingent close with a limit price and that could possibly solve the problem.

danielwang730 commented 1 week ago

Hi Soumen, I'm not sure I fully understand what you're saying, but I'll try my best to address some of your concerns:

Ah, I thought you meant to take a partial profit with the current price.

If you partially close with some given condition, that will be a market order, not a limit order,

and you would not take full advantage of price movement

The current scenario does not allow to a contingent close with a limit price and that could possibly solve the problem.

soumenhalder commented 1 week ago

Thanks for explaining. Just one more question: are you using a modified library imported locally or overriding this function from your own Strategy class?

For my case, I modified the close function of Trade and Position to insert the limit price (and importing the modified backtesting library), but I was looking for some cleaner solution.

closefun

danielwang730 commented 1 day ago

I'm using the _reduce_trade method in the Broker class, so I don't need to change anything. I think your method of adding a liimit_price will also work, but it will execute on the next candle unless you add a time condition as well.

danielwang730 commented 1 day ago

Not sure if this is allowed, but if you want to send me an email at daniel.wang730@gmail.com, we can discuss this further.