I see you tried to explain how you adapt that code to use as an alert.
To set an alert, I’ve used the study + crosses above + [the trailing stop level; for example, i’d use .05 for a 5% stop].
But you did not provide section of code that fully executes what you have described. So there is only one question I can answer for you.
The input named "Period" is used to determine how many bars ago to look for the highest price.
I cannot think of any way the code you presented could be used as a trailing stop. Since you did not provide a section of code that directly serves as a trailing stop I will be deleting this question from the forum after 48 hours to avoid folks looking for this very solution being discourage when they find there is no such solution described anywhere in this post.
For all the newbies trying to learn how to write code, do not follow the bad examples demonstrated in code posted in the body of the question. I will take the time to make corrections to that code for the benefit of those who would like to learn the correct way to write that.
declare lower;
input barsAgo = 12;
input price = close;
def targetPrice = Highest(price, barsAgo);
def value = (targetPrice - price) / targetPrice;
plot pullback = value;
Don't ask me what the code is trying to do. I am only providing some direction on how to write it properly.
Edit: After getting a bit more clarification via the screenshot provided in the comment section below I have an update to the code. The fully functioning section of code that was lacking from this post until now is shown below:
declare lower;
input barsAgo = 12;
input price = close;
def targetPrice = Highest(price, barsAgo);
def value = (targetPrice - price) / targetPrice;
plot signal = value crosses above 0.01;
We have already discovered when working with Study Alerts and Conditional Orders we run into trouble when we try to reference the name of the study instead of using the entire code from the custom chart study. The example above shows the most robust way to apply this custom chart study to a Study Alert. The code is computing the percent change from the highest "price" in "barsAgo" to the current "price". If the highest "price" in "barsAgo" is your intended target for the trailing stop computation then this code is doing exactly what you intended.