Separate a pullback recovery from a weak-market bounce
An RSI recovery above 30 can occur in both an uptrend and a prolonged decline. This Thinkorswim strategy study tests whether requiring price above a 50-bar simple moving average changes the behavior of long-only recovery entries. It is a simulation hypothesis, not a promise of better performance.
Rules and numerical example
Use 14-period RSI with its default Wilder smoothing. A long entry requires the previous RSI at or below 30, current RSI above 30, and close strictly above the 50-bar SMA. Exit when RSI reaches 65 or close falls below the SMA. For example, RSI values 38, 27, 25, 32, 39 give a recovery signal at 32. With close 102 and SMA 100 the entry passes; with close 98 and SMA 100 it fails. A blocked signal is not carried forward: a fresh crossing is required.

Hypothetical RSI paths and trend contexts; the chart compares identical oscillator recoveries under different price filters.
thinkScript strategy implementation
input rsiLength = 14;
input trendLength = 50;
input recovery = 30;
input exitLevel = 65;
input quantity = 1;
def r = RSI(length = rsiLength);
def trend = Average(close, trendLength);
def ready = !IsNaN(trend) and !IsNaN(r[1]);
def buySignal = ready and r crosses above recovery
and close > trend;
def sellSignal = ready and
(r >= exitLevel or close < trend);
rec held = CompoundValue(1,
if held[1] == 0 and buySignal then 1
else if held[1] == 1 and sellSignal then 0
else held[1], 0);
AddOrder(OrderType.BUY_TO_OPEN,
held == 1 and held[1] == 0,
open[-1], quantity, Color.GREEN,
Color.GREEN, "Recovery");
AddOrder(OrderType.SELL_TO_CLOSE,
held == 0 and held[1] == 1,
open[-1], quantity, Color.RED,
Color.RED, "Exit");
Create a strategy rather than an ordinary study. The recursive held variable represents intended simulated exposure after each signal, not a brokerage position. Its transitions suppress repeated entries and exits. AddOrder schedules a simulated order for the next bar; open[-1] is used only as the simulated order price, never as an input to the signal. The current forming bar can change before closing. Use completed bars for interpretation and allow sufficient warm-up history.
Execution and risk limitations
Thinkorswim AddOrder does not submit unattended live orders. The next-bar open is a modeling convention and does not include actual queue position, liquidity, commissions or slippage. This example has no intrabar protective stop; a trend exit may occur after a large adverse move. For risk experiments add a separately defined protective exit and conservative fill assumptions, rather than treating the SMA as a guaranteed stop. The state variable cannot reconcile actual broker fills or rejected orders.
Testing plan
Compare the filtered strategy with an otherwise identical unfiltered RSI recovery strategy. Keep quantity, exit rules and sample periods constant. Measure number of trades, average net trade, drawdown and time in market; account for transaction costs and test trending and declining periods separately. Reserve an out-of-sample period and check sensitivity to 40, 50 and 60-bar trend lengths. The example has not been compiled or backtested here. Trading can result in substantial losses.
For customized strategy studies and simulation logic, see Custom Thinkorswim strategy Development Services.