For code/output blocks: Use ``` (aka backtick or grave accent) in a single line before and after the block. See: http://commonmark.org/help/

# ATR difference with Metatrader? + original welles wilder version

• Hi,

Additionally, I'm looking for the original welles wilder version (non smoothed). on Github, it looks like an SMA was used so I thought it'd be the same as in metatrader.

Anyone who has the original version by any chance?

thanks

• @Jens-Halsberghe I just met this question and solve it.

you should use this class and the ATR .

``````class AverageTrueRange(Indicator):
'''
Defined by J. Welles Wilder, Jr. in 1978 in hiMY_s book *"New Concepts in

The idea is to take the close into account to calculate the range if it
yields a larger range than the daily range (High - Low)

Formula:
- SmoothedMovingAverage(TrueRange, period)

See:
- http://en.wikipedia.org/wiki/Average_true_range
# 修改了算法，使用我自己的方法
'''
alias = ('ATR',)

lines = ('atr',)
params = (('period', 14), ('Average', Average))

def _plotlabel(self):
plabels = [self.p.period]
plabels += [self.p.movav] * self.p.notdefault('movav')
return plabels

def __init__(self):
self.lines.atr = self.p.Average(TR(self.data), period=self.p.period)
super(AverageTrueRange, self).__init__()
``````

• @tianjixuetu

Thanks for your reply. What would your full code be for this? I added the code like this and am getting an error in a strategy. FYI I was getting an error for the following at first so I modified it as you'll be able to see in the code below

``````params = (('period', 14), ('Average', Average))
``````
``````from __future__ import (absolute_import, division, print_function,
unicode_literals)

class TrueHigh(bt.Indicator):
'''
Defined by J. Welles Wilder, Jr. in 1978 in his book *"New Concepts in
Technical Trading Systems"* for the ATR
Records the "true high" which is the maximum of today's high and
yesterday's close
Formula:
- truehigh = max(high, close_prev)
See:
- http://en.wikipedia.org/wiki/Average_true_range
'''
lines = ('truehigh',)

def __init__(self):
self.lines.truehigh = bt.Max(self.data.high, self.data.close(-1))
super(TrueHigh, self).__init__()

class TrueLow(bt.Indicator):
'''
Defined by J. Welles Wilder, Jr. in 1978 in his book *"New Concepts in
Technical Trading Systems"* for the ATR
Records the "true low" which is the minimum of today's low and
yesterday's close
Formula:
- truelow = min(low, close_prev)
See:
- http://en.wikipedia.org/wiki/Average_true_range
'''
lines = ('truelow',)

def __init__(self):
self.lines.truelow = bt.Min(self.data.low, self.data.close(-1))
super(TrueLow, self).__init__()

class TrueRange(bt.Indicator):
'''
Defined by J. Welles Wilder, Jr. in 1978 in his book New Concepts in
Formula:
- max(high - low, abs(high - prev_close), abs(prev_close - low)
which can be simplified to
- max(high, prev_close) - min(low, prev_close)
See:
- http://en.wikipedia.org/wiki/Average_true_range
The idea is to take the previous close into account to calculate the range
if it yields a larger range than the daily range (High - Low)
'''
alias = ('TR',)

lines = ('tr',)

def __init__(self):
self.lines.tr = TrueHigh(self.data) - TrueLow(self.data)
super(TrueRange, self).__init__()

class AverageTrueRange(bt.Indicator):
'''
Defined by J. Welles Wilder, Jr. in 1978 in hiMY_s book *"New Concepts in

The idea is to take the close into account to calculate the range if it
yields a larger range than the daily range (High - Low)

Formula:
- SmoothedMovingAverage(TrueRange, period)

See:
- http://en.wikipedia.org/wiki/Average_true_range
# 修改了算法，使用我自己的方法
'''
alias = ('ATR',)

lines = ('atr',)
params = (('period', 14), ('Average', 14))

def _plotlabel(self):
plabels = [self.p.period]
plabels += [self.p.movav] * self.p.notdefault('movav')
return plabels

def __init__(self):
self.lines.atr = self.p.Average(TR(self.data), period=self.p.period)
super(AverageTrueRange, self).__init__()
``````

when I initialize it it in a strategy as follows, I'm getting an error

``````
def __init__(self):
self.ATR = AverageTrueRange(self.datas)

``````

error:

``````<ipython-input-36-567f96f1b43f> in __init__(self)
82
83     def __init__(self):
---> 84         self.lines.atr = self.p.Average(TR(self.data), period=self.p.period)
85         super(AverageTrueRange, self).__init__()

TypeError: 'int' object is not callable
``````

thanks

• @Jens-Halsberghe you lose some code ,they shold be imported from others. for example

``````from __future__ import (absolute_import, division, print_function,
unicode_literals)

from . import Indicator, Max, Min, MovAv,Average
``````

• @tianjixuetu thanks for the follow-up. when I run this, I'm getting an error however.

``````---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)