how to perform a backtest using 2 ts?

#11
Dear trash, thank you very much.
It works very well and does just what I meant.

May you explain me something more about your program? I'm not so used in object programs. it would be very helpful if you added some rem text to each of new rows.
I don't understand how it avoids to entry in a new position if there is another one already open for the same symbol with the other system.

thank you

cippo.
In fact i was wrong: it still opens more position in the same symbol with the 2 different system.
It opens just 1 position with setOption("maxopenpositions,1"). But if I want more position in the portfolio (but not in the same symbol) and put for example: setOption("maxopenpositions,10") then it happens that same symbol could be traded with the 2 system in overlapped period. And I would avoid it.
I need something like:
if ( InWatchList( WL2 ) ) and ("Symbol1"+name not in trade)
Buy = Cross ....
Sell = Cross ...

but I dont know how properly write ("Symbol1"+name not in trade)

I know I'm boring you but I thank you due I'm learning so much

Cippo
 
#13
Dear trash, as you suggested I cloned my portfolio.
Now I have ~System1_symbolsname in Watchlist 1 and ~System2_symbolsname in Watchlist 2
I run this script you sent me:

WL1 = 1; // watchlist 1
WL2 = 2; // watchlist 2

SetOption( "InitialEquity", 100000 );
SetOption( "FuturesMode", 0 );
SetOption( "MaxOpenPositions", 10 );//I want up to 10 position open in my portfolio
SetOption( "AllowPositionShrinking", 1 );
SetOption( "ExtraColumnsLocation", 1 );

if ( InWatchList( WL1 ) )
{
Buy = Cross( C, MA( C, 20) ); // buy when close crosses ABOVE moving average
Sell = Cross( MA( C, 30), C ); // sell when closes crosses BELOW moving average
Short = Cover = 0;
SetPositionSize( 10, spsPercentOfEquity );
}
if ( InWatchList( WL2 ) )
{
Casuale=mtRandomA();
Buy = casuale > 0.70 AND casuale < 0.80 ;
Sell= Casuale <0.005;
Short = Cover = 0;
SetPositionSize( 10, spsPercentOfEquity );
}

Below you can see the results obtained running this double TS.



You can see that the system1 (Buy = Cross( C, MA( C, 20) )) opens a position for IT Symbol
On 13/03/1992 and close it on 16/6/92 but in this period (on 31/03/1992) a new position is open with System2 (Buy=casuale > 0.70 AND casuale < 0.80).
I’d like to avoid it. I’d be able to open several positions in my portfolio but never 2 positions in the same time for the same symbol.
 
Last edited:

trash

Well-Known Member
#14
Try this one.
Have not tested but just written!

Code:
// system membership
// by trash
// http://www.traderji.com/amibroker/95243-how-perform-backtest-using-2-ts.html#post1007370

WL1 = 0; // watchlist 1
WL2 = 1; // watchlist 2

systemnum = 2;

SetOption( "InitialEquity", 100000 ); 
SetOption( "FuturesMode", 0 ); 
SetOption( "MaxOpenPositions", systemnum * 1 ); 
SetOption( "AllowPositionShrinking", 1 );
SetOption( "ExtraColumnsLocation", 1 );

membership = "";
enterlong = exitlong = 0;
entershort = exitshort = 0;
size = 20;

if ( InWatchList( WL1 ) )
{
    enterlong = Cross( C, MA( C, 20 ) );
    exitlong = Cross( MA( C, 20 ), C );
    entershort = 0;
    exitshort = 0;
    
    SetPositionSize( size/2, spsPercentOfEquity );      
    membership = "System1";
}

if (  InWatchList( WL2 ) )
{
    enterlong = Cross( C, MA( C, 50 ) ); 
    exitlong = Cross( MA( C, 50 ), C ); 
    entershort = 0;
    exitshort = 0;
    
    SetPositionSize( size/2, spsPercentOfEquity );     
    membership = "System2";
}

Buy = Ref( enterlong, -1 ); 
Sell = Ref( exitlong, -1 ); 

Short = Ref( entershort, -1 ); 
Cover = Ref( exitshort, -1 ); 

BuyPrice = SellPrice = Open;
ShortPrice = CoverPrice = Open;
     

StaticVarSetText( "WL_Membership" + Name(), membership ); 
StaticVarSet( "WL_RSI" + Name(), RSI( 15 ) ); 

SetCustomBacktestProc("");  
/* Custom-backtest procedure follows */
if ( Status( "action" ) == actionPortfolio )
{
    bo = GetBacktesterObject();
    bo.PreProcess();	//  Do pre-processing (always required)

    OpenSymbol = "";

    for ( i = 0; i < BarCount; i++ )	//  Loop through all bars
    {
        for ( OpenPos = bo.GetFirstOpenPos(); OpenPos; OpenPos = bo.GetNextOpenPos() )
        {
            // create list of symbols and remove composite prefixes
            OpenSymbol += StrReplace( StrReplace( Openpos.Symbol, "~System1_", "" ), "~System2_", "" );
        }

        for ( sig = bo.GetFirstSignal( i ); sig; sig = bo.GetNextSignal( i ) )
        {
            sigsymbol = StrReplace( StrReplace( sig.Symbol, "~System1_", "" ), "~System2_", "" );
            RSIvar = StaticVarGet( "WL_RSI" + sig.Symbol );

            // iterate through opensymbol list  
            for ( num = 0; ( symbol = StrExtract( OpenSymbol, num ) ) != ""; num++ )
            {
                // if signal symbol is part of open symbol list then don't enter
                //if( StrFind( symbol, sigsymbol ) && sig.IsLong )
                    //sig.Price = -1;

                // or

                // if signal symbol is not part of open symbol list then check position score for new entry
                if ( StrFind( symbol, sigsymbol ) == 0 && sig.IsLong )
                    sig.PosScore = 1000 - RSIvar[i];
                else
                    sig.PosScore = 0;
            }
        }

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

    //#####################################################################################

    for ( trade = bo.GetFirstTrade(); trade; trade = bo.GetNextTrade() )
    {
        system = StaticVarGetText( "WL_Membership" + trade.Symbol );
        trade.AddCustomMetric( "System", system );
    }

    for ( trade = bo.GetFirstOpenPos(); trade; trade = bo.GetNextOpenPos() )
    {
        system = StaticVarGetText( "WL_Membership" + trade.Symbol );
        trade.AddCustomMetric( "System", system );
    }

    bo.PostProcess();
}
 
Last edited:
#15
Thank you Trash but still overlapping trading in the same symbol:
you can see System2 opens a position for BEN on 2/9/92 and close on in 8/4/93 and in this period other positions are open from System1. But I would like just 1 position for each symbol.

thank you for you patience.

Cippo
 

Attachments

Last edited:

trash

Well-Known Member
#16
I did not promise that it would work as I have not put much thought into example code. In any case you have to use custom backtest interface of AmiBroker to check open positions etc.
 
#17
Dear trash,
I'm trying to understand your script and amending it.

Do you think we need a coma here?
OpenSymbol += StrReplace( StrReplace( Openpos.Symbol, "~System1_", "" ), "~System2_", "" );


Here below istruction for StrExtract(list, item):
Extracts given item (substring) from comma-separated list of items.
item is a zero-based index of the item in the list


Another question: can I in someway, while the program is running, knowing the value of a variable? for example the value of "OpenSymbol"?
I have tried with "printf" but dont know where is the 'output windows'.

for ( OpenPos = bo.GetFirstOpenPos(); OpenPos; OpenPos = bo.GetNextOpenPos() )
{
// create list of symbols and remove composite prefixes
OpenSymbol += StrReplace( StrReplace( Openpos.Symbol, "~System1_", "" ), "~System2_", "" );
Printf(OpenSymbol);

}


thank you

cippo
 

trash

Well-Known Member
#18
Dear trash,
I'm trying to understand your script and amending it.

Do you think we need a coma here?
OpenSymbol += StrReplace( StrReplace( Openpos.Symbol, "~System1_", "" ), "~System2_", "" );


Here below istruction for StrExtract(list, item):
Extracts given item (substring) from comma-separated list of items.
item is a zero-based index of the item in the list
StrReplace is not StrExtract

Another question: can I in someway, while the program is running, knowing the value of a variable? for example the value of "OpenSymbol"?
I have tried with "printf" but dont know where is the 'output windows'.

for ( OpenPos = bo.GetFirstOpenPos(); OpenPos; OpenPos = bo.GetNextOpenPos() )
{
// create list of symbols and remove composite prefixes
OpenSymbol += StrReplace( StrReplace( Openpos.Symbol, "~System1_", "" ), "~System2_", "" );
Printf(OpenSymbol);

}


thank you

cippo

_Trace() !



Also see

http://www.amibroker.org/userkb/category/real-time-auto-trading/debugging-afl/
http://www.amibroker.org/userkb/2007/04/21/controlling-debugview-output/
 
#19
StrReplace is not StrExtract

I meant that in the following row, maybe we need to add a coma between symbols:
OpenSymbol += StrReplace( StrReplace( Openpos.Symbol, "~System1_", "" ), "~System2_", "" )+",";

If we dont write a coma between symbols, the instruction StrExtract(list, item) would not be able to extract the different symbols.
 

Similar threads