natural moving average code help

Status
Not open for further replies.

MLlalala

Member
I am trying to translate the following code to thinkscript, i think it is a loop function but i dun really know how to do it, could anyone shed some light? thanks!

ln = log(CLOSE) * 1000
for i = 0 to length - 1
oi = abs(nz(ln) - nz(ln[i + 1]))
num += oi * (sqrt(i + 1) - sqrt(i))
denom += oi

ratio = denom != 0 ? num / denom : 0

nma: = (close* ratio) + (nz(nma[1],close) * (1 - ratio))
 
Solution
@MLlalala
thanks for posting the question, it was a good challenge.
unfortunately,
your post is a good example of why i don't try to convert from code pasted here by a user.
it is incomplete and has typos.
and you didn't provide a source link, which allows others to see the original and read about it.


@jpmcewen
thanks for finding the link and code.
i went to the tv site and copied that code and tried to convert it.
i think the below code is the desired conversion.

-------------------

Find the full ToS conversion of the above Tradingview code:
https://usethinkscript.com/threads/natural-moving-average-for-thinkorswim.13801/
Is this related to Ocean Theory? Can you share a link to where you found this? Your formula is missing some information for example what is nz()? Also I'm not sure you can run a loop starting at 0 and continuing to -1.

Found a potential source (TradingView)

Code:
//@version=4
// Copyright (c) 2019-present, Franklin Moormann (cheatcountry)
// Natural Moving Average [CC] script may be freely distributed under the MIT license.
study("Natural Moving Average [CC]", overlay=true)
inp = input(title="Source", type=input.source, defval=close)
res = input(title="Resolution", type=input.resolution, defval="")
rep = input(title="Allow Repainting?", type=input.bool, defval=false)
bar = input(title="Allow Bar Color Change?", type=input.bool, defval=true)
src = security(syminfo.tickerid, res, inp[rep ? 0 : barstate.isrealtime ? 1 : 0])[rep ? 0 : barstate.isrealtime ? 0 : 1]
length = input(title="Length", type=input.integer, defval=40, minval=1)
ln = log(src) * 1000
num = 0.0, denom = 0.0
for i = 0 to length - 1
    oi = abs(nz(ln[i]) - nz(ln[i + 1]))
    num += oi * (sqrt(i + 1) - sqrt(i))
    denom += oi
    
ratio = denom != 0 ? num / denom : 0
nma = (src * ratio) + (nz(src[1]) * (1 - ratio))
slo = src - nma
sig = slo > 0 ? slo > nz(slo[1]) ? 2 : 1 : slo < 0 ? slo < nz(slo[1]) ? -2 : -1 : 0
alertcondition(crossover(sig, 1), "Strong Buy Signal", "Strong Bullish Change Detected")
alertcondition(crossunder(sig, -1), "Strong Sell Signal", "Strong Bearish Change Detected")
alertcondition(crossover(sig, 0), "Buy Signal", "Bullish Change Detected")
alertcondition(crossunder(sig, 0), "Sell Signal", "Bearish Change Detected")
nmaColor = sig > 1 ? color.green : sig > 0 ? color.lime : sig < -1 ? color.maroon : sig < 0 ? color.red : color.black
barcolor(bar ? nmaColor : na)
plot(nma, color=nmaColor, linewidth=2)
 
Last edited:

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

@MLlalala
thanks for posting the question, it was a good challenge.
unfortunately,
your post is a good example of why i don't try to convert from code pasted here by a user.
it is incomplete and has typos.
and you didn't provide a source link, which allows others to see the original and read about it.


@jpmcewen
thanks for finding the link and code.
i went to the tv site and copied that code and tried to convert it.
i think the below code is the desired conversion.

-------------------

Find the full ToS conversion of the above Tradingview code:
https://usethinkscript.com/threads/natural-moving-average-for-thinkorswim.13801/
 
Last edited:
Solution
Status
Not open for further replies.

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

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