I've been working on trying to reverse the ATRTrailingStop and create ATRTrailingProfit. I have put the following code together, but it's not plotting anything. What have I done wrong? Anyone have any ideas?
Code:
input trailType = {default modified, unmodified};
input ATRPeriod = 5;
input ATRFactor = 3.5;
input firstTrade = {default long, short};
input averageType = AverageType.WILDERS;
Assert(ATRFactor > 0, "'atr factor' must be positive: " + ATRFactor);
def HiLo = Min(high - low, 1.5 * Average(high - low, ATRPeriod));
def HRef = if low <= high[1]
then high - close[1]
else (high - close[1]) - 0.5 * (low - high[1]);
def LRef = if high >= low[1]
then close[1] - low
else (close[1] - low) - 0.5 * (low[1] - high);
def trueRange;
switch (trailType) {
case modified:
trueRange = Max(HiLo, Max(HRef, LRef));
case unmodified:
trueRange = TrueRange(high, close, low);
}
def target = ATRFactor * MovingAverage(averageType, trueRange, ATRPeriod);
def state = {default init, long, short};
def trail;
switch (state[1]) {
case init:
if (!IsNaN(target)) {
switch (firstTrade) {
case long:
state = state.long;
trail = close + target;
case short:
state = state.short;
trail = close - target;
}
} else {
state = state.init;
trail = Double.NaN;
}
case long:
if (close < trail[1]) {
state = state.long;
trail = Max(trail[1], close + target);
} else {
state = state.short;
trail = close - target;
}
case short:
if (close > trail[1]) {
state = state.short;
trail = Min(trail[1], close - target);
} else {
state = state.long;
trail = close + target;
}
}
def BuySignal = Crosses(state == state.long, 0, CrossingDirection.ABOVE);
def SellSignal = Crosses(state == state.short, 0, CrossingDirection.ABOVE);
plot TrailingTarget = trail;
TrailingTarget.SetPaintingStrategy(PaintingStrategy.POINTS);
TrailingTarget.DefineColor("Buy", GetColor(0));
TrailingTarget.DefineColor("Sell", GetColor(1));
TrailingTarget.AssignValueColor(if state == state.long
then TrailingTarget.Color("Sell")
else TrailingTarget.Color("Buy"));