Exit strategy after 7 days


Category:
0
0

the strat below exits after RSI2 crosses above 50.  instead, i’d like the strategy to exit after a user-specified number of days, 7, for example.  how would you do that?  thanks!

input price = close;
input length = 2;

input rsiAverageType = AverageType.WILDERS;

def rsi = reference RSI(price = price, length = length, averageType = rsiAverageType);

AddOrder(OrderType.BUY_to_open, rsi crosses below 5);
AddOrder(OrderType.SELL_to_close, rsi crosses above 50);

 

 

Marked as spam
Posted by (Questions: 6, Answers: 3)
Asked on March 28, 2022 6:57 am
141 views
0
Private answer

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.

Marked as spam
Posted by (Questions: 37, Answers: 4118)
Answered on March 28, 2022 8:25 am