PureBytes Links
Trading Reference Links
|
TRELIFF,
MANY THANKS FOR THE CODE AND THE REF !!
DIMITRIS TSOKAKIS
--- In amibroker@xxxxxxxxxxxxxxx, "treliff" <treliff@xxxx> wrote:
> Byron, Ram and others interested, here is what I've come up with
> re.
> the KASE PeakOscillator and PeakOut Lines. I am quite confident
that
> this code captures the essence of Kase's measurements but less
> confident that code efficiency can't be improved.
>
> Traders using momentum, not averse to math and not yet familiar
with
> Kase's work I recommend to take a look. For me it is the first
> purely technical indicator that caught my serious interest. Always
> turned off by fixed lookbacks I found Kase's approach very
> refreshing: with adaptive lookbacks and a very sound statistical
> foundation her work truly stands out in my opinion.
>
> The use of functions in the code makes me personally see things
> clearer and as I understood these do not slow things down I use
them
> wherever applicable (Dimitris I twisted your dynamic st.dev code
> into a function as well).
>
> This one could replace the "out-moded MACD" (Kase quote),
> though has
> a very different foundation. For those interested in details Kase
> explains most (but not all) in a bunch of articles free available at
>
> http://www.kaseco.com/articles_etc/articles.htm
>
> under "primarily trading related" esp. the 2 articles "the best
> momentum indicators" and "the two faces of momentum".
>
> Read the remarks after the code for some explanations.
>
> /* code start */
>
> /*
> Definition of 3 different standard deviation measurements.
> SD_pop is the population or biased version, similar to AFL StDev.
> SD_sam is the sample or non-biased version which Kase describes in
> her book and assumedly uses in her indicators.
> SD_nnc is the non-centered version in which not the distances
> between the values and the mean are squared but instead the values
> themselves. Though not formally a standard deviation it appears
used
> by traders especially for measuring short-term volatility in strong
> trends.
> */
>
> function SD_pop(input,n)
> { return sqrt( (n*Sum(input^2,n)-(Sum(input,n))^2) / n^2 ); }
>
> function SD_sam(input,n)
> { return sqrt( (n*Sum(input^2,n)-(Sum(input,n))^2) / (n^2-n) ); }
>
> function SD_nnc(input,n)
> { return sqrt( Sum(input^2,n) / n ); }
>
> /* Set your preferred standard deviation measurement SD */
> function SD(input,n) { return SD_pop(input,n); }
> //function SD(input,n) { return SD_sam(input,n); }
> //function SD(input,n) { return SD_nnc(input,n); }
>
> /* Set minimum and maximum lookbacks */
> minLB = 8;
> maxLB = 65;
>
> /* Set percentiles for PeakOut lines using numbers between 81 and
99
> */
> PCcycl = 98;
> PChist = 90;
>
> /* Definition of Kase PeakOscillator KPO */
>
> function Kup(n)
> { return log(H/Ref(L,-n)); }
>
> function Kdn(n)
> { return log(Ref(H,-n)/L); }
>
> function Kvol(n)
> { return SD(log(Ref(C,-1)/C), n); }
>
> function KSDIup(n)
> { return Kup(n)/(Kvol(n)*sqrt(n)); }
>
> function KSDIdn(n)
> { return Kdn(n)/(Kvol(n)*sqrt(n)); }
>
> maxKSDIup = KSDIup(minLB);
> for (i=minLB+1;i<=maxLB;i++)
> { maxKSDIup = IIf(KSDIup(i)>maxKSDIup,KSDIup(i),maxKSDIup); }
>
> maxKSDIdn = KSDIdn(minLB);
> for (i=minLB+1;i<=maxLB;i++)
> { maxKSDIdn = IIf(KSDIdn(i)>maxKSDIdn,KSDIdn(i),maxKSDIdn); }
>
> KPO = maxKSDIup - maxKSDIdn;
>
> /* Definition of PeakOut lines POmax and POmin */
>
> UPcycle = minLB;
> for (i=minLB+1;i<=maxLB;i++)
> { UPcycle = IIf(KSDIup(i)==maxKSDIup,i,UPcycle); }
>
> DNcycle = minLB;
> for (i=minLB+1;i<=maxLB;i++)
> { DNcycle = IIf(KSDIdn(i)==maxKSDIdn,i,DNcycle); }
>
> function t(x)
> { return 1 / (1+0.2316419*x); }
>
> function HAS(x)
> { return 1 - (exp(-0.5*x^2)/sqrt(2*3.141592654))*(0.31938153*t(x)-
> 0.356563782*t(x)^2+1.781477937*t(x)^3-1.821255978*t(x)
> ^4+1.330274429*t(x)^5); }
>
> function g(x)
> { return (2*HAS(x)-1)*100; }
>
> if (PCcycl<81) PCcycl=81;
> if (PCcycl>99) PCcycl=99;
>
> THcycl=1.3;
> for (i=0.01;i<=1.3;i=i+0.01)
> {
> if ( g(i+1.3)>=PCcycl AND g(i+1.29)<PCcycl )
> THcycl = THcycl + i;
> else
> THcycl = THcycl;
> }
>
> if (PChist<81) PChist=81;
> if (PChist>99) PChist=99;
>
> THhist=1.3;
> for (i=0.01;i<=1.3;i=i+0.01)
> {
> if ( g(i+1.3)>=PChist AND g(i+1.29)<PChist )
> THhist = THhist + i;
> else
> THhist = THhist;
> }
>
> CYCL = IIf(KPO>=0,UPcycle,DNcycle);
> MNcycl = MA(KPO,CYCL);
> SDcycl = SD(KPO,CYCL);
> Vcycl1 = MNcycl + THcycl*SDcycl;
> Vcycl2 = MNcycl - THcycl*SDcycl;
> POcycl = IIf(KPO>0,Vcycl1,IIf(KPO<0,Vcycl2,0));
>
> HIST = BarIndex()-maxLB+1;
> MNhist = MA(KPO,HIST);
> SDhist = SD(KPO,HIST);
> Vhist1 = MNhist + THhist*SDhist;
> Vhist2 = MNhist - THhist*SDhist;
> POhist = IIf(KPO>0,Vhist1,IIf(KPO<0,Vhist2,0));
>
> POmax = IIf(abs(POcycl)>abs(POhist),POcycl,POhist);
> POmin = IIf(abs(POcycl)<abs(POhist),POcycl,POhist);
>
> /* Plots */
>
> GraphXSpace = 3;
>
> Plot(POmax,"PeakOutMAX",colorRed,styleLine);
> Plot(POmin,"PeakOutMIN",colorBlue,styleLine);
>
> Col = IIf(abs(KPO)>=abs(POmax) AND Ref(abs(KPO),1)<Ref(abs
> (POmax),1),colorViolet,IIf(abs(KPO)>=abs(POmin) AND Ref(abs(KPO),1)
> <Ref(abs(POmin),1),colorPink,colorGreen));
> Plot(KPO,"KPeakOsc",Col,styleHistogram|styleThick);
>
> //Plot(UPcycle,"UPcycle",colorGreen,styleLine|styleDots);
> //Plot(DNcycle,"DNcycle",colorRed,styleLine|styleDots);
>
> /* code end */
>
> Remarks:
>
> First, as Byron mentioned the code posted earlier by Wayne is not
> looking for the optimum up/down cyclelength but uses Param to
> manually set different lookbacks. The code also sets one lookback
> for both up and down trends. This is definitely not in line with
> Kase: the power is in looping for the strongest up and strongest
> downtrend separately.
>
> Further the PeakOsc there is smoothed with a 3-period WMA. I
> don't
> know who initiated this but Kase indeed (in BridgeTrader) says:
> "the
> PeakOscillator is the difference between KSDIup and KSDIdn with
some
> smoothing added". Nowhere could I find how she implements this
> smoothing but I guess that is the WMA. To me it seemed somewhat
> irrational to top off so much statistical scrutiny with some
> arbitrary WMA with a fixed lookback. Also it turned out this WMA
> somehow interfered with the SD calculations for some very weird
> plots. Two reasons for not smoothing the KPO.
>
> My code is partly a loop-version of Byron's, but in his code
> there
> was one mistake that pretty sure was generated by a "mistake"
> in the
> BridgeTrader article and also refers to point 6 of his earlier
> remarks (which I did not comprehend then but now I do): Kvol in his
> code has a fixed StDev period of 9 which is incorrect. Instead this
> period should run along with the lookback:
>
> Kvol(n) = SD(log(Ref(C,-1)/C), n)
>
> because, looking e.g. at KSDIup(n)
>
> log(H/Ref(L,-n)) / (SD(log(Ref(C,-1)/C), n)*sqrt(n))
>
> the numerator here measures how far this market went "up" (in
> log)
> while the denominator measures exactly one standard deviation of
the
> move this market should have made if it were a random process;
> that's why Kvol has to adjust to the lookback.
>
> If the oscillator > 2 then the market has moved more than 2
> stdev's,
> which has a 5% chance of happening, while if > 3 chances are only
> 0.3%, so in that case we can be more confident that traders
> are "pushing" this market out of randomness into trendiness.
> However
> these %'s are only valid for very large samples and make less
> sense
> when the sample size gets smaller.
>
> It's really like a coin toss 20 times and after 10 tosses
> counting 9
> heads: something's wrong ("this coin is trending"). Is
> it? Perhaps
> not and it's just a coincidence. And of course, counting 90 heads
> after 100 tosses gives just a bit more confidence than 9 heads out
> of 10 tosses.
>
> Byron's point 3 referred to the ability to warn for peaks: if
> things
> get too extreme that should be signaled by the PeakOut lines. Kase
> uses not a break of these lines (which in fact confirms a strong
> trend) but a pullback after an earlier break as a signal of a
> (temporary) pause or possible reversal. In the plot these are
> colored pink for POmin and violet for POmax.
>
> In calculating POcycl and POhist (the max of which is POmax and the
> min is POmin) Kase uses respectively "the 98th percentile of the
> local distribution of the oscillator over the last 30 bars or so"
> ("or so" is literally hers) and "the 90th percentile over
> 80 years
> of commodity history". 30-bars-or-so is not in my dictionary and
> it
> seemed natural instead to measure the SD over exactly the length of
> the current strongest trend, up or down, as determined by dynamic
> CYCL variable.
>
> For POhist I think Kase somehow averaged all her historical data
and
> uses this fixed number in her software but here I think we can both
> simplify and improve by measuring the 90th percentile for that
> particular market we are working on. Of course the more history is
> loaded the more reliable this measurement will be. It seems a good
> idea to increase this percentile especially when there is less
> history and the 90th gives too many signals. I enabled setting both
> percentiles (as well as the lookbacks) in the code to facilitate
> this and backtesting. I wondered why the normal distribution is not
> in the AFL library but since it isn't I had to clutter the code
> with
> the Hastings approximation.
>
> At the end of the code I also added a plot for UPcycle and
DOWNcycle
> which display the max KSDI cyclelengths: how many bars to lookback
> for the strongest up and down trend at that moment. They give some
> further insight in the mechanics of this indicator (better plot
> these in a separate pane).
>
> Look forward to any comments or ideas.
>
> -treliff
>
>
> --- In amibroker@xxxxxxxxxxxxxxx, "Byron Porter" <bporter@xxxx>
> wrote:
> > Treliff and Owen - Thanks for the info on the Kase Book.
> > Wayne - Thanks for the Kase code. I have some different
> interpretations
> > but I do not know if mine are correct.
> >
> > Treliff,
> > I have attached 3 afls to this response and sent a copy to your
> yahoo
> > email in the hopes that you will be sure to get a copy - I think
> we are
> > on different time zones so we may only get respond once a day - I
> live
> > in Houston -6 GMT
> >
> > 1. I have used the log versions and the 2 bar average of kase
> ideas in
> > my afl
> > 2. The non Log form is the Random Walk Index which is an
Amibroker
> built
> > in function see RWI, RWIHI, RWILO. I was looking for the code to
> see
> > how it was coded but have not been able to find it. I think a
> small
> > modification to RWI code would be the Kase indicator. I'll bet
it
> has
> > the loop you are looking for.
> > 2. The Param approach is not my understanding to look back
> problem. My
> > understanding is that at each bar you look back 8 to 65 bars do
the
> > calcs and then pick the max. I have implemented this without
> using a
> > loop but if things become more complicated, a loop may be
required
> and a
> > cleaner code.
> > 3. The think the look back is searching for the peaks for an
> uptrend and
> > a trough for a down trend and the oscillator takes the difference
> > between the two to determine which is controlling. The bigger the
> > difference the stronger the trend either up or down. Is that
what
> you
> > are saying below? Could you elaborate on your concern. I think
> Kase is
> > looking for peak not trends.
> >
> > Kase now uses a loop, within a certain range of lookbacks
(15-
> 100
> > in
> > Wayne's code I think), to find the strongest down-trend and
> the
> > strongest up-trend, and takes the max to determine THE trend.
> > Though
> > a huge improvement over fixed-lookback indicators, I still
> hesitate
> >
> > because if the strongest downtrend is established as 2.8
> while
> > looking back 98 bars, and the strongest uptrend as 2.7
> looking back
> >
> > 15 bars, then this would be defined as a downtrend. Seems
> > questionable to me.
> >
> > 4. I see what you are saying about looping with a weighting
> system. I
> > am not a mathematician so I don't always know the proper
> statistical way
> > to go about approaching a problem, but here is the way I see it.
> There
> > are 2 things going on at the same time, 1) variation about the
> mean -
> > the Brownian motion or the Random Walk and then 2) there is the
> trend.
> > I think the goal is to try to separate the variation from the
> trend.
> > Statistically, mathematically, how do we do this?
> >
> > 5. One thing about Kases work that I have not run across is the
> use of
> > volume. It seems that we have a limited amount of information to
> work
> > with - Open, high ,low ,close and we do get volume. There is
talk
> in
> > the Kase reference document(www.fini.com/kase/k0.ntm) that talks
> about
> > using ticks to create your bars during a day. I guess I have been
> > wondering how to use volume in this method. I use Esignal for
> Forex
> > data and they provide a volume but my understanding that it is not
> > really volume but something like tick count which is as close as
> they
> > could get to "volume" because there is no central place to really
> get
> > the volume in the Forex market.
> >
> > 6. Another issue that I have been thinking about in the Forex
> market is
> > that activity varies considerably during the 24 hr day. At times
> when
> > say the Asian and European markets are both open there is more
> > volatility than when just the US market is open. Should there be
> some
> > way to account for time of day. Should volatility be measure for
9
> > periods back as suggested by Kase or should you take the
> volatility at
> > the same time of day for say 9 days back. Statistically would
> that help
> > separate variation from trend.
> >
> > 7. one of the things that I noticed in the code provided by Wayne
> was in
> > the Kase DevStops- the look back period was dev1 was 30 and dev2
> was 10
> > and dev3 was 21. I think those were picked up from the Kase
> manual. I
> > think that L1=30 is the look back length for all the devs. The
L2
> = 10
> > and L3 = 21 is to be used in 2 moving averages that tell you
when
> to
> > change your plot of stops from long positions to short
positions.
> What
> > do you think? Kase recommends 30 bars for intraday and 20 bars
> for
> > daily, Is there a statistical reason for this. It all gets back
to
> > "fixed look back" periods.
> >
> > Thanks for the thought provoking information.
> > Please look at my afls and see if there are any questionable
> > interpretations of Kase ideas.
> >
> > Byron
> >
> >
> > -----Original Message-----
> > From: treliff [mailto:treliff@x...]
> > Sent: Wednesday, March 10, 2004 12:51 AM
> > To: amibroker@xxxxxxxxxxxxxxx
> > Subject: [amibroker] Re: I need some loop-... / KASE indicators
> >
> >
> > Byron, Kase's book is even less revealing than her articles when
it
> > comes to math and codes. I purchased it after reading the
> > articles but don't regret that in view of the value of her ideas.
> > The good thing is, her smoke and mirrors force you to really dig
> in
> > and fully understand the reasoning before even attempting to
write
> a
> > code.
> >
> > Sometimes she sounds pretty obnoxious, like when referring to "the
> > out-moded MACD" (not to mention those silly moving
> > avg's) but
> > in my opinion she has a right to speak.
> >
> > Re. her PeakOscillator (I did not study her Dev.Stop yet) it is
> > worth noting that it appears she has replaced her initial trend
> > measurement based on ATR, with one that uses what is called
> > Historical Volatility. Specifically where in previous articles
> (and
> > in the book) she uses e.g.
> >
> > (C-Ref(C,-n)) / ATR*sqrt(n)
> >
> > in later articles she uses
> >
> > log(C/Ref(C,-n)) / StDev(log(C/Ref(C,-1),n)*sqrt(n)
> >
> > (Note: in fact she uses H or L instead of C in the left side of
> the
> > equation and splits in an UP and DOWN index.)
> >
> > I find the second approach much more interesting (but don't
> confuse
> > interesting with profitable:-). Wayne's code (thanks Wayne) seems
> to use
> > the ATR approach, but that could probably be easily
> > converted. The programming is not my strongest side but at first
> > glance it seems this code uses Param in order to "loop".
> >
> > Instead of focusing on code though I'd like to share some
thoughts
> on
> > the Kase approach. To recapitulate:
> >
> > Using any bar as starting point and looking back n periods there
> is
> > this undeniable "n-period statistical trend" that is defined
> > as the
> > 2nd formula above and measures how far the market has moved in
> > relation to its volatility: if this formula = 1 than the market
> > moved within exactly 1 standard deviation, which should happen
> about
> > 67% of the time etc. As a math-man, I find this a very, very
> strong
> > concept, by far the best when it comes to trend measurement.
> >
> > Kase now uses a loop, within a certain range of lookbacks (15-100
> in
> > Wayne's code I think), to find the strongest down-trend and the
> > strongest up-trend, and takes the max to determine THE trend.
> Though
> > a huge improvement over fixed-lookback indicators, I still
> hesitate
> > because if the strongest downtrend is established as 2.8 while
> > looking back 98 bars, and the strongest uptrend as 2.7 looking
> back
> > 15 bars, then this would be defined as a downtrend. Seems
> > questionable to me.
> >
> > My thought therefore was to take all trends (the 15-bar trend, 16-
> bar
> > trend etc) and weigh these in a sensible manner, which implies
> > preferably not with fixed weights, but also in some loop-style
> > manner and let the market speak. The code I needed help with was
> the
> > first step in this direction.
> >
> > I am interested in any further thoughts, ideas and test results,
> > also re. the original KASE codes as initiated by Wayne.
> >
> > Rgds,
> > Treliff
> >
> >
> >
> > Send BUG REPORTS to bugs@xxxx
> > Send SUGGESTIONS to suggest@xxxx
> > -----------------------------------------
> > Post AmiQuote-related messages ONLY to: amiquote@xxxxxxxxxxxxxxx
> > (Web page: http://groups.yahoo.com/group/amiquote/messages/)
> > --------------------------------------------
> > Check group FAQ at:
> > http://groups.yahoo.com/group/amibroker/files/groupfaq.html
> > Yahoo! Groups Links
------------------------ Yahoo! Groups Sponsor ---------------------~-->
Buy Ink Cartridges or Refill Kits for your HP, Epson, Canon or Lexmark
Printer at MyInks.com. Free s/h on orders $50 or more to the US & Canada.
http://www.c1tracking.com/l.asp?cid=5511
http://us.click.yahoo.com/mOAaAA/3exGAA/qnsNAA/GHeqlB/TM
---------------------------------------------------------------------~->
Send BUG REPORTS to bugs@xxxxxxxxxxxxx
Send SUGGESTIONS to suggest@xxxxxxxxxxxxx
-----------------------------------------
Post AmiQuote-related messages ONLY to: amiquote@xxxxxxxxxxxxxxx
(Web page: http://groups.yahoo.com/group/amiquote/messages/)
--------------------------------------------
Check group FAQ at: http://groups.yahoo.com/group/amibroker/files/groupfaq.html
Yahoo! Groups Links
<*> To visit your group on the web, go to:
http://groups.yahoo.com/group/amibroker/
<*> To unsubscribe from this group, send an email to:
amibroker-unsubscribe@xxxxxxxxxxxxxxx
<*> Your use of Yahoo! Groups is subject to:
http://docs.yahoo.com/info/terms/
|