Periodicity Weekly & CBI

Hi, I’m working through this:
http://www.amibroker.org/userkb/2008/03/16/amibroker-custom-backtester-interface-2/

Periodicity is set to Weekly.

My code, the top 5 lines are mine, the rest is from the kb:

BuyPrice = Open;	// set buying price array
SellPrice = Open;	// set selling price array
SetTradeDelays( 1, 1, 1, 1 );
Sell = Close > MA(Close,5);
Buy = Close < MA(Close,5);

SetCustomBacktestProc( "" );

if( Status( "action" ) == actionPortfolio )
{
    bo = GetBacktesterObject(); // Get backtester object
    bo.PreProcess(); // Do pre-processing

    for( i = 0; i < BarCount; i++ ) // Loop through all bars
    {
        for( sig = bo.GetFirstSignal( i ); sig; sig = bo.GetNextSignal( i ) )
        {
            // Loop through all signals at this bar
            if( sig.IsEntry() && sig.IsLong() ) // If this signal is a long entry (ie. buy)
            {
                evol = Foreign( "~evol_" + sig.Symbol, "V" ); // Get stock's composite volume array
                psize = sig.PosSize; // Get position size specified in AFL code

                if( psize < 0 ) // If it's negative (a percentage of equity)
                    psize = ( -psize / 100 ) * bo.Equity; // Convert to dollar value using current equity

                scnt = psize / sig.Price; // Calculate number of shares for position size

                if( scnt > evol[i] / 10 ) // If number of shares is > 10% of volume EMA
                {
                    scnt = evol[i] / 10; // Limit number of shares to 10% of EMA value
                    psize = scnt * sig.Price; // Calculate new position size
                }

                if( psize < 5000 ) // If position size is less than $5,000
                    psize = 0; // Set to zero so buy signal will be ignored
                else
                {
                    if( psize > 50000 ) // If position size is greater than $50,000
                        psize = 50000; // Limit to $50,000
                }

                sig.PosSize = psize; // Set modified position size back into object
            }
        } // End of for loop over signals at this bar

        bo.ProcessTradeSignals( i ); // Process trades at this bar
    } // End of for loop over bars

    bo.PostProcess(); // Do post-processing
}

Issue: The trade list shows entries & exits on mid weeks days. I am expecting Monday only for entry and exit.

What is incorrect in my code?

Regards.

If you set the Periodicity to Weekly in the Analysis Settings, then each of your bars in Phase 1 of your AFL will correspond to the last trading day of the week for each of your symbols. Typically, that will be Friday, but may be Thursday for a particular week if Friday is a non-trading day.

In the CBT, the set of bars will correspond to all of end-of-week bars that were used in Phase 1. If you’re using Pad & Align, you will have one bar at the end of each week in the CBT. If you are not using P&A, then you may have multiple bars per week when your data has holes in it.

Incidentally, setting trade delays to 1 with Weekly periodicity means to trade on the next weekly bar, not the next daily bar following the end of the week. If you want to trade on Mondays, you will need to run a test with Daily periodicity, and use time frame compression functions to generate signals based on weekly data.

4 Likes

Wow, great answer, Thanks. I’ll work my way through it.

Have a follow on query.

In my backtest the periodicity is set as Monthly in the Analysis settings and i am running a successful Monthly test.

Now i want to repeat the same test for 2 monthly basis...How to use Timeframeset() that will supercede the periodicity settings in the analysis settings/

Tks