# Turtle Volatility Loss in Python Back Tester – Part 3 in Series

The Turtle N or Volatility is basically a 20-day Average True Range in terms of  Dollars.  This amount is considered the market volatility.  In other words the market, based on the average, can either move up or down by this amount.  It can move much less or much further; this is just an estimate.  If the market moves 2 N against a position, then as a Turtle you were to liquidate your stake in that commodity.  The logic indicates that if the market has a break out and then moves 2 N in the opposite direction, then the break out has failed.  First the code must be defined to represent the market volatility.  This is simple enough by using the sAverage function call and passing it the trueRanges and 20 days.  There’s no use in converting this to dollars because what we want is a price offset.  Once a position is entered the turtleN  is either added to the price [short position] or subtracted from the price [long position] to determine the respective stop levels.  Look at lines 2, 8 and 17 to see how this is handled.  An additional  trade code block must be added to facilitate this stop.  Lines 17 to 28 takes care of exiting a long position when the market moves 2 N in the opposite direction.   This new stop is in addition to the highest/lowest high/low stops for the past 10 -20 days.

``````        atrVal = sAverage(trueRanges,20,i,1)
turtleN = atrVal*2

mp += 1
longNExitStop = price - turtleN
marketPosition[i] = mp
entryPrice.append(price)
entryQuant.append(numShares)
curShares = curShares + numShares
#long Exit - 2 N Loss
if mp >= 1 and myLow[i] <= longNExitStop and barsSinceEntry > 1:
price = min(myOpen[i],longNExitStop)
exitDate =myDate[i]
numShares = curShares
exitQuant.append(numShares)
if curShares == 0 : mp = marketPosition[i] = 0
totProfit += profit
todaysCTE = profit
maxPositionL = maxPositionL - 1``````
Turtle Part 3

# Implementing Turtle Algorithm into the Python Backtester

I include the Python Backtester in my latest book “The Ultimate Algorithmic Trading System Toolbox” book.  A good tutorial on how to use it would be to program the Turtle Algorithm in three different parts.   Here is part 1:

Entry Description: Buy on stop at highest high of last twenty days.  Short on lowest low of last twenty days.

Exit Description: Exit long on stop at lowest low of last ten days.  Exit short on highest high of past ten days.

Position Sizing:  Risk 2% of simulated 100K account on each trade.  Calculate market risk by utilizing the ten day ATR.  Size(shares or contracts) = \$2,000/ATR in dollars.

Python code to  input into the backtester:

``````initCapital = 100000

hh20 = highest(myHigh,20,i,1)
ll20 = lowest(myLow,20,i,1)
hh10 = highest(myHigh,10,i,1)
ll10 = lowest(myLow,10,i,1)
hh55 = highest(myHigh,55,i,1)
ll55 = lowest(myLow,55,i,1)

atrVal = sAverage(trueRanges,10,i,1)

#Long Entry Logic
if (mp==0 or mp==-1) and barsSinceEntry>1 and myHigh[i]>=hh20:
profit = 0
price = max(myOpen[i],hh20)

#Short Logic
if (mp==0 or mp==1) and barsSinceEntry>1 and myLow[i] <= ll20:
profit = 0
price = min(myOpen[i],ll20)

#Long Exit Loss
if mp >= 1 and myLow[i] <= ll10 and barsSinceEntry > 1:
price = min(myOpen[i],ll10)

#Short Exit Loss
if mp <= -1 and myHigh[i] >= hh10 and barsSinceEntry > 1:
price = max(myOpen[i],hh10)
Turtle Part 1

This snippet only contains the necessary code to use in the Python Backtester – it is not in its entirety.

This algorithm utilizes a fixed fractional approach to position sizing.  Two percent or \$2000 is allocated on each trade and perceived market risk is calculated by the ten-day average true range (ATR.)   So if we risk \$2000 and market risk is \$1000 then 2 contracts are traded.  In Part 2, I will introduce the N risk stop and the LAST TRADE LOSER Filter.