Trend Painter Indicator With Buy & Sell Signals for ThinkorSwim

cabe1332

Active member
@Mkirk Here's a basic one for you to get started.

Code:
input ThermoLookBackBars = 50;
input PlotType = {default AdaptiveMovingAverages, Standard};

def HighLowScore = 1000 * ((high - high[1]) / (high[1]) +
(low - low[1]) / low[1]);

#ATR TrailingStop Code
input trailType = {default modified, unmodified};
input ATRPeriod = 5;
input ATRFactor = 3.5;
input firstTrade = {default long, short};

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 ATRMod = ExpAverage(Max(HiLo, Max(HRef, LRef)), 2 * ATRPeriod - 1);

def loss;
switch (trailType) {
case modified:
    loss = ATRFactor * ATRMod;
case unmodified:
    loss = ATRFactor * Average(TrueRange(high,  close,  low),  ATRPeriod);
}

rec state = {default init, long, short};
rec 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 (close > trail[1]) {
        state = state.long;
        trail = Max(trail[1], close - 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.Hide();
#End ATR Trailing Stop Code

def A = Highest(high[1], ThermoLookBackBars);
def B = Lowest(low[1], ThermoLookBackBars);

def FiftyTwoWeekHigh = A;

def FiftyTwoWeekLow = B;

def FiftyTwoWeekScore = 10 * (((high
- FiftyTwoWeekHigh) / FiftyTwoWeekHigh) +
((low - FiftyTwoWeekLow) / FiftyTwoWeekLow));

def ThermoScore = ExpAverage(HighLowScore + FiftyTwoWeekScore, ThermoLookBackBars);

input FastLengthShort = 5;
input SlowLengthShort = 15;
input EffRatioShort = 10;
input FastLengthLong = 10;
input SlowLengthLong = 25;
input EffRatioLong = 5;

def AMA = MovAvgAdaptive(ThermoScore, FastLengthShort, SlowLengthShort, EffRatioShort);
def AMA2 = MovAvgAdaptive(ThermoScore, FastLengthLong, SlowLengthLong, EffRatioLong);

plot Line1;
Line1.Hide();
plot Line2;
Line2.Hide();

switch (PlotType) {
case AdaptiveMovingAverages:
    Line1 = AMA;
    Line2 = AMA2;
case Standard:
    Line1 = ThermoScore;
    Line2 = ThermoScore;
}

def InvisibleLine = close * 0;
plot Line3 = InvisibleLine;
Line3.Hide();

def Buy = Line1 > 0 and Line2 < 0 and state == state.long;
def StrongBuy = Line1 > 0 and Line2 >= 0 and state == state.long;
def Sell = Line1 < 0 and Line2 > 0 and state == state.short;
def StrongSell = Line1 < 0 and Line2 <= 0 and state == state.short;

plot GU = BuySignal;
GU.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
GU.SetDefaultColor(GetColor(8));
GU.SetLineWeight(2);

plot GX = SellSignal;
GX.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
GX.SetDefaultColor(GetColor(8));
GX.SetLineWeight(2);

AssignPriceColor(if Buy then Color.DARK_GREEN else if StrongBuy then Color.GREEN else if Sell then Color.DARK_RED else if StrongSell then Color.RED else Color.BLUE);

AddLabel(yes, Concat("Current Reading is ", (if Buy then "Up Trend" else if StrongBuy then "Strong Up Trend" else if Sell then "Down Trend" else if StrongSell then "Strong Down Trend" else "Neutral")),  if Buy then Color.DARK_GREEN else if StrongBuy then Color.GREEN else if Sell then Color.DARK_RED else if StrongSell then Color.RED else Color.GRAY);

AddOrder(OrderType.BUY_TO_OPEN, condition = GU, price = close,1, tickcolor = Color.GREEN, arrowcolor = Color.GREEN, name = "Long");
AddOrder(OrderType.SELL_TO_CLOSE, condition = GX, price = close,1, tickcolor = Color.GREEN, arrowcolor = Color.GREEN, name = "Cover");
@BenTen anyway you can replace the arrow signals with addchartbubble + close? I tried and not having any luck. I get continuous bubbles with close. @cabe1332
 

cabe1332

Active member
@BenTen anyway you can replace the arrow signals with addchartbubble + close? I tried and not having any luck. I get continuous bubbles with close. @cabe1332
@BenTen ignore my request, I figured it out to add a bubble + close when a bullish signal is triggered. Thank you for sharing the trend painter. Good luck! @cabe1332

sGcdvwD.png
 

mimi_r08

New member
I added Post #6 at the end of the trend painter code; however I am not getting any alerts on TOS.
Am i supposed to have the charts up in order to be alerted? Should i create a watchlist then have all the charts on my screen? Please let me know. Thank you for your reply!!
 
Last edited by a moderator:

rad14733

Well-known member
VIP
I added Post #6 at the end of the trend painter code; however I am not getting any alerts on TOS.
Am i supposed to have the charts up in order to be alerted? Should i create a watchlist then have all the charts on my screen? Please let me know. Thank you for your reply!!
Yes, Study Alerts only work when the Chart is up... Scan Alerts can fire in the background and are editable either via Scan or the Marketwatch > Alerts tab... Chart Alerts, set by Right-Clicking on the Chart and selecting Create Alert should fire regardless of whether the underlying chart is up... Chart Alerts are also maintained via Marketwatch > Alerts tab...
 
Last edited by a moderator:

rad14733

Well-known member
VIP
Thank you so much for your thorough response. Still trying to figure it out, it's frustrating!

If you watch a short watchlist the easiest method would be to create a Scan and then tell it to send alerts when a symbol triggers your conditions... Those have the most flexibility as far as methods of notifications...
 

MerryDay

Administrative
Staff member
Staff
VIP
@vloqnam This indicator is a version of the ATR Trailing Stop. There is nothing in the code that indicates there is any repainting issues.
 

cabe1332

Active member
Hi Cabe1332
I just started with trend painter but really like your indicators, can you share the code or did you and I missed it?

Thanks
@dburg507 if you are looking for the Trend Painter, you can find the code on this site. Use the defaults inputs. On the Plots, I only use GU and GX. I've changed things a bit (screenshot below) add/remove indicators. Good luck! @cabe1332.

hGgSyBD.png
 

dburg507

New member
@dburg507 if you are looking for the Trend Painter, you can find the code on this site. Use the defaults inputs. On the Plots, I only use GU and GX. I've changed things a bit (screenshot below) add/remove indicators. Good luck! @cabe1332.

hGgSyBD.png
Thanks for the speedy reply Cabe...
I have the original code for trend painter but it does not look like yours, the bubbles etc... I also like your momentum and ADX... Are those from this site as well?
Regards
 

cabe1332

Active member
Thanks for the speedy reply Cabe...
I have the original code for trend painter but it does not look like yours, the bubbles etc... I also like your momentum and ADX... Are those from this site as well?
Regards
@dburg507 the buy/sell signals are from Trend Reversal. Set the method to high_low. The ADX you also could find the code here. I have modified them a little to fit my style. Good luck! @cabe1332.
 

Similar threads

Top