Moving Average Crossover with Counter added

xxentre

Member
VIP
This will probably be pretty easy for someone that understands ToS better than me. Just a newbe here.
I started this with thinkscript code from ToS, however I cannot seem to get it to do exactly what I want.
I am trying to get it to show a red arrow on top pointing down when the 8ema close crosses below the 8ema open.
Then a green arrow on bottom pointing up when the 8ema close crosses above the 8ema open.
Also, if not to much trouble to add the ability to turn on and off the plotting of the ema lines in addition to the signals.

Somehow I ended up with a signal and an arrow. in the plots. I broke the script when I removed one.

Code:
#
# TD Ameritrade IP Company, Inc. (c) 2009-2024
#

#wizard input: length1
#wizard text: -period
#wizard input: averageType1
#wizard text: crosses
#wizard input: crossingType
#wizard input: length2
#wizard text: -period
#wizard input: averageType2
#wizard text: Price:
#wizard input: price

input price1 = Open;
input length1 = 8;
input price2 = close;
input length2 = 8;
input averageType1 = AverageType.Simple;
input averageType2 = AverageType.Simple;
input crossingType = {default above, below};

def avg1 = MovingAverage(averageType1, price1, length1);
def avg2 = MovingAverage(averageType2, price2, length2);

plot signalu = crosses(avg1, avg2, crossingType == CrossingType.above);
plot signald = crosses(avg1, avg2, crossingType == CrossingType.below);

#signalu.DefineColor("Above", GetColor(6));
#signald.DefineColor("Below", GetColor(7));
#signalu.AssignValueColor(if crossingType == CrossingType.above then signalu.color("Above") else signald.color("Below"));

plot ArrowUp = if signald crosses above signalu
               then low
               else double.nan;
     ArrowUP.SetPaintingStrategy(PaintingStrategy.Arrow_UP);
     ArrowUP.SetLineWeight(3);
     ArrowUP.SetDefaultColor(Color.Green);
plot ArrowDN = if signald crosses below signalu
               then high
               else double.nan;

 ArrowDN.SetPaintingStrategy(PaintingStrategy.Arrow_DOWN);
     ArrowDN.SetLineWeight(3);
     ArrowDN.SetDefaultColor(Color.Red);
 
Solution
try this for the column

if you want the count to start at the actual crossing point rather than after just add +1 to the count.

Code:
declare lower;

input price1 = Open;
input length1 = 8;
input price2 = close;
input length2 = 8;
input averageType1 = AverageType.Simple;
input averageType2 = AverageType.Simple;
input crossingType = {default above, below};
def avg1 = MovingAverage(averageType1, price1, length1);
def avg2 = MovingAverage(averageType2, price2, length2);
def signalUC =avg1<avg2 ;
def CountU = CompoundValue(1, if signalUC then CountU[1] + 1 else 0, 0);
def signalDC =avg1>avg2 ;
def CountD = CompoundValue(1, if signalDC then CountD[1] + 1 else 0, 0);
AddLabel (yes, if signalUC then "UP " + CountU  else "DOWN " + countD )...
Code:
input ShowAvg = yes;
input ShowArr = Yes;
input ShowCld = Yes;
input p1 = close;
input p2 = open;
input len = 8;
input typ = AverageType.EXPONENTIAL;
def av1 = MovingAverage(typ,p1,len);
def av2 = MovingAverage(typ,p2,len);

plot Avg_1 = if ShowAvg then av1 else double.nan;
plot Avg_2 = if ShowAvg then av2 else double.nan;

def signalu = crosses(av1, av2, CrossingDirection.ABOVE);
def signald = crosses(av1, av2, CrossingDirection.BELOW);

plot ArrowUp = SignalU and ShowArr;
     ArrowUP.SetPaintingStrategy(PaintingStrategy.BOOLEAN_Arrow_UP);
     ArrowUP.SetLineWeight(3);
     ArrowUP.SetDefaultColor(Color.Green);
plot ArrowDN = SignalD and ShowArr;
     ArrowDN.SetPaintingStrategy(PaintingStrategy.BOOLEAN_Arrow_DOWN);
     ArrowDN.SetLineWeight(3);
     ArrowDN.SetDefaultColor(Color.Red);


addcloud(if showcld then av1 else double.nan,if showcld then av2 else double.nan,color.green, color.red);
 
Code:
input ShowAvg = yes;
input ShowArr = Yes;
input ShowCld = Yes;
input p1 = close;
input p2 = open;
input len = 8;
input typ = AverageType.EXPONENTIAL;
def av1 = MovingAverage(typ,p1,len);
def av2 = MovingAverage(typ,p2,len);

plot Avg_1 = if ShowAvg then av1 else double.nan;
plot Avg_2 = if ShowAvg then av2 else double.nan;

def signalu = crosses(av1, av2, CrossingDirection.ABOVE);
def signald = crosses(av1, av2, CrossingDirection.BELOW);

plot ArrowUp = SignalU and ShowArr;
     ArrowUP.SetPaintingStrategy(PaintingStrategy.BOOLEAN_Arrow_UP);
     ArrowUP.SetLineWeight(3);
     ArrowUP.SetDefaultColor(Color.Green);
plot ArrowDN = SignalD and ShowArr;
     ArrowDN.SetPaintingStrategy(PaintingStrategy.BOOLEAN_Arrow_DOWN);
     ArrowDN.SetLineWeight(3);
     ArrowDN.SetDefaultColor(Color.Red);


addcloud(if showcld then av1 else double.nan,if showcld then av2 else double.nan,color.green, color.red);
Thank you so much. You lead me on the path to completion. I like the cloud addition

I added the input for the 2nd moving average

Code:
input ShowAvg = yes;
input ShowArr = Yes;
input ShowCld = Yes;
input p1 = close;
input p2 = open;
input len1 = 8;
input len2 = 8;
input typ = AverageType.EXPONENTIAL;
def av1 = MovingAverage(typ,p1,len1);
def av2 = MovingAverage(typ,p2,len2);

plot Avg_1 = if ShowAvg then av1 else double.nan;
plot Avg_2 = if ShowAvg then av2 else double.nan;

def signalu = crosses(av1, av2, CrossingDirection.ABOVE);
def signald = crosses(av1, av2, CrossingDirection.BELOW);

plot ArrowUp = SignalU and ShowArr;
     ArrowUP.SetPaintingStrategy(PaintingStrategy.BOOLEAN_Arrow_UP);
     ArrowUP.SetLineWeight(3);
     ArrowUP.SetDefaultColor(Color.Green);
plot ArrowDN = SignalD and ShowArr;
     ArrowDN.SetPaintingStrategy(PaintingStrategy.BOOLEAN_Arrow_DOWN);
     ArrowDN.SetLineWeight(3);
     ArrowDN.SetDefaultColor(Color.Red);


addcloud(if showcld then av1 else double.nan,if showcld then av2 else double.nan,color.green, color.red);
 
Last edited:
Can this be made into a watch list column that will be green for uptrend and red for downtrend and displaying the count of how many bars back the crossover took place for the bear or bull.
I have seen something like this done with any script, but not sure which one.
 
Last edited:
try this for the column

if you want the count to start at the actual crossing point rather than after just add +1 to the count.

Code:
declare lower;

input price1 = Open;
input length1 = 8;
input price2 = close;
input length2 = 8;
input averageType1 = AverageType.Simple;
input averageType2 = AverageType.Simple;
input crossingType = {default above, below};
def avg1 = MovingAverage(averageType1, price1, length1);
def avg2 = MovingAverage(averageType2, price2, length2);
def signalUC =avg1<avg2 ;
def CountU = CompoundValue(1, if signalUC then CountU[1] + 1 else 0, 0);
def signalDC =avg1>avg2 ;
def CountD = CompoundValue(1, if signalDC then CountD[1] + 1 else 0, 0);
AddLabel (yes, if signalUC then "UP " + CountU  else "DOWN " + countD );
assignBackgroundColor( if signalUC then color.dark_green else color.red);
 
Solution
try this for the column

if you want the count to start at the actual crossing point rather than after just add +1 to the count.

Code:
declare lower;

input price1 = Open;
input length1 = 8;
input price2 = close;
input length2 = 8;
input averageType1 = AverageType.Simple;
input averageType2 = AverageType.Simple;
input crossingType = {default above, below};
def avg1 = MovingAverage(averageType1, price1, length1);
def avg2 = MovingAverage(averageType2, price2, length2);
def signalUC =avg1<avg2 ;
def CountU = CompoundValue(1, if signalUC then CountU[1] + 1 else 0, 0);
def signalDC =avg1>avg2 ;
def CountD = CompoundValue(1, if signalDC then CountD[1] + 1 else 0, 0);
AddLabel (yes, if signalUC then "UP " + CountU  else "DOWN " + countD );
assignBackgroundColor( if signalUC then color.dark_green else color.red);
Perfect, Thank you so much
 

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