Re: [amibroker] Re: How to backtest or plot a script, which uses SelectedValue or LastValue?
Hello, Yes the answer was: SelectedValue and LastValue functions are used because the point(s) were you draw trendline is subjective matter and these functions are used to establish a pattern location MANUALLY and subjectively. Removing those functions would involve first replacing subjective decision by some objective, mathematically strict criterion. Since all patterns are highly subjective and only seen after the fact, deriving reliable, mathematically strict and not-looking-into-the-future method is IMHO not feasible. If someone thinks otherwise, it is upto given person to code his thoughts by him/herself. If you can not code it yourself you can purchase such AFL codes from www.patternexplorer.com Best regards, Tomasz Janeczko amibroker.com On 2010-04-30 18:09, Bisto wrote: Hi guys, maybe I lost the answer among all the zig zag focused posts... the starting question how to backtest an afl using LastValue? had an answer? Does exist a way different to recode without LastValue? thanks Bisto --- In amibroker@yahoogroups.com, jhnlmnjhn...@... wrote: I was exploring various trading strategies and custom indicators on the Web and saw several interesting examples in the AmiBroker library and this forum, in particular Head Shoulders Pattern and Historical Trendlines and Breakouts at http://finance.groups.yahoo.com/group/amibroker/message/50689 But the problem is that these scripts use either SelectedValue or LastValue functions, which restrict script execution to a single point only (either to the selected bar or to the last bar). As the result, these scripts cannot be used for backtesting or as an indicator (because a good indicator should display a value for each bar, not only for a single one). I see that Dimitris Tsokakis wrote Move the cursor [to the left or to the right] to see the formation changes.. But this is not a workable solution! It will take years for me to manually move custor over thousands of stocks in the database! I need some automated way to backtest these scripts. Basically, I need to make a loop and assign either the SelectedValue or LastValue programmatically, then invoke the script as a function. But these values appear to be read only. For example, SelectedValue = 123; generate an error. Any suggestions? Thank you John IMPORTANT PLEASE READ This group is for the discussion between users only. This is *NOT* technical support channel. TO GET TECHNICAL SUPPORT send an e-mail directly to SUPPORT {at} amibroker.com TO SUBMIT SUGGESTIONS please use FEEDBACK CENTER at http://www.amibroker.com/feedback/ (submissions sent via other channels won't be considered) For NEW RELEASE ANNOUNCEMENTS and other news always check DEVLOG: http://www.amibroker.com/devlog/ Yahoo! Groups Links
Re: [amibroker] Re: How to backtest or plot a script, which uses SelectedValue or LastValue?
Hello, As written in the manual: http://www.amibroker.com/f?lastvalue Returns last calculated value of the specified ARRAY [..] Caveat: since this function fills an entire data array with the last value of another array, it allows a formula to look into the future. So if your chart (or automatic analysis) period ends at April 30, 2010, the LastValue will give you the value from that day (April 30, 2010). If you use it for any trading rule, it will mean that for all bars except today you are using future data. The key part is for any trading rule. If you use LastValue for chart scaling - that is not a problem. If you use LastValue for debugging - no problem, if you use LastValue for commentary / interpretation - it is OK, for anything that is NOT a trading rule. By the way: use AFL Formula Editor, Tools-Code Check And Profile and AmiBroker will tell you how many past and future bars are required to calculate given formula. The formulas that do not look into the future will report that they use 0 (ZERO) future bars. Best regards, Tomasz Janeczko amibroker.com On 2010-04-30 22:04, Bisto wrote: Thanks Tomasz for the fast reply, by the way I am not involved in the pattern recognition issue with the looking in the future problem. I fully agree with you about your consideration replacing subjective decision by mathematically strict criterion I just asked in the general way to understand if it is possible to backtest a system using LastValue function somewhere. My fear is that during the backtest it will produce a future leak because I don't know if AB will consider lastvalue as the last value of all the period or day by day (bar by bar). I suppose that lastvalue will give the true lastvalue of ALL the period producing a future leak. In other words: to have a system with a LastValue is equal to have a system with Zig or Peak -- no backtest is possible! Is it correct? It was just a doubt that I had reading the forum, I didn't perform any test on it --- In amibroker@yahoogroups.com, Tomasz Janeczkogro...@... wrote: Hello, Yes the answer was: SelectedValue and LastValue functions are used because the point(s) were you draw trendline is subjective matter and these functions are used to establish a pattern location MANUALLY and subjectively. Removing those functions would involve first replacing subjective decision by some objective, mathematically strict criterion. Since all patterns are highly subjective and only seen after the fact, deriving reliable, mathematically strict and not-looking-into-the-future method is IMHO not feasible. If someone thinks otherwise, it is upto given person to code his thoughts by him/herself. If you can not code it yourself you can purchase such AFL codes from www.patternexplorer.com Best regards, Tomasz Janeczko amibroker.com On 2010-04-30 18:09, Bisto wrote: Hi guys, maybe I lost the answer among all the zig zag focused posts... the starting question how to backtest an afl using LastValue? had an answer? Does exist a way different to recode without LastValue? thanks Bisto --- In amibroker@yahoogroups.com, jhnlmnjhnlmn@ wrote: I was exploring various trading strategies and custom indicators on the Web and saw several interesting examples in the AmiBroker library and this forum, in particular Head Shoulders Pattern and Historical Trendlines and Breakouts at http://finance.groups.yahoo.com/group/amibroker/message/50689 But the problem is that these scripts use either SelectedValue or LastValue functions, which restrict script execution to a single point only (either to the selected bar or to the last bar). As the result, these scripts cannot be used for backtesting or as an indicator (because a good indicator should display a value for each bar, not only for a single one). I see that Dimitris Tsokakis wrote Move the cursor [to the left or to the right] to see the formation changes.. But this is not a workable solution! It will take years for me to manually move custor over thousands of stocks in the database! I need some automated way to backtest these scripts. Basically, I need to make a loop and assign either the SelectedValue or LastValue programmatically, then invoke the script as a function. But these values appear to be read only. For example, SelectedValue = 123; generate an error. Any suggestions? Thank you John IMPORTANT PLEASE READ This group is for the discussion between users only. This is *NOT* technical support channel. TO GET TECHNICAL SUPPORT send an e-mail directly to SUPPORT {at} amibroker.com TO SUBMIT SUGGESTIONS please use FEEDBACK CENTER at http://www.amibroker.com/feedback/ (submissions sent via other channels won't be considered) For NEW RELEASE ANNOUNCEMENTS and other news always check DEVLOG: http://www.amibroker.com/devlog/
Re: [amibroker] Re: How to backtest or plot a script, which uses SelectedValue or LastValue?
Tomasz how do I calculate this 'Expected Lag' in Bars? So how do I know when the Peak/Through is true ? Regards, Ton. - Original Message - From: Tomasz Janeczko To: amibroker@yahoogroups.com Sent: Tuesday, April 27, 2010 12:59 AM Subject: Re: [amibroker] Re: How to backtest or plot a script, which uses SelectedValue or LastValue? Hello, The script never looks after the BAR, but, of course, it does look after the peak if the peak is in the past. This is *exactly* looking into the future. You can only claim a peak when you look several bars AFTER the peak - that is looking into the future in purest form. You don't know that the PEAK occurred until many bars AFTER the fact. You can call it expected lag and I am calling it looking into the future, both having same net effect - the result is useless for profitable trading, as useless as knowing what kind of market cycle we had one year ago. You can not profit from the information that is several bars (days) old. For me this ends this purely academic discussion. Best regards, Tomasz Janeczko amibroker.com On 2010-04-26 23:43, jhnlmn wrote: Hi, You admit that you are complete novice, yet you make definitive statements ... Sure. This is because I wrote a script, which generates exactly the same output as Peak/Trough in WL. So, I know for sure that this script does not use a Zigzag (WL simply does not have this indicator) and it does not look into the future, that is when I call TroughBars[bar] I do know that it does not use any bars with indexes bar. So, it does not look into the future, but have some lag, which is expected. Please, correct me if I am wrong. (i.e. you must look at bars that occur AFTER THE PEAK) and find out that the price moved in opposite Sure, but I never look AFTER THE BAR when I call TroughBars[bar]. Therefore, my script does not look into the future. That is, it will generate exactly the same answer no matter whether bar is the last in the series or some time later when bar will be in the middle of historical data. Finding that reversal happens is where looking into the future occurs. No. The script never looks after the BAR, but, of course, it does look after the peak if the peak is in the past. *IS* actually zig-zag algorithm and that peak and trough functions are _products_ of zig-zag algorithm. This may be true, but it is not really important. It does not matter how a particular trading program chose to name each function. It is more important what exactly the function does, in particular whether it does look into the future, that is when I call TroughBars[bar] will it look after the bar or not? And here, if I understood you correctly, AB is profoundly different from WL because in WL TroughBars[bar] does NOT touch any data after bar, but AB does. You can not simply remove SelectedValue from such formula because it relied on manual input Of course. I stated this in my original question. In order to remove SelectedValue you would need to come up first with conditions that say when to start and when to end the trendline because it is not always obvious. I already did that in my post http://finance.groups.yahoo.com/group/amibroker/message/148924 I, as well as other users, found it very difficult to understand AB array-based language, so it is very likely that I made some mistake in my rewritten scripts. So, I would appreciate if somebody with experience will comment on it. Thank you John IMPORTANT PLEASE READ This group is for the discussion between users only. This is *NOT* technical support channel. TO GET TECHNICAL SUPPORT send an e-mail directly to SUPPORT {at} amibroker.com TO SUBMIT SUGGESTIONS please use FEEDBACK CENTER at http://www.amibroker.com/feedback/ (submissions sent via other channels won't be considered) For NEW RELEASE ANNOUNCEMENTS and other news always check DEVLOG: http://www.amibroker.com/devlog/ Yahoo! Groups Links
Re: [amibroker] Re: How to backtest or plot a script, which uses SelectedValue or LastValue?
Hello, This has been answered already in my previous response in this thread: http://finance.groups.yahoo.com/group/amibroker/message/148933 (It was back in 2003 http://www.amibroker.com/members/traders/11-2003.html when the formula was provided in the Traders' Tips section that: a) shows how to use Zig/Peak/Trough SAFELY in backtest b) provides actual code that marks points where you know when Peak/Trough is true The 'expected lag' is dynamic and is given by distance between peak/trough and the signal in ZZT variable. ZZT variable crosses zero when peak / trough is guaranteed. Such signals are backtest-safe. Once again the formula: |array = *Close*; amount = Param(Amount, 15.5, 1, 50, 0.5 ); zz0 = Zig( array, amount ); zz1 = Ref( zz0, -1 ); zz2 = Ref( zz0, -2 ); tr = ValueWhen(zz0 zz1 *AND* zz1 zz2, zz1); pk = ValueWhen(zz0 zz1 *AND* zz1 zz2, zz1); PU = tr + 0.01 * abs(tr)*amount; PD = pk - 0.01 * abs(pk)*amount; ZZT = IIf( array = PU *AND* zz0 zz1, 1, IIf( array = PD *AND* zz0 zz1, -1, 0 ) ); ZZT = ValueWhen( ZZT != 0, ZZT ); // plot price bar chart Plot( *Close*, Price, *colorBlack*, *styleBar* ); // plot Zigzag and zigzag trend Plot( ZZT, ZigZagTrend, *colorRed*, *styleOwnScale* ); Plot( zz0, ZigZag line, *colorBlue*, *styleThick* ); // Plot the ribbon ribboncol= IIf( ZZT 0, *colorGreen*, *colorRed* ); Plot( 2, ZZT Ribbon, ribboncol, *styleArea* | *styleOwnScale* | *styleNoLabel*, 0, 100 ); *GraphXSpace* = 10; *Buy* = *Cover* = Cross( ZZT, 0 ); *Sell* = *Short* = Cross( 0, ZZT ); // plot arrows PlotShapes( *Buy* + 2 * *Sell*, ribboncol, 0, IIf( *Buy*, *L*, *H* ), -30 ); PlotShapes( *Buy* + 2 * *Sell*, ribboncol, 0, IIf( *Buy*, *L*, *H* ), -30 );| Best regards, Tomasz Janeczko amibroker.com On 2010-04-27 08:41, Ton Sieverding wrote: Tomasz how do I calculate this 'Expected Lag' in Bars? So how do I know when the Peak/Through is true ? Regards, Ton. - Original Message - *From:* Tomasz Janeczko mailto:gro...@amibroker.com *To:* amibroker@yahoogroups.com mailto:amibroker@yahoogroups.com *Sent:* Tuesday, April 27, 2010 12:59 AM *Subject:* Re: [amibroker] Re: How to backtest or plot a script, which uses SelectedValue or LastValue? Hello, The script never looks after the BAR, but, of course, it does look after the peak if the peak is in the past. This is *exactly* looking into the future. You can only claim a peak when you look several bars AFTER the peak - that is looking into the future in purest form. You don't know that the PEAK occurred until many bars AFTER the fact. You can call it expected lag and I am calling it looking into the future, both having same net effect - the result is useless for profitable trading, as useless as knowing what kind of market cycle we had one year ago. You can not profit from the information that is several bars (days) old. For me this ends this purely academic discussion. Best regards, Tomasz Janeczko amibroker.com On 2010-04-26 23:43, jhnlmn wrote: Hi, You admit that you are complete novice, yet you make definitive statements ... Sure. This is because I wrote a script, which generates exactly the same output as Peak/Trough in WL. So, I know for sure that this script does not use a Zigzag (WL simply does not have this indicator) and it does not look into the future, that is when I call TroughBars[bar] I do know that it does not use any bars with indexes bar. So, it does not look into the future, but have some lag, which is expected. Please, correct me if I am wrong. (i.e. you must look at bars that occur AFTER THE PEAK) and find out that the price moved in opposite Sure, but I never look AFTER THE BAR when I call TroughBars[bar]. Therefore, my script does not look into the future. That is, it will generate exactly the same answer no matter whether bar is the last in the series or some time later when bar will be in the middle of historical data. Finding that reversal happens is where looking into the future occurs. No. The script never looks after the BAR, but, of course, it does look after the peak if the peak is in the past. *IS* actually zig-zag algorithm and that peak and trough functions are _products_ of zig-zag algorithm. This may be true, but it is not really important. It does not matter how a particular trading program chose to name each function. It is more important what exactly the function does, in particular whether it does look into the future, that is when I call TroughBars[bar] will it look after the bar or not? And here, if I understood you correctly, AB is profoundly different from WL because in WL TroughBars[bar] does NOT touch any data after bar, but AB does. You can not
Re: [amibroker] Re: How to backtest or plot a script, which uses SelectedValue or LastValue?
Thanks Tomasz. Yes I've found that thread. I still have problems with the 'Amount' variable for the ZIG. Should not this be a percentage ? Anyway I have got my 'Expected Lag' in Bars. Thanks again ... Regards, Ton. - Original Message - From: Tomasz Janeczko To: amibroker@yahoogroups.com Sent: Tuesday, April 27, 2010 11:43 AM Subject: Re: [amibroker] Re: How to backtest or plot a script, which uses SelectedValue or LastValue? Hello, This has been answered already in my previous response in this thread: http://finance.groups.yahoo.com/group/amibroker/message/148933 (It was back in 2003 http://www.amibroker.com/members/traders/11-2003.html when the formula was provided in the Traders' Tips section that: a) shows how to use Zig/Peak/Trough SAFELY in backtest b) provides actual code that marks points where you know when Peak/Trough is true The 'expected lag' is dynamic and is given by distance between peak/trough and the signal in ZZT variable. ZZT variable crosses zero when peak / trough is guaranteed. Such signals are backtest-safe. Once again the formula: array = Close; amount = Param(Amount, 15.5, 1, 50, 0.5 ); zz0 = Zig( array, amount ); zz1 = Ref( zz0, -1 ); zz2 = Ref( zz0, -2 ); tr = ValueWhen(zz0 zz1 AND zz1 zz2, zz1); pk = ValueWhen(zz0 zz1 AND zz1 zz2, zz1); PU = tr + 0.01 * abs(tr)*amount; PD = pk - 0.01 * abs(pk)*amount; ZZT = IIf( array = PU AND zz0 zz1, 1, IIf( array = PD AND zz0 zz1, -1, 0 ) ); ZZT = ValueWhen( ZZT != 0, ZZT ); // plot price bar chart Plot( Close, Price, colorBlack, styleBar ); // plot Zigzag and zigzag trend Plot( ZZT, ZigZagTrend, colorRed, styleOwnScale ); Plot( zz0, ZigZag line, colorBlue, styleThick ); // Plot the ribbon ribboncol= IIf( ZZT 0, colorGreen, colorRed ); Plot( 2, ZZT Ribbon, ribboncol, styleArea | styleOwnScale | styleNoLabel, 0, 100 ); GraphXSpace = 10; Buy = Cover = Cross( ZZT, 0 ); Sell = Short = Cross( 0, ZZT ); // plot arrows PlotShapes( Buy + 2 * Sell, ribboncol, 0, IIf( Buy, L, H ), -30 ); PlotShapes( Buy + 2 * Sell, ribboncol, 0, IIf( Buy, L, H ), -30 ); Best regards, Tomasz Janeczko amibroker.com On 2010-04-27 08:41, Ton Sieverding wrote: Tomasz how do I calculate this 'Expected Lag' in Bars? So how do I know when the Peak/Through is true ? Regards, Ton. - Original Message - From: Tomasz Janeczko To: amibroker@yahoogroups.com Sent: Tuesday, April 27, 2010 12:59 AM Subject: Re: [amibroker] Re: How to backtest or plot a script, which uses SelectedValue or LastValue? Hello, The script never looks after the BAR, but, of course, it does look after the peak if the peak is in the past. This is *exactly* looking into the future. You can only claim a peak when you look several bars AFTER the peak - that is looking into the future in purest form. You don't know that the PEAK occurred until many bars AFTER the fact. You can call it expected lag and I am calling it looking into the future, both having same net effect - the result is useless for profitable trading, as useless as knowing what kind of market cycle we had one year ago. You can not profit from the information that is several bars (days) old. For me this ends this purely academic discussion. Best regards, Tomasz Janeczko amibroker.com On 2010-04-26 23:43, jhnlmn wrote: Hi, You admit that you are complete novice, yet you make definitive statements ... Sure. This is because I wrote a script, which generates exactly the same output as Peak/Trough in WL. So, I know for sure that this script does not use a Zigzag (WL simply does not have this indicator) and it does not look into the future, that is when I call TroughBars[bar] I do know that it does not use any bars with indexes bar. So, it does not look into the future, but have some lag, which is expected. Please, correct me if I am wrong. (i.e. you must look at bars that occur AFTER THE PEAK) and find out that the price moved in opposite Sure, but I never look AFTER THE BAR when I call TroughBars[bar]. Therefore, my script does not look into the future. That is, it will generate exactly the same answer no matter whether bar is the last in the series or some time later when bar will be in the middle of historical data. Finding that reversal happens is where looking into the future occurs. No. The script never looks after the BAR, but, of course, it does look after the peak if the peak is in the past. *IS* actually zig-zag algorithm and that peak and trough functions are _products_ of zig-zag algorithm.
Re: [amibroker] Re: How to backtest or plot a script, which uses SelectedValue or LastValue?
Hello, Yes 'amount' is expressed in percent. For example 15.5 set via Params window means 15.5%. Best regards, Tomasz Janeczko amibroker.com On 2010-04-27 12:23, Ton Sieverding wrote: Thanks Tomasz. Yes I've found that thread. I still have problems with the 'Amount' variable for the ZIG. Should not this be a percentage ? Anyway I have got my 'Expected Lag' in Bars. Thanks again ... Regards, Ton. - Original Message - *From:* Tomasz Janeczko mailto:gro...@amibroker.com *To:* amibroker@yahoogroups.com mailto:amibroker@yahoogroups.com *Sent:* Tuesday, April 27, 2010 11:43 AM *Subject:* Re: [amibroker] Re: How to backtest or plot a script, which uses SelectedValue or LastValue? Hello, This has been answered already in my previous response in this thread: http://finance.groups.yahoo.com/group/amibroker/message/148933 (It was back in 2003 http://www.amibroker.com/members/traders/11-2003.html when the formula was provided in the Traders' Tips section that: a) shows how to use Zig/Peak/Trough SAFELY in backtest b) provides actual code that marks points where you know when Peak/Trough is true The 'expected lag' is dynamic and is given by distance between peak/trough and the signal in ZZT variable. ZZT variable crosses zero when peak / trough is guaranteed. Such signals are backtest-safe. Once again the formula: |array = *Close*; amount = Param(Amount, 15.5, 1, 50, 0.5 ); zz0 = Zig( array, amount ); zz1 = Ref( zz0, -1 ); zz2 = Ref( zz0, -2 ); tr = ValueWhen(zz0 zz1 *AND* zz1 zz2, zz1); pk = ValueWhen(zz0 zz1 *AND* zz1 zz2, zz1); PU = tr + 0.01 * abs(tr)*amount; PD = pk - 0.01 * abs(pk)*amount; ZZT = IIf( array = PU *AND* zz0 zz1, 1, IIf( array = PD *AND* zz0 zz1, -1, 0 ) ); ZZT = ValueWhen( ZZT != 0, ZZT ); // plot price bar chart Plot( *Close*, Price, *colorBlack*, *styleBar* ); // plot Zigzag and zigzag trend Plot( ZZT, ZigZagTrend, *colorRed*, *styleOwnScale* ); Plot( zz0, ZigZag line, *colorBlue*, *styleThick* ); // Plot the ribbon ribboncol= IIf( ZZT 0, *colorGreen*, *colorRed* ); Plot( 2, ZZT Ribbon, ribboncol, *styleArea* | *styleOwnScale* | *styleNoLabel*, 0, 100 ); *GraphXSpace* = 10; *Buy* = *Cover* = Cross( ZZT, 0 ); *Sell* = *Short* = Cross( 0, ZZT ); // plot arrows PlotShapes( *Buy* + 2 * *Sell*, ribboncol, 0, IIf( *Buy*, *L*, *H* ), -30 ); PlotShapes( *Buy* + 2 * *Sell*, ribboncol, 0, IIf( *Buy*, *L*, *H* ), -30 );| Best regards, Tomasz Janeczko amibroker.com On 2010-04-27 08:41, Ton Sieverding wrote: Tomasz how do I calculate this 'Expected Lag' in Bars? So how do I know when the Peak/Through is true ? Regards, Ton. - Original Message - *From:* Tomasz Janeczko mailto:gro...@amibroker.com *To:* amibroker@yahoogroups.com mailto:amibroker@yahoogroups.com *Sent:* Tuesday, April 27, 2010 12:59 AM *Subject:* Re: [amibroker] Re: How to backtest or plot a script, which uses SelectedValue or LastValue? Hello, The script never looks after the BAR, but, of course, it does look after the peak if the peak is in the past. This is *exactly* looking into the future. You can only claim a peak when you look several bars AFTER the peak - that is looking into the future in purest form. You don't know that the PEAK occurred until many bars AFTER the fact. You can call it expected lag and I am calling it looking into the future, both having same net effect - the result is useless for profitable trading, as useless as knowing what kind of market cycle we had one year ago. You can not profit from the information that is several bars (days) old. For me this ends this purely academic discussion. Best regards, Tomasz Janeczko amibroker.com On 2010-04-26 23:43, jhnlmn wrote: Hi, You admit that you are complete novice, yet you make definitive statements ... Sure. This is because I wrote a script, which generates exactly the same output as Peak/Trough in WL. So, I know for sure that this script does not use a Zigzag (WL simply does not have this indicator) and it does not look into the future, that is when I call TroughBars[bar] I do know that it does not use any bars with indexes bar. So, it does not look into the future, but have some lag, which is expected. Please, correct me if I am wrong. (i.e. you must look at bars that occur AFTER THE PEAK) and find out that the price moved in opposite Sure, but I never look
Re: [amibroker] Re: How to backtest or plot a script, which uses SelectedValue or LastValue?
OK and thanks ... Ton. - Original Message - From: Tomasz Janeczko To: amibroker@yahoogroups.com Sent: Tuesday, April 27, 2010 12:29 PM Subject: Re: [amibroker] Re: How to backtest or plot a script, which uses SelectedValue or LastValue? Hello, Yes 'amount' is expressed in percent. For example 15.5 set via Params window means 15.5%. Best regards, Tomasz Janeczko amibroker.com On 2010-04-27 12:23, Ton Sieverding wrote: Thanks Tomasz. Yes I've found that thread. I still have problems with the 'Amount' variable for the ZIG. Should not this be a percentage ? Anyway I have got my 'Expected Lag' in Bars. Thanks again ... Regards, Ton. - Original Message - From: Tomasz Janeczko To: amibroker@yahoogroups.com Sent: Tuesday, April 27, 2010 11:43 AM Subject: Re: [amibroker] Re: How to backtest or plot a script, which uses SelectedValue or LastValue? Hello, This has been answered already in my previous response in this thread: http://finance.groups.yahoo.com/group/amibroker/message/148933 (It was back in 2003 http://www.amibroker.com/members/traders/11-2003.html when the formula was provided in the Traders' Tips section that: a) shows how to use Zig/Peak/Trough SAFELY in backtest b) provides actual code that marks points where you know when Peak/Trough is true The 'expected lag' is dynamic and is given by distance between peak/trough and the signal in ZZT variable. ZZT variable crosses zero when peak / trough is guaranteed. Such signals are backtest-safe. Once again the formula: array = Close; amount = Param(Amount, 15.5, 1, 50, 0.5 ); zz0 = Zig( array, amount ); zz1 = Ref( zz0, -1 ); zz2 = Ref( zz0, -2 ); tr = ValueWhen(zz0 zz1 AND zz1 zz2, zz1); pk = ValueWhen(zz0 zz1 AND zz1 zz2, zz1); PU = tr + 0.01 * abs(tr)*amount; PD = pk - 0.01 * abs(pk)*amount; ZZT = IIf( array = PU AND zz0 zz1, 1, IIf( array = PD AND zz0 zz1, -1, 0 ) ); ZZT = ValueWhen( ZZT != 0, ZZT ); // plot price bar chart Plot( Close, Price, colorBlack, styleBar ); // plot Zigzag and zigzag trend Plot( ZZT, ZigZagTrend, colorRed, styleOwnScale ); Plot( zz0, ZigZag line, colorBlue, styleThick ); // Plot the ribbon ribboncol= IIf( ZZT 0, colorGreen, colorRed ); Plot( 2, ZZT Ribbon, ribboncol, styleArea | styleOwnScale | styleNoLabel, 0, 100 ); GraphXSpace = 10; Buy = Cover = Cross( ZZT, 0 ); Sell = Short = Cross( 0, ZZT ); // plot arrows PlotShapes( Buy + 2 * Sell, ribboncol, 0, IIf( Buy, L, H ), -30 ); PlotShapes( Buy + 2 * Sell, ribboncol, 0, IIf( Buy, L, H ), -30 ); Best regards, Tomasz Janeczko amibroker.com On 2010-04-27 08:41, Ton Sieverding wrote: Tomasz how do I calculate this 'Expected Lag' in Bars? So how do I know when the Peak/Through is true ? Regards, Ton. - Original Message - From: Tomasz Janeczko To: amibroker@yahoogroups.com Sent: Tuesday, April 27, 2010 12:59 AM Subject: Re: [amibroker] Re: How to backtest or plot a script, which uses SelectedValue or LastValue? Hello, The script never looks after the BAR, but, of course, it does look after the peak if the peak is in the past. This is *exactly* looking into the future. You can only claim a peak when you look several bars AFTER the peak - that is looking into the future in purest form. You don't know that the PEAK occurred until many bars AFTER the fact. You can call it expected lag and I am calling it looking into the future, both having same net effect - the result is useless for profitable trading, as useless as knowing what kind of market cycle we had one year ago. You can not profit from the information that is several bars (days) old. For me this ends this purely academic discussion. Best regards, Tomasz Janeczko amibroker.com On 2010-04-26 23:43, jhnlmn wrote: Hi, You admit that you are complete novice, yet you make definitive statements ... Sure. This is because I wrote a script, which generates exactly the same output as Peak/Trough in WL. So, I know for sure that this script does not use a Zigzag (WL simply does not have this indicator) and it does not look into the future, that is when I call TroughBars[bar] I do know that it does not use any bars with indexes bar. So, it does not look into the future, but have some lag, which is expected. Please,
Re: [amibroker] Re: How to backtest or plot a script, which uses SelectedValue or LastValue?
Hello, jhnlmn wrote: Yes. This is a big problem in AB (in my opinion). In WL I execute the script only once and it keeps the graph and all indicators in memory, so I can view them without rerunning But AB reruns the script each time I click the graph, which makes viewing very slow. What utter non-sense is that? First of all AmiBroker is many times FASTER than Wealth Lab. Independent tests shows that AmiBroker is over 30 times faster than Wealth Lab: http://www.addictfx.over-blog.com/article-60.html (See exacution time 2s in AmiBroker, 1 minute 5 seconds in Wealth Lab) (BTW: since that comparison backtest speed in AMiBroker has been improved) Second, Wealth Lab does NOT refresh indicators in real time every tick (like AmiBroker). Wealth Lab refreshes indicators only when bar is COMPLETE (once a minute) and only PRICE BAR (so called ghost bar) is drawn in real time. Quote from Wealth-Lab User guide Features of Real-time chart scripts: Ghost bar - for real-time providers that support this feature the ghost bar is the last bar at the far right edge of the real-time chart. As new data comes in the ghost bar updates dynamically[...] Trading sytem rules and indicators ARE NOT updated as ghost bar value changes. Instead indicators are updated and the chart is automatically executed only when new bar has completed The only reason for WL to keep the indicators in memory is the fact that it is so slow. So, you are comparing Wealth Lab that executes the formula ONCE PER MINUTE (on minute chart) to AMIBroker that can refresh its charts as often TEN TIMES PER SECOND. So facts are as follows: - AmiBroker runs formulas about 30 times faster than Wealth Lab - AmiBroker refreshes ALL INDICATORS in REAL TIME, while WL only refrehes once bar is complete (once a minute on 1 minute chart and once an hour on hourly chart) You should also read this: http://www.amibroker.com/kb/2008/07/03/quickafl/ to learn that AmiBroker DOES NOT calculate indicator for *all* bars, but just VISIBLE bars. Also, in AmiBroker, if you wish you can also calculate once and store indicator values in static variables. But in AmiBroker you have THE CHOICE. It gives you ultra fast language that is able to actually refresh all indicators in real time without resorting to pathetic workarounds like 'ghost bar' but if you want to prevent your very complex calculations from being re-triggered you can do so too - thats what static variables are for. Best regards, Tomasz Janeczko amibroker.com On 2010-04-28 00:42, jhnlmn wrote:
Re: [amibroker] Re: How to backtest or plot a script, which uses SelectedValue or LastValue?
Hello, You still don't understand anything about zigzag algorithm. If you did you will know that to generate zig you must look into future bars. I am saying that Wealth Lab use *THE SAME* algorithm as ANY other software, there is no magic and no secret in it. First it calculates REGULAR zig zag (identical as in Metastock, AmiBroker and thousands of other softwares) then it applies DELAY. To implement regular zigzag Wealth Lab LOOKS into the future. But later the looking into the future is removed by DELAYING response as many bars as it looks into the future. The same delay can be applied to any zig-based function and the implementation of such delayed peak/trough/zig is shown in this article: http://www.amibroker.com/members/traders/11-2003.html Use this formula and you will see: |array = *Close*; amount = Param(Amount, 15.5, 1, 50, 0.5 ); zz0 = Zig( array, amount ); zz1 = Ref( zz0, -1 ); zz2 = Ref( zz0, -2 ); tr = ValueWhen(zz0 zz1 *AND* zz1 zz2, zz1); pk = ValueWhen(zz0 zz1 *AND* zz1 zz2, zz1); PU = tr + 0.01 * abs(tr)*amount; PD = pk - 0.01 * abs(pk)*amount; ZZT = IIf( array = PU *AND* zz0 zz1, 1, IIf( array = PD *AND* zz0 zz1, -1, 0 ) ); ZZT = ValueWhen( ZZT != 0, ZZT ); // plot price bar chart Plot( *Close*, Price, *colorBlack*, *styleBar* ); // plot Zigzag and zigzag trend Plot( ZZT, ZigZagTrend, *colorRed*, *styleOwnScale* ); Plot( zz0, ZigZag line, *colorBlue*, *styleThick* ); // Plot the ribbon ribboncol= IIf( ZZT 0, *colorGreen*, *colorRed* ); Plot( 2, ZZT Ribbon, ribboncol, *styleArea* | *styleOwnScale* | *styleNoLabel*, 0, 100 ); *GraphXSpace* = 10; *Buy* = *Cover* = Cross( ZZT, 0 ); *Sell* = *Short* = Cross( 0, ZZT ); // plot arrows PlotShapes( *Buy* + 2 * *Sell*, ribboncol, 0, IIf( *Buy*, *L*, *H* ), -30 ); PlotShapes( *Buy* + 2 * *Sell*, ribboncol, 0, IIf( *Buy*, *L*, *H* ), -30 );| (Blue line represents undelayed zig/zag, while RED LINE shows DELAYED signal and the RED LINE indicator DOES NOT LOOK into the future, even though it is based on zig). This shows precisely how it is done to have no look into the future effect - take the indicator that looks into the future and DELAY it as many bars as it looks into the future (note that delay amount CHANGES from leg to leg because it waits until new leg reaches threshold level). This also shows how to use zig/peak/trough in a trading system. Best regards, Tomasz Janeczko amibroker.com PS. Removing my earlier response and repeating your post four times does not add any strength to your argument. On 2010-04-26 04:51, jhnlmn wrote: Clearly you did not read the manual Why not? I do understand the algo of peak/trough in WL. It simply searches for min/max before a reversal. Obviously, if the reversal did not happen yet, then the peak/trough is not registered and the previous peak/trough is reported. This lag is natural and perfectly makes sense to me. And it works well when drawing trendlines, since trendlines should take into account historical data (have some lag). I read advices on the Web not to take the most recent data when drawing trendlines. And, of course, never look into the future, which means that the result should be the same, no matter whether the current bar is the last in the dataset or not. You are saying that AB zigzag can be used only for pattern recognition and not for trading, but what is the purpose of developing a pattern recognition script, which could not be used for real trading or verified using backtesting? This is my understanding. However, I am new to technical analysis, so, please, correct me if I am wrong. Thank you John IMPORTANT PLEASE READ This group is for the discussion between users only. This is *NOT* technical support channel. TO GET TECHNICAL SUPPORT send an e-mail directly to SUPPORT {at} amibroker.com TO SUBMIT SUGGESTIONS please use FEEDBACK CENTER at http://www.amibroker.com/feedback/ (submissions sent via other channels won't be considered) For NEW RELEASE ANNOUNCEMENTS and other news always check DEVLOG: http://www.amibroker.com/devlog/ Yahoo! Groups Links
Re: [amibroker] Re: How to backtest or plot a script, which uses SelectedValue or LastValue?
See also this: http://stockcharts.com/school/doku.php?id=chart_school:technical_indicators:zigzag Take closer look at this sentence The ZigZag has zero predictive power and draws lines base on hindsight. (hindsight in technical analysis is equivalent to looking into the future). On 2010-04-26 11:22, Tomasz Janeczko wrote: Hello, You still don't understand anything about zigzag algorithm. If you did you will know that to generate zig you must look into future bars. I am saying that Wealth Lab use *THE SAME* algorithm as ANY other software, there is no magic and no secret in it. First it calculates REGULAR zig zag (identical as in Metastock, AmiBroker and thousands of other softwares) then it applies DELAY. To implement regular zigzag Wealth Lab LOOKS into the future. But later the looking into the future is removed by DELAYING response as many bars as it looks into the future. The same delay can be applied to any zig-based function and the implementation of such delayed peak/trough/zig is shown in this article: http://www.amibroker.com/members/traders/11-2003.html Use this formula and you will see: |array = *Close*; amount = Param(Amount, 15.5, 1, 50, 0.5 ); zz0 = Zig( array, amount ); zz1 = Ref( zz0, -1 ); zz2 = Ref( zz0, -2 ); tr = ValueWhen(zz0 zz1 *AND* zz1 zz2, zz1); pk = ValueWhen(zz0 zz1 *AND* zz1 zz2, zz1); PU = tr + 0.01 * abs(tr)*amount; PD = pk - 0.01 * abs(pk)*amount; ZZT = IIf( array = PU *AND* zz0 zz1, 1, IIf( array = PD *AND* zz0 zz1, -1, 0 ) ); ZZT = ValueWhen( ZZT != 0, ZZT ); // plot price bar chart Plot( *Close*, Price, *colorBlack*, *styleBar* ); // plot Zigzag and zigzag trend Plot( ZZT, ZigZagTrend, *colorRed*, *styleOwnScale* ); Plot( zz0, ZigZag line, *colorBlue*, *styleThick* ); // Plot the ribbon ribboncol= IIf( ZZT 0, *colorGreen*, *colorRed* ); Plot( 2, ZZT Ribbon, ribboncol, *styleArea* | *styleOwnScale* | *styleNoLabel*, 0, 100 ); *GraphXSpace* = 10; *Buy* = *Cover* = Cross( ZZT, 0 ); *Sell* = *Short* = Cross( 0, ZZT ); // plot arrows PlotShapes( *Buy* + 2 * *Sell*, ribboncol, 0, IIf( *Buy*, *L*, *H* ), -30 ); PlotShapes( *Buy* + 2 * *Sell*, ribboncol, 0, IIf( *Buy*, *L*, *H* ), -30 );| (Blue line represents undelayed zig/zag, while RED LINE shows DELAYED signal and the RED LINE indicator DOES NOT LOOK into the future, even though it is based on zig). This shows precisely how it is done to have no look into the future effect - take the indicator that looks into the future and DELAY it as many bars as it looks into the future (note that delay amount CHANGES from leg to leg because it waits until new leg reaches threshold level). This also shows how to use zig/peak/trough in a trading system. Best regards, Tomasz Janeczko amibroker.com PS. Removing my earlier response and repeating your post four times does not add any strength to your argument. On 2010-04-26 04:51, jhnlmn wrote: Clearly you did not read the manual Why not? I do understand the algo of peak/trough in WL. It simply searches for min/max before a reversal. Obviously, if the reversal did not happen yet, then the peak/trough is not registered and the previous peak/trough is reported. This lag is natural and perfectly makes sense to me. And it works well when drawing trendlines, since trendlines should take into account historical data (have some lag). I read advices on the Web not to take the most recent data when drawing trendlines. And, of course, never look into the future, which means that the result should be the same, no matter whether the current bar is the last in the dataset or not. You are saying that AB zigzag can be used only for pattern recognition and not for trading, but what is the purpose of developing a pattern recognition script, which could not be used for real trading or verified using backtesting? This is my understanding. However, I am new to technical analysis, so, please, correct me if I am wrong. Thank you John IMPORTANT PLEASE READ This group is for the discussion between users only. This is *NOT* technical support channel. TO GET TECHNICAL SUPPORT send an e-mail directly to SUPPORT {at} amibroker.com TO SUBMIT SUGGESTIONS please use FEEDBACK CENTER at http://www.amibroker.com/feedback/ (submissions sent via other channels won't be considered) For NEW RELEASE ANNOUNCEMENTS and other news always check DEVLOG: http://www.amibroker.com/devlog/ Yahoo! Groups Links
Re: [amibroker] Re: How to backtest or plot a script, which uses SelectedValue or LastValue?
Hello, You admit that you are complete novice, yet you make definitive statements like this: It simply loops from 0 to the bar and finds min and max. Then, after a reversal happens, it assigns the last min or max to an array. That's all. You simply see the trees but you don't see the forest. When you say after a reversal happens you don't realise that in order to know that reversal actually happens you must look into the future (i.e. you must look at bars that occur AFTER THE PEAK) and find out that the price moved in opposite direction and then you go back and mark that peak occured at max that you found earlier (so you modify past output based on future). Finding that reversal happens is where looking into the future occurs. Apparently, you are also not aware of the simple fact that the above *IS* actually zig-zag algorithm and that peak and trough functions are _products_ of zig-zag algorithm. Peak in WL is based on zig-zag algorithm. Go ask Wealth Lab support and they will tell you. Any indicator that does not look into the future must a) use only *past* data to calculate current bar output b) never update/adjust past output based on current bar data Peak/Trough/Zig fail at condition b) unless output is delayed as I explained earlier. I also suggest reading some articles for example this: http://store.traders.com/v212zitrinby.html Regarding trendline formula you were interested in - the point is that it used SelectedValue to draw single trendline. You can not simply remove SelectedValue from such formula because it relied on manual input (namely selecting given quote by the user and drawing trendline from such point). In order to remove SelectedValue you would need to come up first with conditions that say when to start and when to end the trendline because it is not always obvious. For ready-to-use solutions regarding pattern trading you may check this 3rd party site: http://www.patternexplorer.com/ that offers pattern formulas that work in automatic manner. Best regards, Tomasz Janeczko amibroker.com On 2010-04-26 20:37, jhnlmn wrote: Hi, Thank you for the explanation. You still don't understand anything about zigzag algorithm. Of course, not. As I said, I am a complete novice to AB. I was using WL before and it does not have Zigzag at all. You are not correct when you say that Peak/Trough in WL is based on Zigzag or looks into the future. It never references any bar after the given one. It simply loops from 0 to the bar and finds min and max. Then, after a reversal happens, it assigns the last min or max to an array. That's all. Earlier I posted a question about SelectedValue or LastValue, but Mike warned me about the danger of Peak and TroughBar functions. So, I looked at AB help and it says only: zig(ARRAY, change ) ... Calculates the minimum % change Zig Zag indicator. Caveat: this function is based on Zig-Zag indicator and may look into the future - this means that you can get unrealistic results when back testing trading system using this indicator. This function is provided rather for pattern and trend recognition formulas. As you see, this does not explain anything. What does minimum % change mean? When does it look into the future and how far? How unrealistic the results would be? How to delay the result to undo the effect of looking into the future? The help is not helpful. So, until you posted this script in your message, I had no way to understand Zigzag. Now I understood. Thank you. However, my original question was not about Zigzag (it was Mike, who raised warning about them). The question was about the proper way of rewriting scripts, which use SelectedValue or LastValue, to make them appropriate for backtesting and indicator drawing. I wonder whether someone could take a look and comment on my attempt to fix the script: http://finance.groups.yahoo.com/group/amibroker/message/148924 PS. Removing my earlier response and repeating your post four times does not add any strength to your argument. I never removed your response (I guess it is impossible to remove someone else's post). I did remove my own duplicated posts, which I accidentally posted after experiencing Yahoo server timeouts on Sunday evening. Thank you John IMPORTANT PLEASE READ This group is for the discussion between users only. This is *NOT* technical support channel. TO GET TECHNICAL SUPPORT send an e-mail directly to SUPPORT {at} amibroker.com TO SUBMIT SUGGESTIONS please use FEEDBACK CENTER at http://www.amibroker.com/feedback/ (submissions sent via other channels won't be considered) For NEW RELEASE ANNOUNCEMENTS and other news always check DEVLOG: http://www.amibroker.com/devlog/ Yahoo! Groups Links
Re: [amibroker] Re: How to backtest or plot a script, which uses SelectedValue or LastValue?
Hello, The script never looks after the BAR, but, of course, it does look after the peak if the peak is in the past. This is *exactly* looking into the future. You can only claim a peak when you look several bars AFTER the peak - that is looking into the future in purest form. You don't know that the PEAK occurred until many bars AFTER the fact. You can call it expected lag and I am calling it looking into the future, both having same net effect - the result is useless for profitable trading, as useless as knowing what kind of market cycle we had one year ago. You can not profit from the information that is several bars (days) old. For me this ends this purely academic discussion. Best regards, Tomasz Janeczko amibroker.com On 2010-04-26 23:43, jhnlmn wrote: Hi, You admit that you are complete novice, yet you make definitive statements ... Sure. This is because I wrote a script, which generates exactly the same output as Peak/Trough in WL. So, I know for sure that this script does not use a Zigzag (WL simply does not have this indicator) and it does not look into the future, that is when I call TroughBars[bar] I do know that it does not use any bars with indexes bar. So, it does not look into the future, but have some lag, which is expected. Please, correct me if I am wrong. (i.e. you must look at bars that occur AFTER THE PEAK) and find out that the price moved in opposite Sure, but I never look AFTER THE BAR when I call TroughBars[bar]. Therefore, my script does not look into the future. That is, it will generate exactly the same answer no matter whether bar is the last in the series or some time later when bar will be in the middle of historical data. Finding that reversal happens is where looking into the future occurs. No. The script never looks after the BAR, but, of course, it does look after the peak if the peak is in the past. *IS* actually zig-zag algorithm and that peak and trough functions are _products_ of zig-zag algorithm. This may be true, but it is not really important. It does not matter how a particular trading program chose to name each function. It is more important what exactly the function does, in particular whether it does look into the future, that is when I call TroughBars[bar] will it look after the bar or not? And here, if I understood you correctly, AB is profoundly different from WL because in WL TroughBars[bar] does NOT touch any data after bar, but AB does. You can not simply remove SelectedValue from such formula because it relied on manual input Of course. I stated this in my original question. In order to remove SelectedValue you would need to come up first with conditions that say when to start and when to end the trendline because it is not always obvious. I already did that in my post http://finance.groups.yahoo.com/group/amibroker/message/148924 I, as well as other users, found it very difficult to understand AB array-based language, so it is very likely that I made some mistake in my rewritten scripts. So, I would appreciate if somebody with experience will comment on it. Thank you John IMPORTANT PLEASE READ This group is for the discussion between users only. This is *NOT* technical support channel. TO GET TECHNICAL SUPPORT send an e-mail directly to SUPPORT {at} amibroker.com TO SUBMIT SUGGESTIONS please use FEEDBACK CENTER at http://www.amibroker.com/feedback/ (submissions sent via other channels won't be considered) For NEW RELEASE ANNOUNCEMENTS and other news always check DEVLOG: http://www.amibroker.com/devlog/ Yahoo! Groups Links
Re: [amibroker] Re: How to backtest or plot a script, which uses SelectedValue or LastValue?
Hello, Clearly you did not read the manual of Wealth-Lab carefully enough. check page 240 of their manual and you will read the following: The Peak function never looks ahead in time, but always returns the Peak value as it would have been determined as of the specified bar. *For this reason, the return value of the Peak function will lag, and report peaks a few bars later than they actually occurred in hindsight.* What they are trying to say is that the peak function DOES look into the future but they internally DELAY the output of their Peak so the last leg of minimum percentage is formed. Such delay makes it unprofitable but well... that is not mentioned in their manual. AmiBroker does NOT delay the Peak output as the primary purpose of Peak/Trough and all Zig-zag family is PATTERN detection and not trading systems. It makes sense to have delay-free Peak because DELAYED Peak function can not be undelayed back. It is possible and quite easy in fact to create WL-style delayed peak by simply delaying AFL's Peak/Trough/Zig zag and it is presented in detail here: http://www.amibroker.com/members/traders/11-2003.html (for more details about that see ZigZag Trend Indicator in Nov 2003 issue of StocksCommodities) Best regards, Tomasz Janeczko amibroker.com On 2010-04-26 01:43, jhnlmn wrote: Thank you for your response. In essence, you are saying that I cannot use Tsokakis example (and many others from the Amibroker and Metastock libraries) without a significant rewriting. I am new to Amibroker. I was looking for a good trendline recognition script and stumbled on Amibroker. So, I downloaded a trial version to quickly test these scripts. So, I need to decide whether it is worse the effort to rewrite these scripts for Amibroker or go back to Wealth-Lab, which I was using before. I am surprised that PeakBars and TroughBars are looking into the future in Amibroker. It is not the case in WL. These are pretty simple functions - they just need to find min or max before a reversal. Why would Amibroker look into the future in this case? Now about replacing SelectedValue/LastValue. I tried to fix Historical Trendlines and Breakouts script by wrapping part of the script in for(i = 0; i BarCount; i++) loop and then replacing: ... pS = TroughBars( s1, per, 1 ) == 0; endt=SelectedValue(ValueWhen( pS, x ,1)); startt=SelectedValue(ValueWhen( pS, x ,2)); dtS =endt-startt; endS = SelectedValue((ValueWhen( pS, s1,1) )); startS = SelectedValue(( ValueWhen( pS, s1 ,2))); aS = (endS-startS)/dtS;bS = endS; trendlineS = aS * ( x -endt ) + bS;//SUPPORT LINE ... Plot(IIf(x=first -d AND x=Last+d,trendlineS,- 1e10),Support,colorBrightGreen,1); by pS = TroughBars( s1, per, 1 ) == 0; troughBarIdxLast = ValueWhen( pS, x ,1); troughBarIdxBeforeLast = ValueWhen( pS, x ,2); troughBarValueLast = ValueWhen( pS, s1, 1); troughBarValueBeforeLast = ValueWhen( pS, s1, 2); ... for(i = startBar; i lastBar; i++) { endt= troughBarIdxLast[i]; startt = troughBarIdxBeforeLast[i]; if(prevEndTrendlineS != endt) { prevEndTrendlineS = endt; dtS = endt - startt; endS = troughBarValueLast[i]; startS = troughBarValueBeforeLast[i]; aS = (endS - startS) / dtS; bS = endS; trendlineS = aS * ( x - endt ) + bS;//SUPPORT LINE Plot(IIf(x= startt - d AND x= endt + d, trendlineS, -1e10), Support, colorBrightGreen, 1); } ... } Is it the right approach to rewriting SelectedValue or LastValue scripts for a continuous drawing or backtesting? (Below is the entire rewritten script). Thank you John x = Cum(1); per = 3;// CALIBRATE THE ZIG() SENSITIVITY s1=L; s11=H; pS = TroughBars( s1, per, 1 ) == 0; troughBarIdxLast = ValueWhen( pS, x ,1); troughBarIdxBeforeLast = ValueWhen( pS, x ,2); troughBarValueLast = ValueWhen( pS, s1, 1); troughBarValueBeforeLast = ValueWhen( pS, s1, 2); pR = PeakBars( s11, per, 1 ) == 0; peakBarIdxLast = ValueWhen( pR, x, 1); peakBarIdxBeforeLast = ValueWhen( pR, x, 2); peakBarValueLast = ValueWhen( pR, s11, 1); peakBarValueBeforeLast = ValueWhen( pR, s11, 2); startBar = Status(FirstVisibleBar); lastBar = Status(LastVisibleBar); _TRACE(Startbar:+startBar); _TRACE(Lastbar:+lastBar); prevEndTrendlineS = -1; prevEndTrendlineR = -1; d=10;// INCREASE d TO EXTEND THE LINES Years=Year(); Months=Month(); Days=Day(); //color = for(i = startBar; i lastBar; i++) { color[i] = colorBlack; } for(i = startBar; i lastBar; i++) { endt= troughBarIdxLast[i]; startt = troughBarIdxBeforeLast[i]; if(prevEndTrendlineS != endt) { prevEndTrendlineS = endt; dtS = endt - startt; endS = troughBarValueLast[i]; startS = troughBarValueBeforeLast[i]; aS = (endS - startS) / dtS; bS = endS; trendlineS = aS * ( x - endt ) + bS;//SUPPORT LINE bearishbreakout = x endt AND x