Very interesting code by Paul.  Thanks for sharing it.

Thinking about how it works, here's how I explain it to myself:

Internally, AB will sort/order the backtester's 'Signal' objects
according to absolute value of PositionScore.  Later in the code, we
will retrieve from these objects by stepping thru them sequentially. 
Since the 'Signal' objects have already been sorted by AB into
PositionScore order, the order in which they will be encountered when
we step-thru them _is_ the ordinal order of the ranking metric.

Anybody care to confirm or correct this?


--- In [email protected], Ken Close <[EMAIL PROTECTED]> wrote: 
> For those interested in the code written for this test by Paul Ho,
here it is.  

run = Optimize("run", 1, 1, 2, 1);

function ranking(Ordinal)

{

    switch(Ordinal)

    {

    case 1:

        res = ROC(C, 14) + 1000;

        break;

    case 2:

        res = RSI(14);

        break;

    default:

        res = EMA(C, 50);

        break;

    }

    return res;

}

PositionScore = ranking(run); // WHAT YOU WANT TO RANK

 

SetOption("MaxOpenPositions", 50 ); //AB only keeps 2* maxpos top rank
signals

SetBacktestMode( backtestRegularRaw );

Buy=1;

Sell= Short = Cover = 0;

ab = CreateObject("broker.Application");

target = ab.Stocks(Name());

target.isDirty = True;

SetCustomBacktestProc("");

if( Status("action")==actionPortfolio )

{

    bo = GetBacktesterObject();

    bo.PreProcess();

    dt = DateTime();

//  fh = fopen("output.csv", "w"); uncomment these if you want to save
it to a csv file as well

//  fputs("Symbol,Date,Rank\n", fh);

    for( i = 0; i < BarCount; i++ )

    {

        k = 1;  

        for( sig = bo.GetFirstSignal( i ); sig; sig =
bo.GetNextSignal( i ) )

        {

//         Line = sig.Symbol + "," + DateTimeToStr(dt[i]) + "," + k +
"\n";

//         fputs(Line, fh);

           target = ab.Stocks(sig.Symbol);

           qt = target.Quotations(DateTimeToStr(dt[i]));

           if(qt)

           {

               if(run == 1)

                   qt.OpenInt = k;

               else

                   qt.OpenInt += k;

           }

           target.isDirty = True;

       k++;

        }

    }

    bo.PostProcess();

//  if(fh)fclose(fh);

}



Reply via email to