Limit orders with rank and criteria

Hi everyone,

I am currently experimenting with placing limit orders with the following rules:

(1) C > 200MA
(2) RSI(2) < 50
(3) Top 5 ROC(100) values
(4) Place limit order at 1% below previous close.

Below is part of the backtesting code that I have which I used for exploration as well.

//Watchlist Lookup
Listname = "MRTSelect";							//Watchlist Name
category = categoryWatchlist;
Listnum = CategoryFind( Listname, category );
List = CategoryGetSymbols( category, Listnum);	

//Parameters
Rule1 = C > MA(C, 200);							// Close above 200MA
Rule2 = RSI(2) < 50;								// 2-day RSI below 50

BuyRule = Rule1 AND Rule2;

//Ranking by ROC

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

     for ( n = 0; ( Symbol = StrExtract( List, n ) )  != "";  n++    )
     {
         SetForeign ( symbol );

         // value used for scoring
         values = ROC(C, 100);
         RestorePriceArrays();
         StaticVarSet (  "values"  +  symbol, values );
         _TRACE( symbol );
     }

     StaticVarGenerateRanks( "rank", "values", 0, 1224 );
}

symbol = Name();

score = StaticVarGet ( "values" +  symbol );
finalrank = StaticVarGet ( "rankvalues" +  symbol );

AddColumn (score, "score");
AddColumn (finalrank, "finalrank");

Filter = BuyRule;

As the rules often give rise to multiple signals, itt is not possible to key in limit orders for all counters.

I tried to overcome the problem by generating a ranking based on ROC(100) and inserted a buy rule of finalrank <= 5 for the backtester.

I also did an exploration of this portion of the code and found the following ranking scores.

Explore

I notice that the ranking was not in cardinal order as I believe the ranking was done on all counters in the universe instead of the ones that fulfilled the buy rule.

As such, for example on 14/2/2022, I would have placed limit orders for 5 counters, DLTR, COP, XOM, CVS and F but due to the buy rule of finalrank <=5, the backtester would place only for DLTR, COP, XOM and CVS.

Would it be possible to "rank the finalrank" so that the buy rule can operate as above?

Thank you experts for your help in advance.

Cheers!

@tanksee, try adding (duplicate) your buy rule logic in the "ranking loop" to lower the ranking of the stocks that do not fit your criteria.

Something like this:

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

    for( n = 0; ( Symbol = StrExtract( List, n ) )  != "";  n++ )
    {
        SetForeign( symbol );

        Rule1 = C > MA( C, 200 );							// Close above 200MA
        Rule2 = RSI( 2 ) < 50;								// 2-day RSI below 50
        BuyRule = Rule1 AND Rule2;
        // value used for scoring
        values = ROC( C, 100 );
        RestorePriceArrays();
        // Assign a low value to the stocks that do not fit the buy rule
        StaticVarSet( "values"  +  symbol, IIf( buyRule, values, -1000 ) );
        _TRACE( symbol );
    }

    StaticVarGenerateRanks( "rank", "values", 0, 1224 );
}

FWIW, probably this is not the best/optimal approach, but I did a very short test and it seems to work.

2 Likes

@tanksee a variation on @beppe solution, include your rules in the score generating line

values = IIf(C > MA(C, 200) AND RSI(2) < 50, ROC(C, 100), 0);

With the above logic you will continue to get ranks for stocks that have a negative ROC(100) but meet your first 2 rules. And they will appear on your Explore. So you could use this,

values = IIf(C > MA(C, 200) AND RSI(2) < 50 AND ROC(C, 100)>0 , ROC(C, 100), 0);

Good luck with the limit order portion, as you should search the forum for proper handling of portfolio level limit orders.

3 Likes

@ beppe, @ portfoliobuilder

Grateful to both of you for the help! The ranking is working great now.

Great use of the IIf function, I really should dive into the manual more.

Thanks!

3 Likes

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