This solution requires that you set the time frame of the chart to daily. This will not work on any other time frame because instead of exiting after 7 days it's exiting after 7 bars. Whatever time frame you set the chart to, the exit will always occur on the 7th bar after the entry signal.
I have included a user input to allow you to adjust the number of bars before exit through the Edit Studies window.
input price = close;
input length = 2;
input rsiAverageType = AverageType.WILDERS;
input exitAfterBars = 7;
def rsi = reference RSI(price = price, length = length, averageType = rsiAverageType);
def longEntry = rsi crosses below 5;
AddOrder(OrderType.BUY_TO_OPEN, longEntry);
AddOrder(OrderType.SELL_TO_CLOSE, longEntry[exitAfterBars]);
A word of caution. Writing strategies for back-testing is deceptively complex and should not be attempted by anyone that is brand new to writing code. Take a year or two writing chart studies before you start writing strategies. And even then you will find you occasionally fall into some trap that only becomes evident when you try to trade the strategy during a live market.
Thinkorswim does not have any safeguards preventing the creation of chart strategies which are impossible to trade during a live market. You will need at least 2 years of writing chart studies before you are able to understand how to avoid those types of mistakes.