Hoping someone can help me with this request: I have this ATR stop script that I found online and works great for my trading style. I'm trying to add a logic to this code that would show me how close the price is to the ATR Stop; which is being defined as "trail" here I think. I tried adding an input and tried to define it as a percent of the trail value, but it's giving me a constant value.
If someone can help me figure out how to make it to work that'd be greatly appreciated. Essentially I'd like to be able to say the VSTOP is within X% of the price and if it is I'd like to plot it.
Code:
input Percent_Near_VStop = 3; def Avg_Above_VStop = trail * (Percent_Near_VStop/100); plot VStopProximity = Avg_Above_VStop;
If someone can help me figure out how to make it to work that'd be greatly appreciated. Essentially I'd like to be able to say the VSTOP is within X% of the price and if it is I'd like to plot it.
Code:
#ATR Calculation
input ATRPeriod = 10;
input ATRFactor = 1.5;
input firstTrade = {default long, short};
input averageType = AverageType.SIMPLE;
Assert(ATRFactor > 0, "'atr factor' must be positive: " + ATRFactor);
def trueRange = TrueRange(high, close, low);
def loss = ATRFactor * MovingAverage(averageType, trueRange, ATRPeriod);
def state = {default init, long, short};
def trail;
def mclose;
switch (state[1]) {
case init:
if (!IsNaN(loss)) {
switch (firstTrade) {
case long:
state = state.long;
mclose = close;
trail = close - loss;
case short:
state = state.short;
mclose = close;
trail = close + loss;
}
} else {
state = state.init;
trail = Double.NaN;
mclose = Double.NaN;
}
case long:
if (close > trail[1]) {
state = state.long;
mclose = Max(mclose[1], close);
trail = Max (trail[1], mclose - loss);
} else {
state = state.short;
mclose = close;
trail = mclose + loss;
}
case short:
if (close < trail[1]) {
state = state.short;
mclose = Min(mclose[1], close);
trail = Min(trail[1], mclose + loss);
} else {
state = state.long;
mclose = close + 0;
trail = mclose - loss;
}
}
#AddLabel(StopLabel, "ATR Stop =" + Round(trail[1], 2), color = Color.BLUE);
plot TrailingStop = trail;
Last edited by a moderator: