Trying to build a divergence indicator

magnetar513

New member
Attempting to build a divergence indicator, but the OK button won't go active to allow the script to be saved, although the "Save As New Script" option is available. No errors are appearing on or below the script text, but the script name goes red when saved in the list.

declare upper;


input price = close;
input rsiAvgTyp = AverageType.WILDERS;
input macdAvgTyp = AverageType.EXPONENTIAL;
input scanLength = 14;
def longScanLength = 2 * scanLength;
def fastEMAlength = 12;
def slowEMAlenght = 26;
def signalLength = ((slowEMAlenght - fastEMAlength) / 2) + Round((fastEMAlength * 0.47), 0);
def macdLine = ExpAverage(price, fastEMAlength) - ExpAverage(price, slowEMAlenght);
def signalLine = MovingAverage(AverageType.EXPONENTIAL, macdLine, signalLength);
def rsiLine = RSI(scanLength, 80, 15, price, AverageType.WILDERS);
def macdHistogram = macdLine - signalLine;
def moneyFlwIndx = MoneyFlowIndex(20, 80, scanLength, signalLength);
def stochRsiLine = StochRSI(close, close, close, 14, 3, 20, 80);

def priceTopUp = Highest(price, scanLength) == Highest(price, longScanLength);
def priceTopDown = Highest(price, scanLength) != Highest(price, longScanLength);
def priceBottomUp = Lowest(price, scanLength) != Lowest(price, longScanLength);
def priceBottomDown = Lowest(price, scanLength) == Lowest(price, longScanLength);


def macdHistoTopUp = Highest(macdHistogram, scanLength) == Highest(macdHistogram, longScanLength);
def macdHistoTopDown = Highest(macdHistogram, scanLength) != Highest(macdHistogram, longScanLength);
def macdHistoBottomUp = Lowest(macdHistogram, scanLength) != Lowest(macdHistogram, longScanLength);
def macdHistoBottomDown = Lowest(macdHistogram, scanLength) == Lowest(macdHistogram, longScanLength);

def rsiTopUp = Highest(rsiLine, scanLength) == Highest(rsiLine, longScanLength);
def rsiTopDown = Highest(rsiLine, scanLength) != Highest(rsiLine, longScanLength);
def rsiBottomUp = Lowest(rsiLine, scanLength) != Lowest(rsiLine, longScanLength);
def rsiBottomDown = Lowest(rsiLine, scanLength) == Lowest(rsiLine, longScanLength);

def stochRsiTopUp = Highest(stochRsiLine, scanLength) == Highest(stochRsiLine, longScanLength);
def stochRsiTopDown = Highest(stochRsiLine, scanLength) != Highest(stochRsiLine, longScanLength);
def stochRsiBottomUp = Lowest(stochRsiLine, scanLength) != Lowest(stochRsiLine, longScanLength);
def stochRsiBottomDown = Lowest(stochRsiLine, scanLength) == Lowest(stochRsiLine, longScanLength);

def moneyFlwIndxTopUp = Highest(moneyFlwIndx, scanLength) == Highest(moneyFlwIndx, longScanLength);
def moneyFlwIndxTopDown = Highest(moneyFlwIndx, scanLength) != Highest(moneyFlwIndx, longScanLength);
def moneyFlwIndxBottomUp = Lowest(moneyFlwIndx, scanLength) != Lowest(moneyFlwIndx, longScanLength);
def moneyFlwIndxBottomDown = Lowest(stochRsiLine, scanLength) == Lowest(stochRsiLine, longScanLength);


def bearMacdDiv = (priceTopUp and macdHistoTopDown) or (priceTopDown and macdHistoTopUp);
def bearRsiDiv = (priceTopUp and rsiTopDown) or (priceTopDown and rsiTopUp);
def bearStochRsiDiv = (priceTopUp and stochRsiTopDown) or (priceTopDown and stochRsiTopUp);
def bearMoneyFlwIndxDiv = (priceTopUp and moneyFlwIndxTopDown) or (priceTopDown and moneyFlwIndxTopUp);

def bullMacdDiv = (priceBottomUp and macdHistoBottomDown) or (priceBottomDown and macdHistoBottomUp);
def bullRsiDiv = (priceBottomUp and rsiBottomDown) or (priceBottomDown and rsiBottomUp);
def bullstochRsiDiv = (priceBottomUp and stochRsiBottomDown) or (priceBottomDown and stochRsiBottomUp);
def bullmoneyFlwIndxDiv = (priceBottomUp and moneyFlwIndxBottomDown) or (priceBottomDown and moneyFlwIndxBottomUp);


AddChartBubble(bearMacdDiv, close, "MACD Bearish Divergence", CreateColor(255,170,170), yes);
AddChartBubble(bearRsiDiv, close, "RSI Bearish Divergence", CreateColor(255,170,170), yes);
AddChartBubble(bearStochRsiDiv, close, "Stochastic Bearish Divergence", CreateColor(255,170,170), yes);
AddChartBubble(bearMoneyFlwIndxDiv, close, "MFI Bearish Divergence", CreateColor(255,170,170), yes);
AddChartBubble(bullMacdDiv, close, "MACD Bullish Divergence", Color.GREEN, yes);
AddChartBubble(bullRsiDiv, close, "RSI Bullish Divergence", Color.GREEN, yes);
AddChartBubble(bullstochRsiDiv, close, "Stochastic Bullish Divergence", Color.GREEN, yes);
AddChartBubble(bullmoneyFlwIndxDiv, close, "MFI Bullish Divergence", Color.GREEN, yes);


Any assistance would be greatly appreciated. Thanks in advance.
 
Solution
Sorry, spoke too soon, I just tried to apply it to the chart for a test run and it goes red when applied to the chart list. The file name is DivergenceFinder02. Are you able to run it on a chart?
Two issues.
1. ThinkScript was created to "plot" and there are no plot statements in the script
2. The syntax for StochRSI does not include close, close, close.

Change StochRSI to:
def stochRsiLine = StochRSI("rsi length" = 14, "over bought" = 80, "over sold" = 20, "rsi price" = close, "k period" = 14, "d period" = 3, "slowing period" =1, "average type" = "simple", "rsi average type" = "wilders");

And add this to the bottom of your script.
plot data = close ;
data.hide();

FYI: the easiest way to debug, is to add one line of code at...
Sorry, spoke too soon, I just tried to apply it to the chart for a test run and it goes red when applied to the chart list. The file name is DivergenceFinder02. Are you able to run it on a chart?
Two issues.
1. ThinkScript was created to "plot" and there are no plot statements in the script
2. The syntax for StochRSI does not include close, close, close.

Change StochRSI to:
def stochRsiLine = StochRSI("rsi length" = 14, "over bought" = 80, "over sold" = 20, "rsi price" = close, "k period" = 14, "d period" = 3, "slowing period" =1, "average type" = "simple", "rsi average type" = "wilders");

And add this to the bottom of your script.
plot data = close ;
data.hide();

FYI: the easiest way to debug, is to add one line of code at a time to see which was greying out the OK button.
Js4N0Y5.png
 
Solution
Two issues.
1. ThinkScript was created to "plot" and there are no plot statements in the script
2. The syntax for StochRSI does not include close, close, close.

Change StochRSI to:
def stochRsiLine = StochRSI("rsi length" = 14, "over bought" = 80, "over sold" = 20, "rsi price" = close, "k period" = 14, "d period" = 3, "slowing period" =1, "average type" = "simple", "rsi average type" = "wilders");

And add this to the bottom of your script.
plot data = close ;
data.hide();

FYI: the easiest way to debug, is to add one line of code at a time to see which was greying out the OK button.
Js4N0Y5.png
Thank you for your expertise!
 

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

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
444 Online
Create Post

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