Momentum Shift RS Engine - Production v3¶
Production-Ready Strategy
Enhanced version with fixed entry logic, dynamic stop-loss recalculation, profit targets, and real-time performance statistics.
Source Code¶
View Source Code Compare with Original
What's New in v3¶
🔧 Critical Fixes¶
| Issue | Original | Production v3 | Impact |
|---|---|---|---|
| Chop Breakout | Required double-bar confirmation | Uses ta.crossover() | ✅ More responsive entries |
| Stop-Loss Update | Fixed at entry (used var) | Recalculates every bar | ✅ Tighter risk management |
| Zero Division | Could error on flat markets | Protected with fallback | ✅ More robust |
✨ New Features¶
| Feature | Description | Benefit |
|---|---|---|
| Profit Targets | Configurable take-profit levels | ✅ Lock in gains systematically |
| Stats Panel | Real-time win rate, profit factor | ✅ Track performance live |
| Better Alerts | Entry/exit alerts with conditions | ✅ Stay informed |
| calc_on_order_fills | More accurate backtesting | ✅ Better simulation |
Key Improvements Explained¶
1. Fixed Entry Logic¶
Original (v1):
chopBreakoutCondition = normalizedValue_short > chopEntryThreshold and normalizedValue_short[1] > chopEntryThreshold
Problem: Required TWO consecutive bars above threshold - could miss fast moves.
Production v3:
Benefit: Triggers on the FIRST bar that crosses above - catches momentum earlier.
2. Dynamic Stop-Loss Recalculation¶
Original (v1):
var float stopPrice = 0.0
if strategy.position_size > strategy.position_size[1]
stopPrice := useStopLoss ? strategy.position_avg_price * (1 - stopLossPercent) : 0.0
Problem: Stop price was set ONCE at entry and never updated. If price moved favorably, stop stayed at entry level.
Production v3:
Benefit: Stop recalculates EVERY bar based on current average entry price. If you scale in, stop adjusts automatically.
3. Profit Taking System¶
New in v3:
useProfitTaking = input.bool(true, title="Use Take Profit Target?")
profitTargetPercent_tp = input.float(3.0, title="Take Profit Target (%)", minval=0.5, step=0.1) / 100
How it works: - When enabled, strategy automatically exits at entry_price × (1 + profitTargetPercent_tp) - Works with all exit rule types - Can be combined with trailing stops
Example: - Entry: $100 - Profit Target: 3% - Exit Price: $103
4. Statistics Panel¶
New Real-Time Metrics:
| Metric | What It Shows | Why It Matters |
|---|---|---|
| Trades | Total closed trades | Track activity level |
| Win Rate | % of profitable trades | Measure consistency |
| Profit Factor | Total Wins / Total Losses | Risk-adjusted performance |
| Total Win | Sum of all winning trades | Track gains |
| Total Loss | Sum of all losing trades | Track losses |
Color Coding: - Win Rate ≥ 50% = Green - Win Rate < 50% = Red - Profit Factor > 1.5 = Green - Profit Factor ≤ 1.5 = Orange
Configuration Comparison¶
Entry Thresholds¶
| Setting | Original | Production v3 | Notes |
|---|---|---|---|
| Entry Threshold | 0.2 | 0.2 | Same |
| Chop Entry Threshold | 0.8 | 0.8 | Same |
| Exit Threshold | 0.2 | 0.2 | Same |
Risk Management¶
| Setting | Original | Production v3 | Change |
|---|---|---|---|
| Stop Loss % | 2.0% | 2.0% | Same |
| Breakeven Trigger | 2.0% | 0.5% | ✅ Moves to BE faster |
| Take Profit | ❌ None | ✅ 3.0% | ✨ NEW |
Filters¶
| Filter | Original | Production v3 | Status |
|---|---|---|---|
| PVT Filter | ✅ | ✅ | Same |
| BBW Filter | ✅ | ✅ | Same |
| VIX Filter | ✅ | ✅ | Same |
| Cooldown | ✅ | ✅ | Same |
Entry Conditions (Unchanged)¶
All entry conditions remain the same as original:
- ✅ Trend Filter: Price structure confirms uptrend
- ✅ Superlong RS: 200-period normalized RS > 0.2
- ✅ Long-term RS: 50-period normalized RS > 0.2
- ✅ Medium-term RS: 20-period normalized RS > 0.0
- ✅ Short-term Breakout: 9-period normalized RS crosses above 0.8 ⚠️ (FIXED)
- ✅ PVT: Price Volume Trend > Signal line (if enabled)
- ✅ BBW: Bollinger Band Width > 0.01 (if enabled)
- ✅ VIX: VIX < 20 SMA AND < 18 (if enabled)
- ✅ Cooldown: Not within X bars of previous exit (if enabled)
Exit Rules (Enhanced)¶
"All Active" (Default)¶
Original: - Medium-term RS crosses below 0.2 (indicator exit) - OR price hits consolidated stop
Production v3: - Medium-term RS crosses below 0.2 (indicator exit) - OR price hits consolidated stop - OR price hits profit target ✨ NEW
"Price Stops Only"¶
Original: - Fixed stop loss - 9 SMA trailing stop - Breakeven stop
Production v3: - Fixed stop loss (recalculated every bar) ⚠️ FIXED - 9 SMA trailing stop - Breakeven stop (triggers at 0.5% instead of 2%) ⚠️ IMPROVED - Profit target ✨ NEW
"Indicator Only"¶
Original: - Medium-term RS crosses below 0.2
Production v3: - Medium-term RS crosses below 0.2 - Profit target ✨ NEW (optional)
"9 SMA Trail Only"¶
Unchanged - Still uses 9 SMA as trailing stop.
Breakeven Stop Improvement¶
| Version | Trigger | Example |
|---|---|---|
| Original | After 2% profit | Entry $100 → BE at $100 when price hits $102 |
| Production v3 | After 0.5% profit | Entry $100 → BE at $100 when price hits $100.50 |
Why This Matters: - Original: Risked giving back 2% before protecting capital - Production v3: Protects capital after just 0.5% gain - Result: Better risk-adjusted returns, fewer "winners turned losers"
Usage Recommendations¶
Conservative Setup¶
| Setting | Value | Reason |
|---|---|---|
| Use Profit Taking | ✅ True | Lock in gains |
| Profit Target | 3.0% | Reasonable target |
| Breakeven Trigger | 0.5% | Protect capital early |
| Exit Rule | All Active | Multiple exit methods |
Aggressive Setup¶
| Setting | Value | Reason |
|---|---|---|
| Use Profit Taking | ❌ False | Let winners run |
| Breakeven Trigger | 1.0% | Give more room |
| Exit Rule | 9 SMA Trail Only | Ride the trend |
Backtesting Improvements¶
New Setting:
What it does: - Recalculates strategy on every order fill - More accurate simulation of real trading - Better handling of partial fills and scaling
When to use: - ✅ Always use for production strategies - ✅ Essential for strategies with multiple entries/exits - ✅ Better for options/futures trading
Migration Guide¶
Upgrading from Original¶
- Copy Production v3 code to TradingView
- Review your settings:
- Breakeven trigger changed from 2% → 0.5% (adjust if needed)
- Profit target now available (enable if desired)
- Test on paper trading first
- Compare performance - Production v3 should show:
- More entries (faster chop breakout detection)
- Better risk management (dynamic stops)
- Higher win rate (earlier breakeven)
Settings to Adjust¶
| If You Want... | Change This... |
|---|---|
| More entries | Lower chopEntryThreshold (0.7 instead of 0.8) |
| Tighter stops | Lower stopLossPercent (1.5% instead of 2%) |
| Faster BE | Keep profitTargetPercent at 0.5% (default) |
| Let winners run | Disable useProfitTaking |
Known Limitations¶
- Profit Target vs Trailing Stop:
- If profit target is hit, trailing stop won't trigger
-
Consider using "All Active" exit rule for best of both
-
Stats Panel Performance:
- Calculates on every bar (can slow down on long charts)
-
Disable
showPanelif experiencing lag -
Breakeven Aggressiveness:
- 0.5% trigger might be too tight for volatile stocks
- Adjust
profitTargetPercentif getting stopped out too early
Related Strategies¶
- Momentum Shift RS Engine (Original) - Base version
- Momentum Shift 5min MTF - Intraday scalping version
Changelog¶
| Version | Date | Changes |
|---|---|---|
| v3 | 2024-12-10 | Fixed entry logic, dynamic stops, profit targets, stats panel |
| v1 | 2024-12-09 | Initial release |
Support & Feedback¶
If you find issues or have suggestions for v4, please document them in your trading journal or notes.