Repaints "The System" Moving Average Cross w/ Pullbacks For ThinkOrSwim

Repaints
@Chemmy I'm sorry I just now perused this thread and thank you for your thoughtful and creative amalgamation of two strategies. To clarify, is the idea to go long when a pink/magenta candle appears, and perhaps continue to add more as additional pink/magenta candles appear? Likewise to sell and/or go short when the lime/yellow candles appear? If not, can you please explain how to use this strategy? I notice when examining the magenta and yellow candles on the daily chart of the S&P 500 that there are many instances of strikingly good signals using the simple approach I just described, although when I apply a floating p/l graph over the past year, the results show a cumulative loss. Interestingly, the floating p/l shows a gain over the past 5 years. In any case, examining the chart by hand over the past year shows some great calls. I suspect I'm still missing something rudimentary and vital, and I'm sorry for that. Can you say how to use the system properly? Thank you for any thoughts and for sharing also something very creative as you have.

So, what I would say is to read through the entirety of the slides and twitter thread in the first post of this thread -- the progenitor of this strategy can explain his own preferences probably much better than I can. In terms of the way that I use any sort of moving average cross strategy is to play the trend that they indicate; I don't usually ever try to snipe tops or bottoms anymore, I play the pullbacks in larger trends.

If you look at the chart I posted along with the study, you'll see magenta candles when the System is up (green), and lime candles when the System is down(red); those are the plays that I would be taking, since in my opinion those are much safer and provide much better opportunities.
 
@Chemmy Thank you for answering. I inquired after briefly studying the slides under the pretense you added something. Thank you for your clear answer. I notice on my charts that there is one majenta or lime green candle followed by many grey ones regardless of the trend direction, unlike what's shown on your chart (that's with the aggregate period set to 'Day' on the daily chart, or '30 min' on the accompanying chart). Any suggestions with that? Thank you again for posting this strategy.
 
Last edited:
@Chemmy Thank you for answering. I inquired after briefly studying the slides under the pretense you added something. Thank you for your clear answer. I notice on my charts that there is one majenta or lime green candle followed by many grey ones regardless of the trend direction, unlike what's shown on your chart (that's with the aggregate period set to 'Day' on the daily chart, or '30 min' on the accompanying chart. Any suggestions with that? Thank you again for posting this strategy.

Oh, so that's a simple fix -- if you go to the study settings, there should be an option that says "color remaining bars". Just set that to no and your candles should paint the same way mine does in the screenshot! Let me know if that doesn't work for some reason.
 
Oh, so that's a simple fix -- if you go to the study settings, there should be an option that says "color remaining bars". Just set that to no and your candles should paint the same way mine does in the screenshot! Let me know if that doesn't work for some reason.
Thanks for the suggestion. When I alter the setting as such, the subsequent candles paint in accordance with whatever happened that day rather than majenta or lime. Not that important but curious why that's the case.

I notice that the magenta candles are subject to change. For example, on the daily chart of /ES, a magenta candle appeared on 2/24 but that is gone now, although a new one appears at today's candle (3/1). So I surmise it really is not possible to accurately look back at previous identifications. Am I misunderstanding something (probably the case)?
 
Thanks for the suggestion. When I alter the setting as such, the subsequent candles paint in accordance with whatever happened that day rather than majenta or lime. Not that important but curious why that's the case.

I notice that the magenta candles are subject to change. For example, on the daily chart of /ES, a magenta candle appeared on 2/24 but that is gone now, although a new one appears at today's candle (3/1). So I surmise it really is not possible to accurately look back at previous identifications. Am I misunderstanding something (probably the case)?
Yep, I believe those bars repaint.. which makes it tough.
 
Well, if this can't be remedied, it should be identified as "repaints" so it doesn't mislead others. Still kind of scratching my head as I notice instances in the past where there are multiple magenta valleys (e.g. May to August 2021 with /ES) without a subsequent lime candle.
 
Yes, I mentioned this in post #11 in the thread -- the peaks and valleys can't possibly be confirmed in real time so that portion of the code does repaint -- @MerryDay if you feel that this section of the code repainting warrants a repaint label on this study then that's absolutely fine with me.

The "System" strategy is just based on the moving averages but the peak valley addition was my own twist -- sorry for any confusion.
 
@Chemmy --Thank you for the clarification but I'm still confused because the indicator appears to convey multiple valleys without subsequent peaks on occasion. Can you explain why sometimes it paints one valley after another and other times it erases prior valleys and paints new ones (see May to August 2021 with /ES on the daily chart for an illustration of the prior phenomenon as compared to what happened between 2/24/23 and 3/1/23 for repainting)?
 
Last edited:
I recently talked to a friend who sent me a thread on an SMA cross strategy dubbed "The System", by TraderBJones on twitter. There is a pdf written about it:
https://docs.google.com/presentatio...h2hMEFe7cFIU/edit#slide=id.g118a2787cc5_0_152
though the general strategies follow any typical moving average cross strategies you've seen.

The thread:
https://t.co/GmFVnoqPP9

I wanted to test this quick script, which plots the two SMAs -- according to the documentation, it should either be used on a 30-minute aggregation for intraday trading or daily for swings, so I allowed for MTF aggregation since we both trade on lower time frames. As an idea, I wanted to see if better entries or adding opportunities could be found by combining this with peaks and valleys, particularly those that qualified as market pullbacks (5% or more):

bAeVH19.png


In general, this should be used to follow the rules of "The System" as it's written, but the identification of strong peaks during a bearish trend or strong valleys during a bullish trend within the system should allow for optimal entries in the direction of the System's trend, while the trend is still strong.

This is just a test script, so if anyone sees any promise in this you're welcome to improve it. Thanks to @halcyonguy for their incredible market pullback study: https://usethinkscript.com/threads/identifying-market-pullback-i-e-5.12744/#post-108853

Code:
# The System
# By @TraderBJones on twitter
# Coded by Chemmy for useThinkScript.com


# corrections_peak_to_valley_00 portion credits
#https://usethinkscript.com/threads/identifying-market-pullback-i-e-5.12744/
#Identifying Market Pullback (i.e. 5%+)
# Thread starterscottsamesame  Start dateToday at 9:01 AM

#.........................................
# ref: peak/valley code , robert  post#10
# https://usethinkscript.com/threads/zigzag-high-low-with-supply-demand-zones-for-thinkorswim.172/#post-7048
#.........................................

# Inputs
input fast = 10;
input slow = 50;
input displace = 0;
input showBreakoutSignals = no;
input addlabels = yes;
input agg = aggregationPeriod.THIRTY_MIN;
def price1 = close(period=agg);
def price2 = close(period=agg);

## SMA plots
plot SMAFast = Average(price1[-displace], fast);
plot SMASlow = Average(price2[-displace], slow);

# Signals plots
plot UpSignal = SMAFast crosses above SMASlow;
plot DownSignal = SMAFast crosses below SMASlow;

UpSignal.SetHiding(!showBreakoutSignals);
DownSignal.SetHiding(!showBreakoutSignals);


## Look and feel settings
SMAFast.SetDefaultColor(GetColor(1));
SMASlow.SetDefaultColor(GetColor(3));
UpSignal.SetDefaultColor(Color.UPTICK);
UpSignal.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
DownSignal.SetDefaultColor(Color.DOWNTICK);
DownSignal.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);

SMAFast.AssignValueColor(if SMAFast >= SMASlow then color.Light_Green else color.Light_Red);
SMASlow.AssignValueColor(if SMAFast >= SMASlow then color.Dark_Green else color.Red);

SMASlow.SetLineWeight(3);

AddLabel(addlabels, if SMAFast >= SMASlow then "System UP" else "System DOWN", if SMAFast >= SMASlow then color.green else color.red);



### Peak and Valley code
input min_percent_change = 5;
input length = 10;

def na = double.nan;
def bn = BarNumber();
def lastBar = HighestAll(if IsNaN(close) then 0 else bn);
def offset = Min(length - 1, lastBar - bn);

def peak = high > highest(high[1], length - 1) and high == GetValue(highest(high, length), -offset);
def valley = low < Lowest(low[1], length - 1) and low == GetValue(Lowest(low, length), -offset);

# set to 0 if peak and valley happen on same bar
def pv = if bn == 1 then 0
  else if peak and valley then 0
  else if peak then 1
  else if valley then -1
  else pv[1];

def peak2 = if bn == 1 then 0
  else if peak and pv[1] != -1 then 0
  else peak;

def valley2 = if bn == 1 then 0
  else if valley and pv[1] != 1 then 0
  else valley;

input color_peak_valley_bars = yes;
input color_remaining_bars = yes;

AssignPriceColor(
       if (peak and color_peak_valley_bars) then Color.LIME
  else if (valley and color_peak_valley_bars) then Color.magenta
  else if (peak and !color_peak_valley_bars) then Color.current
  else if (valley and !color_peak_valley_bars) then Color.current
  else if color_remaining_bars then Color.DARK_GRAY
  else color.current);

def peak_line = if bn == 1 then 0
  else if peak2 then close
  else if valley2[1] then 0
  else peak_line[1];

def valley_line = if bn == 1 then 0
  else if valley2 then close
  else if peak2[1] then 0
  else valley_line[1];

plot zp = if peak_line > 0 then peak_line else na;
zp.setdefaultcolor(color.red);
zp.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);

plot zv = if valley_line > 0 then valley_line else na;
zv.setdefaultcolor(color.green);
zv.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);

#----------------------------
input gain_drop_stats = yes;

def gain = if pv[1] == 0 then 0
  else if peak2 then round(close - valley_line, 2)
  else 0;
def gain_per = if pv[1] == 0 then 0 else round( 100 * gain/valley_line, 1);
def is_gain_big = (gain_per >= min_percent_change);

addchartbubble((gain_drop_stats and peak2 and valley_line > 0 and gain_per > 1.30), high,
gain + "\n" +
gain_per + " %"
, ( if is_gain_big then color.green else color.gray), yes);

#----------------------------
def drop = if pv[1] == 0 then 0
  else if valley2 then round(peak_line - close, 2)
  else 0;
def drop_per = if pv[1] == 0 then 0 else round( 100 * drop/peak_line, 1);
def is_drop_big = (drop_per >= min_percent_change);

addchartbubble((gain_drop_stats and valley2 and peak_line > 0 and drop_per > 1.30), low,
drop + "\n" +
drop_per + " %"
, ( if is_drop_big then color.red else color.gray), no);
#

AddLabel(yes, if (SMAFast >= SMASlow and zp < SMASlow) then "Poss. Entry on Pullback" else " ", if (SMAFast >= SMASlow and zp < SMASlow) then color.green else color.red);

AddLabel(yes, if (SMAFast < SMASlow and zv > SMASlow) then "Poss. Entry on Pullback" else " ", if (SMAFast < SMASlow and zv > SMASlow) then color.red else color.green);


#Alerts
Alert(UpSignal, "System Up!",  Alert.Bar, sound.Ding);
Alert(DownSignal, "System Down!", Alert.Bar, sound.Ding);

# End Code
Is there an input that someone could share or add to allow a drop-down to choose other moving averages?
 
This is a version I modified to remove the agg and allow for different average types. I use a 5 and a 10 simple by default.
Code:
#.........................................
# ref: peak/valley code , robert  post#10
# https://usethinkscript.com/threads/zigzag-high-low-with-supply-demand-zones-for-thinkorswim.172/#post-7048
#.........................................
#mod atcsam
# Inputs
input fast = 5;
input fasttype = AverageType.SIMPLE;
input slow = 10;
input slowtype = AverageType.SIMPLE;
input displace = 0;
input showBreakoutSignals = no;
input addlabels = yes;
#input agg = aggregationPeriod.THIRTY_MIN;
def price1 = close;#(period=agg);
def price2 = close;#(period=agg);

## SMA plots
plot SMAFast = MovingAverage(fasttype, price1[-displace], fast);
plot SMASlow = MovingAverage(slowtype, price2[-displace], slow);

# Signals plots
plot UpSignal = SMAFast crosses above SMASlow;
plot DownSignal = SMAFast crosses below SMASlow;

UpSignal.SetHiding(!showBreakoutSignals);
DownSignal.SetHiding(!showBreakoutSignals);


## Look and feel settings
SMAFast.SetDefaultColor(GetColor(1));
SMASlow.SetDefaultColor(GetColor(3));
UpSignal.SetDefaultColor(Color.UPTICK);
UpSignal.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
DownSignal.SetDefaultColor(Color.DOWNTICK);
DownSignal.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);

SMAFast.AssignValueColor(if SMAFast >= SMASlow then Color.YELLOW else Color.YELLOW);
SMASlow.AssignValueColor(if SMAFast >= SMASlow then Color.DARK_GREEN else Color.RED);

SMASlow.SetLineWeight(3);

AddLabel(addlabels, if SMAFast >= SMASlow then "System UP" else "System DOWN", if SMAFast >= SMASlow then Color.GREEN else Color.RED);



### Peak and Valley code
input min_percent_change = 5;
input length = 10;

def na = Double.NaN;
def bn = BarNumber();
def lastBar = HighestAll(if IsNaN(close) then 0 else bn);
def offset = Min(length - 1, lastBar - bn);

def peak = high > Highest(high[1], length - 1) and high == GetValue(Highest(high, length), -offset);
def valley = low < Lowest(low[1], length - 1) and low == GetValue(Lowest(low, length), -offset);

# set to 0 if peak and valley happen on same bar
def pv = if bn == 1 then 0
  else if peak and valley then 0
  else if peak then 1
  else if valley then -1
  else pv[1];

def peak2 = if bn == 1 then 0
  else if peak and pv[1] != -1 then 0
  else peak;

def valley2 = if bn == 1 then 0
  else if valley and pv[1] != 1 then 0
  else valley;

input color_peak_valley_bars = yes;
input color_remaining_bars = yes;

AssignPriceColor(
       if (peak and color_peak_valley_bars) then Color.LIME
  else if (valley and color_peak_valley_bars) then Color.MAGENTA
  else if (peak and !color_peak_valley_bars) then Color.CURRENT
  else if (valley and !color_peak_valley_bars) then Color.CURRENT
  else if color_remaining_bars then Color.GRAY
  else Color.CURRENT);

def peak_line = if bn == 1 then 0
  else if peak2 then close
  else if valley2[1] then 0
  else peak_line[1];

def valley_line = if bn == 1 then 0
  else if valley2 then close
  else if peak2[1] then 0
  else valley_line[1];

plot zp = if peak_line > 0 then peak_line else na;
zp.SetDefaultColor(Color.RED);
zp.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);

plot zv = if valley_line > 0 then valley_line else na;
zv.SetDefaultColor(Color.GREEN);
zv.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);

#----------------------------
input gain_drop_stats = yes;

def gain = if pv[1] == 0 then 0
  else if peak2 then Round(close - valley_line, 2)
  else 0;
def gain_per = if pv[1] == 0 then 0 else Round( 100 * gain / valley_line, 1);
def is_gain_big = (gain_per >= min_percent_change);

AddChartBubble((gain_drop_stats and peak2 and valley_line > 0 and gain_per > 1.30), high,
gain + "\n" +
gain_per + " %"
, ( if is_gain_big then Color.GREEN else Color.GRAY), yes);

#----------------------------
def drop = if pv[1] == 0 then 0
  else if valley2 then Round(peak_line - close, 2)
  else 0;
def drop_per = if pv[1] == 0 then 0 else Round( 100 * drop / peak_line, 1);
def is_drop_big = (drop_per >= min_percent_change);

AddChartBubble((gain_drop_stats and valley2 and peak_line > 0 and drop_per > 1.30), low,
drop + "\n" +
drop_per + " %"
, ( if is_drop_big then Color.RED else Color.GRAY), no);
#

AddLabel(yes, if (SMAFast >= SMASlow and zp < SMASlow) then "Poss. Entry on Pullback" else " ", if (SMAFast >= SMASlow and zp < SMASlow) then Color.GREEN else Color.RED);

AddLabel(yes, if (SMAFast < SMASlow and zv > SMASlow) then "Poss. Entry on Pullback" else " ", if (SMAFast < SMASlow and zv > SMASlow) then Color.RED else Color.GREEN);

def Buy = price1 >= SMAFast and SMAFast >= SmaFast [1];
def Sell = Price1 <= SMAFast and SMAFast <= SMAFast [1];

#AddOrder(OrderType.BUY_TO_OPEN, Buy);
#AddOrder(OrderType.SELL_TO_CLOSE, Sell);

#Alerts
Alert(UpSignal, "System Up!",  Alert.BAR, Sound.Ding);
Alert(DownSignal, "System Down!", Alert.BAR, Sound.Ding);

# End Code
 

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