How to output to file indicator value in Backtest preprocess

How can I add the value of the MACD to the output text file in the following code?
Thanks in advance

SetCustomBacktestProc( "" ); 

if( Status( "action" ) == actionPortfolio ) 
{ 
    fh = fopen( "filename.txt", "w" ); 
    dt = DateTime(); 
    bo = GetBacktesterObject();   //  Get backtester object 
    bo.PreProcess();   //  Do pre-processing (always required) 

    for( i = 0; i < BarCount; i++ )   //  Loop through all bars 
    { 

        bo.ProcessTradeSignals( i );   //  Process trades at bar (always required) 

        if( fh ) 
        { 
            for( openpos = bo.GetFirstOpenPos(); openpos; openpos = bo.GetNextOpenPos() ) 
            { 

                fputs( openpos.Symbol + "," + DateTimeToStr( dt[ i ] ) + "," + openpos.GetPrice( i, "C" )+"\n", fh ); 
            } 

        } 


    } 
        if( fh ) fclose( fh ); 
    bo.PostProcess();   //  Do post-processing (always required) 
} 

Buy = Cross( MACD(), Signal() ); 
Sell = Cross( Signal(), MACD() ); 
SetPositionSize( 10, spsPercentOfEquity );

Again - new post and code submitted without reading “How to use this site”?
Code is supposed to be surrounded by

[code]
... your code here
[/code]

Fixed it already but please use proper formatting next time.

Getting back to the question - it is answered already in the Knowledge Base:
http://www.amibroker.com/kb/2014/11/20/how-to-show-indicator-values-in-backtest-trade-list/

1 Like

Hi Tomasz

My apologies

Regards

Gerry

1 Like

Hi

Following on from my earlier post,
I wish to add the ATR value to the output in example Code 1.
This code outputs to file data for every day when a trade is open / live for a symbol.

Tomasz pointed me to example code 2 which outputs ATR for each trade.
My understanding is

  1. indicator values have to be stored in Static vars during the 1st ‘symbol level’ pass of backtester and then read during the 2nd ‘portfolio’ pass of backtester.
  2. Code 1 uses mid level PreProcess because it iterates through the bars, and Code 2 uses high level Backtest to iterate through the trades.

Can anyone advise me how to amend code 1 to add the output of the ATR value?

Thank you

// Code 1
// Output to file Symbol, Date, Close on all dates during a trade ( no output for non trade days )

SetCustomBacktestProc( "" ); 

if( Status( "action" ) == actionPortfolio ) 
{ 
    fh = fopen( "filename.txt", "w" ); 
    dt = DateTime(); 
    bo = GetBacktesterObject();   		//  Get backtester object 
    bo.PreProcess();   					//  Do pre-processing (always required) 

    for( i = 0; i < BarCount; i++ )   	//  Loop through all bars 
    { 

        bo.ProcessTradeSignals( i );   	//  Process trades at bar (always required) 

        if( fh ) 
        { 
            for( openpos = bo.GetFirstOpenPos(); openpos; openpos = bo.GetNextOpenPos() ) 
            { 

                fputs( 
					openpos.Symbol + "," + 
					DateTimeToStr( dt[ i ] ) + "," + 
					openpos.GetPrice( i, "C" )+
					"\n", fh ); 
            } 

        } 


    } 
        if( fh ) fclose( fh ); 
    bo.PostProcess();   				//  Do post-processing (always required) 
} 

Buy = Cross( MACD(), Signal() ); 
Sell = Cross( Signal(), MACD() ); 
SetPositionSize( 10, spsPercentOfEquity );
// Code 2
//How to display indicator values in the backtest trade list
//http://www.amibroker.com/kb/2014/11/20/how-to-show-indicator-values-in-backtest-trade-list/

SetCustomBacktestProc( "" );

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( ) )
    {
        // read ATR values and display as custom metric
        symbolATR = StaticVarGet( trade.Symbol + "ATR" );
        trade.AddCustomMetric( "Entry ATR", Lookup( symbolATR, trade.EntryDateTime ) );
    }

    // iterate through open positions
    for ( trade = bo.GetFirstOpenPos( ); trade; trade = bo.GetNextOpenPos( ) )
    {
        // read ATR values and display as custom metric
        symbolATR = StaticVarGet( trade.Symbol + "ATR" );
        trade.AddCustomMetric( "Entry ATR", Lookup( symbolATR, trade.EntryDateTime ) );
    }

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

// your trading system here
Buy = Cross( MACD(), Signal() );
Sell = Cross( Signal(), MACD() );

// assign indicator values to ticker-specific variables
StaticVarSet( Name() + "ATR", ATR( 15 ) );

amifriend

Everything you need to know is there in front of you.

You need to store the ATR in a static variable for each symbol in the non-CBI part of your code (as in the example). Then call back the value with StaticVarGet in the CBI open position loop (as in the example). Then you can use the value in your fputs statement, converted to a string with NumToStr or StrFormat.

You’ll learn much more by trying it and correcting your own mistakes, than by getting someone to do it for you.

Hi HelixTrader

Thank you for your guidance which has enabled me to solve the problem.
I had tried various ideas but you pointed me in the correct direction.

I agree with your last statement, but sometimes you just get stuck.

1 Like

@HelixTrader is right. You have to do some work yourself. This snippet contains all you need, you just need to figure out where to copy-paste it.

symbolATR = StaticVarGet( openpos.Symbol + "ATR" );
yourATRatentry =  Lookup( symbolATR, openpos.EntryDateTime );
line = openpos.Symbol + "," +
         DateTimeToStr( dt[ i ] ) + "," +
         openpos.GetPrice( i, "C" ) + "," +
         yourATRatentry + "\n";
fputs( line, fh );
1 Like