Smoothed Heikin-Ashi with ATR Trail For ThinkOrSwim

JoeDV

Active member
VIP
Smoothed Heikin-Ashi with ATR Trail + P/L (Study)
I put this in Strategies since it's a study that has the P/L built in. It's a combination of things I've found on here over the years (so if you were the original author, just let me know and I'll give credit). It combines a smoothed Heikin-Ashi with an ATR Trail to come up with the buy/sell signals. Not saying it works well or not, just putting it here if someone wants to play around with it. I swing trade so have really only looked at it on a daily timeframe. I'm guessing it would be too slow for day-trading, though you might be able to tweak the settings to make it work.



Ruby:
#hint: Smoothed Heiken Ashi and ATR studies

input period = 20;
input hideCandles = no;
input candleSmoothing = {default Valcu, Vervoort};

input movingAverageType = {Simple, default Exponential, Weighted, Hull, Variable, TEMA};

def openMA;
def closeMA;
def highMA;
def lowMA;

switch (movingAverageType) {
case Simple:
    openMA = compoundValue(1, Average(open, period), open);
    closeMA = compoundValue(1, Average(close, period), close);
    highMA = compoundValue(1, Average(high, period), high);
    lowMA = compoundValue(1, Average(low, period), low);
case Exponential:
    openMA = compoundValue(1, ExpAverage(open, period), open);
    closeMA = compoundValue(1, ExpAverage(close, period), close);
    highMA = compoundValue(1, ExpAverage(high, period), high);
    lowMA = compoundValue(1, ExpAverage(low, period), low);
case Weighted:
    openMA = compoundValue(1, WMA(open, period), open);
    closeMA = compoundValue(1, WMA(close, period), close);
    highMA = compoundValue(1, WMA(high, period), high);
    lowMA = compoundValue(1, WMA(low, period), low);
Case Hull:
    openMA = compoundValue(1, HullMovingAvg(open, period), open);
    closeMA = compoundValue(1, HullMovingAvg(close, period), close);
    highMA = compoundValue(1, HullMovingAvg(high, period), high);
    lowMA = compoundValue(1, HullMovingAvg(low, period), low);
case variable:
    openMA = compoundValue(1, VariableMA(open, period), open);
    closeMA = compoundValue(1, VariableMA(close, period), close);
    highMA = compoundValue(1, VariableMA(high, period), high);
    lowMA = compoundValue(1, VariableMA(low, period), low);
case TEMA:
    openMA = compoundValue(1, TEMA(open, period), open);
    closeMA = compoundValue(1, TEMA(close, period), close);
    highMA = compoundValue(1, TEMA(high, period), high);
    lowMA = compoundValue(1, TEMA(low, period), low);
}

hidePricePlot(hideCandles);

def haOpen;
def haClose;

switch(candleSmoothing) {
case Valcu:
    haOpen = CompoundValue(1, ( (haOpen[1] + (openMA[1] + highMA[1] + lowMA[1] + closeMA[1]) /4.0)/2.0), open);
    haClose = ((OpenMA + HighMA + LowMA + CloseMA)/4.0) ;

case Vervoort:
    haOpen = CompoundValue(1, ( (haOpen[1] + (openMA[1] + highMA[1] + lowMA[1] + closeMA[1]) /4.0)/2.0), open);
    haClose = ((((OpenMA + HighMA + LowMA + CloseMA)/4.0) + haOpen + Max(HighMA, haOpen) + Min(LowMA, haOpen))/4.0);
}

plot o = haOpen + 0;
o.hide();

### Wicks and Shadows

def haLow = min(lowMA, haOpen);
def haHigh = max(highMA,haOpen);


#Red Candlesticks -----------------------------------------------------------------|

def haOpen_fall = if haOpen>haClose
              then haOpen
              else double.nan;
def haHigh_fall   = if haOpen>=haClose
              then haHigh
              else double.nan;
def haLow_fall    = if haOpen>=haClose
              then haLow
              else double.nan;
def haClose_fall    = if haOpen>=haClose
              then haClose
              else double.nan;

AddChart(growColor = Color.plum, fallColor = Color.blue, neutralColor = Color.current, high = haHigh_fall, low = haLow_fall, open = haOpen_fall, close = haClose_fall , type = ChartType.CANDLE);


def haOpen_rise = if haOpen<haClose
                then haClose
                else double.nan;
def haHigh_rise  = if haOpen<=haClose
              then haHigh
              else double.nan;
def haLow_rise   = if haOpen<=haClose
              then haLow
              else double.nan;
def haClose_rise   = if haOpen<=haClose
              then haOpen
              else double.nan;

AddChart(growColor = Color.blue, fallColor = Color.plum, neutralColor = Color.current, high = haHigh_rise, low = haLow_rise, open = haOpen_rise, close = HAclose_rise, type = ChartType.CANDLE);

#############################################################################

#############################################################
###   Determine a flat market
#############################################################


input TradeInFlatRange = Yes;
input BarsForFlatRange = 15;
input BarsReqToStayInRange = 13;

def HH = Highest(high[1], BarsForFlatRange);
def LL = Lowest(low[1], BarsForFlatRange);
def maxH = Highest(HH, BarsReqToStayInRange);
def maxL = Lowest(LL, BarsReqToStayInRange);
def HHn = if maxH == maxH[1] or maxL == maxL then maxH else HHn[1];
def LLn = if maxH == maxH[1] or maxL == maxL then maxL else LLn[1];
def Bh = if high <= HHn and HHn == HHn[1] then HHn else Double.NaN;
def Bl = if low >= LLn and LLn == LLn[1] then LLn else Double.NaN;
def CountH = if IsNaN(Bh) or IsNaN(Bl) then 2 else CountH[1] + 1;
def CountL = if IsNaN(Bh) or IsNaN(Bl) then 2 else CountL[1] + 1;
def ExpH = if BarNumber() == 1 then Double.NaN else
            if CountH[-BarsReqToStayInRange] >= BarsReqToStayInRange then HHn[-BarsReqToStayInRange] else
            if high <= ExpH[1] then ExpH[1] else Double.NaN;
def ExpL = if BarNumber() == 1 then Double.NaN else
            if CountL[-BarsReqToStayInRange] >= BarsReqToStayInRange then LLn[-BarsReqToStayInRange] else
            if low >= ExpL[1] then ExpL[1] else Double.NaN;

plot BoxHigh = if !isnan(expL) and !isnan(ExpH) then ExpH else double.nan;
plot BoxLow = if !isnan(expL) and !isnan(ExpH) then ExpL else double.nan;


addcloud( BoxHigh, BoxLow, color.gray, color.gray);


def Flat = if (!isNan(BoxHigh[1]) and !isNan(BoxLow[1])) AND !TradeInFlatRange then 1 else 0;

#addChartBubble(Flat==1 and isNan(Flat[1]),BoxHigh[1],"Flat Market",color.gray,yes);

input trailType = {default modified, unmodified};
input ATRPeriod = 5;
input ATRFactor = 3.0;
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 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 (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.SetPaintingStrategy(PaintingStrategy.line);
TrailingStop.DefineColor("Buy", color.blue);
TrailingStop.DefineColor("Sell", color.plum);
TrailingStop.AssignValueColor(if state == state.long
    then TrailingStop.Color("Buy")
    else TrailingStop.Color("Sell"));


############################################
##  Define BuySignal and SellSignal above
##  or uncomment them out below and set
##  them to your buy/sell conditions
##
##  If using stops, define them below
############################################

###------------------------------------------------------------------------------------------

input showSignals = yes;
input showLabels  = yes;
input showBubbles = yes;
input useStops = no;
input useAlerts = no;

###------------------------------------------------------------------------------------------

############################################
##  Create Signals -
##  FILL IN THIS SECTION IF NOT DEFINED ABOVE
##
############################################


def BuySignal = state == state.long AND (haOpen < haClose) and (haOpen[1] < haClose[1]) and !Flat ; # insert condition to create long position
def SellSignal = state == state.short AND (haOpen > haClose)  and (haOpen[1] > haClose[1]) and !Flat; # insert condition to create short position

def BuyStop  = if !useStops then 0 else if  state == state.long AND (haOpen > haClose) and Flat then 1 else 0  ; # insert condition to stop in place of the 0<0
def SellStop = if !useStops then 0 else if  state == state.short AND (haOpen < haClose) and Flat then 1 else 0  ; # insert condition to stop in place of the 0>0

#######################################
##  Maintain the position of trades
#######################################

def CurrentPosition;  # holds whether flat = 0 long = 1 short = -1

if (BarNumber()==1) OR isNaN(CurrentPosition[1]) {
    CurrentPosition = 0;
}else{
        if CurrentPosition[1] == 0 {            # FLAT
            if (BuySignal) {
                CurrentPosition = 1;
            } else if (SellSignal){
                CurrentPosition = -1;
            } else {
                CurrentPosition = CurrentPosition[1];
            }
       } else if CurrentPosition[1] == 1 {      # LONG
            if (SellSignal){
                CurrentPosition = -1;
            } else if (BuyStop and useStops){
                CurrentPosition = 0;
            } else {
                CurrentPosition = CurrentPosition[1];
            }
       } else if CurrentPosition[1] == -1 {     # SHORT
            if (BuySignal){
                CurrentPosition = 1;
            } else if (SellStop and useStops){
                CurrentPosition = 0;
            } else {
                CurrentPosition = CurrentPosition[1];
            }
       } else {
            CurrentPosition = CurrentPosition[1];
       }
}


def isLong  = if CurrentPosition == 1 then 1 else 0;
def isShort = if CurrentPosition == -1 then 1 else 0;
def isFlat  = if CurrentPosition == 0 then 1 else 0;

# If not already long and get a BuySignal
Plot BuySig = if (!isLong[1] and BuySignal and showSignals) then 1 else 0;
BuySig.AssignValueColor(color.cyan);
BuySig.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
BuySig.SetLineWeight(5);

Alert(BuySig and useAlerts, "Buy Signal",Alert.bar,sound.Ding);
Alert(BuySig and useAlerts, "Buy Signal",Alert.bar,sound.Ding);

# If not already short and get a SellSignal
Plot SellSig = if (!isShort[1] and SellSignal and showSignals) then 1 else 0;
SellSig.AssignValueColor(color.cyan);
SellSig.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
SellSig.SetLineWeight(5);

Alert(SellSig and useAlerts, "Sell Signal",Alert.bar,sound.Ding);
Alert(SellSig and useAlerts, "Sell Signal",Alert.bar,sound.Ding);

# If long and get a BuyStop
Plot BuyStpSig = if (BuyStop and isLong[1] and showSignals and useStops) then 1 else 0;
BuyStpSig.AssignValueColor(color.light_gray);
BuyStpSig.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
BuyStpSig.SetLineWeight(3);

# If short and get a SellStop
Plot SellStpSig = if (SellStop and isShort[1] and showSignals and useStops) then 1 else 0;
SellStpSig.AssignValueColor(color.light_gray);
SellStpSig.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
SellStpSig.SetLineWeight(3);


#######################################
##  Orders
#######################################

def isOrder = if CurrentPosition == CurrentPosition[1] then 0 else 1; # Position changed so it's a new order

# If there is an order, then the price is the next days close
def orderPrice = if (isOrder and (BuySignal or SellSignal)) then close else orderPrice[1];

#######################################
##  Price and Profit
#######################################

def profitLoss;


if (!isOrder or orderPRice[1]==0){
    profitLoss = 0;
} else if ((isOrder and isLong[1]) and (SellSig or BuyStpSig)){
    profitLoss = close - orderPrice[1];
} else if ((isOrder and isShort[1]) and (BuySig or SellStpSig)) {
    profitLoss = orderPrice[1] - close;
} else {
    profitLoss = 0;
}


# Total Profit or Loss
def profitLossSum = compoundValue(1, if isNaN(isOrder)  or barnumber()==1 then 0 else if isOrder then profitLossSum[1] + profitLoss else profitLossSum[1], 0);

# How many trades won or lost
def profitWinners = compoundValue(1, if isNaN(profitWinners[1]) or barnumber()==1 then 0 else if isOrder and profitLoss > 0 then profitWinners[1] + 1 else profitWinners[1], 0);
def profitLosers = compoundValue(1, if isNaN(profitLosers[1])  or barnumber()==1 then 0 else if isOrder and profitLoss < 0 then profitLosers[1] + 1 else profitLosers[1], 0);
def profitPush = compoundValue(1, if isNaN(profitPush[1])  or barnumber()==1 then 0 else if isOrder and profitLoss == 0 then profitPush[1] + 1 else profitPush[1], 0);

def orderCount = (profitWinners + profitLosers + profitPush) - 1;

# Current Open Trade Profit or Loss
def TradePL = If isLong then Round(((close - orderprice)/TickSize())*TickValue()) else if isShort then Round(((orderPrice - close)/TickSize())*TickValue()) else 0;

# Convert to actual dollars based on Tick Value for bubbles
def dollarProfitLoss = if orderPRice[1]==0 or isNaN(orderPrice[1]) then 0 else round((profitLoss/Ticksize())*Tickvalue());

# Closed Orders dollar P/L
def dollarPLSum = round((profitLossSum/Ticksize())*Tickvalue());


# Split profits or losses by long and short trades
def profitLong = compoundValue(1, if isNan(profitLong[1])  or barnumber()==1 then 0 else if isOrder and isLong[1] then profitLong[1]+dollarProfitLoss else profitLong[1],0);
def profitShort = compoundValue(1, if isNan(profitShort[1])  or barnumber()==1 then 0 else if isOrder and isShort[1] then profitShort[1]+dollarProfitLoss else profitShort[1],0);
def countLong = compoundValue(1, if isNaN(countLong[1])  or barnumber()==1 then 0 else if isOrder and isLong[1] then countLong[1]+1 else countLong[1],0);
def countShort = compoundValue(1, if isNaN(countShort[1])  or barnumber()==1 then 0 else if isOrder and isShort[1] then countShort[1]+1 else countShort[1],0);

# What was the biggest winning and losing trade
def biggestWin = compoundValue(1, if isNaN(biggestWin[1]) or barnumber()==1 then 0 else if isOrder and (dollarProfitLoss > 0) and (dollarProfitLoss > biggestWin[1]) then dollarProfitLoss else biggestWin[1], 0);
def biggestLoss = compoundValue(1, if isNaN(biggestLoss[1]) or barnumber()==1 then 0 else if isOrder and (dollarProfitLoss < 0) and (dollarProfitLoss < biggestLoss[1]) then dollarProfitLoss else biggestLoss[1], 0);

# What percent were winners
def PCTWin = round((profitWinners/orderCount)*100,2);


# Average trade
def avgTrade = round((dollarPLSum/orderCount),2);


#######################################
##  Create Labels
#######################################

AddLabel(showLabels, GetSymbol()+" Tick Size: "+TickSize()+" Value: "+TickValue(), color.white);
AddLabel(showLabels, "Closed Orders: " + orderCount + " P/L: " + AsDollars(dollarPLSum), if dollarPLSum > 0 then Color.GREEN else if dollarPLSum< 0 then Color.RED else Color.GRAY);
AddLabel(if !IsNan(orderPrice) and showLabels then 1 else 0, "Closed+Open P/L: "+ AsDollars(TradePL+dollarPLSum), if ((TradePL+dollarPLSum) > 0) then color.green else if ((TradePL+dollarPLSum) < 0) then color.red else color.gray);

AddLabel(showLabels, "Avg per Trade: "+ AsDollars(avgTrade), if avgTrade > 0 then Color.Green else if avgTrade < 0 then Color.RED else Color.GRAY);
AddLabel(showLabels, "Winners: "+ PCTWin +"%",if PCTWin > 50 then color.green else if PCTWin > 40 then color.yellow else color.gray);

AddLabel(showLabels, "MaxUp: "+ AsDollars(biggestWin) +" MaxDown: "+AsDollars(biggestLoss), color.white);
AddLabel(showLabels, "Long Profit: " +AsDollars(profitLong), if profitLong > 0 then color.green else if profitLong < 0 then color.red else color.gray);
AddLabel(showLabels, "Short Profit: " +AsDollars(profitShort), if profitShort > 0 then color.green else if profitShort < 0 then color.red else color.gray);
AddLabel(if !IsNan(CurrentPosition) and showLabels then 1 else 0, "Open: "+ (If isLong then "Bought" else "Sold") + " @ "+orderPrice, color.white);
AddLabel(if !IsNan(orderPrice) and showLabels then 1 else 0, "Open Trade P/L: "+ AsDollars(TradePL), if (TradePL > 0) then color.green else if (TradePl < 0) then color.red else color.gray);



#######################################
##  Chart Bubbles for Profit/Loss
#######################################


AddChartBubble(showSignals and showBubbles and isOrder and isLong[1], low, "$"+dollarProfitLoss, if dollarProfitLoss == 0 then Color.LIGHT_GRAY else if dollarProfitLoss > 0 then Color.GREEN else color.Red, 0);
AddChartBubble(showSignals and showBubbles and isOrder and isShort[1], high,  "$"+dollarProfitLoss, if dollarProfitLoss == 0 then Color.LIGHT_GRAY else if dollarProfitLoss > 0 then Color.GREEN else color.Red, 1);

#AssignPriceColor(if CurrentPosition == 1 then color.green else if CurrentPosition == -1 then color.red else color.gray);

Cy9VkMF.png



and yes, I picked one where it works really well on a daily 1yr. Some aren't so pretty. :)

Here's one that's not as pretty, but not awful either, also a 1yr daily

7xi35KW.png
 
Last edited by a moderator:
love the P&L backtest. thanks for sharing. so far on the backtest, seems like larger atr has better avg return without optimization.
 
love the P&L backtest. thanks for sharing. so far on the backtest, seems like larger atr has better avg return without optimization.

Thanks, it can be applied to any study, just need to define the buy and sell signals (and/or stops). Doesn't give history like a strategy but makes it quick and easy to play with inputs and see the effect.
 
Thanks for sharing. How do we interpret the "shaded cloud" area?

Oh, that's where it tries to determine a "flat market". (again, something I found on here), and you have the option of not trading during those times. It will close out a position if signaled, but won't re-enter until outside of that range.
 
I've done well using the original smoothed heiken ashi indicator along with the RSI. I usually trade /GC and /ZB. How do you propose I use your indicator? Any suggestions? Thanks. Good work btw.
 
I've done well using the original smoothed heiken ashi indicator along with the RSI. I usually trade /GC and /ZB. How do you propose I use your indicator? Any suggestions? Thanks. Good work btw.
It combines a smoothed Heikin-Ashi with an ATR Trail to come up with the buy/sell signals. Not saying it works well or not, just putting it here if someone wants to play around with it. https://usethinkscript.com/threads/...th-atr-trail-for-thinkorswim.9992/#post-89555
 
JoeDV,

I'm exploring the strategy now, thank you for sharing. But as a newbie to thinkscript, I largely have been trying to dismantle the coding to try and learn how it works. A lot to explore in your strategy. But in this post, I'm actually also very interested in the AngleTrend that was with the share. I am unfamiliar with it and have searched on the site and with Google and have not come up with any real information about it. Can you point me towards it's origin so I might be able to learn more about it?

More specifically, in trying to pull it apart, I'm a bit confused by the line:

"def height = avg - avg[length]"

Specifically, what you mean by "avg [length]". I'm familiar with the use of brackets and numbers to refer back to earlier values, but does this mean to take the difference of the current simple average now and the average from 7 days ago (using your default length of 7)?? Sorry if a dumb question, just trying to learn.
 
JoeDV,

I'm exploring the strategy now, thank you for sharing. But as a newbie to thinkscript, I largely have been trying to dismantle the coding to try and learn how it works. A lot to explore in your strategy. But in this post, I'm actually also very interested in the AngleTrend that was with the share. I am unfamiliar with it and have searched on the site and with Google and have not come up with any real information about it. Can you point me towards it's origin so I might be able to learn more about it?

More specifically, in trying to pull it apart, I'm a bit confused by the line:

"def height = avg - avg[length]"

Specifically, what you mean by "avg [length]". I'm familiar with the use of brackets and numbers to refer back to earlier values, but does this mean to take the difference of the current simple average now and the average from 7 days ago (using your default length of 7)?? Sorry if a dumb question, just trying to learn.
https://usethinkscript.com/threads/how-to-find-slope-of-line-in-thinkorswim.8936/#post-82203
 
JoeDV,

I'm exploring the strategy now, thank you for sharing. But as a newbie to thinkscript, I largely have been trying to dismantle the coding to try and learn how it works. A lot to explore in your strategy. But in this post, I'm actually also very interested in the AngleTrend that was with the share. I am unfamiliar with it and have searched on the site and with Google and have not come up with any real information about it. Can you point me towards it's origin so I might be able to learn more about it?

More specifically, in trying to pull it apart, I'm a bit confused by the line:

"def height = avg - avg[length]"

Specifically, what you mean by "avg [length]". I'm familiar with the use of brackets and numbers to refer back to earlier values, but does this mean to take the difference of the current simple average now and the average from 7 days ago (using your default length of 7)?? Sorry if a dumb question, just trying to learn.

Not a dumb question. As @MerryDay posted above, it simply finds the angle of the last "length" bars close using the method in the other post, and then plots the exponential average as a smoothing factor. Add some color and a cloud and there you have it. I'm a trend trader, so it's just another custom indicator I sometimes use as confirmation.
 
JoeDV,

I am trying to figure out how to change the stops to MA21 crosses MA9 or some other type of stop. Just wanting to play around with stop types. Any assistance is appreciated.

def PLBuyStop = if !useStops then 0 else if (state == state.long and (haOpen > haClose) and (haOpen[1] > haClose[1])) then 1 else 0 ; # insert condition to stop in place of the 0<0
def PLSellStop = if !useStops then 0 else if ( state == state.short and (haOpen < haClose))then 1 else 0 ; # insert condition to stop in place of the 0>0
 
Last edited:
JoeDV,

I am trying to figure out how to change the stops to MA21 crosses MA9 or some other type of stop. Just wanting to play around with stop types. Any assistance is appreciated.

def PLBuyStop = if !useStops then 0 else if (state == state.long and (haOpen > haClose) and (haOpen[1] > haClose[1])) then 1 else 0 ; # insert condition to stop in place of the 0<0
def PLSellStop = if !useStops then 0 else if ( state == state.short and (haOpen < haClose))then 1 else 0 ; # insert condition to stop in place of the 0>0

You just put your stop conditions in place of what's there now:

def PLBuyStop = if !useStops then 0 else if ( YOUR BUY STOP CONDITION HERE ) then 1 else 0 ; # insert condition to stop in place of the 0<0
def PLSellStop = if !useStops then 0 else if ( YOUR SELL STOP CONDITION HERE ) then 1 else 0 ; # insert condition to stop in place of the 0>0
 
You just put your stop conditions in place of what's there now:

def PLBuyStop = if !useStops then 0 else if ( YOUR BUY STOP CONDITION HERE ) then 1 else 0 ; # insert condition to stop in place of the 0<0
def PLSellStop = if !useStops then 0 else if ( YOUR SELL STOP CONDITION HERE ) then 1 else 0 ; # insert condition to stop in place of the 0>0
Can you help me code a stop at - 2% of trade and a stop code for - .50 from order price. I am still trying to learn how to code.
 
Awesome strategy! Love the P/L bubbles. I'm trying to use this in NT if anyone has any input on the conversion.
 
Is there a way to get rid of the grayed out boxes ?
The grayed areas are showing a flat market. There is an entire script section for calculating it, but there is one lineto plot it on line 138 (in my script anyway:

addcloud( BoxHigh, BoxLow, color.gray, color.gray);

Simply put a hashtag (#) in front of that line and it will get rid of the gray. If you want to remove all of that script either hashtag the entire section (each line) or delete the whole section. I'd recommend just hashtagging.

Snowthunder
 
Is there any scanner for this?

I use this in combination with ATRTrailingStop. Have to set the values to match whatever you're using obviously.
For a long signal, I look for SignalUp to cross above 0 within 2 bars and ATRTrailingStop to cross below the close within 2 bars.
However, you can adjust or modify to your needs.

Ruby:
#SHAScanner
input period = 20;

def openMA;
def closeMA;
def highMA;
def lowMA;

openMA = compoundValue(1, ExpAverage(open, period), open);
closeMA = compoundValue(1, ExpAverage(close, period), close);
highMA = compoundValue(1, ExpAverage(high, period), high);
lowMA = compoundValue(1, ExpAverage(low, period), low);

def haOpen;
def haClose;

haOpen = CompoundValue(1, ( (haOpen[1] + (openMA[1] + highMA[1] + lowMA[1] + closeMA[1]) /4.0)/2.0), open);
haClose = ((OpenMA + HighMA + LowMA + CloseMA)/4.0) ;

def haLow = min(lowMA, haOpen);
def haHigh = max(highMA,haOpen);

Plot SignalUp = (haOpen < haClose) and (haOpen[1] < haClose[1]);
Plot SignalDn = (haOpen > haClose) and (haOpen[1] > haClose[1]);
 

Join useThinkScript to post your question to a community of 21,000+ developers and traders.

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
201 Online
Create Post

The Market Trading Game Changer

Join 2,500+ subscribers inside the useThinkScript VIP Membership Club
  • Exclusive indicators
  • Proven strategies & setups
  • Private Discord community
  • ‘Buy The Dip’ signal alerts
  • Exclusive members-only content
  • Add-ons and resources
  • 1 full year of unlimited support

Frequently Asked Questions

What is useThinkScript?

useThinkScript is the #1 community of stock market investors using indicators and other tools to power their trading strategies. Traders of all skill levels use our forums to learn about scripting and indicators, help each other, and discover new ways to gain an edge in the markets.

How do I get started?

We get it. Our forum can be intimidating, if not overwhelming. With thousands of topics, tens of thousands of posts, our community has created an incredibly deep knowledge base for stock traders. No one can ever exhaust every resource provided on our site.

If you are new, or just looking for guidance, here are some helpful links to get you started.

What are the benefits of VIP Membership?
VIP members get exclusive access to these proven and tested premium indicators: Buy the Dip, Advanced Market Moves 2.0, Take Profit, and Volatility Trading Range. In addition, VIP members get access to over 50 VIP-only custom indicators, add-ons, and strategies, private VIP-only forums, private Discord channel to discuss trades and strategies in real-time, customer support, trade alerts, and much more. Learn all about VIP membership here.
How can I access the premium indicators?
To access the premium indicators, which are plug and play ready, sign up for VIP membership here.
Back
Top