PureBytes Links
Trading Reference Links
|
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 FindEquityAtDateTime function can be simplified to a single line as follows:
function FindEquityAtDateTime(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 amibroker@xxxxxxxxxxxxxxx, "wadebullock" <wadebullock@xxx> 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 > > SetCustomBacktestProc(""); > function FindEquityAtDateTime( 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 = FindEquityAtDateTime( eq,dt,trade.EntryDateTime ); > PctLossGain = (trade.GetProfit()/EquityAtEntry)*100; > trade.AddCustomMetric("Equity at Entry", EquityAtEntry ); > trade.AddCustomMetric("% Gain/Loss of Equity", PctLossGain ); > st = bo.GetPerformanceStats(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. >
__._,_.___
**** 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/
__,_._,___
|