[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[amibroker] Re: calculating the mean of only some of the observations in an array ?



PureBytes Links

Trading Reference Links

There is a modification to the Sharpe Ratio called the Sortino Ratio
which distinguishes between good and bad volatility.  In the following
 AFL I calculate the denominator of the Sortino Ratio - the Semi
Deviation as follows:

Diff           = 100 * (Close - Ref(Close, -1)) / Ref(Close, -1) ; //
Calcluate daily gain/Loss in %;


	// ***  Sortino ratio - Semi Deviation   ***;

	// Compute Semi Deviation;
		SemiDiff      = IIf( Diff < 0, Diff, 0 );          // Make array
with only % losses, gains set to 0;
		SemiDevTerm2  = Sum(SemiDiff, VPers ) ^ 2.0;       // add up each
neg % over Pers bars, then square it;
		SemiDiffSqd   = SemiDiff * SemiDiff ;              // Make array
with each % loss squared
		SemiDevTerm1  = Sum(SemiDiffSqd, VPers );          // Add up each
neg % squared over Pers bars;
		SDArray       = IIf( Diff < 0,  1.0, 0 );          // Make array to
count # of losses in Pers bars;
		SDCount       = Sum(SDArray, VPers );              // Add up the
number of losses in Pers bars;
		SemiDevTerm3  = SDCount * (SDCount - 1) ;          // The
denominator of the Std Dev formula
		SemiDev       = ((SDCount * SemiDevTerm1 - SemiDevTerm2) /
SemiDevTerm3 ) ^ 0.5 ;

The SemiDiff array above contains only daily losses, with any gains
set to zero.  Then I sum it up in the next line SemiDevTerm2.  This is
the numerator for the average computation.

 To calculate the denominator, I create SDArray  -  a sparsely
populated array containing 1's and 0's which I sum up to be the number
of values to be averaged.

This method uses neither user programmed loops or ATC - fairly simple.

ReefBreak

--- In amibroker@xxxxxxxxxxxxxxx, "Gordon Sutherland" <gosuth@xxx> wrote:
>
> Another approach that does not use loops would be to use the
AddToComposite
> Function (ATC) as this will sum the specific event bar values and
then you
> can divide by a separate ATC that gives you the total number of such
events.
> I just love ATC!
> 
> Cheers,
> 
> Gordon Sutherland
> 
> -----Original Message-----
> From: amibroker@xxxxxxxxxxxxxxx [mailto:amibroker@xxxxxxxxxxxxxxx]
On Behalf
> Of cdepuy
> Sent: Tuesday, 29 April 2008 10:22 a.m.
> To: amibroker@xxxxxxxxxxxxxxx
> Subject: [amibroker] Re: calculating the mean of only some of the
> observations in an array ?
> 
> Ara, thanks for responding.  Actually what I'm trying to do is a 
> little different.  Here is what the array might look like
> 
> 	signal	hhv@xxxxxx	avgofmyobsv
> 	0	--
> 	1	--
> 	0	--
> 	0	--
> 	0	--
> 	0	--
> 	0	--
> 	0	--
> 	0	--
> 	0	--
> 	0	--
> 	0	--
> 	0	--
> 	0	.05		.050
> 	0	--
> 	0	--
> 	1	--
> 	0	--
> 	0	--
> 	0	--
> 	0	--
> 	0	--
> 	0	--
> 	0	--
> 	0	--
> 	0	--
> 	0	--
> 	0	--
> 	0	.06		.055
> 	0	--
> 	0	--
> 	1	--
> 	0	--
> 	0	--
> 	0	--
> 	0	--
> 	0	--
> 	0	--
> 	0	--
> 	0	--
> 	0	--
> 	0	--
> 	0	--
> 	0	.07		.060
> 
> 
> --- In amibroker@xxxxxxxxxxxxxxx, "Ara Kaloustian" <ara1@> wrote:
> >
> > if you are using loops and you know when your signal was generated, 
> use that 
> > bar number as the starting poing point for your loop. Pick an end 
> point as 
> > you edesire.
> > 
> > a = SignalBarNumber;
> > b = Bars required in your calculation;
> > 
> > for (i=a; i< a+b; i++)
> > ...
> > 
> > ----- Original Message ----- 
> > From: "cdepuy" <cdepuy@>
> > To: <amibroker@xxxxxxxxxxxxxxx>
> > Sent: Monday, April 28, 2008 2:40 PM
> > Subject: [amibroker] calculating the mean of only some of the 
> observations 
> > in an array ?
> > 
> > 
> > > Without requiring loops that work through the whole barcount, can
> > > someone point out to me how to exclude certain values in an array
> > > when calculating a statistic across an array?
> > >
> > > I must be missing something.  I am trying to calculate the mean of
> > > some values that occur only occassionally (after my signal) using
> > > Amibroker.  But,what happens is all the values between my signals 
> get
> > > included in the calculation.  I know the solution is easy, but I
> > > cannot figure it out.
> > >
> > > //figure highest value that occurs between my signals and the
> > > next "x" following the signal
> > > Cond6 = {some condition that i am investigating, like v > 5*ma
> (v,50)
> > > NumBars = 12;
> > > hV = IIf(BarsSince(Cond6) == NumBars,HHV(H,BarsSince(Cond6))-
> ValueWhen
> > > (Cond6,C),Null);
> > >
> > > //now calculate the mean of all the hv 's that occur
> > > mean = ma(hv,NumBars*50);  //this line I am having trouble with
> > > filter=1;
> > > addcolumn(mean,"mean",1.2);
> > > //then perform more statistics...(etc).
> > >
> > >
> > >
> > > ------------------------------------
> > >
> > > Please note that this group is for discussion between users only.
> > >
> > > To get support from AmiBroker please send an e-mail directly to
> > > SUPPORT {at} amibroker.com
> > >
> > > For NEW RELEASE ANNOUNCEMENTS and other news always check DEVLOG:
> > > http://www.amibroker.com/devlog/
> > >
> > > For other support material please check also:
> > > http://www.amibroker.com/support.html
> > > Yahoo! Groups Links
> > >
> > >
> > >
> >
> 
> 
> 
> ------------------------------------
> 
> Please note that this group is for discussion between users only.
> 
> To get support from AmiBroker please send an e-mail directly to 
> SUPPORT {at} amibroker.com
> 
> For NEW RELEASE ANNOUNCEMENTS and other news always check DEVLOG:
> http://www.amibroker.com/devlog/
> 
> For other support material please check also:
> http://www.amibroker.com/support.html
> Yahoo! Groups Links
>



------------------------------------

Please note that this group is for discussion between users only.

To get support from AmiBroker please send an e-mail directly to 
SUPPORT {at} amibroker.com

For NEW RELEASE ANNOUNCEMENTS and other news always check DEVLOG:
http://www.amibroker.com/devlog/

For other support material please check also:
http://www.amibroker.com/support.html
Yahoo! Groups Links

<*> To visit your group on the web, go to:
    http://groups.yahoo.com/group/amibroker/

<*> Your email settings:
    Individual Email | Traditional

<*> To change settings online go to:
    http://groups.yahoo.com/group/amibroker/join
    (Yahoo! ID required)

<*> To change settings via email:
    mailto:amibroker-digest@xxxxxxxxxxxxxxx 
    mailto:amibroker-fullfeatured@xxxxxxxxxxxxxxx

<*> 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/