Combining 15-Minute Moving Average Trends with 5-Minute RSI Signals
Summary
This strategy uses two bar intervals to set its direction and timing. It builds 15-minute bars and compares a fast and slow simple moving average; their relationship sets a bullish or bearish trend state. On 5-minute bars, it calculates RSI and considers an entry only when the trend state is set and the RSI has crossed the relevant threshold around its midpoint. The strategy opens a long in an upward trend or a short in a downward trend, then closes the position if the trend reverses or RSI crosses back through 50.
The source is strategy code, not a performance study: it reports no backtest results, transaction costs, or market-specific evidence. Orders are submitted at prices offset from the latest bar close, but the excerpt does not explain fill behavior or slippage. It also does not define stop-loss or profit-target rules. The approach therefore illustrates multi-timeframe signal construction, but its practical risk and reliability cannot be assessed from the document alone.
Key ideas
- The 15-minute fast and slow moving averages determine the strategy’s directional trend state.
- The 5-minute RSI provides entry and exit timing within that trend state.
- Long and short entries require RSI to move beyond thresholds set around its midpoint.
- Positions close when the trend direction reverses or RSI returns across the midpoint.
- The source gives no performance results or explicit stop-loss and profit-target rules.
Tags
Full text
# MultiTimeframeStrategy
# MultiTimeframeStrategy
## Source (MIT)
```python
from howtrader.app.cta_strategy import (
CtaTemplate,
StopOrder
)
from howtrader.trader.object import TickData, BarData, TradeData, OrderData
from howtrader.trader.utility import BarGenerator, ArrayManager
from decimal import Decimal
class MultiTimeframeStrategy(CtaTemplate):
""""""
author = "用Python的交易员"
rsi_signal = 20
rsi_window = 14
fast_window = 5
slow_window = 20
fixed_size = 1
rsi_value = 0
rsi_long = 0
rsi_short = 0
fast_ma = 0
slow_ma = 0
ma_trend = 0
parameters = ["rsi_signal", "rsi_window",
"fast_window", "slow_window",
"fixed_size"]
variables = ["rsi_value", "rsi_long", "rsi_short",
"fast_ma", "slow_ma", "ma_trend"]
def __init__(self, cta_engine, strategy_name, vt_symbol, setting):
""""""
super().__init__(cta_engine, strategy_name, vt_symbol, setting)
self.rsi_long = 50 + self.rsi_signal
self.rsi_short = 50 - self.rsi_signal
self.bg5 = BarGenerator(self.on_bar, 5, self.on_5min_bar)
self.am5 = ArrayManager()
self.bg15 = BarGenerator(self.on_bar, 15, self.on_15min_bar)
self.am15 = ArrayManager()
def on_init(self):
"""
Callback when strategy is inited.
"""
self.write_log("策略初始化")
self.load_bar(10)
def on_start(self):
"""
Callback when strategy is started.
"""
self.write_log("策略启动")
def on_stop(self):
"""
Callback when strategy is stopped.
"""
self.write_log("策略停止")
def on_tick(self, tick: TickData):
"""
Callback of new tick data update.
"""
self.bg5.update_tick(tick)
def on_bar(self, bar: BarData):
"""
Callback of new bar data update.
"""
self.bg5.update_bar(bar)
self.bg15.update_bar(bar)
def on_5min_bar(self, bar: BarData):
""""""
self.cancel_all()
self.am5.update_bar(bar)
if not self.am5.inited:
return
if not self.ma_trend:
return
self.rsi_value = self.am5.rsi(self.rsi_window)
if self.pos == 0:
if self.ma_trend > 0 and self.rsi_value >= self.rsi_long:
price = bar.close_price * 1.01
self.buy(Decimal(price), Decimal(self.fixed_size))
elif self.ma_trend < 0 and self.rsi_value <= self.rsi_short:
price = bar.close_price * 0.99
self.short(Decimal(price), Decimal(self.fixed_size))
elif self.pos > 0:
if self.ma_trend < 0 or self.rsi_value < 50:
price = bar.close_price * 0.99
self.sell(Decimal(price), Decimal(abs(self.pos)))
elif self.pos < 0:
if self.ma_trend > 0 or self.rsi_value > 50:
price = bar.close_price * 1.01
self.cover(Decimal(price), Decimal(abs(self.pos)))
self.put_event()
def on_15min_bar(self, bar: BarData):
""""""
self.am15.update_bar(bar)
if not self.am15.inited:
return
self.fast_ma = self.am15.sma(self.fast_window)
self.slow_ma = self.am15.sma(self.slow_window)
if self.fast_ma > self.slow_ma:
self.ma_trend = 1
else:
self.ma_trend = -1
def on_order(self, order: OrderData):
"""
Callback of new order data update.
"""
pass
def on_trade(self, trade: TradeData):
"""
Callback of new trade data update.
"""
self.put_event()
def on_stop_order(self, stop_order: StopOrder):
"""
Callback of stop order update.
"""
pass
```Shown in full with attribution under the source's licence. Licence: MIT
This summary was written by Stratmill's research agent from the original; it is not a copy of the source.