LWMA: Linear Weighted Moving Average

Couldn't find searching for Linearly Weighted Moving Average (LWMA) in tradingview. Found one with the LWMA title, but it uses plain WMA calculation without the linearity which more heavily weights recent price data, which I need, so I try to made one.

LWMAs are also quicker to react to price changes than SMA and EMA . If you want a moving average with less lag than an SMA , try a LWMA.

It kind of also have a more clarity in defining the price trend and reversals. Trade signals usually based on crossovers, they can also indicate areas of potential support or resistance. But beware though, multiple false signals may also occur before a significant trend develops. Use a filter, some decent volatility oscillator might do the job.

The formula for this Linearly Weighted Moving Average is:

LWMA = ((Pn∗W1)+(Pn−1∗W2)+(Pn−2∗W3)...) / ∑W

​P = Price for the period
n = The most recent period, n-1 is the prior period, and n-2 is two periods prior
W = The assigned weight to each period, with the highest weight going first and then descending linearly based on the number of periods being used.

I hope I'm doing right translating it to Pine Script 4. Let me know if I miss something.

Phát hành các Ghi chú: Added directional color change
Phát hành các Ghi chú: Added custom weight parameter.

I just realized the native TV's WMA is already using the same linearity result, (maybe) using period value as the weight. So I added custom weight value parameter for this update and modified the function. By default it has a slight difference in plot result with the WMA even if period and weight has the same value.

I've read that most popular choice when using weighted moving averages is to use a higher weighting for recent values. I'm guessing that we can use much lower weight value if need contemporary or faster response (ie. intraday, binary, short-term), and higher weighting than the period for long-term confirmation, but both without losing context or moving away from our chosen lookback period span.
Phát hành các Ghi chú: Cleaned up a bit
Phát hành các Ghi chú: Added modification of one period factor to the function. Now it match the WMA plot when using same value for period and weight! Thanks to @allanster for spotting this, he gave me code samples comparing previous scripts, which then lead me to this small modification but significant in result. My profitable rate in backtest results has just been upped a notch by 4%! :D

By default the WMA is using period value as its weight so when it reach the last price the weight is 1. A more complex way is to choose a different weight for the most recent value. Which this script trying to do about. Let's say we're using 50-period lookback, but I want the weight is only about half of it, maybe 30. Then each value will need to drop by 30/50 consecutively in each price data. So that when n-49(50th period) is reached the weight is also will be 1.
Phát hành các Ghi chú: Just added some small alerts, as requested by @lazarevicivan.
Alert triggers when direction change.
Mã nguồn mở

Với tinh thần của TradingView, tác giả đã xuất bản tập lệnh theo mã nguồn mở, vì thế trader có thể dễ dàng hiểu và tùy chỉnh được. Bạn có thể sử dụng miễn phí, hoặc tùy chỉnh lại mã đã được cấp phép bởi Quy tắc Chung. Bạn có thể sử dụng nó trên biểu đồ.

Bạn muốn sử dụng tập lệnh này trên biểu đồ?

Bình luận

I tried to change some parameters and found some interesting curves and very good scripts. Thank you very much!
+1 Phản hồi
@levith, glad to hear it :)
Phản hồi
levith io72signals
@io72signals, Excuse me,Will you share your Dominant Cycle rsi scripts?
+1 Phản hồi
@levith, it hasn't finished yet, I'm still working on it. Still on backtesting and forward-testing phase
+1 Phản hồi
its different with the lwma in metatrader and ctrader . take care
Phản hồi
Looks nice. I am not sure from your description if your intent was to recreate TradingView's wma() function, but your script's values match their native code.
Phản hồi
sickojacko allanster
@allanster, indeed. I just checked and this code has the same result than the native wma() function.
Phản hồi
io72signals sickojacko
@sickojacko, updated :)

Now we can choose a different weight for the most recent value.
Phản hồi
io72signals allanster
woow.. first thing first,.. @allanster, you're one of my PineScript hero! :D I've learnt a lot from many of your published script. Guess It's not everyday you got one of your hero as first commenter on your first post :)))

Thanks for the double-check, my bad. I was trying to recreate the LWMA I use in MT4. It has a slight differences with WMA in my backtest result. But looking back, probably it might also had something to do with my other filter, or maybe just the WMA I use is using different equation.

Anyway, I've added the custom weight value input for the above.
+1 Phản hồi
allanster io72signals
@io72signals, thanks for the kind words and very nice to know I have helped someone else along the way.
+2 Phản hồi
Trang chủ Bộ lọc cổ phiếu Bộ lọc Forex Bộ lọc Tiền điện tử Lịch kinh tế Giới thiệu Tính năng Biểu đồ Trả phí Giới thiệu bạn Quy tắc Áp dụng Trung tâm Trợ giúp Giải pháp cho Website & Nhà môi giới Widget Giải pháp biểu đồ Thư viện Biểu đồ Lightweight Blog & Tin tức Twitter