I’m trying to set an alert that’s equivalent to a trailing-stop order. I found this study elsewhere but I’m not sure it’s doing what I want, so I thought I’d ask you for your opinion.
input Period = 12;
input Price = close;
def HP = Highest(Price,Period);
def PB = (HP-Price)/HP;
plot Pullback = PB;
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]. When I look at the condition preview, it seems to do what I want, but I’m not exactly sure what impact the input period has, which in this case is 12. Thoughts?
Also, when I simply plot the study on a chart, I’m not sure how to read it.
I guess you can tell I’m not sure about a few things here, and I’d appreciate any help you could offer. Thanks!
Marked as spam
I see you tried to explain how you adapt that code to use as an alert.
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.
Don't ask me what the code is trying to do. I am only providing some direction on how to write it properly.
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.
Marked as spam
i’m not sure what section of code you’re talking about but such is the price of my own evident ignorance. meanwhile, i took your code and made an alert out of it and it looks pretty much the same as the alerts i got from that other code. all i’m after is an alert telling me when a stock — or in this case, a mutual fund, IOFIX — declines by a certain percentage amount. in the example, 1%. as I think you can see, both alerts triggered on 3/11, the day on which the decline was greater than or equal to 1%. meanwhile, part 2: is there such a thing as a trailing-stop alert that exactly mimics what a trailing-stop order might be? here are two pix, one of your code as an alert and the other what i used before: https://drive.google.com/file/d/1SjVVQKi4vkkwjoMGI-_UKaG4mNkD7y8B/view?usp=sharing https://drive.google.com/file/d/1Sar0o6vghxaPIQWTA-PKxr729sbB5lGf/view?usp=sharing
The screenshot helps a great deal. Thank you. When building study alerts we have already found it's best to use the entire code and not a reference to the code as you have done. Especially when it comes to custom chart studies. So I did not even have that idea anywhere in view when trying to understand your statements. I will include what that should look like in my answer as an edit. However I still cannot provide answer until you provide all the details. It may seem obvious to you when you say "declines by a certain percentage". However what you have left out is "from what"? Declined by a certain percentage from what reference point? We cannot even compute a percentage decline unless we know two data points. The starting point and the ending point.
got it. i think. okay, here's the definition of a trailing stop order, which i'm sure you already know: "A sell trailing stop order sets the stop price at a fixed amount below the market price with an attached "trailing" amount. As the market price rises, the stop price rises by the trail amount, but if the stock price falls, the stop loss price doesn't change, and a market order is submitted when the stop price is hit." in this example case, the 'fixed amount below the market price' is 1%, which trails upward as the price rises. I understand what you're saying about reference point, but does the definition make it any clearer what i'm trying to do? the def says a market order is submitted when the stop price is hit, but instead of an order, i just want an alert. am i still being about as clear as mud?
Yep, I understand. And I believe the code you are using does exactly that. Except that you need to adjust the input for price. It's currently set to the close but according to what you just described you need that to be set to the high. And once you do you that you will want to change the following line: def value = (targetPrice - price) / targetPrice; to this def value = (targetPrice - close) / targetPrice; Those two changes will make the code tracking new highs from which to compute the percent change and then it will used the close (current price being traded) to measure the percent decline and trigger your exit.
outstanding, pete, and thank you ever so much for figuring this out for me. left to my own devices -- well, ya know. pax, erik
Please log in to post questions.