What’s the correct script to show EMA crossover in study


Category:
0
0

I’m wanting to have a study at the bottom of a chart that shows ETFs and the top 10 holdings and if how those holdings look if they have a bullish or bearish EMA crossover. If it is bullish (5 is over 21) then a green dot, bearish (5 below 21) then a red dot. The script section of this code is giving me issues to achieve this. I’ve left out part of the code (max 5000 characters) but I have all 10 spaces listed.

Declare lower;

#inputs
input symbol5 = \”QQQ\”;
input symbol4 = \”SPY\”;
input symbol3 = \”XLE\”;
input symbol2 = \”XLK\”;
input symbol1 = \”XLF\”;
input showLabels = yes;
input length1 = 5;
input length2 = 21;
input price = close;
input displace = 0;

defineGlobalColor(\”BearishEMA\”, color.red);
defineGlobalColor(\”BullishEMA\”, color.green);

script SymbolExpAverage {
input symbol = \”\”;
def length1 = \”\”;
def length2 = \”\”;
def price = close;
def displace = 0;
if length1 == ExpAverage(close(period = P1), length1) and
length2 == ExpAverage(close(period = P1), length2) then {length1 = Double.NaN; length2 = Double.NaN;} }

def s1s = SymbolExpAverage(symbol1);
def s2s = SymbolExpAverage(symbol2);
def s3s = SymbolExpAverage(symbol3);
def s4s = SymbolExpAverage(symbol4);
def s5s = SymbolExpAverage(symbol5);

#plots

plot symb1 = if !IsNaN(close) and !isNan(close(symbol1)) then 1 else Double.NaN;
symb1.SetPaintingStrategy(PaintingStrategy.POINTS);
symb1.SetLineWeight(5);
symb1.AssignValueColor(if s1s >= 0 then GlobalColor(\”BullishEMA\”) else GlobalColor(\”BearishEMA\”));
AddChartBubble(showLabels and isNan(close) and !isNan(close[1]) and !isNan(close(symbol1)[1]), 1, \”1: \” + symbol1,if s1s[1] >= 0 then GlobalColor(\”BullishEMA\”) else GlobalColor(\”BearishEMA\”), yes);
symb1.HideBubble();

plot symb2 = if !IsNaN(close) and !isNan(close(symbol2) ) then 2 else Double.NaN;
symb2.SetPaintingStrategy(PaintingStrategy.POINTS);
symb2.SetLineWeight(5);
symb2.AssignValueColor(if s2s >= 0 then GlobalColor(\”BullishEMA\”) else GlobalColor(\”BearishEMA\”));
AddChartBubble(showLabels and isNan(close[4]) and !isNan(close[5]) and !isNan(close(symbol2)[5]), 2, \”2: \” + symbol2,if s2s[5] >= 0 then GlobalColor(\”BullishEMA\”) else GlobalColor(\”BearishEMA\”), yes);
symb2.HideBubble();

plot symb3 = if !IsNaN(close) and !isNan(close(symbol3) ) then 3 else Double.NaN;
symb3.SetPaintingStrategy(PaintingStrategy.POINTS);
symb3.SetLineWeight(5);
symb3.AssignValueColor(if s3s >= 0 then GlobalColor(\”BullishEMA\”) else GlobalColor(\”BearishEMA\”));
AddChartBubble(showLabels and isNan(close) and !isNan(close[1]) and !isNan(close(symbol3)[1]), 3, \”3: \” + symbol3,if s3s[1] >= 0 then GlobalColor(\”BullishEMA\”) else GlobalColor(\”BearishEMA\”), yes);
symb3.HideBubble();

plot symb4 = if !IsNaN(close) and !isNan(close(symbol4) ) then 4 else Double.NaN;
symb4.SetPaintingStrategy(PaintingStrategy.POINTS);
symb4.SetLineWeight(5);
symb4.AssignValueColor(if s4s >= 0 then GlobalColor(\”BullishEMA\”) else GlobalColor(\”BearishEMA\”));
AddChartBubble(showLabels and isNan(close[4]) and !isNan(close[5]) and !isNan(close(symbol4)[5]), 4,\”4: \” + symbol4,if s4s[5] >= 0 then GlobalColor(\”BullishEMA\”) else GlobalColor(\”BearishEMA\”), yes);
symb4.HideBubble();

plot symb5 = if !IsNaN(close) and !isNan(close(symbol5) ) then 5 else Double.NaN;
symb5.SetPaintingStrategy(PaintingStrategy.POINTS);
symb5.SetLineWeight(5);
symb5.AssignValueColor(if s5s >= 0 then GlobalColor(\”BullishEMA\”) else GlobalColor(\”BearishEMA\”));
AddChartBubble(showLabels and isNan(close) and !isNan(close[1]) and !isNan(close(symbol5)[1]), 5, \”5: \” + symbol5,if s5s[1] >= 0 then GlobalColor(\”BullishEMA\”) else GlobalColor(\”BearishEMA\”), yes);
symb5.HideBubble();

#Spacers
symb1.hideTitle();
symb2.hideTitle();
symb3.hideTitle();
symb4.hideTitle();
symb5.hideTitle();

plot spacer0 = if !isNan(close) and !isNan(close(symbol10)) then 10.5 else double.nan;
plot spacer1 = if !isNan(close) and !isNan(close(symbol1)) then 1.5 else double.nan;
plot spacer2 = if !isNan(close) and !isNan(close(symbol2)) then 2.5 else double.nan;
plot spacer3 = if !isNan(close) and !isNan(close(symbol3)) then 3.5 else double.nan;
plot spacer4 = if !isNan(close) and !isNan(close(symbol4)) then 4.5 else double.nan;
plot spacer5 = if !isNan(close) and !isNan(close(symbol5)) then 5.5 else double.nan;
spacer0.setdefaultColor(color.gray);
spacer1.setdefaultColor(color.gray);
spacer2.setdefaultColor(color.gray);
spacer3.setdefaultColor(color.gray);
spacer4.setdefaultColor(color.gray);
spacer5.setdefaultColor(color.gray);
spacer0.hideTitle();
spacer1.hideTitle();
spacer2.hideTitle();
spacer3.hideTitle();
spacer4.hideTitle();
spacer5.hideTitle();

Marked as spam
Posted by (Questions: 21, Answers: 47)
Asked on June 21, 2018 11:12 am
215 views
0
Private answer

Wow, how about you cut this down to the very bare essentials? You should have presented a very simplistic sample code, something similar to the following.

input symbol1 = ”XLF”;
input length1 = 5;
input length2 = 21;
input price = close;
input displace = 0;
script SymbolExpAverage {
input symbol = ””;
def length1 = ””;
def length2 = ””;
def price = close;
def displace = 0;
if length1 == ExpAverage(close(period = P1), length1) and length2 == ExpAverage(close(period = P1), length2) then
{
length1 = Double.NaN; length2 = Double.NaN;
}
}
def s1s = SymbolExpAverage(symbol1);

Of which I can tell you the following items do not belong in the script statement:

def length1 = ””;
def length2 = ””;
def price = close;
def displace = 0;

Those have all been declared previously in your code. By declaring them again within your script you are creating local versions of those variables and you  have disconnected your script from those previously declared variables. If you want to use those values in your script you will need to set them as inputs. Then you will need to pass them to the script each time you call it.

And that if statement makes no sense whatsoever if your stated goal is to check for a moving average crossover.

And your script is missing a plot statement. So there is no output. No return value. There should have been an error:

Error processing referenced script SymbolExpAverage: At least one plot should be defined

What to do Next

Here is what I recommend. Stop trying to use the script statement entirely. Get your project done using methods you already understand. It’s clear you need to practice working with scripts in your studies using much easier and simplistic structures.

If you want my assistance in learning how to use scripts in your studies than you will create some very basic examples to practice. Then when you run across something you can’t work out on your own, post a new question in the forum. Don’t try to build a rocket engine until you can successfully work with bottle rockets.

Where to Start

For your practice, here is a great place to start:

input length = 5;
def price = close;
script myScript {
input scriptPrice = close;
input scriptLength = 10;
def ema = ExpAverage(scriptPrice, scriptLength);
plot myScript = ema;
}
plot test = myScript(price, length);

Before the script statement we have one input “length” and one declared variable “price”. Inside the script statement we have two inputs “scriptPrice” and “scriptLength”. Each is named to train your eyes to identify they are tied to the script. They have default values assigned, but you must past those values each time you call the script.

Notice there is one declared variable inside the script, “ema”. Which is computing an exponential moving average based on the values you pass to it.

The last line of the script is required. This is a plot statement that does not plot anything on the chart. This is the output value of the script. The return value. Having assembled a proper script, we can then plot the output of the script, by assigning its value to a plot statement.

Summary

This is the most basic example which includes all of the elements your current project is trying to utilize. The only thing lacking is the second moving average, followed by code that checks for crossovers. Of which there are dozens of examples in this forum. Start simple, build it slowly. One piece at a time. Always add the smallest piece at each stage. If that piece breaks it, you can quickly go back to a working version.

Marked as spam
Posted by (Questions: 37, Answers: 4084)
Answered on June 21, 2018 12:28 pm