MoveTrendLine function

Studies that have been contributed to the community by other users. If you’ve got something useful to share, that’s great!
bowlesj3
Posts: 2180
Joined: 21 Jul 2007
Has thanked: 227 times
Been thanked: 429 times

MoveTrendLine function

Postby bowlesj3 » 28 Jul 2010

NOTE: The final version is attached to the last post. Please read that post and study the functions before using them.

I have always found that moving trend lines was more of a challenge than it should be so I finally wrote this function to do it for me properly. I searched and this appears to be the first function like this. It can save you a lot of headaches in strange trend line moves.

So far it works. I tested it moving a trend line to various places in every combination I could come up with and could not get it to fail. My test script is included in the zip but it requires an external program to signal it for running so it may not be of any value to you. I just wrote it yesterday so it has not had long term testing. Therefore, if someone does find a situation where it does not measure up and you create a fix for it please let me know.

You have to submit the date1 and time1 as in the past relative to the date2 and time2 set which are to be in the future. If you invert them it aborts the function. There is a caller field at the very start where you can indicate where you called it from to help with debugging if these date/time pairs ever get flipped the wrong way. I already have found this useful in finding a bug in my external program which was submitting the date1/time1 to date2/time2 pairs in the incorrect order. I had 25 calls to the function in the study and I gave each call to this function a letter from "A" through to "Y". By doing that I found the problem within 3 or 4 minutes (and like I said the problem was outside the study so without this technique it could have been hours before I found the problem).

Note1: It does not seem to require you create any fine adjustments to the value1 and value2 to get it to work. It works no matter what you give it and no matter where the trend line is already. Just make sure that your trend line start date/time info is alway more in the past than your trend line end date/time info.

Note2: I find it is always better to create my trend lines in advance on currentbar = 1, hiding them and when I need to see them move them into place when I need then. This way I avoid deletion commands which can cause problems in MC. I often switch the ID to negative as a way to have the logic know in advance that the line is hidden and inactive. Hide them with negative value and black.

Here is a sample call:

value1 = A_MoveTrendLine("CallLocA",TL_ID,Date1,Time1,Value1,Date2,Time2,Value2);
Last edited by bowlesj3 on 10 Aug 2010, edited 4 times in total.

User avatar
TJ
Posts: 7740
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 1033 times
Been thanked: 2221 times

Postby TJ » 28 Jul 2010

interesting work.
thanks for sharing.

bowlesj3
Posts: 2180
Joined: 21 Jul 2007
Has thanked: 227 times
Been thanked: 429 times

Postby bowlesj3 » 28 Jul 2010

I wish it was available built in with MC. It would have saved me hours of work and I see no use for the setbegin setend pair of commands unless you want to only setend alone to extend it but not off the chart.

It could probably test for moving only one end or the other if there is no change to the other end. However I don't think the difference would be noticable.

bowlesj3
Posts: 2180
Joined: 21 Jul 2007
Has thanked: 227 times
Been thanked: 429 times

Re: MoveTrendLine function

Postby bowlesj3 » 06 Aug 2010

Since I started using this function to move trend lines, I have seen only one incorrect move of the trend line. The next execution of the function corrected it. Now that I know that in very rare situations it will get the two commands inverted incorrectly (TL_SetBegin and TL_SetEnd) I have it on my todo list to put a debug log in try so I can figure out what to change without causing it to do a lesser job than the almost perfect job it seems to be doing so far.

User avatar
RobotMan
Posts: 375
Joined: 12 Jul 2006
Location: Los Altos, California, USA
Has thanked: 31 times
Been thanked: 13 times
Contact:

Re: MoveTrendLine function

Postby RobotMan » 06 Aug 2010

Thanks John!
Nice work.

bowlesj3
Posts: 2180
Joined: 21 Jul 2007
Has thanked: 227 times
Been thanked: 429 times

Re: MoveTrendLine function

Postby bowlesj3 » 08 Aug 2010

NOTE: The final version is attached to the last post. Please read that post and study the functions before using them.


Your welcome Bob/TJ.

I have adjusted the routine with a debugging trap to attempt to detect if the line has been moved to the destination input parameters correctly or not. If the trap idea works and the line is not moved correctly it will issue an alert and log all the information for me to study. Hopefully at that point I can make an adjustment to the logic so it works perfectly 100% of the time. I will be testing the trap this week. Based on my experience so far, with the function as it stands now, it could be weeks before it finally traps anything so I may post the trap version here so others can use it and if their version traps the error maybe they can post the information log file here for me to study.
Last edited by bowlesj3 on 10 Aug 2010, edited 1 time in total.

bowlesj3
Posts: 2180
Joined: 21 Jul 2007
Has thanked: 227 times
Been thanked: 429 times

Re: MoveTrendLine function

Postby bowlesj3 » 09 Aug 2010

NOTE: The final version is attached to the last post. Please read that post and study the functions before using them.

This zip file contains

1/ a seconds bar movetrendline_s function and the original function.

2/ Both have the new trap to try and detect if the resulting move of the line does not match the intended move (see post above where I mention I saw one miss move after 1 week of use - much better than without the function). I have been running this trap version all day and it does not seem to mess up the current functionality. If it does detect that the move was done incorrectly then there is a file write of all the information required for debugging was well as an alert. If this occurs please send me a copy of the file output since I should be able to recreate the situation perfectly and hopefully revise the code to prevent the problem.
Last edited by bowlesj3 on 10 Aug 2010, edited 2 times in total.

bowlesj3
Posts: 2180
Joined: 21 Jul 2007
Has thanked: 227 times
Been thanked: 429 times

Re: MoveTrendLine function

Postby bowlesj3 » 10 Aug 2010

Good News. It turns out that the trap I wrote to detect bad moves of the trend line is in fact working. It detected that I was incorrectly placing a vertical trend line pointing down rather than pointing up the way you are suppose to do it (see MC manual under "TL_SetEnd" where it talks about this.). So I fixed my call to the function. In addition I made these changes to both of the functions. These changes are reattached to this post (I have deleted all prior attaches.).

1/ Now the functions edit to make sure you are correctly placing vertical trend lines pointing up.
!!!!!!!! Be Warned that these new ones will abort your script if the coordinates have your trend line pointing down. For me this is not a problem since I am discretionary. If you auto trade you might want to change it to an alert rather than a RaiseRunTimeError as I have it now.

2/ I put the currenttime_s in the first line of the log at the far right side so you can tell if a change you have made to your calling script has effected the error log.

3/ I also fixed the MoveTrendLine_s version to have the "_s" extention in the log file name. I also made a few other similar corrections like this in the script.
Attachments
A_MoveTrendLine.zip
(4.79 KiB) Downloaded 613 times

User avatar
TJ
Posts: 7740
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 1033 times
Been thanked: 2221 times

Re: MoveTrendLine function

Postby TJ » 10 Aug 2010

... it is one damn little job after another...

:-)>

bowlesj3
Posts: 2180
Joined: 21 Jul 2007
Has thanked: 227 times
Been thanked: 429 times

Re: MoveTrendLine function

Postby bowlesj3 » 10 Aug 2010

Yes, for sure. It is a learning function. LOL, like Star Track "adventuring where no man has dared go before".

In the end you will just call it and these strange trend line moves will never occur again. I use to get maybe 10 of these strange moves daily. Now it is down to one so far since I installed it about a week before the first post. (not sure of the cycle obviously). I am really curious what caused it to make that one strange move (hoping the trap catches it).
Last edited by bowlesj3 on 10 Aug 2010, edited 1 time in total.

User avatar
TJ
Posts: 7740
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 1033 times
Been thanked: 2221 times

Re: MoveTrendLine function

Postby TJ » 10 Aug 2010

I can get carried away too...


Image


lots of numbers missing in between... don't know where did they go.
Attachments
trendline.gif
# of trendlines
(5.55 KiB) Downloaded 2614 times

bowlesj3
Posts: 2180
Joined: 21 Jul 2007
Has thanked: 227 times
Been thanked: 429 times

Re: MoveTrendLine function

Postby bowlesj3 » 10 Aug 2010

That is a high number. I avoid deleting trend lines now (you gave me that idea TJ actually). I find MC gets confused when deleting if two different studies on the same chart are adding new trend lines and also deleting trend lines. It gets mixed up and deletes the wrong one. I believe it is because it does not lock them in place until BarStatus = 2. You can not use IntraBarPersist on the ID of the trend line because of the way MC works. You have to leave it off and store the ID only on the BarStatus = 2 bar.

It is funny. I delete arrows but never have a problem. Probably because they are all deleted from only one study on one chart. The other studies have a specific number of arrows that are retained.


Return to “User Contributed Studies and Indicator Library”