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.39k stars 1.05k forks source link

i want to set buy limit orders and sell limit orders at the same time , but the sell orders can only be used to reduce or close positions , do not open short position, how to fullfill? #1049

Open xiaobaomao opened 1 year ago

xiaobaomao commented 1 year ago
    if self.position.size >= self.initial_position:
        for i in greater_than_target:
            if i == greater_than_target[0]:
                self.sell(size=-self.initial_position, limit=i)
            else:
                self.sell(size=int(-self.initial_position*self.ratio), limit=i)
    elif 0 < self.position < self.initial_position:
        for i in greater_than_target:
            self.sell(size=int(-self.initial_position * self.ratio), limit=i)

only using the sell limit order to reduce or colse positions, do not open short positions, how to do ?

kernc commented 1 year ago

I'm not quite sure I understand what you're trying to do.

Have you tried using Trade.close(portion=...) API to close existing trades?