Testing the Two Least Risky Markets Per Sector

Limiting sector exposure can help limit draw down.  When back testing with most platforms you have to throw a huge portfolio at an algorithm and hope for the best.  Does it matter that all six markets in your currency sector is currently long?  You bet it does.  If a geo-political event occurs then you could potentially get stopped out of all six positions on the same day and experience a substantial loss.  What if you could rank each market in a sector by its risk and then choose the two or three least risky ones to trade on the next trade signal?  You would still be trading the same number of sectors but each sector would not be overexposed.

Here is an example of a Python list that includes the sector number, market number and long entry risk:

(0, 6, 2019), (0, 3, 2649), (0, 5, 3200), (0, 2, 3429), (0, 0, 5000), (0, 1, 9012), (0, 7, 11012)

 

Notice only one sector is shown – the grouping of the three values where the first value is the same.  You will notice that the third value in the grouping is ascending.  Now suppose you have a huge portfolio where you have 30 or more markets.  Here is an example of the first two sectors of a 30+ market portfolio risk listing:

                   sect mkt long risk short risk
0 tuple (0, 6, 2019, 2480)
1 tuple (0, 3, 2649, 6079)
2 tuple (0, 5, 3225, 9012)
3 tuple (0, 2, 3429, 3110)
4 tuple (0, 0, 5000, 2937)
5 tuple (0, 1, 9012, 3074)
6 tuple (0, 4, 11899, 4074)
7 tuple (1, 7, 2549, 6760)
8 tuple (1, 10, 3032, 8626)
9 tuple (1, 8, 4115, 7434)
10 tuple (1, 9, 14510, 6099)


So in sector #0 you would trade market #6 and market # 3
And in sector #1 you would trade market #7 and market #10

Having the ability to sort all the markets in each sector prior to the trading day provides the information to make an informed decision.  Once you rank the risk on a per market per sector basis, you can then turn on the markets that are the two least risky in each sector.

Here is how you build your list that contains the sector, market and long and short 
risk.

if curMarket == 0 and firstMarketLoop == False:
for cnt in range(0,numMarkets):
curSect = getCurrentSector(myComNameList[cnt],sectorList)
sectMarketRisk[cnt] = (curSect,cnt,marketVal1[cnt],marketVal2[cnt])
canBuyList[cnt] = canShortList[cnt] = 0

Here I am loading the list sectMarketRisk[cnt] with the curSect, market number, long risk
and short risk.

Here is how I defined the list - remember in Python you can a have list of sublists.
I initialize the list using square '[' brackets. Each element will contain 4 sub
elements [sector number, market number, long risk and short risk].

sectMarketRisk = list()
sectMarketLongRisk = list()
sectMarketShortRisk = list()
canBuyList = list()
canShortList = list()

for curMarket in range(0,numMarkets):
sectMarketRisk.append([0,0,0,0]) <------ initialize list with 0's
canBuyList.append(99999) <----- this list contains [0 - numMarkets] so load with 99999
canShortList.append(99999) <----- same here - these lists contain the number of the market
<----- so can't be zero because zero is the first market in port.
Long and short risk is defined as follows:

buyLevel = highest(myHigh,80,curBar,1)
shortLevel = lowest(myLow,80,curBar,1)
longExit = lowest(myLow,20,curBar,1)
shortExit = highest(myHigh,20,curBar,1)
marketVal1[curMarket] = (buyLevel - longExit)*myBPV <--- long risk
marketVal2[curMarket] = (shortExit - shortLevel)*myBPV <--- short risk

Remember you must use a list to store each market's risk values indexed by the market number
marketVal1[curMarket] - holds the long risk value for the curMarket market

Okay now that the list has been built copy the information
pertaining to long risk and short risk into their own lists

sectMarketShortRisk = sectMarketLongRisk = sectMarketRisk
sectMarketLongRisk.sort(key=operator.itemgetter(0,2),reverse = False)

Here I use sectMarketShortRisk and sectMarketLongRisk and assign them the
sectMarketRisk - this is like x = y = 0 where x and y == 0. I do this so
I can sort the two lists in place and key off the sector number and either the long
or short risk. The sort method of a list can key of whatever element is in the sublist.
Here I sort first by the sector number and then by long risk. The second ShortRisk list
is sorted by sector number and shortRisk.

Once you build your riskList it is very simple
in python to sort them on more than one key

sectMarketLongRisk.sort(key=operator.itemgetter(0,2),reverse = False)
sectMarketShortRisk.sort(key=operator.itemgetter(0,3),reverse = False)

So the lists will be sorted first by sector and then by long/short risk

///////////////////////////////////////////////////////////////////////////////////////////
Loop through each sector
and then loop through all the markets
on a daily basis. Turn on the first two markets
in each sector sublisting

for j in range(0,len(sectorList)):
lsectCnt = 0
for i in range(0,len(sectMarketLongRisk)):
if sectMarketLongRisk[i][0] == j:
if lsectCnt < 2: --> stop assigning after first 2 markets
canBuyList[i] = sectMarketLongRisk[i][1]
lsectCnt += 1


ssectCnt = 0
for i in range(0,len(sectMarketShortRisk)):
if sectMarketShortRisk[i][0] == j:
if ssectCnt < 2: --> stop assigning after first 2 markets
canShortList[i] = sectMarketShortRisk[i][1]
ssectCnt += 1

//////////////////////////////////////////////////////////////////////////////////////////
Conceptually allowing 3 markets per sector,
CURRENCY ENERGY
B E C A J S D C H R N
P C D S Y F X L O B G
Your buy list might look like [1,0,1,0,0,0,1,1,1,1,0,0...
Trade BP, CD, DX, CL, HO, RB
In reality it would look like this if you traded 3 markets per sector:
0 int 0 ---> currency sector trade BP, DX an CD
1 int 6
2 int 2
3 int 99999
4 int 99999
5 int 99999
6 int 99999
7 int 7 ---> energy sector trade CL, HO, NG
8 int 8
9 int 9
10 int 99999
11 int 11 ---> next sector trade markets 11, 12, 13
12 int 12
13 int 13
14 int 99999
15 int 99999
16 int 18 ---> next sector trade markets 18, 17, 19
17 int 17
18 int 19
19 int 99999
20 int 99999


Code that aligns the first two least risky markets with market loop

This is pretty heady stuff, but with a little work you can work yourself through the logic and create similar analysis.  In my next post I will reveal the results to see if there is any benefit to trading the least two or three risky markets per sector.  I will post the complete logic a little later.  I am thinking of posting stand-alone modules on gitHub or a similar type website.

classic

yes

About This Site

This site is home to George’s Excellent Adventure into TradingSimula_18 and Python.  George grew tired of the old and expensive back testing software so he created his own and now is able to test and develop  Trend Following Systems utilizing EOD data and EOD intra-testing portfolio management.  This software, TradingSimula_18 can be found in his Trend Following Systems: A DIY Project – Batteries Included book – now in its 2nd edition.

December 2024
M T W T F S S
 1
2345678
9101112131415
16171819202122
23242526272829
3031