Hello, I thought I ask for help here. This is a think or swim provided strategy. I wanted to ask if it is possible to essentially get this strategy to be "visualized" on a combined lower indicator. If not, then possibly just have it show "preliminary" buys and exit's (like arrows that show once conditions are met) on the chart before the official fixed entries itself. Hopefully someone can help.
This code can be found under "R2Trend" in strategies.
#
# TD Ameritrade IP Company, Inc. (c) 2016-2022
#
input length = 18;
input rAverageLength = 3;
input lag = 10;
input averageLength = 50;
input trendLevel = 0.42;
input maxLevel = 0.85;
input lrCritLevel = 10;
input averageType = AverageType.SIMPLE;
def ma = MovingAverage(averageType, close, averageLength);
def r2 = reference RSquared(length = length, "r average length" = rAverageLength);
def lr = reference LinearRegressionSlope(price = close, length = length) * 100;
AddOrder(OrderType.BUY_TO_OPEN, r2 crosses above trendLevel and
r2 < maxLevel and
r2 > r2[lag] and
close > ma and
lr > lrCritLevel, tickcolor = GetColor(0), arrowcolor = GetColor(0), name = "R2_StrongUptrendLE");
AddOrder(OrderType.SELL_TO_OPEN, r2 crosses above trendLevel and
r2 < maxLevel and
r2 > r2[lag] and
close < ma and
lr < -lrCritLevel, tickcolor = GetColor(2), arrowcolor = GetColor(2), name = "R2_StrongDowntrendSE");
AddOrder(OrderType.SELL_TO_CLOSE, close crosses below ma, tickcolor = GetColor(4), arrowcolor = GetColor(4), name = "MovAvgLX");
AddOrder(OrderType.BUY_TO_CLOSE, close crosses above ma, tickcolor = GetColor(5), arrowcolor = GetColor(5), name = "MovAvgSX");
Here is a description of the strategy on the Think or swim webpage...
Simulated exit orders are added based on crossovers of the close price plot with its moving average: when the close price crosses below the average, a simulated sell-to-close order is added; when it crosses above - the simulation of buy-to-close takes place.
This code can be found under "R2Trend" in strategies.
#
# TD Ameritrade IP Company, Inc. (c) 2016-2022
#
input length = 18;
input rAverageLength = 3;
input lag = 10;
input averageLength = 50;
input trendLevel = 0.42;
input maxLevel = 0.85;
input lrCritLevel = 10;
input averageType = AverageType.SIMPLE;
def ma = MovingAverage(averageType, close, averageLength);
def r2 = reference RSquared(length = length, "r average length" = rAverageLength);
def lr = reference LinearRegressionSlope(price = close, length = length) * 100;
AddOrder(OrderType.BUY_TO_OPEN, r2 crosses above trendLevel and
r2 < maxLevel and
r2 > r2[lag] and
close > ma and
lr > lrCritLevel, tickcolor = GetColor(0), arrowcolor = GetColor(0), name = "R2_StrongUptrendLE");
AddOrder(OrderType.SELL_TO_OPEN, r2 crosses above trendLevel and
r2 < maxLevel and
r2 > r2[lag] and
close < ma and
lr < -lrCritLevel, tickcolor = GetColor(2), arrowcolor = GetColor(2), name = "R2_StrongDowntrendSE");
AddOrder(OrderType.SELL_TO_CLOSE, close crosses below ma, tickcolor = GetColor(4), arrowcolor = GetColor(4), name = "MovAvgLX");
AddOrder(OrderType.BUY_TO_CLOSE, close crosses above ma, tickcolor = GetColor(5), arrowcolor = GetColor(5), name = "MovAvgSX");
Here is a description of the strategy on the Think or swim webpage...
R2Trend
Description
As the first step, the R2Trend (RSquared) strategy calculates the RSquared, a technical indicator which measures how closely a data set of close prices conforms to the linear regression trendline. If the correlation is found to be higher than a certain threshold (the default value is 0.42), the strategy measures the steepness of the trendline, using Linear Regression Slope. Based on the premise that steeper slopes signify stronger trends (when the absolute value of the slope found is greater than the specified value), the overall trending conditions are evaluated by the strategy as follows:- RSquared values being greater than both a lower threshold (trend level in the input parameters) and its own prior value while the trendline slope is greater than critical - read as indication of a strong uptrend;
- RSquared values being greater than both a lower threshold (trend level in the input parameters) and its own prior value while the trendline slope is less than the negative critical - read as indication of a strong downtrend.
Simulated exit orders are added based on crossovers of the close price plot with its moving average: when the close price crosses below the average, a simulated sell-to-close order is added; when it crosses above - the simulation of buy-to-close takes place.
Input Parameters
length | Defines the period for the calculation of the RSquared. |
r average length | Defines the period for the calculation of the moving average for RSquared optimization. |
lag | Defines the lookback period for the prior RSquared value. |
average length | Defines the period for the calculation of the moving average. |
trend level | Defines the minimum RSquared level at which strong trending conditions are recognized. |
max level | Defines the maximum RSquared level at which strong trending conditions are recognized. |
lr crit level | Defines the minimum trendline slope at which strong trending conditions are recognized. |
average type | The type of moving average to be used in calculations: simple, exponential, weighted, Wilder's, or Hull. |