熊猫函数,自引用变量问题

时间:2018-12-14 09:09:24

标签: python pandas function reference self

下面是我在编程时遇到的麻烦的功能的简化版本。

问题是SmoothedTrueRange变量,它在计算中也使用了以前的SmoothedTrueRange值,在这种情况下,我收到一个错误“ UnboundLocalError:分配前引用的本地变量'SmoothedTrueRange'”。

如何更改此设置以使其正常运行?

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)

我希望STM(ohlc)的输出类似于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

错误:

>>> 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
>>> 
>>> 

计算的SmoothedTrueRange的第一个值应为:

0 - ( 0 / 14 ) + 2.000000 = 2 

下一个值应为

2 - ( 2 / 14 ) + 1.170000 = 3.02714285714285

然后:

3.02714285714285 - ( 3.02714285714285 / 14 ) + 2.790001 = 5.600919367346933

0 个答案:

没有答案