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.