Thanks for your help Mike.  It's a lot to digest.  I'll see if I can get it to 
work more effectively with your code and suggestions.
Wade

--- On Wed, 8/5/09, Mike <[email protected]> wrote:

From: Mike <[email protected]>
Subject: [amibroker] Re: Custom Metric not working in Alpha 5.27.1
To: [email protected]
Date: Wednesday, August 5, 2009, 4:56 PM












 
 




    
                  Correction,



"Note that since you passed in Ref(..., -1) as 'eq', this array will not have a 
final element (i.e. index of BarCount - 1) since tomorrow hasn't happened yet 
so today's value is unknown. This means that if a trade's entry date is the 
last bar in the range, you will be trying to reference a non existent equity 
value."



Should read



"Note that since you passed in Ref(..., -1) as 'eq', this array will not have a 
*first* element (i.e. index of 0) since day prior to first day is unknown. This 
means that if a trade's entry date is the *first* bar in the range, you will be 
trying to reference a non existent equity value."



http://www.amibroke r.com/guide/ h_understandafl. html



Mike



--- In amibro...@yahoogrou ps.com, "Mike" <sfclimbers@ ...> wrote:

>

> 

> Hi,

> 

> Your code is somewhat overly complex. If BarCount is zero, 'found' will

> be -1, which is an invalid index. If 'Value' does not match any element

> in 'dt' then found will be BarCount, which is also an invalid index.

> 

> Note that since you passed in Ref(..., -1) as 'eq', this array will not

> have a final element (i.e. index of BarCount - 1) since tomorrow hasn't

> happened yet so today's value is unknown. This means that if a trade's

> entry date is the last bar in the range, you will be trying to reference

> a non existent equity value.

> 

> At any rate, your FindEquityAtDateTim e function can be simplified to a

> single line as follows:

> 

> 

> function FindEquityAtDateTim e(eq, dt, Value) {

>     return LastValue(ValueWhen (dt == Value, eq));

> }

> 

> 

> Also, it is highly redundant to fetch the performance stats and

> calculate 'pratio' at each trade. The value will never change, so just

> do it once either before or after looping through the trades, not inside

> the loop.

> 

> Finally, be wary of using summary measures such as WinnersPercent and

> PayoffRatio. These are based on the assumption that open trades have

> been closed at their current price (i.e. assumes that all open trades

> get closed based on final bar's closing price). If you have more than a

> few open trades, they can really throw off the summary statistics.

> Better to calculate the stats yourself based only on closed trades.

> 

> Mike

> 

> 

> --- In amibro...@yahoogrou ps.com, "wadebullock" <wadebullock@ > wrote:

> >

> > Hi all,

> >

> > In previous beta versions the following custom metrics were working

> fine. Now I get message Error 10...it also is below. If anyone sees a

> problem with the code I would greatly appreciate the help. I pieced it

> together from several examples but still have difficulty understanding

> it. Thanks

> >

> > SetCustomBacktestPr oc("");

> > function FindEquityAtDateTim e( eq, dt, Value )

> > {

> > found = -1;

> > for( i = 0; i < BarCount AND found == -1; i++ )

> > {

> > if( dt[ i ] == Value ) found = i;

> > }

> > return IIf( found != -1, eq[ found - 0 ], Null );

> > }

> > if( Status("action" ) == actionPortfolio )

> > {

> > bo = GetBacktesterObject ();

> > bo.Backtest( 1);

> > dt = DateTime();

> > eq = Ref(Foreign( "~~~EQUITY" , "C" ),-1);

> >

> > for( trade = bo.GetFirstTrade( ); trade; trade = bo.GetNextTrade( ) )

> > {

> > EquityAtEntry = FindEquityAtDateTim e( eq,dt,trade. EntryDateTime );

> > PctLossGain = (trade.GetProfit( )/EquityAtEntry) *100;

> > trade.AddCustomMetr ic("Equity at Entry", EquityAtEntry );

> > trade.AddCustomMetr ic("% Gain/Loss of Equity", PctLossGain );

> > st = bo.GetPerformanceSt ats(0);

> > pratio=(st.GetValue ("WinnersPercent "

> )/100)*st.GetValue( "PayoffRatio" );

> >

> > }

> > bo.ListTrades( );

> > bo.AddCustomMetric( "Performance Ratio", pratio );

> >

> > }

> >

> >

> > Error 10 Message///// ///////// ///////// ///////// ///////// ///////// ///

> > {

> >

> > if( dt[ i ] == Value ) found = i;

> >

> > }

> >

> > return IIf( found != -1, eq[ found - 0

> > ------------ --------- --------- --------^

> >

> > Error 10.

> > Subscript out of range.

> > You must not access array elements outside 0..(BarCount- 1) range.

> >

>




 

      

    
    
        
         
        
        




        




        
        


        
        
        

Reply via email to