Moving Average Shift [ChartPrime] For ThinkOrSwim

rip78

Member
The paintbars that this indicator generates seem to be much better than most other studies.
Author states: Moving Average Shift [ChartPrime] indicator combines multiple moving average (MA) types with a unique MA Shift Oscillator to help traders visualize trend direction, price deviations, and mean reversion states.

⯁ HOW TO USE
Use the indicator to follow the trend based on MA direction and price relation to it.
The MA Shift Oscillator helps identify potential mean reversion points where price may revert toward the MA.
The threshold setting allows traders to filter out weak mean reversion signals and focus on significant shifts.
The four-color oscillator visually indicates trend momentum and potential trend shifts.

⯁ CONCLUSION
The Moving Average Shift [ChartPrime] indicator is a powerful tool that merges trend-following and mean reversion strategies into one comprehensive system. By allowing traders to select different types of moving averages, it provides flexibility in trend analysis while visually enhancing price action with dynamic candle coloring. The MA Shift Oscillator further strengthens decision-making by detecting deviations and highlighting potential mean reversion points.

QHuy1HN.png



Here is the original Tradingview code:
https://www.tradingview.com/v/aApUyBnk/

For the new ThinkOrSwim code, you must scroll down to the next post
 
Last edited by a moderator:

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

I recently discovered this indicator on TV and have been using it for a few days now. The paintbars that it generates seem to be much better than most other studies. It would be great if @samer800 could convert this to Thinkscript. Thanks! https://www.tradingview.com/v/aApUyBnk/
check the below:

Upper Study:
CSS:
#// Indicator for TOS
#// © ChartPrime
#indicator("Moving Average Shift [ChartPrime]", "MA Shift [ChartPrime]", Upper Study)

#Converted by Sam4Cok@Samer800    - 03/2025
input colorBars = no;
input showCrosses = yes;
input movAvgType = AverageType.SIMPLE; # "Type"
input source = hl2;
input length = 40;
input oscLength = 15;
input oscThreshold = 0.5;

def na = Double.NaN;
def last = IsNaN(close);

#// MA
def MA = MovingAverage(movAvgType, source, length);

#// Osc
def diff = source - MA;
def perc_r = Highest(diff, 1000) * 0.99;
def osc_Val = diff / perc_r;
def osc = HullMovingAvg(osc_Val - osc_Val[oscLength], 10);
def osc_col = if osc > 0 then
              (if osc > osc[1] then 2 else 1) else (if osc < osc[1] then -2 else -1);

#/ Oscillator Plot

plot MovAvgShift = if !last then MA else na;
MovAvgShift.SetLineWeight(2);
MovAvgShift.AssignValueColor(if source > MA then Color.CYAN else
                             if source < MA then Color.MAGENTA else Color.GRAY);

#-- Signals
def sig_up = if showCrosses then (osc crosses above osc[2]) and osc[1] < -oscThreshold else na;
def sig_dn = if showCrosses then (osc crosses below osc[2]) and osc[1] > oscThreshold else na;

plot sigUp = if sig_up then low else na;
plot sigDn = if sig_dn then high else na;

sigUp.SetPaintingStrategy(PaintingStrategy.BOOLEAN_WEDGE_DOWN);
sigDn.SetPaintingStrategy(PaintingStrategy.BOOLEAN_WEDGE_UP);
sigUp.SetDefaultColor(Color.CYAN);
sigDn.SetDefaultColor(Color.MAGENTA);

#-- Bar Colors

AssignPriceColor(if !colorBars then Color.CURRENT else
                 if osc_col == 2 then Color.GREEN else
                 if osc_col == 1 then Color.DARK_GREEN else
                 if osc_col == -2 then Color.RED else
                 if osc_col == -1 then Color.DARK_RED else Color.GRAY);


#-- END of CODE

Lower Study:

CSS:
#// Indicator for TOS
#// © ChartPrime
#indicator("Moving Average Shift [ChartPrime]", "MA Shift [ChartPrime]", Lower Study)

#Converted by Sam4Cok@Samer800    - 03/2025
Declare lower;

input colorBars = no;
input showCrosses = yes;
input movAvgType = AverageType.SIMPLE; # "Type"
input source = hl2;
input length = 40;
input oscLength = 15;
input oscThreshold = 0.5;


def na = DOuble.NaN;
def last = isNaN(close);

#// MA
def MA = MovingAverage(movAvgType, source, length);

#// Osc
def diff = source - MA;
def perc_r = highest(diff, 1000) * 0.99;
def osc_Val = diff / perc_r;
def osc = HullMovingAvg(osc_Val - osc_Val[oscLength], 10);
def osc_col = if osc > 0 then
             (if osc > osc[1] then 2 else 1) else (if osc < osc[1] then -2 else -1);

#/ Oscillator Plot

plot MovAvgShift = if !last then osc else na;
plot top = if !last then oscThreshold else na;
plot bot = -top;
plot mid = if !last then 0 else na;

MovAvgShift.SetPaintingStrategy(PaintingStrategy.SQUARED_HISTOGRAM);
MovAvgShift.AssignValueColor(if osc_col==2 then Color.CYAN else
                             if osc_col==1 then Color.VIOLET else
                             if osc_col==-2 then Color.YELLOW else
                             if osc_col==-1 then Color.DARK_ORANGE else Color.GRAY);
mid.SetDefaultColor(Color.GRAY);
top.SetPaintingStrategy(PaintingStrategy.SQUARES);
top.AssignValueColor(if osc_col==2 then Color.GREEN else
                     if osc_col==1 then Color.DARK_GREEN else
                     if osc_col==-2 then Color.RED else
                     if osc_col==-1 then Color.DARK_RED else Color.GRAY);
bot.SetPaintingStrategy(PaintingStrategy.POINTS);
bot.AssignValueColor(if osc_col==2 then Color.GREEN else
                     if osc_col==1 then Color.DARK_GREEN else
                     if osc_col==-2 then Color.RED else
                     if osc_col==-1 then Color.DARK_RED else Color.GRAY);
#-- Signals
def sig_up = if showCrosses then (osc Crosses Above osc[2]) and osc[1] < -oscThreshold else na;
def sig_dn = if showCrosses then (osc Crosses Below osc[2]) and osc[1] > oscThreshold else na;

plot sigUp = if sig_up then osc - 0.1 else na;
plot sigDn = if sig_dn then osc + 0.1 else na;

sigUp.SetPaintingStrategy(PaintingStrategy.SQUARES);
sigDn.SetPaintingStrategy(PaintingStrategy.SQUARES);

sigUp.SetDefaultColor(Color.CYAN);
sigDn.SetDefaultColor(Color.YELLOW);

#-- Bar Colors

AssignPriceColor(if !colorBars then Color.CURRENT else
                 if osc_col==2 then Color.GREEN else
                 if osc_col==1 then Color.DARK_GREEN else
                 if osc_col==-2 then Color.RED else
                 if osc_col==-1 then Color.DARK_RED else Color.GRAY);

#-- END of CODE
 
check the below:

Upper Study:
CSS:
#// Indicator for TOS
#// © ChartPrime
#indicator("Moving Average Shift [ChartPrime]", "MA Shift [ChartPrime]", Upper Study)

#Converted by Sam4Cok@Samer800    - 03/2025
input colorBars = no;
input showCrosses = yes;
input movAvgType = AverageType.SIMPLE; # "Type"
input source = hl2;
input length = 40;
input oscLength = 15;
input oscThreshold = 0.5;

def na = Double.NaN;
def last = IsNaN(close);

#// MA
def MA = MovingAverage(movAvgType, source, length);

#// Osc
def diff = source - MA;
def perc_r = Highest(diff, 1000) * 0.99;
def osc_Val = diff / perc_r;
def osc = HullMovingAvg(osc_Val - osc_Val[oscLength], 10);
def osc_col = if osc > 0 then
              (if osc > osc[1] then 2 else 1) else (if osc < osc[1] then -2 else -1);

#/ Oscillator Plot

plot MovAvgShift = if !last then MA else na;
MovAvgShift.SetLineWeight(2);
MovAvgShift.AssignValueColor(if source > MA then Color.CYAN else
                             if source < MA then Color.MAGENTA else Color.GRAY);

#-- Signals
def sig_up = if showCrosses then (osc crosses above osc[2]) and osc[1] < -oscThreshold else na;
def sig_dn = if showCrosses then (osc crosses below osc[2]) and osc[1] > oscThreshold else na;

plot sigUp = if sig_up then low else na;
plot sigDn = if sig_dn then high else na;

sigUp.SetPaintingStrategy(PaintingStrategy.BOOLEAN_WEDGE_DOWN);
sigDn.SetPaintingStrategy(PaintingStrategy.BOOLEAN_WEDGE_UP);
sigUp.SetDefaultColor(Color.CYAN);
sigDn.SetDefaultColor(Color.MAGENTA);

#-- Bar Colors

AssignPriceColor(if !colorBars then Color.CURRENT else
                 if osc_col == 2 then Color.GREEN else
                 if osc_col == 1 then Color.DARK_GREEN else
                 if osc_col == -2 then Color.RED else
                 if osc_col == -1 then Color.DARK_RED else Color.GRAY);


#-- END of CODE

Lower Study:

CSS:
#// Indicator for TOS
#// © ChartPrime
#indicator("Moving Average Shift [ChartPrime]", "MA Shift [ChartPrime]", Lower Study)

#Converted by Sam4Cok@Samer800    - 03/2025
Declare lower;

input colorBars = no;
input showCrosses = yes;
input movAvgType = AverageType.SIMPLE; # "Type"
input source = hl2;
input length = 40;
input oscLength = 15;
input oscThreshold = 0.5;


def na = DOuble.NaN;
def last = isNaN(close);

#// MA
def MA = MovingAverage(movAvgType, source, length);

#// Osc
def diff = source - MA;
def perc_r = highest(diff, 1000) * 0.99;
def osc_Val = diff / perc_r;
def osc = HullMovingAvg(osc_Val - osc_Val[oscLength], 10);
def osc_col = if osc > 0 then
             (if osc > osc[1] then 2 else 1) else (if osc < osc[1] then -2 else -1);

#/ Oscillator Plot

plot MovAvgShift = if !last then osc else na;
plot top = if !last then oscThreshold else na;
plot bot = -top;
plot mid = if !last then 0 else na;

MovAvgShift.SetPaintingStrategy(PaintingStrategy.SQUARED_HISTOGRAM);
MovAvgShift.AssignValueColor(if osc_col==2 then Color.CYAN else
                             if osc_col==1 then Color.VIOLET else
                             if osc_col==-2 then Color.YELLOW else
                             if osc_col==-1 then Color.DARK_ORANGE else Color.GRAY);
mid.SetDefaultColor(Color.GRAY);
top.SetPaintingStrategy(PaintingStrategy.SQUARES);
top.AssignValueColor(if osc_col==2 then Color.GREEN else
                     if osc_col==1 then Color.DARK_GREEN else
                     if osc_col==-2 then Color.RED else
                     if osc_col==-1 then Color.DARK_RED else Color.GRAY);
bot.SetPaintingStrategy(PaintingStrategy.POINTS);
bot.AssignValueColor(if osc_col==2 then Color.GREEN else
                     if osc_col==1 then Color.DARK_GREEN else
                     if osc_col==-2 then Color.RED else
                     if osc_col==-1 then Color.DARK_RED else Color.GRAY);
#-- Signals
def sig_up = if showCrosses then (osc Crosses Above osc[2]) and osc[1] < -oscThreshold else na;
def sig_dn = if showCrosses then (osc Crosses Below osc[2]) and osc[1] > oscThreshold else na;

plot sigUp = if sig_up then osc - 0.1 else na;
plot sigDn = if sig_dn then osc + 0.1 else na;

sigUp.SetPaintingStrategy(PaintingStrategy.SQUARES);
sigDn.SetPaintingStrategy(PaintingStrategy.SQUARES);

sigUp.SetDefaultColor(Color.CYAN);
sigDn.SetDefaultColor(Color.YELLOW);

#-- Bar Colors

AssignPriceColor(if !colorBars then Color.CURRENT else
                 if osc_col==2 then Color.GREEN else
                 if osc_col==1 then Color.DARK_GREEN else
                 if osc_col==-2 then Color.RED else
                 if osc_col==-1 then Color.DARK_RED else Color.GRAY);

#-- END of CODE
Thank you @samer800. You are the best!
 

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

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