BurtonM
New member
Python RSI KER Algo Trading Strategy That PRINTS Money
This S&P 500 trading strategy comes from a video by Ali Casey at StatOasis on YouTube,Thank you Ali for sharing all your data analysis, and backtesting.
RSI Trading Strategy That PRINTS Money

It is important you watch the video link above to understand the what I have coded.
IMPORTANT: This Strategy is developed and backtested on the S&P500 Daily timeframe.
KER refers to the Kaufman Efficiency Ratio.
Entry/Exit conditions are:
- Enter when RSI < entry_level & KER > threshold
- Exit when RSI > exit_level or max bars held
- StopLoss added to code
I have coded 4 versions: MT4 version is here. MT5 version is here. TradingView version is here.
Here is a detailed guide on how to use the RSI Trading Bot Python code.
- Initial Setup
# Install required packages
pip install pandas numpy matplotlib yfinance
- Save the Code
- Save the complete bot code in a file named rsi_ker_trading_bot.py
- Make sure to include all the imports and the complete class definition
- Basic Usage Example
from rsi_ker_trading_bot import RSIKERTradingBot
from datetime import datetime, timedelta
# Create bot instance
bot = RSIKERTradingBot(
symbol="EURUSD=X", # Trading symbol
rsi_period=2, # RSI calculation period
rsi_entry_level=25, # RSI level to enter trades
rsi_exit_level=65, # RSI level to exit trades
ker_period=21, # KER calculation period
ker_threshold=0.2331, # KRT level to enter trades
max_bars_hold=5, # Maximum bars to hold position
lot_size=0.01, # Trading lot size
stop_loss=50 # Stop loss in points
)
# Define backtest period
start_date = datetime.now() - timedelta(days=365) # 1 year of data
end_date = datetime.now()
# Run backtest
results = bot.backtest(start_date, end_date)
# Display results
bot.plot_results(results)
- Available Parameters
# You can customize these parameters when creating the bot
bot = RSIKERTradingBot(
symbol="EURUSD=X", # Available symbols: any Yahoo Finance symbol
# Forex pairs need =X suffix (e.g., "EURUSD=X")
# Stocks use regular ticker (e.g., "AAPL")
# Strategy Parameters
rsi_period=2, # Shorter period = more signals
# Typical ranges: 2-14
rsi_entry_level=25, # Lower value = fewer but stronger signals
# Typical ranges: 20-30
rsi_exit_level=65, # Higher value = longer holds
# Typical ranges: 60-80
ker_period=21,
ker_threshold=0.2331,
max_bars_hold=5, # Maximum holding period
# Prevents getting stuck in trades
# Trading Parameters
lot_size=0.01, # Trading size
# 0.01 = 1 micro lot
# 0.1 = 1 mini lot
# 1.0 = 1 standard lot
stop_loss=50 # Stop loss in points
# 50 = 50 pips for forex
)
- Accessing Results
# After running backtest, you can access various data:
results = bot.backtest(start_date, end_date)
# Get all signals
signals = results[results['Signal'] != 0]
# Get entry points
entries = results[results['Signal'] == 1]
# Get exit points
exits = results[results['Signal'] == -1]
# Get RSI values
rsi_values = results['RSI']
# Get equity curve
equity = results['Equity_Curve']
# Calculate key metrics
total_return = results['Equity_Curve'].iloc[-1] / results['Equity_Curve'].iloc[0] - 1
sharpe_ratio = np.sqrt(252) * results['Strategy'].mean() / results['Strategy'].std()
max_drawdown = (results['Equity_Curve'] / results['Equity_Curve'].cummax() - 1).min()
number_of_trades = len(results[results['Signal'] != 0])
- Testing Different Timeframes
# Test different periods
# Short period (1 month)
start_date = datetime.now() - timedelta(days=30)
# Medium period (6 months)
start_date = datetime.now() - timedelta(days=180)
# Long period (2 years)
start_date = datetime.now() - timedelta(days=730)
# Different data frequency
# When calling yf.download, you can use different intervals:
# '1m' - 1 minute
# '5m' - 5 minutes
# '15m' - 15 minutes
# '30m' - 30 minutes
# '1h' - 1 hour
# '1d' - 1 day
- Error Handling
try:
bot = RSIKERTradingBot(symbol="EURUSD=X")
results = bot.backtest(start_date, end_date)
except Exception as e:
print(f"An error occurred: {e}")
- For Live Trading
# While the bot is designed for backtesting, you can use process_bar()
# For live trading. Here's a basic example:
import time
while True:
# Get latest data
end = datetime.now()
start = end - timedelta(days=1) # Get last day of data
data = yf.download(bot.symbol, start=start, end=end, interval='1h')
# Process the latest bar
bot.process_bar(data)
# Wait for next bar
time.sleep(3600) # Wait 1 hour for next bar
Remember:
- Always test with small amounts first
- Monitor the bot's performance regularly
- Adjust parameters based on your trading goals
- Keep track of all trades and performance metrics
- Consider market conditions when interpreting results
Attachments
Last edited by a moderator: