# 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

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