# Pyramiding with Python

A reader of the “UATSTB” asked for an example of how to pyramid in the Python System BackTester (PSB).  I had original bug where you couldn’t peel off all of the positions at once.  If you tried then the PSB would crash.  I have now fixed that problem and here are the necessary lines to add on another position after the initial positions is put on.   I am using a simple moving average crossover to initiate the first position and then if the longer term moving average is positive for 4 days in a row I add on another position.  That’s it.  A max of two position only.  The system either gets reversed to the other side, stopped out or takes profits.  Attached in this post is the module that fixes the pyramiding problem and the code for this system in its entirety.   I will also put the fix in the version 2.0 download.

``````        upCnt = 0
dnCnt = 0
for j in range(4):
if sAverage(myClose,39,i,j) > sAverage(myClose,39,i,j+1):
upCnt += 1
if sAverage(myClose,39,i,j) < sAverage(myClose,39,i,j+1):
dnCnt += 1

#Long Entry Logic
if (mp != 1) and avg1 > avg2 and prevAvg1 < prevAvg2:
profit = 0
price = myClose[i]

#Long Pyramid Logic
if (mp == 1) and upCnt == 4:
profit = 0
price = myClose[i]
Pyramiding the Long Side of a Dual Moving Average

I only put in the summary of code that will create the initial position and then add 1 more.  Notice the highlighted code from line 3 to 7.  Here I am using a simple loop to determine if the the 39-day moving average is increasing/decreasing consecutively over the past four days.  If the upCnt == 4 then I add a long position.  Notice how I test in the Long Pyramid Logic the values of mp and upCnt.  I only go long another position if I am already long 1 and upCnt == 4.

Here is a print out of some of the trades:

``````20040923      DMA Buy  1 127.79000       0.00       0.00
20040923     LongPyra  1 127.79000       0.00       0.00
20041006       L-Prof  2 130.79000    5800.00   14560.00
20041116    DMA Short  1 126.14000       0.00       0.00
20041119    ShortPyra  1 128.64000       0.00       0.00
20041201       S-Prof  2 125.64000    3300.00   17860.00
20050121      DMA Buy  1 127.85000       0.00       0.00
20050202     LongPyra  1 126.01000       0.00       0.00
20050222       L-Prof  2 129.01000    3960.00   21820.00
20050418    DMA Short  1 128.18000       0.00       0.00
20050419     S-MMLoss  1 130.28000   -2200.00   19620.00
20050616      DMA Buy  1 131.41000       0.00       0.00
20050621     LongPyra  1 133.02000       0.00       0.00
20050630     L-MMLoss  2 130.48000   -3670.00   15950.00
20050809    DMA Short  1 135.95000       0.00       0.00
20050810   RevShrtLiq  1 137.78000   -1930.00   14020.00
20050810      DMA Buy  1 137.78000       0.00       0.00
20050810     LongPyra  1 137.78000       0.00       0.00
20050829       L-Prof  2 140.98000    6200.00   20220.00``````
Trade Listing Of DMA with Pyramiding

While I was posting the trades I found something that struck my funny – look at line 5 (highlighted).  The short pyramid trade occurs at a higher price than the initial short – at first I thought I had made a programming error.  So I thought I would double check the code and then do some debugging.  Instead of invoking the debugger which is really cool and easy to use I decided to just print out the results to the console.

``````        for j in range(4):
if sAverage(myClose,39,i,j) > sAverage(myClose,39,i,j+1):
upCnt += 1
if sAverage(myClose,39,i,j) < sAverage(myClose,39,i,j+1):
dnCnt += 1
if tempDate == 20041119:
print(myDate[i]," ",j," ",sAverage(myClose,39,i,j))

'''Output of debugging using a print statement
20041119   0   130.68692307692308
20041119   1   130.69538461538463
20041119   2   130.74871794871794
20041119   3   130.7723076923077'''``````
Debugging using Print Statements

As you can see the longer term moving average is moving down event though price has increased.  Take a look at this chart and you can see multiple occurrences of this.

Remember to copy and replace the tradeClass.py in you PSB2.0 directory – this fixes the pyramid bug.  Also copy the DMAPyra.py to the same directory.  If you haven’t as of yet download the PSB from this website and buy the book for a very good description.

PostPythonCode