R
RickAns
Guest
@tron That is really cool you had a chance to chat with Markus. I like his locked in Stop and I use it but I feel better also having a trailing stop for when the market gets news it does not like and decides to swing against me. If the market trends nicely in my direction I am less concerned on the stops. When the market behaves like it has an upset stomach then I start to think about trailing stops more. Good example being today's 400 point dip in the NASDAQ. Much rather get trailing stopped out early while in a profit then have it drop through the entry point and on down to the stop for a loss.
I took the ATR TRailing Stop of thinkorswim and modifed it to mimic Markus' Stop while adjusting to a new days gains. Wanted something quick and easy to test my idea, only been using this a few weeks. Also, I find this useful if I decide that the stock will continue running up beyond Markus' Profit Target and let a partial position ride to make futher gains. Easier for me to see when it has finished it's run and to step out. Instead of getting out completely as Markus suggests and moving on to another stock.
Basically, I changed the ATRperiod to 7, ATRfactor to 1.5, changed Aggregation to day, commented out their plot signals, added my own plots, label and an alert. Used the unmodified version since the modified smooths the signal. Not sure if averagetype should be Wilders but since this is not meant to be exact I left it. This is Long side only, not made the leap to Shorting side yet so did not code for it. Can add another instance of this and set to 4Hour, hour, thirty min, etc. if wanting an earlier warning.
It can also be set for the bar's low or it's close to fall below the trailing stop to trigger alert. I run this on my intra-day 15 min chart. I may re-write this to be less sloppy later on when I have decided what direction to take it.
Hope this explains things.
I took the ATR TRailing Stop of thinkorswim and modifed it to mimic Markus' Stop while adjusting to a new days gains. Wanted something quick and easy to test my idea, only been using this a few weeks. Also, I find this useful if I decide that the stock will continue running up beyond Markus' Profit Target and let a partial position ride to make futher gains. Easier for me to see when it has finished it's run and to step out. Instead of getting out completely as Markus suggests and moving on to another stock.
Basically, I changed the ATRperiod to 7, ATRfactor to 1.5, changed Aggregation to day, commented out their plot signals, added my own plots, label and an alert. Used the unmodified version since the modified smooths the signal. Not sure if averagetype should be Wilders but since this is not meant to be exact I left it. This is Long side only, not made the leap to Shorting side yet so did not code for it. Can add another instance of this and set to 4Hour, hour, thirty min, etc. if wanting an earlier warning.
It can also be set for the bar's low or it's close to fall below the trailing stop to trigger alert. I run this on my intra-day 15 min chart. I may re-write this to be less sloppy later on when I have decided what direction to take it.
Hope this explains things.
Code:
#
# TD Ameritrade IP Company, Inc. (c) 2009-2021
#
## Unmodified = calculations use the ATR on defined period multiplied by the specified factor.
## Modified = calculations uses long period MA, minimizing gaps between adjacent bars, smoothing out sharp surges in price.
input trailType = {default unmodified, modified};
input ATRPeriod = 7; # default 5, changed to make more like PowerX
input ATRFactor = 1.5; #default 3.5, changed to make more like PowerX
input firstTrade = {default long, short};
input averageType = AverageType.WILDERS;
Assert(ATRFactor > 0, "'atr factor' must be positive: " + ATRFactor);
## *modified formula*
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);
## *end of modified formula*
# my add for 1 Day trailing stop
def highHr = high(Period = AggregationPeriod.Day);
def closeHr = close(Period = AggregationPeriod.Day);
def lowHr = low(Period = AggregationPeriod.Day);
def trueRange;
switch (trailType) {
case modified:
trueRange = Max(HiLo, Max(HRef, LRef));
case unmodified:
trueRange = TrueRange(highHr, closeHr, lowHr);
}
def loss = ATRFactor * MovingAverage(averageType, trueRange, ATRPeriod);
def state = {default init, long, short};
def trail;
switch (state[1]) {
case init:
if (!IsNaN(loss)) {
switch (firstTrade) {
case long:
state = state.long;
trail = close - loss;
case short:
state = state.short;
trail = close + loss;
}
} else {
state = state.init;
trail = Double.NaN;
}
case long:
if (closeHr > trail[1]) {
state = state.long;
trail = Max(trail[1], closeHr - loss);
} else {
state = state.short;
trail = close + loss;
}
case short:
if (close < trail[1]) {
state = state.short;
trail = Min(trail[1], close + loss);
} else {
state = state.long;
trail = close - loss;
}
}
#def BuySignal = Crosses(state == state.long, 0, CrossingDirection.ABOVE);
#def SellSignal = Crosses(state == state.short, 0, CrossingDirection.ABOVE);
#plot TrailingStop = trail;
#TrailingStop.SetPaintingStrategy(PaintingStrategy.POINTS);
#TrailingStop.DefineColor("Buy", GetColor(0));
#TrailingStop.DefineColor("Sell", GetColor(1));
#TrailingStop.AssignValueColor(if state == state.long
# then TrailingStop.Color("Sell")
# else TrailingStop.Color("Buy"));
## my add
plot TrailStop = if state == state.long then Trail else Double.NaN;
TrailStop.SetDefaultColor(Color.dark_orange);
TrailStop.SetPaintingStrategy(PaintingStrategy.squares);
AddLabel(yes and state == state.long, "TrailStop-Day: " +round(Trail, 2)+ "", color.dark_orange);
AddLabel(yes and state == state.long and low crosses below Trail, "!!! ALERT : 1Day Trail Stop Hit !!!", color.plum);
alert(state == state.long and low crosses below Trail,"!!! ALERT : 1Day Trail Stop Hit !!!",alert.BAR,sound.RING);