Skip to content
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

[Help needed] How to implement buy limit order & close opened trade after 10 days holding the stock #647

Open
tan-yong-sheng opened this issue Aug 20, 2023 · 3 comments
Labels

Comments

@tan-yong-sheng
Copy link

tan-yong-sheng commented Aug 20, 2023

Hi all, I wanna ask if we could backtest this strategy with vectorbt (as shown in the image below).

Screenshot_2023-08-20-10-39-36-292_com google android youtube

I have found few challenges to implement the entries and exits as below:

  1. entries - how to put buy limit order for vectorbt?
  2. exits - how to close trade after trades opened for 10 days?

Hope for help, thank you.

@tan-yong-sheng
Copy link
Author

tan-yong-sheng commented Aug 26, 2023

For buy limit, I found here got the way to apply buy limit order: #64. But, still not so understand yet. Currently, looking into it, if anyone got any better ideas to implement buy limit order, hope for advice. Thanks.

@tan-yong-sheng
Copy link
Author

tan-yong-sheng commented Aug 26, 2023

@polakowo These are how I try to backtest this strategy: https://www.youtube.com/watch?v=CxFv_EUY0ZA as the image attached above

Let me clarify more about the entry and exit points as below

  • Entry: Stock price above SMA_200 OR Lower band of Bollinger bands with 2.5 standard deviation crossed above the stock price, and if these criteria are met we will put a 3% buy limit order to start buying at next close price.
  • Exit: 2-day RSI crossed above 50 OR after holding the stocks for 10 trading days

Below is part of code that forms the entries and exits of the trades. For example, should I use forward shift exits signals by 1 (e.g., vbt.signals.fshift(1)) or any way to improve to make this backtest more realistic?

Please let me know if anything need to amend, or is there any suggestion. Thanks.

SMA_200 = vbt.MA.run(data["Close"], window=200)
BBANDS_20 = vbt.talib("BBANDS").run(data["Close"], timeperiod=20,
                           nbdevup=2.5, nbdevdn=2.5)
RSI = vbt.RSI.run(data["Close"], window=2)

out_dict={}
order_price = data["Close"].shift(1)
entries = SMA_200.ma_below(data["Close"]).vbt | BBANDS_20.lowerband_crossed_above(data["Close"]).vbt
entries = entries.vbt.signals.generate_ohlc_stop_exits(
    data["Open"], data["High"], data["Low"], data["Close"],
    sl_trail=False,
    out_dict=out_dict,
    sl_stop=0.03, # in %
    exit_wait=1) # buy the trade at next close price when buy signal is triggered

timebased_stop_loss = entries.vbt.signals.fshift(10) # close open trades after holding it for 10 days
exits = (RSI.rsi_crossed_above(50) | timebased_stop_loss)

entries, exits = entries.vbt.signals.clean(exits)

pf= vbt.Portfolio.from_signals(data["Close"],
                          entries,
                          exits,
                          fees=0.002, # in %
                          slippage=0.001, # in %
                          direction="longOnly",
                          init_cash=1000,
                          freq="1d"
                          )

pf.stats()

Copy link

github-actions bot commented Feb 5, 2024

This issue is stale because it has been open for 90 days with no activity.

@github-actions github-actions bot added the stale label Feb 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant