Day Trader's Anchored Moving Averages [wbburgin] for ThinkOrSwim

samer800

Moderator - Expert
VIP
Lifetime
X2lC4wZ.png

Author Message:
For day traders, establishing a trend at the start of the day is critically important for setting targets and entering positions. This can be difficult when traditional moving averages lag from previous days, causing late entry and/or incorrect trend interpretation.

The Day Trader's Anchored MA indicator plots three dynamic moving averages which restart on each new period (session or monthly - more coming soon). This eliminates the lag in traditional moving averages while better identifying the trend, as the moving averages essentially 'build up' their lengths as the day progresses, until they reach your chosen maximum length.

This means that these anchored moving averages are
  1. Quicker to identify the start-of-day trend, as markets tend to establish and then follow one trend throughout the day;
  2. Dynamically increasing throughout the day (to your specifications)
  3. Completely independent from previous days

Quick usage note: make sure that your moving average length is less than the number of bars in the period, or it won't reach the maximum length you specified.

CODE:

CSS:
#// https://www.tradingview.com/v/XqWFNfGN/
#// © wbburgin
#indicator("Day Trader's Anchored Moving Averages [wbburgin]"
# Converted and mod by Sam4Cok@Samer800    - 09/2023
input source = close;        # "Source",
input showCloud = yes;    # "Highlighting"
input colorBar = yes;
input diplayMovAvg = {default "Average Line", "Multiple Lines"};
input showMovAvg1 = yes;         # "Moving Average #1"
input length1 = 50;
input showMovAvg2 = yes;         # "Moving Average #2"
input length2 = 100;
input showMovAvg3 = yes;         # "Moving Average #3"
input length3 = 200;
input period = {default "Day", "Month"};    # "Anchor Change Period"

def na = Double.NaN;
def multi = diplayMovAvg == diplayMovAvg."Multiple Lines";
def cloudCond = multi and showCloud;
def ohlc = ohlc4;
def dayofmonth = GetDayOfMonth(GetYYYYMMDD());
def month = GetMonth();
def day = period == period."Day";
def anchor_reset = if day then (dayofmonth - dayofmonth[1])
                   else (month - month[1]);

script anchored_ma {
    input source = close;
    input length = 50;
    input anchor_reset_period = yes;
    def barssince = if anchor_reset_period then 0 else barssince[1] + 1;
    def num = Min(barssince, length);
    def sum = fold i = 0 to num with p do
              p + GetValue(source, i);
    def ma = if anchor_reset_period then source else
             if num > 0 then sum / num else source;
    plot out = ma;
}

def ma_1 = anchored_ma(source, length1, anchor_reset);
def ma_2 = anchored_ma(source, length2, anchor_reset);
def ma_3 = anchored_ma(source, length3, anchor_reset);
def ma1 = ma_1;
def ma2 = ma_2;
def ma3 = ma_3;
def avg_Ma = (ma1 + ma2 + ma3) / 3;

def extUp = ohlc > ma1 and  ma1 > ma2 and ma2 > ma3;
def norUp = ohlc > ma1 and  ma1 > ma3;
def extDn = ohlc < ma1 and  ma1 < ma2 and ma2 < ma3;
def norDn = ohlc < ma1 and  ma1 < ma3;

plot AvgMa = if multi then na else avg_Ma;
plot map1 = if !showMovAvg1 or !multi then na else ma1;    # "Anchored MA 1"
plot map2 = if !showMovAvg2 or !multi then na else ma2;    # "Anchored MA 2"
plot map3 = if !showMovAvg3 or !multi then na else ma3;    # "Anchored MA 3"

AvgMa.SetDefaultColor(Color.MAGENTA);
map1.SetDefaultColor(Color.CYAN);
map2.SetDefaultColor(Color.WHITE);
map3.SetDefaultColor(Color.ORANGE);

AddCloud(if cloudCond then ma1 else na, ohlc,  Color.DARK_RED,  Color.DARK_GREEN);
AddCloud(if cloudCond then ma2 else na, ohlc, Color.DARK_RED, Color.DARK_GREEN);
AddCloud(if cloudCond then ma3 else na, ohlc, Color.DARK_RED, Color.DARK_GREEN);


AssignPriceColor(if !colorBar then Color.CURRENT else
                 if extUp then Color.GREEN else
                 if norUp then Color.DARK_GREEN else
                 if extDn then Color.RED else
                 if norDn then Color.DARK_RED else Color.GRAY);


#-- END of CODES
 
@samer800 interesting, not sure which average type it is, hull, simple, or exponential?

If you have a great need to name this dynamic moving average; you could kind-of, sort-of go with "simple".
But not really.

It is dynamic.
At the beginning of the day, it has only a few inputs, it averages in more data points as the day goes on.

THIS is of critical importance and MUST be customized to every chart; this study is use on:
make sure that your moving average length is less than the number of bars in the period, or it won't reach the maximum length you specified.
Meaning the number of bars in a day on a 5min chart is obviously VASTLY different from the number of bars in a day on a 15min chart and you MUST adjust accordingly.
 
Last edited:
Soooo, are we turning off extended hours?

When adding an indicator to your chart. There is a customization period.
You need to analyze the settings of all the inputs of this indicator.
You need to analyze the timeframes and the bars that you populate your chart.

THIS is of critical importance and MUST be customized to every chart; this study is use on:
make sure that your moving average length is less than the number of bars in the period, or it won't reach the maximum length you specified.
Meaning the number of bars in a day on a 5min chart is obviously VASTLY different from the number of bars in a day on a 15min chart and you MUST adjust accordingly.

And yes, you need to review it with both extended hours on and off.

See how the various customizations interact with your strategy to better confirm your signals across your three timeframes, across multiple instruments, and through various market conditions.
 
Last edited:
this is interesting and wondering how does one use this? i see the MA being used are 50,100,200 but for a day trader on a 5m scalp, how is this to be used?
 
X2lC4wZ.png

Author Message:
For day traders, establishing a trend at the start of the day is critically important for setting targets and entering positions. This can be difficult when traditional moving averages lag from previous days, causing late entry and/or incorrect trend interpretation.

The Day Trader's Anchored MA indicator plots three dynamic moving averages which restart on each new period (session or monthly - more coming soon). This eliminates the lag in traditional moving averages while better identifying the trend, as the moving averages essentially 'build up' their lengths as the day progresses, until they reach your chosen maximum length.

This means that these anchored moving averages are
  1. Quicker to identify the start-of-day trend, as markets tend to establish and then follow one trend throughout the day;
  2. Dynamically increasing throughout the day (to your specifications)
  3. Completely independent from previous days

Quick usage note: make sure that your moving average length is less than the number of bars in the period, or it won't reach the maximum length you specified.

CODE:

CSS:
#// https://www.tradingview.com/v/XqWFNfGN/
#// © wbburgin
#indicator("Day Trader's Anchored Moving Averages [wbburgin]"
# Converted and mod by Sam4Cok@Samer800    - 09/2023
input source = close;        # "Source",
input showCloud = yes;    # "Highlighting"
input colorBar = yes;
input diplayMovAvg = {default "Average Line", "Multiple Lines"};
input showMovAvg1 = yes;         # "Moving Average #1"
input length1 = 50;
input showMovAvg2 = yes;         # "Moving Average #2"
input length2 = 100;
input showMovAvg3 = yes;         # "Moving Average #3"
input length3 = 200;
input period = {default "Day", "Month"};    # "Anchor Change Period"

def na = Double.NaN;
def multi = diplayMovAvg == diplayMovAvg."Multiple Lines";
def cloudCond = multi and showCloud;
def ohlc = ohlc4;
def dayofmonth = GetDayOfMonth(GetYYYYMMDD());
def month = GetMonth();
def day = period == period."Day";
def anchor_reset = if day then (dayofmonth - dayofmonth[1])
                   else (month - month[1]);

script anchored_ma {
    input source = close;
    input length = 50;
    input anchor_reset_period = yes;
    def barssince = if anchor_reset_period then 0 else barssince[1] + 1;
    def num = Min(barssince, length);
    def sum = fold i = 0 to num with p do
              p + GetValue(source, i);
    def ma = if anchor_reset_period then source else
             if num > 0 then sum / num else source;
    plot out = ma;
}

def ma_1 = anchored_ma(source, length1, anchor_reset);
def ma_2 = anchored_ma(source, length2, anchor_reset);
def ma_3 = anchored_ma(source, length3, anchor_reset);
def ma1 = ma_1;
def ma2 = ma_2;
def ma3 = ma_3;
def avg_Ma = (ma1 + ma2 + ma3) / 3;

def extUp = ohlc > ma1 and  ma1 > ma2 and ma2 > ma3;
def norUp = ohlc > ma1 and  ma1 > ma3;
def extDn = ohlc < ma1 and  ma1 < ma2 and ma2 < ma3;
def norDn = ohlc < ma1 and  ma1 < ma3;

plot AvgMa = if multi then na else avg_Ma;
plot map1 = if !showMovAvg1 or !multi then na else ma1;    # "Anchored MA 1"
plot map2 = if !showMovAvg2 or !multi then na else ma2;    # "Anchored MA 2"
plot map3 = if !showMovAvg3 or !multi then na else ma3;    # "Anchored MA 3"

AvgMa.SetDefaultColor(Color.MAGENTA);
map1.SetDefaultColor(Color.CYAN);
map2.SetDefaultColor(Color.WHITE);
map3.SetDefaultColor(Color.ORANGE);

AddCloud(if cloudCond then ma1 else na, ohlc,  Color.DARK_RED,  Color.DARK_GREEN);
AddCloud(if cloudCond then ma2 else na, ohlc, Color.DARK_RED, Color.DARK_GREEN);
AddCloud(if cloudCond then ma3 else na, ohlc, Color.DARK_RED, Color.DARK_GREEN);


AssignPriceColor(if !colorBar then Color.CURRENT else
                 if extUp then Color.GREEN else
                 if norUp then Color.DARK_GREEN else
                 if extDn then Color.RED else
                 if norDn then Color.DARK_RED else Color.GRAY);


#-- END of CODES
What setting would you recommend on 1 min time frame?

Also, can this be converted into multi-tike frame indicator?

Thank you!
 
this is interesting and wondering how does one use this? i see the MA being used are 50,100,200 but for a day trader on a 5m scalp, how is this to be used?
What setting would you recommend on 1 min time frame?

Also, can this be converted into multi-tike frame indicator?

Thank you!

Yes, this a day trading indicator.
No, you would not use this on scalping timeframes.
Regardless of what changes you make to the moving average lengths.
They would still be a lagging indicator so not appropriate for scalping.

Read more:
https://usethinkscript.com/threads/...-indicator-in-your-opinion.16487/#post-130704
 
Last edited:

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
296 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