Write Out Of Sample trades to a file during walk-forward optimization

I am attempting to write various information about OOS trades during walk-forward optimization. For example, I would like to save the closing price of a stock for every day my system is in a trade. The following code works well for a regular backtest, but it appears I only write the IS trade data during a walk-forward optimization. Is there a way to access the closed OOS trades during optimization?

Thanks in advance.

SetCustomBacktestProc("");
if (Status("action") == actionPortfolio)
{
    bo = GetBacktesterObject();
    bo.PreProcess();

    path = "test.csv";
    fh = fopen(path, "a");
    fputs("Bar#,Symbol,Date,Shares,Price\n", fh);

    y = Year();
    m = Month();
    d = Day();
    thisDateTime = DateTime();

    for (bar = 0; bar < BarCount; bar++)
    {
        for (trade = bo.GetFirstTrade(); trade; trade = bo.GetNextTrade())
        {
            // trade variable now holds Trade object
            if (trade.ExitDateTime == thisDateTime[bar - tradeDelay])
            {
                totalBars = trade.BarsInTrade + tradeDelay;
                for (i = -totalBars; i < 0; i++)
                {
                    lookback = bar + i;

                    bStr = StrFormat("%g,", totalBars + i);
                    fputs(bStr, fh);

                    symbols = trade.Symbol;
                    fputs(symbols + ",", fh);

                    dStr = StrFormat("%02.0f%02.0f%02.0f,", y[lookback], m[lookback], d[lookback]);
                    fputs(dStr,fh);

                    shStr = StrFormat("%4.2f,", trade.Shares);
                    fputs(shStr, fh);

                    symC = Foreign(trade.Symbol, "C");
                    prStr = StrFormat("%4.2f\n", symC[lookback]);
                    fputs(prStr, fh);
                }
            }
        }

        bo.ProcessTradeSignals(bar);
    }

    bo.PostProcess();
    fclose(fh);
}

I don't see the point when AmiBroker automatically produces Out-of-sample backtest report with trade list and it contains all data you want, so you can just read what is already there. This is way faster than custom backtest. You can use Report Explorer to access all past reports.

As for your code - it is incorrect. To list closed threads you should call bo.Backtest() method and list trades using GetFirstTrade/GetLastTrade as shown in the manual in Examples 2 and 3: http://www.amibroker.com/guide/a_custommetrics.html

And you should definitely NOT use Foreign. That is big no-no. Trade exit/entry prices are property of trade object so you definitely don't need to use Foreign.

I used Foreign because I want the closing price of the equity for every bar while in the trade and not just the Trade exit/entry prices. I didn't use bo.Backtest() for the same reason.

I am processing some data outside of Amibroker in a separate script. How would I access the day-to-day price changes for the equity during the trade from the backtest report?

I am not saying what I am doing is correct, just explaining my reasoning.

You can get the close price in different way WITHOUT the use of Foreign. To display ANY value (including prices or indicators) you don't need that. Read the knowledge base: http://www.amibroker.com/kb/2014/11/20/how-to-show-indicator-values-in-backtest-trade-list/

Thank you! Just what I needed.