polakowo / vectorbt

Find your trading edge, using the fastest engine for backtesting, algorithmic trading, and research.
https://vectorbt.dev
Other
4.38k stars 619 forks source link

Time-based stoploss / take profit #175

Closed 010011x closed 3 years ago

010011x commented 3 years ago

Hi!

I'm using generate_ohlc_stop_exits to test stop loss values. I want to adjust my stop loss based on the duration of the trade. Is there an easy way to do that ? Or should I use from_order_func to do that? Is there an example maybe?

Thanks a lot!

polakowo commented 3 years ago

@Potato334343 How you want your stop loss to change with the trade duration?

Anyways, from_order_func is the way (there is an example for tracking a stop price under from_order_func documentation). You can also implement your own generate_ohlc_stop_exits and then use from_signals. But you need to do it on your own, there is nothing comparable in any backtesting framework, to my knowledge. In the next release, I'll add stop loss/take profit to from_signals.

010011x commented 3 years ago

@polakowo

Understood, thank you.

I was thinking about something like this https://www.freqtrade.io/en/latest/strategy-advanced/#time-based-trailing-stop

polakowo commented 3 years ago

@Potato334343 maybe I can incorporate this into from_signals. I already implemented stop loss, trailing stop loss, and take profit. I could give you an option to pass a function that returns stop value as freqtrade does it, but with slightly different arguments. Something like this:

def adjust_stop_nb(init_i: int, init_price: float, last_close: float, i: int, stop: float) -> float:
    return stop

You can then get the trade duration by doing i - init_i and profit using last_close - init_price. This function will be run before processing the current timestamp i. But you wouldn't be able to switch between standard and trailing stop loss because they are treated as different types.

010011x commented 3 years ago

@polakowo

that would be great, thank you!

polakowo commented 3 years ago

Implemented in 0.19.0.