无法将此自定义指标添加到 Amibroker 回测报告

Unable to add this custom metric to Amibroker backtest report

我想添加一个额外的列来指示回测报告中的波动性。

这是我的代码。额外的列 volatility_recent 出现,但该列中没有值出现。但是,如果我使用注释行 trade.AddCustomMetric( "proceeds", trade.Shares*trade.ExitPrice );,列中会出现一些数值。

代码有什么问题?

if ( Status( "action" ) == actionPortfolio )
{
    bo = GetBacktesterObject();
    // run default backtest procedure without generating the trade list
    bo.Backtest( True );

    volatility_recent = ATR(30);

    // iterate through closed trades
    for ( trade = bo.GetFirstTrade( ); trade; trade = bo.GetNextTrade( ) )
    {
        trade.AddCustomMetric( "volatility_recent", volatility_recent );
        //trade.AddCustomMetric( "proceeds", trade.Shares*trade.ExitPrice );
    }

    // iterate through open positions
    for ( trade = bo.GetFirstOpenPos( ); trade; trade = bo.GetNextOpenPos( ) )
    {       

        trade.AddCustomMetric( "volatility_recent", volatility_recent );
        //trade.AddCustomMetric( "proceeds", trade.Shares*trade.ExitPrice );
    }

    // generate trade list
    bo.ListTrades( );
}

自定义指标需要是标量(数字),而不是数组。 ATR(30) 是一个数组。因此,使用 LastValue 获取数组的最后一个值或使用 Lookup 获取指定柱的值。通过静态变量将 ATR 符号数组从回测的第一阶段传递到第二阶段。然后在自定义指标行中使用查找在特定日期时间(trade.EntryDateTimetrade.ExitDateTime)提取数组元素。

StaticVarSet( "CBT_ATR_" + Name(), ATR(30) );

if ( Status( "action" ) == actionPortfolio )
{
    bo = GetBacktesterObject();
    // run default backtest procedure without generating the trade list
    bo.Backtest( True );    

    // iterate through closed trades
    for ( trade = bo.GetFirstTrade( ); trade; trade = bo.GetNextTrade( ) )
    {
        trade.AddCustomMetric( "volatility_recent", Lookup( StaticVarGet( "CBT_ATR_" + trade.Symbol ), trade.ExitDateTime ) );
        //trade.AddCustomMetric( "proceeds", trade.Shares*trade.EntryPrice );
    }

    // iterate through open positions
    for ( trade = bo.GetFirstOpenPos( ); trade; trade = bo.GetNextOpenPos( ) )
    {       

        trade.AddCustomMetric( "volatility_recent", Lookup( StaticVarGet( "CBT_ATR_" + trade.Symbol ), Trade.ExitDateTime ) );
        //trade.AddCustomMetric( "proceeds", trade.Shares*trade.EntryPrice );
    }

    // generate trade list
    bo.ListTrades( );
}

编辑:感谢 fxshrat,他 post 在 https://forum.amibroker.com/t/unable-to-add-this-custom-metric-to-backtest-report/7153/2 编辑了答案 他的回答是 posted 在这里,post 没有参考是粗鲁的。向 fxshrat 和 Tomasz 致歉。

我发现您逐行复制其他人的文本和代码解决方案而不提供参考真的很有趣。

你在 Whosebug 的第二个 post 是逐行 复制 Tomasz 和我对 在 forum.amibroker.com

https://forum.amibroker.com/t/unable-to-add-this-custom-metric-to-backtest-report/7153