polakowo / vectorbt

Find your trading edge, using the fastest engine for backtesting, algorithmic trading, and research.

Home Page:https://vectorbt.dev

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Reverse position with from_signals

amahouachi opened this issue · comments

I am trying to migrate a strategy from pinescript to vectorbt.

The bahavior is : When I have a shoring signal, i first close long position then i open short. and vice versa for long signal.

So these are 2 successive orders.

TradingView handles this as expected, as you see in the example screenshot. On 2022-12-31 14:00 UTC, we close long then open short. Slippage is ignored for the sake of simplicity. The filling price of these 2 orders is the open of the OHLC bar starting at 14:00. open=1201.11. So this is the expected behavior.

Screenshot_2024-05-25_12-58-30

In vectorbt, I am using Portfolio.from_signals without size specification and with direction=both, to have a similar behavior. We see in the screenshot that it is closing long position at 13:00 (is that the end of 13:00 so 14:00 ?) with price=1201.11 and opening short at 14:00 with price=1203.44 (which is the close of 14:00 OHLC bar).

Screenshot_2024-05-25_13-18-23

Is it because it supports one order at a step? or is it something else i missed or messed.

My inputs are 1h OHLCV csv data downloaded from binance and handled by pandas.

I am calling from_signals like this :

portfolio = vbt.Portfolio.from_signals(
    data0['Close'],
    entries=entries_long,
    exits= exits_long,
    init_cash=10000,
    fees=0.0005,
    upon_opposite_entry='close',
    cash_sharing= True,
    log=True,
    direction= 'both',
    freq='1h'
)

Ok I just removed :

upon_opposite_entry='close',

Now it works exactly like in TradingView and closes long then opens short on the same datetime.