Heikin_Ashi Indicator For ThinkOrSwim

whew, bonbon -- glad my copy/paste skills haven't gone totally into the toilet. and thanks for continuing to weigh in!
 

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

@RickK thank you for the feedback. I will definitely be looking into that as well. I am one of those traders who exit early and so I did some research on an indicator to add to my trading plan. Research and encountering the work of Vervoort along with scripts written by members contributed to my completion of this indicator. I am thankful that some members took up the work recently and this may be why you couldn't find any info on it. I utilized my research and analytical skills to figure out that the HA, zigzagsign, wave, etc. to decide on the course of action. Trial and error!! :giggle:.

I have learnt a lot from this forum and reached out to @BenTen as I wanted to not only receive feedback but share the completed indicator with members.

@Ghs I would refer to the the time frames to determine if they are also in an upward trend i.e if I am going long they should be green. This gives additional confirmation. Also I use the RSI to look for divergence etc. before taking the trade. Also, as a reminder, a stock pullbacks briefly before continuing in the trend so those minor pullbacks I would look at the RSI or stoch. However, ultimately the green and red HA smooth trends show the current trend with confirmation from the timeframes. If I missed the initial entry those pullbacks could be used to enter the trade.

@BonBon , sorry... I must have been unclear. It was not your collective work that I "couldn't find any info" on. It was the TrendTraderPro indicator that was for sale, yet there were no posts anywhere discussing or reviewing....not on futures.io, elitetrader, or youtube.

Keep up the good work.
 
I noticed that a number of you are using Fibs to complement this strategy. If you've never used Pivot Points (with midpoint lines), you owe it to yourself to give it a looksee. The market (at least in futures) seems to really like these levels (including the midpoints...which most pivot point indicators don't include) as reversal points.

For the image below and the purposes of this post, disregard the heavy orange, light blue and green lines. That is Volume Profile.

Here's the indicator for those who want to check it out. https://tos.mx/abDJzLJ

Here's the code:

Code:
## PivotPointsFull

input marketThreshold = 0.00;
input timeFrame = AggregationPeriod.DAY;
Input MidPlot = yes;
input showOnlyToday = no;
input showBubble = yes;
input ShiftBubble = 5;def n1 = ShiftBubble+1;

def PP2 = high(period = timeFrame)[2] + low(period = timeFrame)[2] + close(period = timeFrame)[2];

plot R4;
plot R3;
plot Mid3;
plot R2;
plot Mid2;
plot R1;
plot Mid1;
plot PP;
plot Mid_1;
plot S1;
plot Mid_2;
plot S2;
plot Mid_3;
plot S3;
plot S4;

if showOnlyToday and !IsNaN(close(period = timeFrame)[-1])
then {
R1 = Double.NaN;
R2 = Double.NaN;
R3 = Double.NaN;
R4 = Double.NaN;
PP = Double.NaN;
S1 = Double.NaN;
S2 = Double.NaN;
S3 = Double.NaN;
S4 = Double.NaN;
Mid1 = Double.NaN;
Mid2 = Double.NaN;
Mid3 = Double.NaN;
Mid_1 = Double.NaN;
Mid_2 = Double.NaN;
Mid_3 = Double.NaN;

} else {

PP = (high(period = timeFrame)[1] + low(period = timeFrame)[1] + close(period = timeFrame)[1]) / 3;
#
R1 = 2 * PP - low(period = timeFrame)[1];
S1 = 2 * PP - high(period = timeFrame)[1];
#
MID1 = PP + ((R1 - PP) /2);
MID_1 = PP - ((PP - S1) /2);
#
R2 = PP + (High(period = timeFrame)[1] - Low(period = timeFrame) [1]);
S2 = PP - (High(period = timeFrame) [1] - Low(period = timeFrame) [1]);
#
MID2 = R1 + ((R2 - R1) /2);
MID_2 = S1 - ((S1 - S2) /2);
#
R3 = R2 + high(period = timeFrame)[1] - low(period = timeFrame)[1];
S3 = S2 - high(period = timeFrame)[1] + low(period = timeFrame)[1];
#
MID3 = R2 + ((R3 - R2) /2);
MID_3 = S2 - ((S2 - S3) /2);
#
R4 = R3 + high(period = timeFrame)[1] - low(period = timeFrame)[1];
S4 = S3 - high(period = timeFrame)[1] + low(period = timeFrame)[1];


}

PP.SetDefaultColor(GetColor(0));
R1.SetDefaultColor(Color.Red);
R2.SetDefaultColor(Color.Red);
R3.SetDefaultColor(Color.Red);
R4.SetDefaultColor(Color.Red);
S1.SetDefaultColor(Color.Yellow);
S2.SetDefaultColor(Color.Yellow);
S3.SetDefaultColor(Color.Yellow);
S4.SetDefaultColor(Color.Yellow);
Mid1.SetDefaultColor(GetColor(3));
Mid2.SetDefaultColor(GetColor(3));
Mid3.SetDefaultColor(GetColor(3));
Mid_1.SetDefaultColor(GetColor(3));
Mid_2.SetDefaultColor(GetColor(3));
Mid_3.SetDefaultColor(GetColor(3));

PP.SetStyle(Curve.firm);
R1.SetStyle(Curve.SHORT_DASH);
R2.SetStyle(Curve.SHORT_DASH);
R3.SetStyle(Curve.SHORT_DASH);
R4.SetStyle(Curve.SHORT_DASH);
S1.SetStyle(Curve.SHORT_DASH);
S2.SetStyle(Curve.SHORT_DASH);
S3.SetStyle(Curve.SHORT_DASH);
S4.SetStyle(Curve.SHORT_DASH);
Mid1.SetStyle(Curve.SHORT_DASH);
Mid2.SetStyle(Curve.SHORT_DASH);
Mid3.SetStyle(Curve.SHORT_DASH);
Mid_1.SetStyle(Curve.SHORT_DASH);
Mid_2.SetStyle(Curve.SHORT_DASH);
Mid_3.SetStyle(Curve.SHORT_DASH);

def paintingStrategy = if timeFrame == AggregationPeriod.WEEK then PaintingStrategy.DASHES else if timeFrame == AggregationPeriod.MONTH then PaintingStrategy.DASHES else PaintingStrategy.LINE_VS_POINTS;

PP.SetPaintingStrategy(PaintingStrategy.DASHES);
R1.SetPaintingStrategy(PaintingStrategy.DASHES);
R2.SetPaintingStrategy(PaintingStrategy.DASHES);
R3.SetPaintingStrategy(PaintingStrategy.DASHES);
R4.SetPaintingStrategy(PaintingStrategy.DASHES);
S1.SetPaintingStrategy(PaintingStrategy.DASHES);
S2.SetPaintingStrategy(PaintingStrategy.DASHES);
S3.SetPaintingStrategy(PaintingStrategy.DASHES);
S4.SetPaintingStrategy(PaintingStrategy.DASHES);
Mid1.SetPaintingStrategy(PaintingStrategy.DASHES);
Mid2.SetPaintingStrategy(PaintingStrategy.DASHES);
Mid3.SetPaintingStrategy(PaintingStrategy.DASHES);
Mid_1.SetPaintingStrategy(PaintingStrategy.DASHES);
Mid_2.SetPaintingStrategy(PaintingStrategy.DASHES);
Mid_3.SetPaintingStrategy(PaintingStrategy.DASHES);

def cond = showBubble and isNaN(close[ShiftBubble]) and !isNaN(close[n1]) ;
AddChartBubble(cond,PP,Concat("PP: ",Round(PP)),color.magenta);
AddChartBubble(cond,R1,Concat("R1: ",Round(R1)),color.magenta);
AddChartBubble(cond,R2,Concat("R2: ",Round(R2)),color.magenta);
AddChartBubble(cond,R3,Concat("R3: ",Round(R3)),color.magenta);
AddChartBubble(cond,R4,Concat("R4: ",Round(R4)),color.magenta);
AddChartBubble(cond,S1,Concat("S1: ",Round(S1)),color.magenta);
AddChartBubble(cond,S2,Concat("S2: ",Round(S2)),color.magenta);
AddChartBubble(cond,S3,Concat("S3: ",Round(S3)),color.magenta);
AddChartBubble(cond,S4,Concat("S4: ",Round(S4)),color.magenta);
AddChartBubble(cond,Mid1,Concat("Mid1: ",Round(Mid1)),color.magenta);
AddChartBubble(cond,Mid2,Concat("Mid2: ",Round(Mid2)),color.magenta);
AddChartBubble(cond,Mid3,Concat("Mid3: ",Round(Mid3)),color.magenta);
AddChartBubble(cond,Mid_1,Concat("Mid_1: ",Round(Mid_1)),color.magenta);
AddChartBubble(cond,Mid_2,Concat("Mid_2: ",Round(Mid_2)),color.magenta);
AddChartBubble(cond,Mid_3,Concat("Mid_3: ",Round(Mid_3)),color.magenta);

Mid1.SetHiding (!MidPlot);
Mid2.SetHiding (!MidPlot);
Mid3.SetHiding (!MidPlot);
Mid_1.SetHiding (!MidPlot);
Mid_2.SetHiding (!MidPlot);
Mid_3.SetHiding (!MidPlot);

##### end

Below is an example image.

xJoKfb7.png
 
Last edited:
@Ghs, here is a script for a backtest.
Code:
# Backtest of BonBon Heiken_Ashi
# BonBon 1/24/2021

input period = 50;

def openMA = CompoundValue(1, Average(open, period), open);
def closeMA = CompoundValue(1, Average(close, period), close);
def highMA = CompoundValue(1, Average(high, period), high);
def lowMA = CompoundValue(1, Average(low, period), low);


def  haOpen = CompoundValue(1, ( (haOpen[1] + (openMA[1] + highMA[1] + lowMA[1] + closeMA[1]) / 4.0 ) / 2.0), open);
def haClose = ((((openMA + highMA + lowMA + closeMA) / 4.0) + haOpen + Max(highMA, haOpen) + Min(lowMA, haOpen)) / 4.0);

def haLow =  Min(lowMA, haOpen);
def haHigh = Max(highMA, haOpen);

def trend = haClose >= haOpen ;
def trendup = trend and !trend[1];

def trendd = haClose < haOpen ;
def trendDown = trendd and !trendd[1];

AddOrder(OrderType.BUY_TO_OPEN, trendup, halow, 100, color.light_green, color.light_green,"BUY");

AddOrder(OrderType.SELL_TO_CLOSE,trenddown, hahigh, 100,color.red, color.red,"SELL");
Is there a way to close out the order at market close and open an order at market open depending if it's currently on a buy or sell? I'm backtesting this with futures but I don't want to hold the positions overnight.
 
Is there a way to close out the order at market close and open an order at market open depending if it's currently on a buy or sell? I'm backtesting this with futures but I don't want to hold the positions overnight.

You do know there is no auto-trading capability in TOS, right...??? The best you can do is throw an Alert() or use AddLabel() in live trading... However, if you know your trade criteria you could also create a MOC Conditional Order based on said criteria... You would want this to trigger several minutes before market close just to be safe... Explaining the logistics of all of that might be beyond the scope of these forums... We wouldn't want you to feel that we were responsible for your financial losses if and/or when it happened... In order to backtest you would need a time constraint built into your backtesting strategy to trigger AddOrder() to Close...
 
@BonBon , sorry... I must have been unclear. It was not your collective work that I "couldn't find any info" on. It was the TrendTraderPro indicator that was for sale, yet there were no posts anywhere discussing or reviewing....not on futures.io, elitetrader, or youtube.

Keep up the good work.
@RickK During my research I saw a number of indicators for sale. The trendtraderpro was one of them. The Heiken Ashi mirrors the indicator although I did not realize it until I began my research. I have included some of the websites that I visited as a reference for everyone. Reading these articles added to my knowledge base. I hope it can do the same for others.

I tried the pivot points awhile ago and found I preferred the Fibonacci. However, I always keep indicators and strategies in my playbook just in case.

http://traders.com/Documentation/FEEDbk_docs/2008/05/TradersTips/TradersTips.html#metastock
https://docplayer.net/24587491-Trading-with-the-heikin-ashi-candlestick-oscillator.html
https://www.tradingview.com/script/UyhY8FuQ-Vervoort-Heiken-Ashi-Candlestick-Oscillator/
https://www.tradingview.com/script/3yjUzix2-HEIKIN-ASHI-COLOUR-CHANGE-ALERT/
http://traders.com/Documentation/FEEDbk_docs/2013/06/Vervoort.html
https://blog.traderslibrary.com/traders-library/2013/05/irsts-the-1-2-3-wave-count.html
http://stocata.org/sts8/123wavesintro.html
https://lessonsfinancial.com/2020/0...rrtQ--_R6k6OaYgH7mSRbSCkBgD0RnGhoCizcQAvD_BwE
https://bullwaves.org/elliott-wave-trading-signals/
https://tradingwithtrend.com/heiken-ashi-strategy
https://tradingstrategyguides.com/heiken-ashi-strategy/
https://blog.earn2trade.com/heiken-ashi/
https://kodify.net/tradingview/indicators/tema/
http://traders.com/Documentation/FEEDbk_docs/2012/07/TradersTips.html#item2
https://www.mesasoftware.com/papers/ZeroLag.pdf
https://www.quantshare.com/item-1607-zero-lag-exponential-moving-average
https://alanhull.com/hull-moving-average
https://www.tradingview.com/script/aDYKgCBv-Heiken-Ashi-zero-lag-EMA-v1-1-by-JustUncleL/
http://traders.com/Documentation/FEEDbk_docs/2013/09/TradersTips.html#item4
https://www.scribd.com/document/260...Fibonacci-Trading-System-The-Avinash-Khilnani
http://stocata.org/metastock/heikin_ashi.html
https://www.streetdirectory.com/etoday/the-heikin-ashi-candle-chart-wfcpff.html
 
You do know there is no auto-trading capability in TOS, right...??? The best you can do is throw an Alert() or use AddLabel() in live trading... However, if you know your trade criteria you could also create a MOC Conditional Order based on said criteria... You would want this to trigger several minutes before market close just to be safe... Explaining the logistics of all of that might be beyond the scope of these forums... We wouldn't want you to feel that we were responsible for your financial losses if and/or when it happened... In order to backtest you would need a time constraint built into your backtesting strategy to trigger AddOrder() to Close...
Yeah I'm not looking to auto-trade. I'm backtesting a strategy for futures and I want to see if it still works for rth.
Fortunately I've been trading long enough that I don't blame others for my losses 😉
 
@Branch Yes it was fixed. It was a a simply error.... quotes missing for the "15 mins" and an extra quote after daily. The original was edited with the changes.
Have a look at the threads referring to the script. It was fixed and the changes were added. I notice you did not keep the hashtag in front of the input for the longtermperiod. A hashtag needs to be there.
 
@floydddd and others my apologies. I realized I made some changes to the code to accommodate the HA smooth and did not incorporate it in this script. I will edit the thread and it should be ready to be copied in a few minutes.

@rad14733 that is ok. The reason we are here is to learn from each other.

hi, bonbon: firstly, thanks for that great resource list. will be digging into it soon. next: i still get errors appending the code in post 64 to the code in your first post. is it just me or ....? thanks!
 
So I've been using this for the past couple of days and I like it very much. But then, I realized that my brain was becoming a bit scrambled looking at the two sets of candles. So, luckily @BonBon included a midline (denoted 'o'). I intended on simply replacing the smoothed heiken ashi candles with the midline. I made a mod to the line by getting it to act like the smoothed heiken ashi candles (changing colors when reversing) and setting the weight to (5). Then I commented out the candles. I have set the default period of the indicator to 34 (a fibonacci number that I saw recommended once for moving averages) to enhance lower time frame charts. I also added a 34 period TEMA to watch crossovers of the two lines as a bit of a confirmation.

For anyone who is interested in using this, the modified code is below the screenshot. Note: in order to see the HA midline, you need to checkmark "Show Plot" inside the indicator's settings.

NFFXk2j.png


Code:
#Heiken_Ashi based on Sylvan Verboort's Trading with HA Candlestick Oscillator
#Bon Bon _last update Jan 17th 2021
#Influenced by script from HoboTheClown / blt,http://www.thinkscripter.com, TD Hacolt etc.
#MTF based on HannTech's MTF_MACD script
#update 1/2/21 - changed the default moving average to TEMA.  Changed the period to 35.
#update changed reversal arrows to reversal bubbles with price

# mods by @RickK 1/28/21: changed default period to 34 (fibonacci number),
# commented out AddChart statements so the candles would not display,
# added colors and weight to the 'o' (line) variable. (You must checkmark
# "Show Plot" in the settings for this to display. )  Purpose: having 2 sets
# of candles displaying on my charts was scrambling my brain.

### YOU MUST HAVE THE STYLE SETTING FIT STUDIES ENABLED ###
#hint: The style setting Fit Studies must be enabled to use these bars.


input period = 34; #orig 50
input hideCandles = yes;
input candleSmoothing = {default Valcu, Vervoort};
input show_bubble_labels = yes;
input bubbles = yes;
input arrows = yes;


#input smoothingLength = 3;

input movingAverageType = {default  TEMA, Exponential, Hull };

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

switch (movingAverageType) {
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 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 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) + haOpen + Max(highMA, haOpen) + Min(lowMA, haOpen)) / 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;
o.AssignValueColor(if o > o[-1] then Color.magenta else if o < o[-1] then Color.cyan else color.gray);
o.SetLineWeight (5);
o.Hide();

def haLow =  Min(lowMA, haOpen);
def haHigh = Max(highMA, haOpen);

#Zero Lag System - MetaStock Crossover Formula
#zero-lagging principle
#Zero-lagging TEMA average on closing prices

#Medium-term price reversals - upward trend

def avg = 34;
def TMA1 = reference TEMA(haClose, avg); # triple exponential moving average (TEMA) of 34 bars
def TMA2 =  reference TEMA(TMA1, avg);
def Diff = TMA1 - TMA2;
def ZlHa = TMA1 + Diff; #Zero-lagging TEMA average on closing prices - medium term uptrend;

#Medium-term price reversals - downward trend
def TMA1_ = reference TEMA((high + low) / 2, avg);
def Diff2 = TMA1_ - TMA2;
def ZlCl = TMA1_ + Diff2; #Zero-lagging TEMA average on closing prices - medium term doenwardtrend;

def ZlDif = ZlCl - ZlHa; # Zero-Lag close - Zero-Lag HA(green candle) Uptrend when ZlDif is equal to or greater than zero

#uptrend {green candle}
def keep1 = if (haClose >= haOpen and haClose[1] >= haOpen[1]) then 1 else 0;
def keep2 = if ZlDif >= 0 then 1 else 0;
def keep3 = if (AbsValue(close - open) < ((high - low) * 0.35)) and high >= low[1] then 1 else 0;
def keeping = if (keep1 or keep2) then 1 else 0;
def keepall = if keeping or (keeping[1]) and close >= open or close >= (close[1]) then 1 else 0;

def utr = if keepall or (keepall[1]) and keep3 then 1 else 0;

#downtrend red candle

def keep1_ = if (haClose < haOpen and haClose[1] < haOpen[1]) then 1 else 0;
def keep2_ = if ZlDif < 0 then 1 else 0;
def keep3_ = if (AbsValue(close - open) < ((high - low) * 0.35)) and low <= high[1] then 1 else 0;
def keeping_ = if (keep1_ or keep2_) then 1 else 0;
def dkeepall_ = if keeping_ or (keeping_[1]) and close < open or close < (close[1]) then 1 else 0;

def dtr = if dkeepall_ or (dkeepall_[1] - 1) and keep3_ == 1 then 1 else 0;  #downtrend
def upw = if dtr and (dtr[1]) and utr then 1 else 0;
def dnw = if !utr and (utr[1] ) and dtr then 1 else 0;

def results = if upw then 1 else if dnw then 0 else results[1];

#Change the color of HA and Japanese Candles - turn off to show only HA on chart
#AssignPriceColor(if haClose >= haOpen
             #   then Color.cyan else
            #   if  haClose < haOpen
             #   then Color.magenta else Color.WHITE);


#Heiken_A script

#####################################################################################################
input charttype = ChartType.CANDLE;

def haopen_ = if haClose <= haOpen
              then haOpen + 0
             else Double.NaN;

def HAhi   = if haClose <= haOpen
              then haHigh
              else Double.NaN;

def HAlo =   if haClose <= haOpen
              then haLow
              else Double.NaN;


#AddChart(growColor = Color.red, neutralColor = Color.CURRENT, high = HAhi, low = HAlo, open = haopen_, close = haClose, type = ChartType.CANDLE);

def HAclose1 = ohlc4;
def HAopen1  = if haClose >= haOpen
               then CompoundValue(1, (haOpen[1] + haClose[1]) / 2, (open[1] + close[1]) / 2) 
               else Double.NaN;

def haopen_1 = if haOpen <= haClose
               then HAopen1 + 0  else Double.NaN;

def HAhigh1  = haHigh;
def HAlow1   = haLow;


#AddChart(growColor = Color.green, neutralColor = Color.CURRENT,  high = HAhigh1, low = HAlow1, open = haopen_1, close = haClose, type = ChartType.CANDLE);

#####################################################################################################
#Buy and sell signals
def trend =  haClose >= haOpen; #high color;
def trendup = trend and !trend[1];
def trendd =  haClose < haOpen;
def trendDown = trendd and !trendd[1];

AddChartBubble(bubbles and trendup and trendup, HAlow1, ( Round(HAlow1, 2)), Color.GREEN, no);

AddChartBubble(bubbles and trendDown and trendDown, HAhigh1, ( Round(HAhigh1, 2)), Color.LIGHT_RED, yes);

### end
########################################################
 
Last edited:
So I've been using this for the past couple of days and I like it very much. But then, I realized that my brain was becoming a bit scrambled looking at the two sets of candles. So, luckily @BonBon included a midline (denoted 'o'). I intended on simply replacing the smoothed heiken ashi candles with the midline. I made a mod to the line by getting it to act like the smoothed heiken ashi candles (changing colors when reversing) and setting the weight to (5). Then I commented out the candles. I have set the default period of the indicator to 34 (a fibonacci number that I saw recommended once for moving averages) to enhance lower time frame charts. I also added a 34 period TEMA to watch crossovers of the two lines as a bit of a confirmation.

For anyone who is interested in using this, the modified code is below the screenshot. Note: in order to see the HA midline, you need to checkmark "Show Plot" inside the indicator's settings.

NFFXk2j.png


Code:
#Heiken_Ashi based on Sylvan Verboort's Trading with HA Candlestick Oscillator
#Bon Bon _last update Jan 17th 2021
#Influenced by script from HoboTheClown / blt,http://www.thinkscripter.com, TD Hacolt etc.
#MTF based on HannTech's MTF_MACD script
#update 1/2/21 - changed the default moving average to TEMA.  Changed the period to 35.
#update changed reversal arrows to reversal bubbles with price

# mods by @RickK 1/28/21: changed default period to 34 (fibonacci number),
# commented out AddChart statements so the candles would not display,
# added colors and weight to the 'o' (line) variable. (You must checkmark
# "Show Plot" in the settings for this to display. )  Purpose: having 2 sets
# of candles displaying on my charts was scrambling my brain.

### YOU MUST HAVE THE STYLE SETTING FIT STUDIES ENABLED ###
#hint: The style setting Fit Studies must be enabled to use these bars.


input period = 34; #orig 50
input hideCandles = yes;
input candleSmoothing = {default Valcu, Vervoort};
input show_bubble_labels = yes;
input bubbles = yes;
input arrows = yes;


#input smoothingLength = 3;

input movingAverageType = {default  TEMA, Exponential, Hull };

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

switch (movingAverageType) {
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 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 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) + haOpen + Max(highMA, haOpen) + Min(lowMA, haOpen)) / 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;
o.AssignValueColor(if o > o[-1] then Color.magenta else if o < o[-1] then Color.cyan else color.gray);
o.SetLineWeight (5);
o.Hide();

def haLow =  Min(lowMA, haOpen);
def haHigh = Max(highMA, haOpen);

#Zero Lag System - MetaStock Crossover Formula
#zero-lagging principle
#Zero-lagging TEMA average on closing prices

#Medium-term price reversals - upward trend

def avg = 34;
def TMA1 = reference TEMA(haClose, avg); # triple exponential moving average (TEMA) of 34 bars
def TMA2 =  reference TEMA(TMA1, avg);
def Diff = TMA1 - TMA2;
def ZlHa = TMA1 + Diff; #Zero-lagging TEMA average on closing prices - medium term uptrend;

#Medium-term price reversals - downward trend
def TMA1_ = reference TEMA((high + low) / 2, avg);
def Diff2 = TMA1_ - TMA2;
def ZlCl = TMA1_ + Diff2; #Zero-lagging TEMA average on closing prices - medium term doenwardtrend;

def ZlDif = ZlCl - ZlHa; # Zero-Lag close - Zero-Lag HA(green candle) Uptrend when ZlDif is equal to or greater than zero

#uptrend {green candle}
def keep1 = if (haClose >= haOpen and haClose[1] >= haOpen[1]) then 1 else 0;
def keep2 = if ZlDif >= 0 then 1 else 0;
def keep3 = if (AbsValue(close - open) < ((high - low) * 0.35)) and high >= low[1] then 1 else 0;
def keeping = if (keep1 or keep2) then 1 else 0;
def keepall = if keeping or (keeping[1]) and close >= open or close >= (close[1]) then 1 else 0;

def utr = if keepall or (keepall[1]) and keep3 then 1 else 0;

#downtrend red candle

def keep1_ = if (haClose < haOpen and haClose[1] < haOpen[1]) then 1 else 0;
def keep2_ = if ZlDif < 0 then 1 else 0;
def keep3_ = if (AbsValue(close - open) < ((high - low) * 0.35)) and low <= high[1] then 1 else 0;
def keeping_ = if (keep1_ or keep2_) then 1 else 0;
def dkeepall_ = if keeping_ or (keeping_[1]) and close < open or close < (close[1]) then 1 else 0;

def dtr = if dkeepall_ or (dkeepall_[1] - 1) and keep3_ == 1 then 1 else 0;  #downtrend
def upw = if dtr and (dtr[1]) and utr then 1 else 0;
def dnw = if !utr and (utr[1] ) and dtr then 1 else 0;

def results = if upw then 1 else if dnw then 0 else results[1];

#Change the color of HA and Japanese Candles - turn off to show only HA on chart
#AssignPriceColor(if haClose >= haOpen
             #   then Color.cyan else
            #   if  haClose < haOpen
             #   then Color.magenta else Color.WHITE);


#Heiken_A script

#####################################################################################################
input charttype = ChartType.CANDLE;

def haopen_ = if haClose <= haOpen
              then haOpen + 0
             else Double.NaN;

def HAhi   = if haClose <= haOpen
              then haHigh
              else Double.NaN;

def HAlo =   if haClose <= haOpen
              then haLow
              else Double.NaN;


#AddChart(growColor = Color.red, neutralColor = Color.CURRENT, high = HAhi, low = HAlo, open = haopen_, close = haClose, type = ChartType.CANDLE);

def HAclose1 = ohlc4;
def HAopen1  = if haClose >= haOpen
               then CompoundValue(1, (haOpen[1] + haClose[1]) / 2, (open[1] + close[1]) / 2)
               else Double.NaN;

def haopen_1 = if haOpen <= haClose
               then HAopen1 + 0  else Double.NaN;

def HAhigh1  = haHigh;
def HAlow1   = haLow;


#AddChart(growColor = Color.green, neutralColor = Color.CURRENT,  high = HAhigh1, low = HAlow1, open = haopen_1, close = haClose, type = ChartType.CANDLE);

#####################################################################################################
#Buy and sell signals
def trend =  haClose >= haOpen; #high color;
def trendup = trend and !trend[1];
def trendd =  haClose < haOpen;
def trendDown = trendd and !trendd[1];

AddChartBubble(bubbles and trendup and trendup, HAlow1, ( Round(HAlow1, 2)), Color.GREEN, no);

AddChartBubble(bubbles and trendDown and trendDown, HAhigh1, ( Round(HAhigh1, 2)), Color.LIGHT_RED, yes);

### end
########################################################
@RickK . Nice!!!! I did the same thing with the "o" plot and the VWMA and the TEMA and the VWMA. I have been observing and monitoring them during trading and making some adjustments and tweaks to my entry and exits. Great job and thanks for contributing. I am still trying to eliminate the numerous minor signals (those that occur after 1-5 or 6 candles) during the trend. My goal is to have signals at the beginning of the up and downtrend , and the wave 2 correction and then continuation only.

Hopefully someone here can figure it out.
 
Rickk - I've loaded the study, and get the pink and blue HA smoothed midline to show, BUT my candles have gone away...? You indicated you "commented out" the candles. Exactly how do I do that as I would like to have my price candles showing. Thanks.
 
Rickk - I've loaded the study, and get the pink and blue HA smoothed midline to show, BUT my candles have gone away...? You indicated you "commented out" the candles. Exactly how do I do that as I would like to have my price candles showing. Thanks.

@JoeSD to comment out something is to edit the script. When you place a # sign before a command, the script will not recognize the command. I placed a # sign before the two instances of "AddChart" in the script. To see the candles again, take the # signs away.

@floydddd , the "hide candles" selector has to do with the secondary candles, not the indicator's HA candles.
 
@RickK . Nice!!!! I did the same thing with the "o" plot and the VWMA and the TEMA and the VWMA. I have been observing and monitoring them during trading and making some adjustments and tweaks to my entry and exits. Great job and thanks for contributing. I am still trying to eliminate the numerous minor signals (those that occur after 1-5 or 6 candles) during the trend. My goal is to have signals at the beginning of the up and downtrend , and the wave 2 correction and then continuation only.

Hopefully someone here can figure it out.

@BonBon , at the risk of seriously convoluting screen real estate, here's a thought on looking for signals. Some time ago I followed a thread here on Distribution Testing. Member @mcdon030 has shared a quite fascinating bit of work there. The thread can get a bit confusing in terms of what the latest version is, so go to https://usethinkscript.com/threads/distribution-testing.1444/ and look for post #30 (a RTH version) and post #37 (an aftermarket version) and also special edits in post #48. At the least, it may give you some ideas for qualifying your entries and exits.

Here's how I've applied it.

 
Last edited:
Thanks once to everyone who contributed to the discussion. Finally, I have been successful in eliminating the numerous signals keeping the lowest and the highest reversal signals. I am cleaning up the script and will post the final results.
 

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
278 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