RTFM: Backtest Rotational Open Long

Good morning,

I'm using a monthly rotational system and I'd like to understand why the backtest is closing the trade in the middle of the current month.

Pad and align is active and Add artificial future bar is not active. I would like to see in Open long current position. If Pad and align is not active, I get the same result. Is anything in my code incorrect or should I change anything from settings to see in the backtest result the current Open Long?

Thanks in advanced!

imatge

// --- detect watchlist ---
wlnumber       = GetOption( "FilterIncludeWatchlist" );
watchlist      = GetCategorySymbols( categoryWatchlist, wlnumber );

// --- backtester settings --- 
PosQty = 1;
SetOption( "InitialEquity", 100000 );
SetOption( "MaxOpenLong", PosQty );
SetOption( "MaxOpenPositions", PosQty );
SetOption( "WorstRankHeld", PosQty );
SetOption( "AllowPositionShrinking", True );
SetPositionSize( 100 / PosQty, spsPercentOfEquity );
SetTradeDelays( 0, 0, 0, 0 );
SetBacktestMode( backtestRotational );
RoundLotSize = 1;


if ( Status( "stocknum" ) == 0 )
{
    StaticVarRemove( "Mom*" );
    StaticVarRemove( "RankMom*" );

    for ( i = 0; ( symbol = StrExtract( watchlist, i ) )  != "";  i++ )
    {
        SetForeign ( symbol );
			Mom  = ROC(Close,20) + ROC(Close,10);
        RestorePriceArrays();
        
        StaticVarSet( "Mom" + symbol, Mom );
    }
    // generate ranks for Momentum
    StaticVarGenerateRanks( "Rank", "Mom", 0, 1224 );
    RestorePriceArrays();
}

// --- get values and ranks for Momentum ---
symbol  =  Name();
Mom     =  StaticVarGet( "Mom"     +  symbol );
RankMom =  StaticVarGet( "RankMom" +  symbol );

// --- portfolio rebalancing ---
PositionScore = 1000 - RankMom; // always positive to prevent short trades

Filter = 1;

// --- generate columns for strategy exploration ---
AddColumn    ( Mom    , "Momentum(%)", 3.3, 1  );
AddColumn    ( RankMom, "RankMom"    , 1.0, 1 );
AddColumn    ( Close, "Close"    , 1.0, 1 );
AddTextColumn( Name() , "Tickers"    , 1.0, 1 );

Just some thoughts... As I have clue at what you should change but just want to share what I see.

SetTradeDelays( 0, 0, 0, 0 );

Means you trade at the close.

18th of february is the last day of the month (atleast, as long as we have not seen the 21st, at what point the 21st will be the last day of the month)

So you trade on the last day of the month, you trade at the close.

System is doing exactly programmed it to do....

One solution, making it more realistic also, is change the tradedelays to 1. My guess is that you then will not have this problem. It will also save you from a lot of trouble if you would want to trade this system in real life. Why create a monhtly system if you are going to trade it at the close of the last day of the month, requirying you to have real time data ec.

1 Like

Hi Henry! Thanks for your comments. You're right, if I set the delay to (1,1,0,0) and in settings the buy and sell price to Open, the open Long is correct. But as you can see the date is not correct. It looks it Opened the QQQ trade the 31th of july, but the trade was made the 1st of July.

imatge

The date wouldn't be a problem for me, but I went back to check what I would have seen in the backtest the first of july and as I was expecting I wouldn't have seen the open trade until the end of july, one month later.

imatge

Then, or I'm doing something wrong or this doesn't solve my problem.

Thanks again

If you use the SetTradeDelays then the settings in the settings menu are overruled. It does not matter what you set there.

How do you know it made the trade the 1st of july ? If the date showing in that is is showing 31st july, how do you know that is wrong ? (I have other price data.... so I can not verify it....)

I think you are using monthly data... I can imagine that will have a bar dated with the end of the month but you are actualy trading the first day of the month....

Set the date to the 2nd of july. 1st of july should work I would think... because there is trading data there, but just give a try to be sure. It might be "to the 1st" and not "including the 1st".

That's incorrect! It does not always override settings menu.
See his backtest mode

And then look here ->

AFL Function Reference - ENABLEROTATIONALTRADING

Important:
The rotational trading mode uses "buy price" and "buy delay" from the Settings Trade page as trade price and delay for both entries and exits (long and short).


Because you should read here (and upper quote from there)
https://www.amibroker.com/guide/afl/enablerotationaltrading.html

2 Likes

Thanks for the correction FX.

So Vegeta, if I understand correctly now, you will need to set the settings to use the open and also set the trade delay there. Your screenshot shows that you have it on the open but the buy delay is still at 0 which is not overruled in this case.

Curious if that is the fix...

1 Like

What I realized is that if I want this to work the settradedelays must not be in the code.

Thanks everyone

This topic was automatically closed 100 days after the last reply. New replies are no longer allowed.