เจอตัวปัญหาละครับ
พอดีผมขยันไปหน่อย ไปลองเขียน user-defined metrics ให้มันแสดง expectancy นู่นนี่เพิ่ม(จิงๆผมก็ก๊อปสูตรมาจากคู่มือของ ami เลย)
พอลองเอาออก row ที่ซ้ำกัน 2 อัน ก็เหลือ row เด๊วละครับ
อันนี้โค้ดที่ไปเอามาใส่ ไม่รุอะไรทำให้มันเป็นอย่างนั้น
/*===== User-defined Metrics =======*/
/* First we need to enable custom backtest procedure and
** tell AmiBroker to use current formula
*/
SetCustomBacktestProc(“”);
/* Now custom-backtest procedure follows */
if( Status(“action”) == actionPortfolio )
{
bo = GetBacktesterObject();
bo.Backtest(); // run default backtest procedure
/*###### Expectancy 1 ###########*/
st = bo.GetPerformanceStats(0); // get stats for all trades
// Expectancy calculation (the easy way)
// %Win * AvgProfit – %Los * AvgLos
// note that because AvgLos is already negative
// in AmiBroker so we are adding values instead of subtracting them
// we could also use simpler formula NetProfit/NumberOfTrades
// but for the purpose of illustration we are using more complex one
expectancy = st.GetValue(“WinnersAvgProfit”)*st.GetValue(“WinnersPercent”)/100 +
st.GetValue(“LosersAvgLoss”)*st.GetValue(“LosersPercent”)/100;
// Here we add custom metric to backtest report
bo.AddCustomMetric( “Expectancy ($)”, expectancy );
/*###### Expectancy 2 ###########*/
SumProfitPer100Inv = 0;
NumTrades = 0;
// iterate through closed trades first
for( trade = bo.GetFirstTrade(); trade; trade = bo.GetNextTrade() )
{
// here we sum up profit per $100 invested
SumProfitPer100Inv = SumProfitPer100Inv + trade.GetPercentProfit();
NumTrades++;
}
// iterate through eventually still open positions
for( trade = bo.GetFirstOpenPos(); trade; trade = bo.GetNextOpenPos() )
{
SumProfitPer100Inv = SumProfitPer100Inv + trade.GetPercentProfit();
NumTrades++;
}
expectancy2 = SumProfitPer100Inv / NumTrades;
bo.AddCustomMetric( “Expectancy (per $100 inv.)”, expectancy2 );
/*###### Expectancy 3 ###########*/
MaxLossPercentStop = 10; // 10% max. loss stop
SumProfitPerRisk = 0;
NumTrades = 0;
// iterate through closed trades first
for( trade = bo.GetFirstTrade(); trade; trade = bo.GetNextTrade() )
{
// risk is calculated as the maximum value we can loose per trade
// in this example we are using max. loss stop
// it means we can not lose more than (MaxLoss%) of invested amount
// hence ris
Risk = ( MaxLossPercentStop / 100 ) * trade.GetEntryValue();
RMultiple = trade.GetProfit()/Risk;
trade.AddCustomMetric(“Initial risk $”, Risk );
trade.AddCustomMetric(“R-Multiple”, RMultiple );
SumProfitPerRisk = SumProfitPerRisk + RMultiple;
NumTrades++;
}
expectancy3 = SumProfitPerRisk / NumTrades;
bo.AddCustomMetric( “Expectancy (per risk)”, expectancy3 );
bo.ListTrades();
}