-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to take partials using close() or sell() methods #1180
Comments
ESLa funcionalidad de configurar tu TP parcial por defecto, No existe. Pero me parece que podes simularlo haciendo un seguimiento de la orden Ejemplo: ENThe 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) |
Hey Daniel, |
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:
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. |
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. |
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.
|
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 |
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
The text was updated successfully, but these errors were encountered: