Register Login
TutorialsStock Rotation & Screening    February 22, 2012
Stock Rotation & Screening

TradersStudio makes it easy to develop relative strength trading models


In TradersStudio it’s easy to implement many strategies which are impossible in other platforms. Let’s look at the concept of relative strength analysis with a filter. This is a powerful but simple concept. We want to buy only the top ranking stocks on a relative basis in each portfolio. We also want to keep our investment in balance, so if one stock increases in value it does not become too large of a percentage of our portfolio. We use dynamic rebalancing, by buying or selling shares once a month in order to have the same dollar value invested in each stock.


TradersStudio has two major constructs; the first is the session. The Session allows us to run a set of rules, a system on one or more markets. We will use this portfolio capability in the following example. Let’s now look at our system as below.


Session Level Code (Trading System)


The code below calculates a “Filter which is price relative to a 200 bar moving average”. We also have a ranking function which is a 50 bar percentage rate of change. We then pass these to the trade plan level. We use the marketvar function to set a MAPermission screen value to true/false.


' TradersStudio® copyright© 2004-2011, All rights reserved


Sub RankSystem1()
    Dim MADiff
    Dim RawRank
    MADiff= Close-Average(Close,200,0)
    RawRank=(Close-Close[50])/Close[50]
    marketvar("MAPermission")=MADiff>0
    marketvar("RawRank")=RawRank
End Sub


This system is used for all the sessions which we will use. We will use the Nasdaq 100 stocks in our example and in order to see exactly how this works we will create 3 different sessions.

  1. RelativeRankSess1, contains 6 stocks starting with A
  2. RelativeRankSess2, contains 4 stocks starting with B
  3. RelativeRankSess2, contains 6 stocks starting with C

Since, we are using letters of the alphabet in this test; it will make it easier to see if it’s working correctly. Let’s start with our first trade plan. This trade plan takes the top ranked stock from each session and on passing the permission screen; it will either buy or rebalance it monthly.


Create a trade plan and use the RelativeRankTest script and add all the above three sessions. Make the account size $100,000. Now, run the trade plan. You shall need to select overlapping date rate range since we are doing relative strength analysis.


Let’s first look at our custom ranking function which creates a ranking array for a given session.


' TradersStudio® copyright© 2004-2011, All rights reserved


Function RelativeRankFunc(SessionObject as tsprocessor.ISession) As Array
    Dim MCount As Integer
    Dim CustomPer As Array
    Dim count As Integer
    MCount=SessionObject.MarketCount
    ReDim(CustomPer,MCount,2)

    For count=0 To MCount-1
        CustomPer[count,0]=SessionObject.Market(count).symbol(0)
        CustomPer[count,1]=SessionObject.Market(count).MarketVar("RawRank")
    Next
    RelativeRankFunc=CustomPer
End Function


The above function is used in the trade plan below; the “Stock plan” which takes the top stocks from each session based on their relative strength measures of custom performance.


' Stock plan takes the top market from each session based on relative strength measures
' or custom performance
' TradersStudio® Copyright© 2004-2011, All rights reserved


Sub RelativeRankTest()
    Dim M As Integer
    Dim DollarPerTrade
    Dim StartAccount
    Dim DollarsPerTrade
    Dim Measure
    Dim custper As Array
    Dim sCount As Integer

    For sCount =0 To tradeplan.SessionCount -1
        DollarsPerTrade=tradeplan.SummEquity/(TradePlan.SessionCount)
        TradePlan.Session(sCount).UnitSize = 1
        TradePlan.Session(sCount).RankingType() = Ordinal ' eordinal

        custper=RelativeRankFunc(TradePlan.Session(sCount))
        Tradeplan.Session(sCount).SetCustomPerformance(custper)

        ' For each session Loop though the trading plans.
        For M = 0 To TradePlan.Session(sCount).MarketCount - 1
                If Month(tradeplan.Session(sCount).Market(M).data(0,"Date",0))<>Month(tradeplan.Session(sCount).Market(M).data(0,"Date",1)) Then

                        Measure = tradeplan.Session(sCount).CustomPerformance(tradeplan.session(sCount).Market(M).symbol(0))

                        Dim sizeadjust as integer

                        If Measure = 1 and tradeplan.session(sCount).Market(M).marketvar("MAPermission")=true Then

                            '**************************************
                            If tradeplan.MarketType=3 Then
                                sizeadjust=Floor((DollarsPerTrade)/TradePlan.Session(sCount).Market(M).Data(0,"TSClose",0))-TradePlan.Session(sCount).Market(M).NumContractsHeld
                            End If

                            If tradeplan.MarketType=1 Then
                                sizeadjust=Floor((DollarsPerTrade)/TradePlan.Session(sCount).Market(M).Data(0,"Close",0))-TradePlan.Session(sCount).Market(M).NumContractsHeld
                            End If

                            If sizeadjust<>0 Then
                                If sizeadjust>0 Then
                                    TradePlan.Session(sCount).Market(M).TSBuy("",sizeadjust,0,Market,Day)
                                End If

                                If sizeadjust<0 Then
                                    sizeadjust=sizeadjust*-1
                                    TradePlan.Session(sCount).Market(M).TSExitlong("","",sizeadjust,0,Market,Day)
                                End If
                            End If

                        Else

                            If TradePlan.Session(sCount).Market(M).MarketPositionPlus("")=1 Then
                                TradePlan.Session(sCount).Market(M).TSExitlong("","",TradePlan.Session(sCount).Market(M).NumContractsHeld,0,Market,Day)
                            End If
                        End If
                    End if
                Next
            Next
End Sub


You can see that all orders buy and sell “new buy’s” or “buy’s and sell’s” as per the rebalancing done inside the trade plan. Also all exits are (a) a complete exit, if the stock is no longer top ranked or, (b) a partial exit as per the rebalancing done inside the trade plan.


Let’s now look at how this trade plan works. Below are the results of the “Trades for Trade Plan



Firstly, we can see that we rebalance on the second trading day of the month. Our rebalance active orders are placed when Month(date)<>Month(Date[1]), thus these orders are executed on the next bar. This code selects the top ranked stock from each session and invests in the same. It also ensures that we have an equal dollar value for each stock, for example let’s look at CELG; we purchased 702 shares on 07/05/2006. On 08/02/2006, we exited 18 shares as we now have a higher percentage of our money in CELG. In August we exited the remaining 684 shares and purchased 1423 shares of CSCO.


Likewise, in AKAM we purchased 908 shares on 07/05/2006. Using rebalancing, we exited 77 of them on 08/02/2006. We then exited another 87 on 10/03/2006 as the price of AKAM increases. We finally exited our complete position on 12/04/2006.


Let’s look at our current positions, the number of shares owned in each portfolio are AAPL (165 shares), BBBY (1118) and COST (757). These shall be rebalanced on the first trading day of November.


Click below for white paper on using Stock Rotation & Screening.

Stock Rotation & Screening.pdf


HYPOTHETICAL DISCLAIMER:
NOTICE: HYPOTHETICAL PERFORMANCE RESULTS HAVE MANY INHERENT LIMITATIONS, SOME OF WHICH ARE DESCRIBED BELOW. NO REPRESENTATION IS BEING MADE THAT ANY ACCOUNT/SYSTEM WILL OR IS LIKELY TO ACHIEVE PROFITS OR LOSSES SIMILAR TO THOSE SHOWN. IN FACT, THERE ARE FREQUENTLY SHARP DIFFERENCES BETWEEN HYPOTHETICAL PERFORMANCE RESULTS AND THE ACTUAL RESULTS SUBSEQUENTLY ACHIEVED BY ANY PARTICULAR TRADING PROGRAM. ONE OF THE LIMITATIONS OF HYPOTHETICAL PERFORMANCE RESULTS IS THAT THEY ARE GENERALLY PREPARED WITH THE BENEFIT OF HINDSIGHT. IN ADDITION, HYPOTHETICAL TRADING DOES NOT INVOLVE FINANCIAL RISK, AND NO HYPOTHETICAL TRADING RECORD CAN COMPLETELY ACCOUNT FOR THE IMPACT OF FINANCIAL RISK IN ACTUAL TRADING. FOR EXAMPLE, THE ABILITY TO WITHSTAND LOSSES OR TO ADHERE TO A PARTICULAR TRADING PROGRAM IN SPITE OF TRADING LOSSES ARE MATERIAL POINTS WHICH CAN ALSO ADVERSELY AFFECT ACTUAL TRADING RESULTS. THERE ARE NUMEROUS OTHER FACTORS RELATED TO THE MARKETS IN GENERAL OR TO THE IMPLEMENTATION OF ANY SPECIFIC TRADING PROGRAM WHICH CANNOT BE FULLY ACCOUNTED FOR IN THE PREPARATION OF HYPOTHETICAL PERFORMANCE RESULTS AND ALL OF WHICH CAN ADVERSELY AFFECT ACTUAL TRADING RESULTS. TRADING INVOLVES RISK OF LOSS AND IS NOT SUITABLE FOR EVERYONE.

 TradersStudio, Inc. ® Copyright 2004-2012 All Rights Reserved   Terms Of Use  Privacy Statement