Why TrendlineValue returns 0 ?
Author: grtrader
Creation Date: 3/24/2014 7:11 AM
profile picture

grtrader

#1
I wrote a script which after drawing a manual trendline, should:



1. Give a sell stop value for the next day
2. Give an alert with the sell stop value

I can get the sell stop value through DrawLabel, but the alert always shows a value of 0. If I type in a value, lets say 22, in place of "st" in the SellAtStop, the alert will show 22. For some reason it is not picking up the variable value "st". How can I fix this?


CODE:
Please log in to see this code.
profile picture

Eugene

#2
Zero is returned when the named trendline Resistance could not be found for the current symbol and time scale.
profile picture

grtrader

#3
Then why does it print out fine for DrawLabel (PricePane, "Sell Stop Value is " + st);? How do I get it to work?
profile picture

Eugene

#4
It prints out because you coded the script to DrawLabel unconditionally, i.e. always.

To get it to work, draw a trendline named Resistance, precisely, or condition the DrawLabel with a check for the trendline value != 0.
profile picture

grtrader

#5
Did this - it still does not work. The variable "st" value always returns fine for the DrawLabel, but returns 0 for the SellStop. I basically used the same script structure which Cone provided in his TrendlineStopAlert from a previous post.
profile picture

Eugene

#6
What "this" you did, precisely?

What position size are you using? Try switching to Raw Profit, Fixed Shares.
profile picture

grtrader

#7
Before, I drew a trendline, labeled it Resistance, and extended it to the right. This time I drew a trendline to the last bar, named it Resistance, and it still did not work. If you run this script, you will get the same thing. You will get the proper value in the DrawLabel. But, when you activate the script in the Strategy monitor, activate it, and run the strategy, the Sell Stop does come up as an alert, but always shows a price value of 0.
profile picture

Cone

#8
TrendlineValue won't work in the Strategy Monitor because the Chart object does not exist there.
profile picture

grtrader

#9
Is there a way to convert the Trendline value to a value that the Strategy monitor will recognize? If I replace

double st = TrendlineValue( bar, "Resistance" );

with

double st = 22;

The value of 22 shows up in both the DrawLabel and the SellStop price in the Strategy monitor.
profile picture

Eugene

#10
Finally, in post #7 (!) comes a mention of the Strategy Monitor. ;)

No, it's impossible to make it work in the SM. You can run push the value from a regular Strategy to the global memory and pull it from your script running in the SM, but in general, this is a waste of time.

Keep in mind that TrendlineValue will also not work in Multi-Symbol mode because the Chart object doesn't exist there as well.
profile picture

grtrader

#11
Do you think I can I create a dataseries from two of the values from the trendline and then use the dataseries to get the last value which the Strategy Monitor will recognize?
profile picture

Eugene

#12
QUOTE:
TrendlineValue won't work in the Strategy Monitor because the Chart object does not exist there.


QUOTE:
No, it's impossible to make it work in the SM.


Please tell me what is not clear?
profile picture

grtrader

#13
Here is the script from Cone for developing a SellStop from Troughs. This does work well. It sounds like the only way to do what I would like to do is to draw a trendline, manually get the bar and y values for two points, and then use the procedure in this script to accomplish it.
profile picture

Cone

#14
If you have the chart open and you've drawn the trendline, why don't you get the Stop trigger price from TrendLineValue while you're still looking at the chart?

Is it that you have many charts with lines and you want to update the stops each day? Something else?
profile picture

grtrader

#15
That's the idea. If I have let's say 4 stocks with trendlines - just for that it would take a lot of time to create new orders each day. With the Strategy Monitor, I could have the orders created automatically each day and then just go ahead and place them to take advantage of the features of the Strategy Monitor.

I am in the process of learning C+, and am trying to see if its feasible to create a second variable with just the value of the first variable (not where it came from, ie. TrendlineValue). If that is possible, might be able to get this to work, since creating a variable and assigning a number to it works fine for the SellStop in the Strategy Monitor.
profile picture

Cone

#16
Here's a Cadillac 2-script solution for you. (I sort of wanted to do this anyway)

Script 1: Write Trendline Data to a file
After you draw and name (or delete) your trendline named "Resistance", click "Go" to run this script, which writes the data to a file in your Wealth-Lab User \Data directory named MyTrendLineAlerts.txt. Repeat the process for any symbol and scale using this same script. The only thing to remember is to make sure you name the trendline the same name that you assign to TLName in the script, which is "Resistance" here.

CODE:
Please log in to see this code.



Script 2: Read Trendline Data from a file (and create an Stop Alert)
Of course, you can change the signal to BuyAtLimit or SellAtStop, etc. Note that this actually calculates the value of the trendline on the future bar, and, this script will work for you in the S. Monitor (or anywhere) Cool, eh?

CODE:
Please log in to see this code.
profile picture

grtrader

#17
Thanks! Just what I was looking for.

By the way, I find studying your scripts in detail is a great way to learn.
profile picture

Cone

#18
You're welcome. Yes, there are no shortages of examples around here. :)

For maintenance, I just remembered that I need to add some code to remove "old" trendlines. Currently Script 1 will just keep adding new lines to the file for the same TLName, but Script 2 will read only the first one. For this exercise and to avoid confusion, there should be only one Trendline with a unique name for a particular symbol/scale. And, if you delete the trendline Script 1 should also remove it from the file. Then I think the solution will be complete.
profile picture

Cone

#19
I've updated Script 1 (above) per the previous comments.
profile picture

grtrader

#20
Cannot get these scripts to work properly. I looked in the Wealthlab6 directory for MyTrenLineAlerts.txt and could not find it. Where does the data file exist? Do I have to create it for the scripts to run?
CODE:
Please log in to see this code.
profile picture

Cone

#21
QUOTE:
Do I have to create it for the scripts to run?
No. You have to:
1. Compile the first script.
2. Draw a Trendline
3. Name the Trendline Resistance
4. Click "Go" (or hit F5) to write the TL Resistance data to the file. (*This step is imperative!)

Then you only have to run the second script on the same symbol in the same scale. It will work.

The file is in the User Data folder (User Guide > Data > Where Data are Stored. This is not the install directory. (You don't really need to look for/at this file.)

Also, if you delete or change the position of the Trendline, you need to click "Go" again to remove/change the data for the Resistance Trendline in the file.
profile picture

grtrader

#22
I found the file with a search on the drive for MyTrendLineAlerts.

Followed your instructions, and it works.

Couple questions:

1. When you say click "Go", do you mean Run the Strategy or something else?
2. Once this is set up, does it automatically update each day for the sell stop?
3. For another stock with another trend line (named New Name), is it correct to run the two scripts with the New Name replacing "Resistance"?
profile picture

Cone

#23
1. Yes, you have to run the Script 1 Strategy once the Trendline is drawn/placed and named. You should run Script 1 whenever making any adjustments to the Trendline, or if you delete it.

2. Yes. Script 2 will re-calculate the new value each day. Remember, as it's written, Script 2 gives you the future value of the trendline, so it returns the value of the line for the stop at bar + 1 (tomorrow).

3. First note that you can use the same Script 1 and 2 for all symbols and all scales. The only requirement is that each symbol/scale has one trendline named 'Resistance'.

Of course you can create two new scripts that work for a different TL Name, but this is hardly necessary. With pretty simple modifications, the scripts can support multiple trendline names. And, of course, what you do with the value of the trendline in Script 2 is completely up to you. You said you wanted Stop order, so that's what I gave you.
profile picture

grtrader

#24
The stop value comes out ok on the Drawlabel, but a different (wrong value) shows up on the alert. I tried the entire procedure twice from scratch, the second time after removing the data from the file.
profile picture

Cone

#25
Since the same variable y is used for the stop price and the label, it's literally impossible for them to be different (unless you've modified the script).
profile picture

grtrader

#26
Finally realized I had the charts' last date set for about a week ago. One fixed, the scripts worked fine - I set it up on 3 different stocks and obtained the correct results.

This will end up saving me and anyone else who uses these a lot of time. Will still be going through the scripts to follow exactly what you have done.

Thanks again!
profile picture

pscanlon1

#27
Is there no way to access the trendlines manually drawn on the symbol within code? How does Wealthlab currently save the trendlines, and why can't we get access to that? If the manually drawn trendlines were in a list object or dictionary then we wouldn't have to name the lines.
profile picture

Eugene

#28
See TrendlineValue in the QuickRef.
profile picture

pscanlon1

#29
Eugene, I know that this is there, I was asking if we can grab them without specifying their name. I see in the folder DrawingObjects.Standard all the trendlines are saved as .DRW files. Is there a way to read these? There is some encoding and its not possible with notepad.
profile picture

Eugene

#30
Got it. Depends on which answer do you prefer: "No, it's not implemented" or "Feel free to reverse engineer it on your own"? ;)
This website uses cookies to improve your experience. We'll assume you're ok with that, but you can opt-out if you wish (Read more).