Using TradingSimula-18 to Poll and Sort Daily Market Risk and Limit Market/Sector Exposure
In this post I hope to demonstrate the power of TS-18. I want to back test a simple Donchian system that only trades the three least risky (defined by long entry price – long exit price or short entry price – short exit price) markets on a per sector basis. In this test there will be seven sectors:
- Currencies
- Energies
- Metals
- grains
- Financials
- Softs
- Meats
To accomplish this we will need to do the following:
- Collect daily risk for long and short trades
- Store that data for later use
- Sort long and short risk from lowest to highest
- Assign each market to its correct sector
- When signal triggered make sure we can are allowed to execute the long or short trade
Step 1 and 2:
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
marketVal2[curMarket] = (shortExit - shortLevel)*myBPV
#marketVal1 contains long risk in $s and
#marketVal2 contains short risk in $s
Calculate Long/Short Entries/Exits and their Associated Risk Values
Step 3:
sectMarketRisk.append([0,0,0,0])
#looks like this [sectorNum, marketNum, longRisk$, shortRisk$]
canBuyList.append(99999)
canShortList.append(99999)
for cnt in range(0,numMarkets):
#get corresponding sector for the market
curSect = getCurrentSector(myComNameList[cnt],sectorList)
#assign the sectorNum, marketNum and long/short risk to the sectMarketRisk list
sectMarketRisk[cnt] = (curSect,cnt,marketVal1[cnt],marketVal2[cnt])
#default canBuy/Short lists to 99999 - can't trade
canBuyList[cnt] = canShortList[cnt] = 99999
#copy contents of list to ShortRisk list
sectMarketShortRisk =sectMarketRisk.copy()
#copy contents of list ot LongRisk list
sectMarketLongRisk = sectMarketRisk.copy()
#sort LongRisk list by first the sectorNum and then longRisk Value
sectMarketLongRisk.sort(key=operator.itemgetter(0,2),reverse = False)
#sort ShortRisk list by first the sectorNum and then shortRisk Value
sectMarketShortRisk.sort(key=operator.itemgetter(0,3),reverse = False)
#This is what (partial) the sectMarkRisk list contains:
20100324 Sector: Currency Market: SF LongRisk: 11437.5 ShortRisk: 4275
20100324 Sector: Currency Market: EC LongRisk: 21412.5 ShortRisk: 4837.5
20100324 Sector: Currency Market: BP LongRisk: 12618.7 ShortRisk: 4975
20100324 Sector: Currency Market: CD LongRisk: 5760 ShortRisk: 6640
20100324 Sector: Currency Market: DX LongRisk: 1935 ShortRisk: 6660
20100324 Sector: Currency Market: AD LongRisk: 4880 ShortRisk: 7080
20100324 Sector: Currency Market: JY LongRisk: 10250 ShortRisk: 8337.5
20100324 Sector: Energies Market: CL LongRisk: 7170 ShortRisk: 13510
20100324 Sector: Energies Market: HO LongRisk: 11428.2 ShortRisk: 12919.2
20100324 Sector: Energies Market: NG LongRisk: 20650 ShortRisk: 9410
20100324 Sector: Energies Market: RB LongRisk: 7883.4 ShortRisk: 15548.4
#Here is what sectMarketLongRisk looks like sorted
#So you would trade the DX, AD and CD (long side) only from the Currency Sector
20100324 Sector: Currency Market: DX LongRisk: 1935 ShortRisk: 6660
20100324 Sector: Currency Market: AD LongRisk: 4880 ShortRisk: 7080
20100324 Sector: Currency Market: CD LongRisk: 5760 ShortRisk: 6640
20100324 Sector: Currency Market: JY LongRisk: 10250 ShortRisk: 8337.5
20100324 Sector: Currency Market: SF LongRisk: 11437.5 ShortRisk: 4275
20100324 Sector: Currency Market: BP LongRisk: 12618.7 ShortRisk: 4975
20100324 Sector: Currency Market: EC LongRisk: 21412.5 ShortRisk: 4837.5
---
Sort Trade Risk on Sector Num and Then Risk$
The true beauty of Python lists is all of the different methods you can use on them. Here I sort the sectorMarketLongRisk first on the sectorNum key and then on the longRisk value key.
Step 4:
Now that we have the long and short risk sorted in ascending order (by sectorNum and then risk), the first three values in each list contains the three least risky long and short markets. So, you can loop through each sector and insert the market number for the three least risky markets on a sector basis into the CanBuy and CanShort lists. Before we loop let’s take a look at what the risk lists look like:
20100324
Currency = (0,3,1935,6659)(0,2,4880,7079)(0,6,5759,6640)(0,5,10249,8337)(0,1,11437,4275)(0,0,12618,4974)(0,4,21412.5,4837)
Energies = (1,7,7170,13510)(1,10,7883 15548)(1,8,11428.2,12919)(1,9,20649,9410)
Metals = (2,15,6769,13619)(2,14,7860,12655)(2,13,9837,16512)(2,11,13979,10129)(2,12,19449,14969)
Grains = (3,19,2202,2867)(3,18,3787.5,1675.0)(3,20,5869,3260)(3,17,6800.0,2525.0),(3,21,8600.,3429.)(3,16,8662.5,3937.5)
Financials = (4,22,262,650)(4,25,875,2593),(4,26,1453,3265)(4,24,3265,4296.),(4,23,7281,5968)
Softs = (5,32,2377,5197)(5,30,3065,8764)(5,31,4036,5577)(5,29,7930,3540)(5,28,8568,3206)(5,27,13406,7851)
Meats = (6,34,2100,3510)(6,33,2659,5189)(6,35,3737,8987)
SectMarketLongRisk
So sector number 0 (Currencies) shows markets 3, 2 and 6 as the least risky currencies. These are the only markets (DX, AD, CD) that will be allowed from the currency sector to initiate a new trade. Here is the looping mechanism that converts this data into the CanBuy and CanShort lists.
#Loop through 7 sectors
#Remember
# SectNum MktNum LongRisk Short Risk
# (0, 0, 500, 1000)
# Zero One Two Three - >element #
#
for j in range(0,len(sectorList)):
lsectCnt = 0
#Loop through 30+ Markets
for i in range(0,len(sectMarketLongRisk)):
#If 0 elemenent in each tuple is the current sector
#And lsectCnt < 3 then assign the 1 element of the
#Tuple to the canBuyList
if sectMarketLongRisk[i][0] == j:
if lsectCnt < 3:
canBuyList[i] = sectMarketLongRisk[i][1]
lsectCnt += 1
ssectCnt = 0
for i in range(0,len(sectMarketShortRisk)):
if sectMarketShortRisk[i][0] == j:
if ssectCnt < 3:
canShortList[i] = sectMarketShortRisk[i][1]
ssectCnt += 1
Sort Through Sectors and Pull Out First 3 Markets
After the looping and extraction this is what your CanBuy and CanShort lists look like:
20100324:[3,2,6,999,999,999,999,7,10,8,999,15,14,13,999,999,19,
18,20,999,999,999,22,25,26,999,999,32,30,31,999,999,999,34,33,35]
20100324:[1,4,0,999,999,999,999,9,8,7,999,11,14,15,999,999,18,
17,19,999,999,999,22,25,26,999,999,28,29,32,999,999,999,34,33,35]
CanBuy and CanShort Lists
Now that you have these lists you just need to check and see if the current market in the loop is included in the long or short risk lists. If they are included and sector exposure is less than 3 markets then you can proceed to take the trade. Here is the if construct:
if myHigh[curBar]>=buyLevel and curMarket in canBuyList and sectorPositions < 3 and mp !=1:
price = max(myOpen[curBar],buyLevel)
if mp == 0 : sectorTradesTodayList[curSector] +=1
tradeName = "Simple Buy"
numShares = posSize
if mp <= -1:
profit,curShares,trades = bookTrade(-1,0,price,myDate[curBar],"RevshrtLiq",curShares)
marketMonitorList[curMarket].tradesList.append(trades);todaysCTE = profit
profit,curShares,trades = bookTrade(entry,buy,price,myDate[curBar],tradeName,numShares)
barsSinceEntry = 1
marketMonitorList[curMarket].setSysMarkTrackingInfo(tradeName,cumuProfit,mp,barsSinceEntry,curShares,trades)
If Construct To Limit Execution to CanBuy List and Sector Exposure
This was a long post so I will show results of only trading less risky markets on sector basis in the next one.