An Explanation of the numPosCurrentSector function

Here is some code and visual aid to help understand how TradingSimula-18 figures out how many current positions are in the same sector as the market that is currently being tested.

Part A - determine existing sector positions

def numPosCurrentSector(sectorList,curMarket,symbolList,positionList):
for sector in range(0,len(sectorList)):
whichSector = -1
if curMarket in sectorList[sector].sectMarket:
whichSector = sector
break
numSectorPos = 0
if whichSector != -1:
for sectorMarkets in range (0,len(sectorList[whichSector].sectMarket)):
for symbols in range(0,len(symbolList)):
tempMarket1 = sectorList[whichSector].sectMarket[sectorMarkets]
tempMarket2 = symbolList[symbols]
if tempMarket1 == tempMarket2:
if positionList != []:
curMktPos = symbols - len(symbolList)
if positionList[curMktPos] > 0:
numSectorPos +=1
return(numSectorPos)


#Here is an explanation of the code sections
---------------------------------------------------------
for sector in range(0,len(sectorList)):
whichSector = -1
if curMarket in sectorList[sector].sectMarket:
whichSector = sector
break
---------------------------------------------------------
I have 7 sectors and here they are:

if sector == 0 "Currency","BPSFADDXECJYCD"
if sector == 1 "Energies","CLHONGRB"
if sector == 2 "Metals","GCSIHGPLPA"
if sector == 3 "Grains","S_W_C_BOSMRR"
if sector == 4 "Financials","USTYTUFVED"
if sector == 5 "Softs","SBKCCCCTLBOJ"
if sector == 6 "Meats","LCLHFC"

So if I am testing BP
if BP in sectorList[0] then set whichSector to 0
So if I am testing CL
if CL in sectorList[1] then set whichSector to 1
Part A

Now that I know the sector the current market belongs to I can use that information to extract the total positions in that sector.

Part B - determine existing sector positions

def numPosCurrentSector(sectorList,curMarket,symbolList,positionList):
for sector in range(0,len(sectorList)):
whichSector = -1
if curMarket in sectorList[sector].sectMarket:
whichSector = sector
break
numSectorPos = 0
if whichSector != -1:
for sectorMarkets in range (0,len(sectorList[whichSector].sectMarket)):
for symbols in range(0,len(symbolList)):
tempMarket1 = sectorList[whichSector].sectMarket[sectorMarkets]
tempMarket2 = symbolList[symbols]
if tempMarket1 == tempMarket2:
if positionList != []:
curMktPos = symbols - len(symbolList)
if positionList[curMktPos] > 0:
numSectorPos +=1
return(numSectorPos)


#Here is an explanation of the code sections
---------------------------------------------------------
numSectorPos = 0
if whichSector != -1:
for sectorMarkets in range (0,len(sectorList[whichSector].sectMarket)):
for symbols in range(0,len(symbolList)):
tempMarket1 = sectorList[whichSector].sectMarket[sectorMarkets]
tempMarket2 = symbolList[symbols]
if tempMarket1 == tempMarket2:
if positionList != []:
curMktPos = symbols - len(symbolList)
if positionList[curMktPos] > 0:
numSectorPos +=1
return(numSectorPos)
---------------------------------------------------------
If whichSector is not equal to -1 then you know the market
is included in one of the 7 sectors.

First loop through all the markets in the correct sector -
Assume market is BP so loop through the currency sector - "BPSFADDXECJYCD"
At the same time start looping through the entire portfolio and
extracting the sector for each market in the portfolio.
If the current sector matches the test sector then
accumulate the number of positions in the positionList for the sector
Accumulate market positions in the same sector

Here is some output that might help understand how I do this…

Part C - accumulate current sector positions

Watches Type Value
sectorList[0].sectMarket list ['BP', 'SF', 'AD', 'DX', 'EC', 'JY', ...]
0 str 'BP'
1 str 'SF'
2 str 'AD'
3 str 'DX'
4 str 'EC'
5 str 'JY'
6 str 'CD'
curMarket str 'SF'
whichSector int 0
symbolList list ['BP', 'SF', 'AD', 'DX', 'EC', 'JY', ...]
0 str 'BP'
1 str 'SF'
2 str 'AD'
3 str 'DX'
4 str 'EC'
5 str 'JY'
6 str 'CD'
7 str 'CL'
8 str 'HO'
9 str 'NG'
10 str 'RB'
11 str 'GC'
12 str 'SI'
13 str 'HG'
14 str 'PL'
15 str 'PA'
16 str 'S_'
17 str 'W_'
18 str 'C_'
19 str 'BO'
20 str 'SM'
21 str 'RR'
22 str 'US'
23 str 'TY'
24 str 'TU'
25 str 'FV'
26 str 'ED'
27 str 'SB'
28 str 'KC'
29 str 'CC'
30 str 'CT'
31 str 'LB'
32 str 'OJ'
33 str 'LC'
34 str 'LH'
35 str 'FC'
tempMarket1 str 'BP'
tempMarket2 str 'BP'
positionList list [0, 0, 0, 0, 1, 1, ...]
0 int 0 - BP
1 int 0 - SF
2 int 0 - AD
3 int 0 - DX
4 int 1 - EC
5 int 1 - JY
6 int 0 - CD
7 int 1
8 int 1
9 int 0
10 int 1
11 int 0
12 int 0
13 int 1
14 int 1
15 int 1
16 int 0
17 int 0
18 int 0
19 int 0
20 int 0
21 int 1
22 int 0
23 int 0
24 int 0
25 int 0
26 int 1
27 int 1
28 int 0
29 int 0
30 int 0
31 int 0
32 int 1
33 int 0
34 int 1
35 int 1


If tempMarket1 and tempMarket2 both are EC then
curMktPos = 4 - 36 or 32
So look at element 4 and its a 1 : numSectPos = 1

If tempMarket1 and tempMarket2 both are JY then
curMktPos = 5 - 36 or 31
Sl look at element 5 and its a 1 : numSectPos = 2

 

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