discuss feature - generate Alert near Trend Line

Questions about MultiCharts and user contributed studies.
User avatar
geizer
Posts: 375
Joined: 16 Jun 2008
Has thanked: 40 times
Been thanked: 38 times

discuss feature - generate Alert near Trend Line

Postby geizer » 06 Feb 2009

Hello TS Support,

Is it possible to generate an alert if price closed near a Trend Line? Setting percentage, or dollar amount would work.
The main reason behind the feature is that any study is only a probability of what may happen. Instead of guestimating where to draw Trend Line, the trader can just draw it very quickly; then tell the system to Alert if price closes within certain price range from the trend line.

Opinions of other traders on the subject would be valuable, or maybe this can be implemented in PowerLangauge?
-
Pavel

bowlesj3
Posts: 2016
Joined: 21 Jul 2007
Has thanked: 171 times
Been thanked: 402 times

Postby bowlesj3 » 06 Feb 2009

You would have to have the code get the time and values of the start and end of the trend line when it was not active and calculate its rate of change per bar. Then you would have to use this to determine the trend line value at the current bar. Once this was done you could determine how close the current ticks close price was from the trend line. You would have to put this into a function and detemine the trend line IDs of all inactive trend lines one at a time and submit the ID number to the function as well as the distance from the trend line you require to issue the alert. The function could issue the alert or signal or whatever I suppose. This function will have to determine the number of bars from the last anchor point of the trendline until the current bar as part of the calculation.

I say you would have to do this if the trend line is inactive because if you move the trend line you do not want the alert to be tripped until you are finished with the proper placement of the trend line. Once the trend line is placed properly you would click anywhere on the chart to deactivate the trend line so it will then be detected by your code for the processing.

All the commands you need for this are under the trend line group. It is doable for sure but it could take a while to get this up and running. A lot harder than just the standard alert upon touch or cross over of the trend line.

User avatar
geizer
Posts: 375
Joined: 16 Jun 2008
Has thanked: 40 times
Been thanked: 38 times

Postby geizer » 06 Feb 2009

bowlesj3,
Sounds viable. I only don't understand how you get the current status of the trendline to ignore the alerts. Also the above implies the alerts can be generated from Powerlanguage code itself. Did I understand you correctly?
The code will take some debugging for sure, but broken in smaller pieces it does not seem too scary anymore.
Last edited by geizer on 07 Feb 2009, edited 1 time in total.

bowlesj3
Posts: 2016
Joined: 21 Jul 2007
Has thanked: 171 times
Been thanked: 402 times

Postby bowlesj3 » 07 Feb 2009

Hi Pavel

In the help for the powerlanguage editor you will find a group of commands under the trend line in the contents. TL_GetActive states "Returns a numerical value indicating the trendline ID number of the currently selected trendline; returns a value of -1 if no trendlines are currently selected." This is the one you would use to ensure that you are not currently moving the trend line and that the function should be called to determine if the current price is close to the trend line (as close as your parameter is set to).

To find trend lines that have been manually placed on the chart the commands TL_GetFirst and TL_GetNext are used from the group. You get the ID number from them and submit it to the function. You must set up a loop since you could have 2 or 3 trend lines out there. As each loop finds one you call the function. Lets call it funcIsPriceCloseToLine(ID, DistanceParam)

If you read that whole section you will find the the "TL_Get" group of commands. That will tell you how to get the info on the two anchor points of the trend line within the function.

The alert is just the alert command which the study or function would popup. You can set the alert properties in the format of the study. Actually You may need to put the alert in the study and have it tripped if the function returns a value that says that prices are close to the line. I am not sure if you can put an alert inside a function. It probably makes more sense to have the alert outside anyway since the function may be used in other ways that do not require an alert (such as tripping an order).
Alert("Price Is close to line"); Sarch for "alert" in the help.

Question? Are you using this on minute bars and higher or are you using this on second bars which have to use time_s? It effects the determination of the number of bars prices have moved past the second anchor point of your trend line. Also the number of bars between the anchor points of the line for the "rate of change" calculation.

Once you learn all these Trend Line Commands you basically know the commands that apply to arrows and text too. I have one study whch detects two arrows I place on the screen. It draws two lines based on their location. Lines use to detect stalling prices and trigger my trade. I can move the arrows and highlight the first line to tell the study to redraw. I can Highlight the second line to tell the study to delete both lines and both arrrows. It is very powerful what you can do. You could have the sstudy put text letters out and you activate these to tell the study what to do at any point in time. Kind of like the restaurant touch screen that they use when you order food.

John.

User avatar
geizer
Posts: 375
Joined: 16 Jun 2008
Has thanked: 40 times
Been thanked: 38 times

Postby geizer » 07 Feb 2009

Hi John,
To answer your question: I draw trend lines on the daily charts. Once the stock come close to the trend line it may bounce and form the inflection point. I expect the stock to hesitate near the trend line before either breaking it or bounce back.

After reading your post, I've some ideas how all of these what you previously said can be applied. See the picture. We could use it to determine the inflection points. Once the price closed inside the "uncertainty zone" (point "A"), we do nothing and wait for a signal. If bar closes outside the "uncertainty zone" without ever crossing the trend line (or crossing it even number of times) - then the trend is confirmed (point "B"). Leaving the uncertainty zone through point "C" is an indication of the broken trend.
After the trend is confirmed we would need 15 min intraday charts to look for an entry point. With a proper risk management this should work.
What do you think?
-
Pavel
Attachments
picture.pdf
(29.94 KiB) Downloaded 48 times

bowlesj3
Posts: 2016
Joined: 21 Jul 2007
Has thanked: 171 times
Been thanked: 402 times

Postby bowlesj3 » 07 Feb 2009

Hi Pavel,

since you are using daily bars the TL get commands will get the date you need. Since there are days without trading you can not use the julian calc to determine the number of bars between anchor points nor the bars from the last anchor point point to the current bar. I trink you need to use the binary search shown below. I think it will work on daily bars but I have only used it on minute bars. Run some tests on it. It will give you the offset for each date and that can be used to get your #bars count.

I personally do not use trend lines any more. I found that with my symbol trends and bounces and breaks occur more often on other indicators. Here is an idea to seriously consider because I do not want to suggest any one indicator to use. All indicators have lines that might serve as a bounce line. Find the most popular (do a google search and count the hits, that should give you the most popular). Once you have these create a spread sheet to do your counting. One row for each line of the indicator and maybe a summary row for each indicator. One column for bounces generally and other columns for number of 2 bounces in a row and another column for number of 3 bounces off a line in a row (I have seen up to 8 bounces in a row on one of the indicators I use). Don't do the counting for too long a period. For daily maybe just a week or a month. If the traders are taking bounces off certain lines on the symbol you like to trade you will see it soon enough. Also for some indicators look for levels that swings take place on rather than existing lines. What I means is if the last swing took place at 67 did it have trouble getting past that level the next time (or near it somehow). Try angle lines too (trend lines on the indicator rather than on price). This may only apply to certian indicators that have a number associated with them. I think this exercise may just really open your eyes to new ideas.

John.









Code: Select all


{
Written by John Bowles based off the web page "http://en.wikipedia.org/wiki/Binary_search" modified for EL offset.

Sample Call:
   offset = A_FindOffsetViaBinary(1,BarNumber,Arw1Date,Arw1Time);
   
   See the comments below for each input parameter to know how to run this function
   
   The above call submits the Arrow #1 Date and time along with the last bar in the chart bar number
   and you get the offset from the last bar in the chart where that arrow is located.
   your code can then figure out the high/low/close price data you want from that offset.
}

inputs:
   BarNumberLower(NumericSimple), {Normally value 1 which is the first bar of the chart}
   BarNumberUpper(NumericSimple), {Submit "BarNumber" for the last bar of the chart}
   DateIn(NumericSimple), {Submit the Arrow Date In}
   TimeIn(NumericSimple)  {submit the Arrow Time In}
    ;
variables:
   MyL(0),
   MyN(0),
   MyR(0),
   MyP(0),   {Pointer to the bar which is being changed throughout the code with the binary logic}
   Offset(0)
   ;

MyL = BarNumberLower;
MyN = BarNumberUpper;
MyR = MyN + 1; {Ending-Range is adjusted by 1}
MyP = IntPortion((MyR - MyL)/2); {We apply the binary division trimming off the decimal}


A_FindOffsetViaBinary = 0;
While MyP <> 0
   begin
   
   
   MyP = MyL + MyP;
   Offset = BarNumberUpper - MyP; {MyP is an index from the first bar. Offset is an offset from the last bar}

   If date[Offset] < DateIn or (date[Offset] = DateIn and time[Offset] < TimeIn) then
      begin
      MyL = MyP; {The price bar inspected is too far back so we bring our starting-range(MyL) up to MyP}
      MyP = IntPortion((MyR  - MyL)/2); {We apply the binary division trimming off the decimal}
      end
   else
      begin
      If date[Offset] > DateIn or (date[Offset] = DateIn and time[Offset] > TimeIn) then
         begin
         MyR = MyP; {The price bar just inspected is too far forward so we bring our ending-range(MyR) down to MyP}
         MyP = IntPortion((MyR  - MyL)/2); {We apply the binary division trimming off the decimal}
         end
      else
         begin
         If date[Offset] = DateIn and time[Offset] = TimeIn then
            begin
            {We found the Bar with matching date and time so we set the function output to the correct offset}
            A_FindOffsetViaBinary = BarNumberUpper - MyP;
            {Now that we have what we are looking for we force the loop to stop by setting MyP to zero}
            MyP = 0;
            end;
         end;
      end;

   end;



bowlesj3
Posts: 2016
Joined: 21 Jul 2007
Has thanked: 171 times
Been thanked: 402 times

Postby bowlesj3 » 08 Feb 2009

I forgot to mention that you will need the binary search for the 15 minute bars as well since the calc below for calculating minutes ahead/back will not detect the gaps and will give you the wrong number of bars for your ROC in the trend line. If it did work the method below would be faster since you could divide the result by 15 to get the number of bars but unfortunately there is no cigar on that idea. Binary wins again.

John


Newtime = MinutesToTime(TimeToMinutes(OldTime) - 30);

bowlesj3
Posts: 2016
Joined: 21 Jul 2007
Has thanked: 171 times
Been thanked: 402 times

Postby bowlesj3 » 09 Feb 2009

Because I knew binary was the way to go I didn't give the above command much thought. It would have been

Bars = (TimeToMinutes(AnchorTime2) - TimeToMinutes(AnchorTime1)) / 15.

However even if there were no gaps in time without trading that would not work either because your trend line may cross over from 1 day to the next. It becomes even more likely if you are removing the overnight from your charts. Binary search is the only way to guarantee accuracy. It can handle anything you dish at it.

If you are using bars with seconds you need to modify the binary search.

User avatar
geizer
Posts: 375
Joined: 16 Jun 2008
Has thanked: 40 times
Been thanked: 38 times

Postby geizer » 11 Feb 2009

Hi John,

It took me a while to respond to your post (I am back to day trading after two months of not trading, and I must tell what a great feeling to be back and try to conquer basic emotions - fear and greed.). Anyway, back to the topic.

The binary search you provided is indeed powerful, I can't express how thankful I am for you sharing your expertise and example of the code. I will definitely give it a try.

Thank you very much for your insight into programming.

Warm regards,
Pavel

P.S.
once I get my system up and running I would like to share the result with you, if you don't mind looking at it. It might take me couple three months before I get there, but I will try.

bowlesj3
Posts: 2016
Joined: 21 Jul 2007
Has thanked: 171 times
Been thanked: 402 times

Postby bowlesj3 » 11 Feb 2009

Hi Pavel,

Don't feel too bad if it takes more than 3 months. I am a discretionary trader. I use MC as a workload reduction tool and use a database program as well for my trading. My approach is far too complex to fully automate. Being a programmer for a long time before (and fussy about my programming), it took me 1.6 years to program it all after buying MC. I probably have 30,000 lines of code in total (most in the database program). Only recently do I feel it is truly done. The more you learn the more you tend to want to put into it. I write down every idea immediately and it can overload you.

Regarding the trend lines, I used them for 6 years.
I dumped them for several reasons.
** I just could not trust them for my symbol.
** They are too time consuming for day trading. Speed is important.
** You loose two swings just for anchoring them.
** You don't really know if they are anchored properly at times.
** I started noticing bounces on other indicator lines more and more.

When they do work I figure it is just luck at this point. I am sure a % of traders are using them but it is probably very low. There are other trend lines at work. You have to find them on your own and learn to trust what you find. You can not copy others. If you have to ask for help you are not ready yet. It takes a lot of work and a long time to trust your system. It is the main reason one must believe in "Each to his own".

Regarding strategy, I track multiple levels of waves (all levels) and trade bounces and stalls when the waves line up. That is all really. It is simple but very complex at the same time.

Good Luck.

John.

P.S. when TTS send around the idea of putting out a case study I thought I would create one since I use MC different than most of the traders who try to fully automate and also to help TSS get sales since they have a great product (I am very appreciative) and helping keeping them in business helps me too. It has some ideas you might be interested in.

bowlesj3
Posts: 2016
Joined: 21 Jul 2007
Has thanked: 171 times
Been thanked: 402 times

Postby bowlesj3 » 17 Feb 2009

Hi Pavel,

I was just doing a scan through the editor for functions that may be of use. I noticed some that may be of value to you regarding your original post. They are TLslop, TLvalue and TLangle. Basically anything that starts with TL. You might want to study then in detail.

John.


Return to “MultiCharts”