Repaints Heikin/Kaufman For ThinkOrSwim

Repaints
Hi,
Anyone able to help converting this into TOS? Thanks in advance!

https://www.tradingview.com/v/ri7P5Cvh/



study("Heikin/Kaufman Strategy ",shorttitle="KAMA",overlay=true)
res1 = input(title="Heikin Ashi EMA Time Frame", type=resolution, defval="D")
test = input(0,"Heikin Ashi EMA Shift")
sloma = input(20,"Slow EMA Period")
//Kaufman MAK
Length = input(5, minval=1)
xPrice = input(hlc3)
xvnoise = abs(xPrice - xPrice[1])
Fastend = input(2.5,step=.5)
Slowend = input(20)
nfastend = 2/(Fastend + 1)
nslowend = 2/(Slowend + 1)
nsignal = abs(xPrice - xPrice[Length])
nnoise = sum(xvnoise, Length)
nefratio = iff(nnoise != 0, nsignal / nnoise, 0)
nsmooth = pow(nefratio * (nfastend - nslowend) + nslowend, 2)
nAMA = nz(nAMA[1]) + nsmooth * (xPrice - nz(nAMA[1]))
//Heikin Ashi Open/Close Price
ha_t = heikinashi(tickerid)
ha_close = security(ha_t, period, nAMA)
mha_close = security(ha_t, res1, hlc3)
//Moving Average
fma = ema(mha_close[test],1)
sma = ema(ha_close,sloma)
plot(fma,title="MA",color=black,linewidth=2,style=line)
plot(sma,title="SMA",color=red,linewidth=2,style=line)
plotshape(crossover(fma,sma), style=shape.triangleup, location=location.belowbar, size=size.small, color=green, text="B")
plotshape(crossunder(fma,sma), style=shape.triangledown, location=location.abovebar, size=size.small, color=red, text="S")
find below
CSS:
#https://www.tradingview.com/v/ri7P5Cvh/
#@TradeAlchemy
#study("Heikin/Kaufman Strategy ",shorttitle="KAMA",overlay=true)
# Converted by Sam4Cok@Samer800    - 05/2023
input HeikinAshiTimeframe = AggregationPeriod.DAY;    # "Heikin Ashi EMA Time Frame"
input HeikinAshiShift = 0;                            # "Heikin Ashi EMA Shift"
input slowEmaPeriod = 20;#,"Slow EMA Period")
input Length = 5;
input Source = hlc3;
input kamaFastEnd = 2.5;
input kamaSlowEnd = 20;

def na = Double.NaN;
def last = isNaN(close);
def h    = high(Period=HeikinAshiTimeframe);
def l    = low(Period=HeikinAshiTimeframe);
def o    = open(Period=HeikinAshiTimeframe);
def c    = close(Period=HeikinAshiTimeframe);
#-----Color
DefineGlobalColor("fuchsia", CreateColor(255, 0, 255));
DefineGlobalColor("purple" , CreateColor(128, 0, 128));
DefineGlobalColor("blue"   , CreateColor(0, 0, 255));
DefineGlobalColor("black"  , Color.DARK_GRAY);#CreateColor(238,242,17));
DefineGlobalColor("lime"   , CreateColor(0, 255, 0));
DefineGlobalColor("green"  , CreateColor(0, 128, 0));
DefineGlobalColor("yellow" , CreateColor(255, 255, 0));
DefineGlobalColor("orange" , CreateColor(255, 127, 0));

#//KAMA
#kama(a, b)=>
script kama {
    input price = hlc3;
    input length = 5;
    input fast = 2.5;
    input slow = 20;
        def xvnoise = AbsValue(Price - Price[1]);
        def nfastend = 2/(Fast + 1);
        def nslowend = 2/(Slow + 1);
        def nsignal = AbsValue(Price - Price[Length]);
        def nnoise = sum(xvnoise, Length);
        def nefratio = if(nnoise != 0, nsignal / nnoise, 0);
        def nsmooth = power(nefratio * (nfastend - nslowend) + nslowend, 2);
        def nAMA = CompoundValue(1, nAMA[1] + nsmooth * (Price - nAMA[1]), Price);
    plot out = nAMA;
}

def haClose = (o + h + l + c) / 4;
def haOpen = if isNaN(haopen[1]) then (o + c) / 2 else (haopen[1] + haclose[1]) / 2;
def haHigh = Max(h,max(haOpen, haClose));
def haLow  = Min(l,min(haOpen, haClose));

def hahlc3 = (haHigh+haLow+haClose) / 3;

def ha_close = kama(Source, Length, kamaFastEnd, kamaSlowEnd);
def mha_close = hahlc3;

#//Moving Average
def fma = ExpAverage(mha_close,1);
def sma = ExpAverage(ha_close,slowEmaPeriod);
def col = sma>sma[1];

plot smaPlot = sma;        # "SMA"
plot fmaPlot = if last then na else fma[HeikinAshiShift];        # "MA"

smaPlot.AssignValueColor(if col then Color.GREEN else Color.RED);
fmaPlot.SetDefaultColor(Color.GRAY);

AddChartBubble((fmaPlot crosses above smaPlot),  low, "B", Color.GREEN, no);
AddChartBubble((fmaPlot crosses below smaPlot), high, "S", Color.RED , yes);



#-- END of CODE
 
find below
CSS:
#https://www.tradingview.com/v/ri7P5Cvh/
#@TradeAlchemy
#study("Heikin/Kaufman Strategy ",shorttitle="KAMA",overlay=true)
# Converted by Sam4Cok@Samer800    - 05/2023
input HeikinAshiTimeframe = AggregationPeriod.DAY;    # "Heikin Ashi EMA Time Frame"
input HeikinAshiShift = 0;                            # "Heikin Ashi EMA Shift"
input slowEmaPeriod = 20;#,"Slow EMA Period")
input Length = 5;
input Source = hlc3;
input kamaFastEnd = 2.5;
input kamaSlowEnd = 20;

def na = Double.NaN;
def last = isNaN(close);
def h    = high(Period=HeikinAshiTimeframe);
def l    = low(Period=HeikinAshiTimeframe);
def o    = open(Period=HeikinAshiTimeframe);
def c    = close(Period=HeikinAshiTimeframe);
#-----Color
DefineGlobalColor("fuchsia", CreateColor(255, 0, 255));
DefineGlobalColor("purple" , CreateColor(128, 0, 128));
DefineGlobalColor("blue"   , CreateColor(0, 0, 255));
DefineGlobalColor("black"  , Color.DARK_GRAY);#CreateColor(238,242,17));
DefineGlobalColor("lime"   , CreateColor(0, 255, 0));
DefineGlobalColor("green"  , CreateColor(0, 128, 0));
DefineGlobalColor("yellow" , CreateColor(255, 255, 0));
DefineGlobalColor("orange" , CreateColor(255, 127, 0));

#//KAMA
#kama(a, b)=>
script kama {
    input price = hlc3;
    input length = 5;
    input fast = 2.5;
    input slow = 20;
        def xvnoise = AbsValue(Price - Price[1]);
        def nfastend = 2/(Fast + 1);
        def nslowend = 2/(Slow + 1);
        def nsignal = AbsValue(Price - Price[Length]);
        def nnoise = sum(xvnoise, Length);
        def nefratio = if(nnoise != 0, nsignal / nnoise, 0);
        def nsmooth = power(nefratio * (nfastend - nslowend) + nslowend, 2);
        def nAMA = CompoundValue(1, nAMA[1] + nsmooth * (Price - nAMA[1]), Price);
    plot out = nAMA;
}

def haClose = (o + h + l + c) / 4;
def haOpen = if isNaN(haopen[1]) then (o + c) / 2 else (haopen[1] + haclose[1]) / 2;
def haHigh = Max(h,max(haOpen, haClose));
def haLow  = Min(l,min(haOpen, haClose));

def hahlc3 = (haHigh+haLow+haClose) / 3;

def ha_close = kama(Source, Length, kamaFastEnd, kamaSlowEnd);
def mha_close = hahlc3;

#//Moving Average
def fma = ExpAverage(mha_close,1);
def sma = ExpAverage(ha_close,slowEmaPeriod);
def col = sma>sma[1];

plot smaPlot = sma;        # "SMA"
plot fmaPlot = if last then na else fma[HeikinAshiShift];        # "MA"

smaPlot.AssignValueColor(if col then Color.GREEN else Color.RED);
fmaPlot.SetDefaultColor(Color.GRAY);

AddChartBubble((fmaPlot crosses above smaPlot),  low, "B", Color.GREEN, no);
AddChartBubble((fmaPlot crosses below smaPlot), high, "S", Color.RED , yes);



#-- END of CODE
@samer800 great job like always, a quick question or request. Do you think it is possible to add the scan for a swing trade? thank you in advance. other request sorry to bother you, i really like paint a candle Green with a Buy signal, and Red for a short signal.

@samer800 great job like always, a quick question or request. Do you think it is possible to add the scan for a swing trade? thank you in advance. other request sorry to bother you, i really like paint a candle Green with a Buy signal, and Red for a short signal.
hi @samer800 i hope you can help me with this request. thank you in advance
 
Last edited by a moderator:
@samer800 great job like always, a quick question or request. Do you think it is possible to add the scan for a swing trade? thank you in advance. other request sorry to bother you, i really like paint a candle Green with a Buy signal, and Red for a short signal.


hi @samer800 i hope you can help me with this request. thank you in advance
add this for Candle Color
CSS:
def long = (fmaPlot > smaPlot) and (fmaPlot[1] <= smaPlot[1]);
def short = (fmaPlot < smaPlot) and (fmaPlot[1] >= smaPlot[1]);

AssignPriceColor(if long then Color.CYAN else if short then Color.MAGENTA else Color.CURRENT);
 
add this for Candle Color
CSS:
def long = (fmaPlot > smaPlot) and (fmaPlot[1] <= smaPlot[1]);
def short = (fmaPlot < smaPlot) and (fmaPlot[1] >= smaPlot[1]);

AssignPriceColor(if long then Color.CYAN else if short then Color.MAGENTA else Color.CURRENT);
@samer800 thank you some much for all your help. really appreciate your work. please one last thing dont forget the scan please. Thank you in advance.
 
This indicator appears to be repainting. It's better or worse depending on the settings; For example if heikin is negative or if the heikin time frame setting and the charts time frame don't match. Can this be fixed?
 
This indicator appears to be repainting. It's better or worse depending on the settings; For example if heikin is negative or if the heikin time frame setting and the charts time frame don't match. Can this be fixed?

This is an Multi-Time-Frame indicator.
MTF indicators WILL repaint because they reference the current higher timeframe candle which will paint signals and erase signals on every tick until that higher timeframe bar closes.
At which point, it will go back and do a final repaint of all the candles with the higher aggregation price that you requested.

This means if you are using a 4hour aggregation calculation in your buy signal on a 15min chart;
it with continually repaint 16 bars until the 4hour candle closes and does its final repaint.

You are correct. If the heikin time frame setting is set to the chart setting, it eliminates the MTF and the repainting.
 

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

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
559 Online
Create Post

Similar threads

Similar threads

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