[amibroker] Re: How to backtest or plot a script, which uses SelectedValue or LastValue?

2010-05-01 Thread progster01

 to have a system with Zig or Peak -- no backtest is possible!

This statement is not correct.

Code provided earlier in this thread, and at:

  http://www.amibroker.com/members/traders/11-2003.html

uses Zig() and backtests validly.

Zig/Peak/Trough functions CAN be used to create a valid backtest.  It just 
requires special care to do it correctly.


--- In amibroker@yahoogroups.com, Bisto bistoma...@... wrote:
 
 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?
 




[amibroker] Re: How to backtest or plot a script, which uses SelectedValue or LastValue?

2010-04-30 Thread Bisto
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, jhnlmn jhn...@... 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





Re: [amibroker] Re: How to backtest or plot a script, which uses SelectedValue or LastValue?

2010-04-30 Thread Tomasz Janeczko
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







[amibroker] Re: How to backtest or plot a script, which uses SelectedValue or LastValue?

2010-04-30 Thread Bisto
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 Janeczko gro...@... 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/
 
  Yahoo! Groups Links
 
 
 
 
 





Re: [amibroker] Re: How to backtest or plot a script, which uses SelectedValue or LastValue?

2010-04-30 Thread Tomasz Janeczko
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?

2010-04-27 Thread Ton Sieverding
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?

2010-04-27 Thread Tomasz Janeczko

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?

2010-04-27 Thread Ton Sieverding
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?

2010-04-27 Thread Tomasz Janeczko

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 

[amibroker] Re: How to backtest or plot a script, which uses SelectedValue or LastValue?

2010-04-27 Thread progster01
Re: the ZigZag part of this discussion - 

AB's implementation of backtesting is different than many other programs.

In AmiBroker, the backtest itself is not a process that proceeds on a 
bar-by-bar basis.

At the detail level, the criteria necessary for a valid bar-by-bar backtest 
and the criteria necessary for a valid all-at-once backtest (AmiBroker) are 
not identical.

What I'm saying in this context is that it is not only about how functions 
work, but about how the backtesting process itself works.

No one should be saying that a measured retracement from a pivot point can 
never have a statistically tradable probability of further extension.  If 
anyone is saying that, they are incorrect, as a single counterexample will 
prove.  However, I don't think that is what was said (though what was said 
could easily and predictably be interpreted as such, IMO).

How a measured retracement from a pivot is detected in AmiBroker, for both 
validly backtesting and realtime trading, is the real issue at hand.  

Code has been presented for this.  This code provides insight into the AB way.

No doubt, there are multiple ways in AFL to accomplish the same thing.


--- In amibroker@yahoogroups.com, jhnlmn jhn...@... wrote:

 What does matter is how functions work.
 Every trader must know exactly whether TroughBars[bar] does
 look into the future after bar or not. In WL it does not.
 Therefore, I can use TroughBars[bar] in WL for backtesting.




[amibroker] Re: How to backtest or plot a script, which uses SelectedValue or LastValue?

2010-04-27 Thread jhnlmn


Hi,
Thank you for your response

 In AmiBroker, the backtest itself is not a process that proceeds on a 
 bar-by-bar basis.
 At the detail level, the criteria necessary for a valid bar-by-bar backtest
 and the criteria necessary for a valid all-at-once backtest (AmiBroker) are
 not identical.


I do not understand this.
I was under impression (from the AB help) that operation on arrays all-at-once
(like in buy = cross(...)) can be rewritten using
bar-by-bar technique, such as 
for( i = 0; i  BarCount; i++ ) {... buy[i] = ...}

Personally, I found the loop code easier to understand and test,
but it does not matter.
It should not be any difference whether I am using TroughBar in a all-at-once
operation, such as ValueWhen( TroughBars( s1, per, 1 ) == 0, x ,1)
or in a loop like troughBars[i]. The value in array  troughBars at index X in 
both cases
should be the same, right? 

Then why are you saying that AB's implementation of backtesting is different 
than many other programs?
Where is the difference, other than in implementation of some indicators, such 
as TroughBars?

Thank you
John



[amibroker] Re: How to backtest or plot a script, which uses SelectedValue or LastValue?

2010-04-27 Thread progster01
In a traditional bar-by-bar backtest, all the arrays (often called series') 
grow in populated length one bar at a time during the process of the backtest.

In AB, all the arrays are fully and finally evaluated across the entire time 
period under test prior to the Backtester even looking at them.

That's the fundamental difference.  Understanding this and keeping it fully in 
mind is essential, IMO, for anyone who plans to write code in both AB and other 
platforms.

Using loop notion in AB is a means of getting inside array cells and altering 
them as of that point in the AFL.  Your AFL is still executed once (not on 
every bar), after which the Backtester goes to work (in the AA window) to 
evaluate the trading results of all those complete arrays you've 
calculated/populated in your AFL.

Important to note is that in realtime (i.e. indicator mode, on a chart), as new 
bars form, the whole shebang gets re-done with every new bar.  IOW, all your 
arrays are recalculated from scratch.  This is why it is very important when 
using AFL for realtime trading not to load more history than actually needed.

Above is my current high-level understanding, with which I'm able to code 
successfully in both AB and other platforms.  I hope what I've stated is 
literally correct, but if not, rephrasing or correction is invited.

Good luck!

--- In amibroker@yahoogroups.com, jhnlmn jhn...@... wrote:

 
 
 Hi,
 Thank you for your response
 
  In AmiBroker, the backtest itself is not a process that proceeds on a 
  bar-by-bar basis.
  At the detail level, the criteria necessary for a valid bar-by-bar 
  backtest
  and the criteria necessary for a valid all-at-once backtest (AmiBroker) 
  are
  not identical.
 
 
 I do not understand this.
 I was under impression (from the AB help) that operation on arrays all-at-once
 (like in buy = cross(...)) can be rewritten using
 bar-by-bar technique, such as 
 for( i = 0; i  BarCount; i++ ) {... buy[i] = ...}
 
 Personally, I found the loop code easier to understand and test,
 but it does not matter.
 It should not be any difference whether I am using TroughBar in a all-at-once
 operation, such as ValueWhen( TroughBars( s1, per, 1 ) == 0, x ,1)
 or in a loop like troughBars[i]. The value in array  troughBars at index X in 
 both cases
 should be the same, right? 
 
 Then why are you saying that AB's implementation of backtesting is different 
 than many other programs?
 Where is the difference, other than in implementation of some indicators, 
 such as TroughBars?
 
 Thank you
 John





Re: [amibroker] Re: How to backtest or plot a script, which uses SelectedValue or LastValue?

2010-04-27 Thread Ton Sieverding
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, 

[amibroker] Re: How to backtest or plot a script, which uses SelectedValue or LastValue?

2010-04-27 Thread jhnlmn


 In a traditional bar-by-bar backtest, all the arrays (often called series')
 grow in populated length one bar at a time during the process of the backtest.
 In AB, all the arrays are fully and finally evaluated across the entire time
 period under test prior to the Backtester even looking at them.
 That's the fundamental difference. 

Hi,

Sorry, but I do not see the difference.
I can populate arrays using a single operation, or in a loop
or just load from a file - it does not matter. As long as value
of a certain array at certain index (for example Close[i] or Trough[i])
stays the same, it does not matter whether it is calculated before or during
the backtesting.

To prove my point I wrote a simple script for AB:

EmaArray = EMA( Close, 20);
TroughArray = trough( Close, 15, 1);
Buy = Ref(Cross(Close, EmaArray), -1); 
Sell = Ref(Cross(EmaArray, Close), -1); 

fh = fopen( G:\\Invest\\result.txt, w); 
for(i = 20; i  BarCount; i++)
{
fputs( i= + i +  Close[i]= + Close[i] +  ema= + EmaArray[i] +  
TroughArray[i]= + TroughArray[i] + \n, fh);

}
fclose( fh ); 

And the same for WL:

   DataSeries EmaArray = EMA.Series( Close, 20, EMACalculation.Modern);
   DataSeries TroughArray = Trough.Series( Close, 15, PeakTroughMode.Percent);
   StringBuilder output = new StringBuilder();
   for(int i = 20; i  Bars.Count; i++)
   {
if ( CrossOver( i, Close, EmaArray ) )
{
 BuyAtMarket( i+1 );
}
if (ActivePositions.Count  0  CrossOver( i, EmaArray, Close ) )
{
 SellAtMarket(i+1, ActivePositions[0]);
}
output.Append(i= + i +  close[i]= + Close[i] +  ema= + 
Math.Round(EmaArray[i], 4) +  TroughArray[i]= + TroughArray[i] + \r\n);
   }
   System.IO.File.WriteAllText(G:\\Invest\\result_WL.txt, output.ToString());

I found that results of ema[i] are almost identical between WL and AB (there is 
a difference in the forth digit due to rounding).
Since I generated Buy and Sell signals based on EMA, I got exactly the same 
trades when backtesting on WL and AB.
But values of TroughArray are different due to lag/see-the-future problem 
discussed before.

So, I do not think that backtesting in AB and WL is different as long as we use 
the same input and the same indicators (like EMA).
But some indicators do differ (even if they have the same name, like Trough).
This is confusing for newcomers.

 Important to note is that in realtime (i.e. indicator mode, on a chart), as 
 new
 bars form, the whole shebang gets re-done with every new bar. 

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
the script all the time. But AB reruns the script each time I click the graph, 
which makes
viewing very slow. But again, as I said, if array values do not change from run 
to run,
then repeated script re-execution should not yield different results.

This is my understanding.
Please, correct me if I am wrong.

Thank you
John



Re: [amibroker] Re: How to backtest or plot a script, which uses SelectedValue or LastValue?

2010-04-27 Thread Tomasz Janeczko
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?

2010-04-26 Thread Tomasz Janeczko

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?

2010-04-26 Thread Tomasz Janeczko

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




   






[amibroker] Re: How to backtest or plot a script, which uses SelectedValue or LastValue?

2010-04-26 Thread jhnlmn


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




Re: [amibroker] Re: How to backtest or plot a script, which uses SelectedValue or LastValue?

2010-04-26 Thread Tomasz Janeczko
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







[amibroker] Re: How to backtest or plot a script, which uses SelectedValue or LastValue?

2010-04-26 Thread jhnlmn


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






Re: [amibroker] Re: How to backtest or plot a script, which uses SelectedValue or LastValue?

2010-04-26 Thread Tomasz Janeczko
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







[amibroker] Re: How to backtest or plot a script, which uses SelectedValue or LastValue?

2010-04-26 Thread jhnlmn


 You can only claim a peak
 when you look several bars AFTER the peak
 - that is looking into the future in purest form.

No, it is not. You keep focusing your eyes on the peak,
so the time period after the peak appears as future to you.
But the script has one variable called bar, which points
to the now moment. So, the future is not after the peak,
as you incorrectly assume, but after the bar variable.


 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,
 For me this ends this purely academic discussion.

I agree that the discussion shifted to a word game.
It does not matter how do you call things.
What does matter is how functions work.
Every trader must know exactly whether TroughBars[bar] does
look into the future after bar or not. In WL it does not.
Therefore, I can use TroughBars[bar] in WL for backtesting.
But in AB (I was told) TroughBars[bar] does look after bar
and, therefore, I cannot use TroughBars[bar] for backtesting.'
This is the difference.


  the result is useless for profitable trading, as useless as knowing
 what kind of market cycle we had one year ago.

This is simply not true. Trendlines, for examples,
are often drawn using several historical peaks or troughts,
sometimes many months back.

John




[amibroker] Re: How to backtest or plot a script, which uses SelectedValue or LastValue?

2010-04-25 Thread jhnlmn





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  endt + d AND Cross(trendlineS, C);
color = IIf (bearishbreakout,colorRed,color);
Plot(IIf(x = startt - d AND x = endt + d, trendlineS, -1e10), 
Support, colorBrightGreen, 1);
}

endt1 = peakBarIdxLast[i];
startt1 = peakBarIdxBeforeLast[i];
if(prevEndTrendlineR != endt1)
{
prevEndTrendlineR = endt1;
dtR =endt1-startt1;
endR = peakBarValueLast[i];
startR = peakBarValueBeforeLast[i];
aR = (endR-startR)/dtR;
bR = endR;

trendlineR = aR * ( x -endt1 ) + bR;//RESISTANCE LINE
bullishbreakout = x  endt1 AND x  endt1 + d AND Cross(C,trendlineR);
color = IIf (bullishbreakout,colorGreen,color);
Plot(IIf(x = startt1 - d AND x = endt1 + d, trendlineR, -1e10), 
Resistance, colorRed, 1);
}
}// for(i = startBar; i  lastBar; i++)
Plot(C,Close,color,64);




Re: [amibroker] Re: How to backtest or plot a script, which uses SelectedValue or LastValue?

2010-04-25 Thread Tomasz Janeczko

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  

[amibroker] Re: How to backtest or plot a script, which uses SelectedValue or LastValue?

2010-04-25 Thread jhnlmn
 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. And if Peak/Trough in AB do, indeed, look into the future
(that is use the current bar or future bars in their calculation),
then they cannot be used for purposes of backtesting.
This is my understanding.

However, I am new to technical analysis,
so, please, correct me if I am wrong.

Thank you
John



[amibroker] Re: How to backtest or plot a script, which uses SelectedValue or LastValue?

2010-04-25 Thread jhnlmn
 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




[amibroker] Re: How to backtest or plot a script, which uses SelectedValue or LastValue?

2010-04-25 Thread jhnlmn
 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



[amibroker] Re: How to backtest or plot a script, which uses SelectedValue or LastValue?

2010-04-25 Thread jhnlmn
 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



[amibroker] Re: How to backtest or plot a script, which uses SelectedValue or LastValue?

2010-04-22 Thread Mike
Hi,

Aside from your SelectedValue problem, you will sometimes find that charting 
looks into the future and will thus not translate easily into a clean backtest.

The Tsokakis example you gave is one such instance. The script uses PeakBars 
and TroughBars, both of which are forward looking and thus not suitable for 
backtesting without modification.

Before dealing with the SelectedValue issue, you would first have to add code 
to delay the recognition of the peaks and troughs until they would actually 
have been knowable under live conditions. See 
http://www.amibroker.com/members/library/detail.php?id=353 as an example of how 
to do this.

Once you've got logic that is not forward looking, the answer to your original 
question is that the code must be continuous, rather than rely on user 
intervention (i.e. remove SelectedValue entirely).

Mike

--- In amibroker@yahoogroups.com, jhnlmn jhn...@... 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