You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I am having problem with my strategy that is supposed to take trades when RSI is above 70, and ROC is above 5, as an example.
But when I print out the values in the trades afterwards, it shows RSI below 70. I also tried with only the RSI signals and then use the "SelectMomentum" in the strategy with no luck, same result.
Can someone please shed the light for me here?
importbtimportpandasaspdimporttalibasta# List of stock symbols to teststock_symbols= ['AAPL', 'GOOGL', 'MSFT', 'AMZN', 'META']
data=bt.get(stock_symbols, start='2023-06-01', end='2023-12-31')
# Fill NaN values with zerodata_filled=data.fillna(0)
# Calculate RSI for each stockrsi=data_filled.apply(lambdax: ta.RSI(x, timeperiod=14))
# Calculate ROC for each stockroc=data_filled.apply(lambdax: ta.ROC(x, timeperiod=25))
#roc = data_filled.pct_change(25) * 100 # 25-day ROC, multiplied by 100 for percentage# Create signals based on RSI and momentumrsi_signal=rsi>70# Create a signal based on ROCroc_signal=roc>5# Create the combined signalcombined_signal=rsi_signal&roc_signal# Create the strategymy_strategy=bt.Strategy('s1', [
bt.algos.RunAfterDate('2023-07-01'), # Run after the specified datebt.algos.RunWeekly(),
bt.algos.SelectWhere(combined_signal),
#bt.algos.SelectMomentum(n=1, lookback=pd.DateOffset(days=25)),bt.algos.WeighEqually(),
bt.algos.Rebalance()
])
# Create the backtest and run ittest=bt.Backtest(my_strategy, data)
result=bt.run(test)
# Access RSI valuesrsi_values_list= []
roc_values_list= []
# Get the latest transactionslatest_transactions=result.get_transactions().tail(10) # You can adjust the number as needed# Print RSI values for the stocks in the latest transactions#for stock, date in latest_transactions.index:fordate, stockinlatest_transactions.index:
rsi_value=rsi.loc[date, stock]
roc_value=roc.loc[date, stock]
print(f"Stock: {stock}, Date: {date}, RSI: {rsi_value}, ROC: {roc_value}")
print("end")
The text was updated successfully, but these errors were encountered:
anpo2030
changed the title
Not filtering RSI and ROC properly
Not filtering RSI and ROC properly (code included)
Nov 29, 2023
Hi,
I am having problem with my strategy that is supposed to take trades when RSI is above 70, and ROC is above 5, as an example.
But when I print out the values in the trades afterwards, it shows RSI below 70. I also tried with only the RSI signals and then use the "SelectMomentum" in the strategy with no luck, same result.
Can someone please shed the light for me here?
The text was updated successfully, but these errors were encountered: