1

Below is a simplified version of the function I am having a hard time with programming.

The problem is the SmoothedTrueRange variable, it also uses the previous SmoothedTrueRange value in its calculation, I get an error in this case “UnboundLocalError: local variable 'SmoothedTrueRange' referenced before assignment”.

How can I change this so it works correct?

import pandas as pd

def STM(ohlc, period=14):
    TrueRange = TR(ohlc)
    SmoothedTrueRange =  pd.Series( SmoothedTrueRange.shift() - (SmoothedTrueRange.shift()/period) + TrueRange )
    return pd.Series(SmoothedTrueRange)

def TR(ohlc):
    TR1 = pd.Series(ohlc['high'] - ohlc['low'])  
    TR2 = pd.Series(ohlc['high'] - ohlc['close'].shift()).abs()
    TR3 = pd.Series(ohlc['low'] - ohlc['close'].shift()).abs()
    _TR = pd.concat([TR1, TR2, TR3], axis=1)
    _TR['TR'] = _TR.max(axis=1)
    return pd.Series(_TR['TR'], name="TR")

from pandas import Timestamp
ohlc = pd.DataFrame( {'volume': {Timestamp('2018-12-03 00:00:00'): 298376, Timestamp('2018-11-30 00:00:00'): 428800, Timestamp('2018-11-29 00:00:00'): 382800, Timestamp('2018-11-28 00:00:00'): 399800, Timestamp('2018-11-27 00:00:00'): 319600, Timestamp('2018-11-26 00:00:00'): 446800, Timestamp('2018-11-23 00:00:00'): 142200, Timestamp('2018-11-21 00:00:00'): 132300, Timestamp('2018-11-20 00:00:00'): 531300, Timestamp('2018-11-19 00:00:00'): 340600, Timestamp('2018-11-16 00:00:00'): 583200, Timestamp('2018-11-15 00:00:00'): 529100, Timestamp('2018-11-14 00:00:00'): 766600, Timestamp('2018-11-13 00:00:00'): 854800, Timestamp('2018-11-12 00:00:00'): 586400, Timestamp('2018-11-09 00:00:00'): 1394200, Timestamp('2018-11-08 00:00:00'): 2718900, Timestamp('2018-11-07 00:00:00'): 1640200, Timestamp('2018-11-06 00:00:00'): 1883400, Timestamp('2018-11-05 00:00:00'): 1963500}, 'open': {Timestamp('2018-12-03 00:00:00'): 28.0, Timestamp('2018-11-30 00:00:00'): 26.01000022888184, Timestamp('2018-11-29 00:00:00'): 27.920000076293945, Timestamp('2018-11-28 00:00:00'): 27.649999618530273, Timestamp('2018-11-27 00:00:00'): 27.53000068664551, Timestamp('2018-11-26 00:00:00'): 28.420000076293945, Timestamp('2018-11-23 00:00:00'): 28.739999771118164, Timestamp('2018-11-21 00:00:00'): 29.71999931335449, Timestamp('2018-11-20 00:00:00'): 29.8799991607666, Timestamp('2018-11-19 00:00:00'): 31.29999923706055, Timestamp('2018-11-16 00:00:00'): 31.79999923706055, Timestamp('2018-11-15 00:00:00'): 30.309999465942386, Timestamp('2018-11-14 00:00:00'): 30.53000068664551, Timestamp('2018-11-13 00:00:00'): 31.18000030517578, Timestamp('2018-11-12 00:00:00'): 32.59999847412109, Timestamp('2018-11-09 00:00:00'): 32.889999389648445, Timestamp('2018-11-08 00:00:00'): 29.5, Timestamp('2018-11-07 00:00:00'): 28.45000076293945, Timestamp('2018-11-06 00:00:00'): 29.209999084472656, Timestamp('2018-11-05 00:00:00'): 28.38999938964844}, 'close': {Timestamp('2018-12-03 00:00:00'): 28.25029945373535, Timestamp('2018-11-30 00:00:00'): 27.34000015258789, Timestamp('2018-11-29 00:00:00'): 26.270000457763672, Timestamp('2018-11-28 00:00:00'): 27.85000038146973, Timestamp('2018-11-27 00:00:00'): 27.81999969482422, Timestamp('2018-11-26 00:00:00'): 27.18000030517578, Timestamp('2018-11-23 00:00:00'): 27.79999923706055, Timestamp('2018-11-21 00:00:00'): 28.790000915527344, Timestamp('2018-11-20 00:00:00'): 29.60000038146973, Timestamp('2018-11-19 00:00:00'): 30.29999923706055, Timestamp('2018-11-16 00:00:00'): 31.29999923706055, Timestamp('2018-11-15 00:00:00'): 31.32999992370605, Timestamp('2018-11-14 00:00:00'): 30.85000038146973, Timestamp('2018-11-13 00:00:00'): 30.8799991607666, Timestamp('2018-11-12 00:00:00'): 30.309999465942386, Timestamp('2018-11-09 00:00:00'): 32.580001831054695, Timestamp('2018-11-08 00:00:00'): 32.93000030517578, Timestamp('2018-11-07 00:00:00'): 29.489999771118164, Timestamp('2018-11-06 00:00:00'): 28.32999992370605, Timestamp('2018-11-05 00:00:00'): 29.1299991607666}, 'high': {Timestamp('2018-12-03 00:00:00'): 28.88999938964844, Timestamp('2018-11-30 00:00:00'): 27.63999938964844, Timestamp('2018-11-29 00:00:00'): 28.261999130249023, Timestamp('2018-11-28 00:00:00'): 28.0, Timestamp('2018-11-27 00:00:00'): 28.2549991607666, Timestamp('2018-11-26 00:00:00'): 28.86000061035156, Timestamp('2018-11-23 00:00:00'): 28.78000068664551, Timestamp('2018-11-21 00:00:00'): 29.940000534057614, Timestamp('2018-11-20 00:00:00'): 30.0, Timestamp('2018-11-19 00:00:00'): 32.25, Timestamp('2018-11-16 00:00:00'): 31.79999923706055, Timestamp('2018-11-15 00:00:00'): 31.739999771118164, Timestamp('2018-11-14 00:00:00'): 31.84000015258789, Timestamp('2018-11-13 00:00:00'): 31.60000038146973, Timestamp('2018-11-12 00:00:00'): 32.59999847412109, Timestamp('2018-11-09 00:00:00'): 33.513999938964844, Timestamp('2018-11-08 00:00:00'): 34.915000915527344, Timestamp('2018-11-07 00:00:00'): 30.36000061035156, Timestamp('2018-11-06 00:00:00'): 29.5, Timestamp('2018-11-05 00:00:00'): 30.06999969482422}, 'low': {Timestamp('2018-12-03 00:00:00'): 27.290000915527344, Timestamp('2018-11-30 00:00:00'): 26.01000022888184, Timestamp('2018-11-29 00:00:00'): 26.0, Timestamp('2018-11-28 00:00:00'): 26.76000022888184, Timestamp('2018-11-27 00:00:00'): 26.979999542236328, Timestamp('2018-11-26 00:00:00'): 26.979999542236328, Timestamp('2018-11-23 00:00:00'): 27.57999992370605, Timestamp('2018-11-21 00:00:00'): 28.709999084472656, Timestamp('2018-11-20 00:00:00'): 28.459999084472656, Timestamp('2018-11-19 00:00:00'): 30.10000038146973, Timestamp('2018-11-16 00:00:00'): 30.71999931335449, Timestamp('2018-11-15 00:00:00'): 30.309999465942386, Timestamp('2018-11-14 00:00:00'): 30.309999465942386, Timestamp('2018-11-13 00:00:00'): 30.5, Timestamp('2018-11-12 00:00:00'): 30.04999923706055, Timestamp('2018-11-09 00:00:00'): 31.54999923706055, Timestamp('2018-11-08 00:00:00'): 29.28000068664551, Timestamp('2018-11-07 00:00:00'): 27.56999969482422, Timestamp('2018-11-06 00:00:00'): 28.32999992370605, Timestamp('2018-11-05 00:00:00'): 28.06999969482422}, 'adjclose': {Timestamp('2018-12-03 00:00:00'): 28.25029945373535, Timestamp('2018-11-30 00:00:00'): 27.34000015258789, Timestamp('2018-11-29 00:00:00'): 26.270000457763672, Timestamp('2018-11-28 00:00:00'): 27.85000038146973, Timestamp('2018-11-27 00:00:00'): 27.81999969482422, Timestamp('2018-11-26 00:00:00'): 27.18000030517578, Timestamp('2018-11-23 00:00:00'): 27.79999923706055, Timestamp('2018-11-21 00:00:00'): 28.790000915527344, Timestamp('2018-11-20 00:00:00'): 29.60000038146973, Timestamp('2018-11-19 00:00:00'): 30.29999923706055, Timestamp('2018-11-16 00:00:00'): 31.29999923706055, Timestamp('2018-11-15 00:00:00'): 31.32999992370605, Timestamp('2018-11-14 00:00:00'): 30.85000038146973, Timestamp('2018-11-13 00:00:00'): 30.8799991607666, Timestamp('2018-11-12 00:00:00'): 30.309999465942386, Timestamp('2018-11-09 00:00:00'): 32.580001831054695, Timestamp('2018-11-08 00:00:00'): 32.93000030517578, Timestamp('2018-11-07 00:00:00'): 29.489999771118164, Timestamp('2018-11-06 00:00:00'): 28.32999992370605, Timestamp('2018-11-05 00:00:00'): 29.1299991607666}} )

STM(ohlc)

I want the output of STM(ohlc) to be similar to TR(ohlc):

>>> TR(ohlc)
2018-11-05    2.000000
2018-11-06    1.170000
2018-11-07    2.790001
2018-11-08    5.635000
2018-11-09    1.964001
2018-11-12    2.549999
2018-11-13    1.290001
2018-11-14    1.530001
2018-11-15    1.430000
2018-11-16    1.080000
2018-11-19    2.150000
2018-11-20    1.840000
2018-11-21    1.230001
2018-11-23    1.210001
2018-11-26    1.880001
2018-11-27    1.275000
2018-11-28    1.240000
2018-11-29    2.261999
2018-11-30    1.629999
2018-12-03    1.599998
Name: TR, dtype: float64

The error:

>>> STM(ohlc)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 3, in STM
UnboundLocalError: local variable 'SmoothedTrueRange' referenced before assignment
>>> 
>>> 

The first value calculated of SmoothedTrueRange should be:

0 - ( 0 / 14 ) + 2.000000 = 2 

The next value should be

2 - ( 2 / 14 ) + 1.170000 = 3.02714285714285

then:

3.02714285714285 - ( 3.02714285714285 / 14 ) + 2.790001 = 5.600919367346933
Siem
  • 11
  • 2
  • Could you elaborate more on the logic behind calculating `SmoothedTrueRange`? It's not clear what "*previous SmoothedTrueRange value in its calculation*" means. What would be the initial `SmoothedTrueRange `, for example? – Georgy Dec 14 '18 at 10:52
  • Thanks, but it's better to include that in the post. Comments are meant to be used to ask for clarifications. – Georgy Dec 14 '18 at 11:18
  • Possible duplicate of [Recursive definitions in Pandas](https://stackoverflow.com/questions/26267809/recursive-definitions-in-pandas) – Georgy Dec 14 '18 at 11:38
  • Take a look at the link. You have the same thing. In `B[t] = a * A[t] + b * B[t-1]`, in your case `a = 1`, `b = 1 - 1 / period`, `A[t] = TrueRange` and `B[t]` is `SmoothedTrueRange `. So, you would want to replace the line that gives you an error by this: `SmoothedTrueRange = lfilter([1], [1, 1 - 1 / period], TrueRange)` – Georgy Dec 14 '18 at 11:41
  • I tried it myself and for the first two values of `SmoothedTrueRange` I get the same results as what you have. But after that there are discrepancies. For some reason when I run `TR(ohlc)` I get different results from what you showed. I get `2018-11-07 2.790001`, not `2018-11-07 5.360001`. – Georgy Dec 14 '18 at 11:45
  • yes the TR value's I used were based on another stock, I replaced it when I edited the post, with a stock with a shorter history. ( now it is correct in the post ) – Siem Dec 14 '18 at 12:24
  • I replaced the code (and imported scipy.signal), but the value's are incorrect: 0 2.000000 1 -0.687143 2 3.428062 3 2.451800 – Siem Dec 14 '18 at 12:31
  • Looks like I made a mistake. Instead of `1 - 1/period` it should be `-(1 - 1/period)`: `SmoothedTrueRange = lfilter([1], [1, -(1 - 1 / period)], TrueRange)` – Georgy Dec 14 '18 at 12:48
  • Hi Georgy, many many thanks! It works now, I was stuck with this for days. – Siem Dec 14 '18 at 14:10
  • You are welcome! I think you can close your question as a duplicate of the one that I linked, as they are basically the same. But don't delete it! Not everyone will use the same terminology when looking for this problem, so they could find your question first and refer to another post. – Georgy Dec 14 '18 at 14:17

0 Answers0