Need Scanner for MACD averages

saithatr

New member
I have got the following code from an online/youtube video [100% credit goes to the AUTHOR ]. I would like to turn this into a scanner where I want to see GREEN BARS in my results or wanted to see only "UpTrend" data in my search result. any help would be appreciated.


declare lower;
input midTermPeriod = {"1 min", "3 min", "5 min", "15 min", "30 min", "60 min", "120 min", "Daily", default "Weekly", "Monthly"};
input longTermPeriod = {"3 min", "5 min", "15 min", "30 min", "60 min", "120 min", "Daily", "Weekly", default "Monthly"};

input fastLength = 12;
input slowLength = 26;
input MACDLength = 9;

input midTermFastLength = 12;
input midTermSlowLength = 26;
input midTermMACDLength = 9;

input longTermFastLength = 12;
input longTermSlowLength = 26;
input longTermMACDLength = 9;

def middleAggregation;

switch (midTermPeriod) {
case "1 min":
middleAggregation = AggregationPeriod.MIN;
case "3 min":
middleAggregation = AggregationPeriod.THREE_MIN;
case "5 min":
middleAggregation = AggregationPeriod.FIVE_MIN;
case "15 min":
middleAggregation = AggregationPeriod.FIFTEEN_MIN;
case "30 min":
middleAggregation = AggregationPeriod.THIRTY_MIN;
case "60 min":
middleAggregation = AggregationPeriod.HOUR;
case "120 min":
middleAggregation = AggregationPeriod.TWO_HOURS;
case "Daily":
middleAggregation = AggregationPeriod.DAY;
case "Weekly":
middleAggregation = AggregationPeriod.WEEK;
case "Monthly":
middleAggregation = AggregationPeriod.MONTH;
}

def highestAggregation;
switch (longTermPeriod) {
case "3 min":
highestAggregation = AggregationPeriod.THREE_MIN;
case "5 min":
highestAggregation = AggregationPeriod.FIVE_MIN;
case "15 min":
highestAggregation = AggregationPeriod.FIFTEEN_MIN;
case "30 min":
highestAggregation = AggregationPeriod.THIRTY_MIN;
case "60 min":
highestAggregation = AggregationPeriod.HOUR;
case "120 min":
highestAggregation = AggregationPeriod.TWO_HOURS;
case "Daily":
highestAggregation = AggregationPeriod.DAY;
case "Weekly":
highestAggregation = AggregationPeriod.WEEK;
case "Monthly":
highestAggregation = AggregationPeriod.MONTH;
}

DefineGlobalColor("UpTrend", color.GREEN);
DefineGlobalColor("DownTrend", color.RED);
DefineGlobalColor("NoTrend", color.LIGHT_GRAY);

def timeFrame = getAggregationPeriod();
def testTimeFrames = if timeFrame < middleAggregation and middleAggregation < highestAggregation then yes else no;


# This section is for the chart level MACD
def fastAvg = ExpAverage(close, fastLength);
def slowAvg = ExpAverage(close, slowLength);

plot Value = fastAvg - slowAvg;
Value.SetDefaultColor(color.CYAN);
plot Avg = ExpAverage(Value, MACDLength);
Avg.SetDefaultColor(color.YELLOW);
plot Diff = (value - avg)*3;

# This section is for the medium term MACD
def midTermFastAvg = ExpAverage(close(period = middleAggregation) , midTermFastLength);
def midTermSlowAvg = ExpAverage(close(period = middleAggregation) , midTermSlowLength);

def midTermValue = midTermFastAvg - midTermSlowAvg;
def midTermAvg = ExpAverage(midTermValue, midTermMACDLength);
plot midTermDiff = (midTermValue - midTermAvg)*3;
midTermDiff.Hide();
midTermDiff.HideBubble();

# This section is for the long term MACD
def longTermFastAvg = ExpAverage(close(period = highestAggregation) , longTermFastLength);
def longTermSlowAvg = ExpAverage(close(period = highestAggregation) , longTermSlowLength);

def longTermValue = longTermFastAvg - longTermSlowAvg;
def longTermAvg = ExpAverage(longTermValue, longTermMACDLength);
plot longTermDiff = (longTermValue - longTermAvg)*3;
longTermDiff.Hide();
longTermDiff.HideBubble();


def midTermLower = midTermDiff < midTermDiff[1];
def midTermHigher = midTermDiff > midTermDiff[1];
rec midTermSignal = if midTermLower then yes else if midTermSignal[1] == yes and midTermHigher == no then yes else no;
#plot test = midTermSignal;
def longTermLower = longTermDiff < longTermDiff[1];
def longTermHigher = longTermDiff > longTermDiff[1];
rec longTermSignal = if longTermLower then yes else if longTermSignal[1] == yes and longTermHigher == no then yes else no;

midTermDiff.AssignValueColor(if midTermSignal then color.RED else color.BLUE);
longTermDiff.AssignValueColor(if longTermSignal then color.RED else color.BLUE);

Diff.AssignValueColor(if Diff > Diff[1] and midTermSignal == no and longTermSignal == no then GlobalColor("UpTrend") else if Diff < Diff[1] and midTermSignal == yes and longTermSignal == yes then GlobalColor("DownTrend") else GlobalColor("NoTrend") );
Diff.SetPaintingStrategy(PaintingStrategy.HISTOGRAM);
Diff.SetLineWeight(3);

plot zeroLine = if close[-1] > 0 then 0 else Double.Nan;
zeroLine.AssignValueColor(if Diff > Diff[1] and midTermSignal == no and longTermSignal == no then GlobalColor("UpTrend") else if Diff < Diff[1] and midTermSignal == yes and longTermSignal == yes then GlobalColor("DownTrend") else GlobalColor("NoTrend") );
zeroLine.SetPaintingStrategy(PaintingStrategy.POINTS);
zeroLine.SetLineWeight(3);

AddLabel(yes, if Diff > Diff[1] and midTermSignal == no and longTermSignal == no then "U" else if Diff < Diff[1] and midTermSignal == yes and longTermSignal == yes then "D" else "N");
 

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