python not recognizing pandas_ta module

1.2k Views Asked by At
import requests
import pandas as pd
import pandas_ta as ta

def stochFourMonitor():
    k_period = 14
    d_period = 3
    data = get_data('BTC-PERP',14400,1642935495,1643165895)
    print(data)
    data = data['result'] 
    df = pd.DataFrame(data)
    df['trailingHigh'] = df['high'].rolling(k_period).max()
    df['trailingLow'] = df['low'].rolling(k_period).min()
    df['%K'] = (df['close'] - df['trailingLow']) * 100 / (df['trailingHigh'] - df['trailingLow'])
    df['%D'] = df['%K'].rolling(d_period).mean()
    df.index.name = 'test'
    df.set_index(pd.DatetimeIndex(df["startTime"]), inplace=True)
    print(df)
    df.drop(columns=['startTime'])
    print(df)
    df.ta.stoch(high='High', low='Low',close= 'Close', k=14, d=3, append=True)
    #t = ta.stoch(close='close',high='high', low='low', k=14, d=3, append=True)
    #df.ta.stoch(close='close',high='high', low='low', k=14, d=3, append=True)

 

def get_data(marketName,resolution,start_time,end_time):
        data = requests.get('https://ftx.com/api/markets/' + marketName + '/candles?resolution=' + str(resolution) + '&start_time=' + str(start_time) + '&end_time=' + str(end_time)).json()
        return data

I keep receiving the error 'NoneType' object has no attribute 'name'. See below for full exception. It seems like the code is not recognizing the pandas_ta module but I don't understand why. Any help troubleshooting would be much appreciated.

Exception has occurred: AttributeError (note: full exception trace is shown but execution is paused at: ) 'NoneType' object has no attribute 'name' File "C:\Users\Jason\Documents\TradingCode\FTX Websocket\testing21.py", line 21, in stochFourMonitor df.ta.stoch(high='High', low='Low',close= 'Close', k=14, d=3, append=True) File "C:\Users\Jason\Documents\TradingCode\FTX Websocket\testing21.py", line 31, in (Current frame) stochFourMonitor()

1

There are 1 best solutions below

2
On BEST ANSWER

You have to few values in your dataframe. You need at least 17 values (k=14, d=3)

>>> pd.Timestamp(1642935495, unit='s')
Timestamp('2022-01-23 10:58:15')

>>> pd.Timestamp(1643165895, unit='s')
Timestamp('2022-01-26 02:58:15')

>>> pd.DataFrame(get_data('BTC-PERP',14400,1642935495,1643165895)['result'])
0   2022-01-23T12:00:00+00:00  1.642939e+12  35690.0  36082.0  35000.0  35306.0  6.315513e+08
1   2022-01-23T16:00:00+00:00  1.642954e+12  35306.0  35460.0  34601.0  34785.0  7.246238e+08
2   2022-01-23T20:00:00+00:00  1.642968e+12  34785.0  36551.0  34712.0  36271.0  9.663773e+08
3   2022-01-24T00:00:00+00:00  1.642982e+12  36271.0  36283.0  35148.0  35351.0  6.007333e+08
4   2022-01-24T04:00:00+00:00  1.642997e+12  35351.0  35511.0  34821.0  34896.0  5.554126e+08
5   2022-01-24T08:00:00+00:00  1.643011e+12  34895.0  35610.0  33033.0  33709.0  1.676436e+09
6   2022-01-24T12:00:00+00:00  1.643026e+12  33709.0  34399.0  32837.0  34260.0  2.021096e+09
7   2022-01-24T16:00:00+00:00  1.643040e+12  34261.0  36493.0  33800.0  36101.0  1.989552e+09
8   2022-01-24T20:00:00+00:00  1.643054e+12  36101.0  37596.0  35990.0  36673.0  1.202684e+09
9   2022-01-25T00:00:00+00:00  1.643069e+12  36673.0  36702.0  35974.0  36431.0  4.538093e+08
10  2022-01-25T04:00:00+00:00  1.643083e+12  36431.0  36500.0  35719.0  36067.0  3.514587e+08
11  2022-01-25T08:00:00+00:00  1.643098e+12  36067.0  36824.0  36030.0  36431.0  5.830712e+08
12  2022-01-25T12:00:00+00:00  1.643112e+12  36431.0  37200.0  35997.0  36568.0  9.992247e+08
13  2022-01-25T16:00:00+00:00  1.643126e+12  36568.0  37600.0  36532.0  37079.0  8.225219e+08
14  2022-01-25T20:00:00+00:00  1.643141e+12  37077.0  37140.0  36437.0  36980.0  7.892745e+08
15  2022-01-26T00:00:00+00:00  1.643155e+12  36980.0  37242.0  36567.0  37238.0  3.226400e+08

>>> pd.DataFrame(get_data('BTC-PERP',14400,1642935495,1643165895)['result'])
...
AttributeError: 'NoneType' object has no attribute 'name'

Now change 1642935495 ('2022-01-23 10:58:15') by 1642845495 ('2022-01-22 10:58:15':

>>> pd.DataFrame(get_data('BTC-PERP',14400,1642845495,1643165895)['result']).ta.stoch()
    STOCHk_14_3_3  STOCHd_14_3_3
13            NaN            NaN
14            NaN            NaN
15      80.824814            NaN
16      74.665546            NaN
17      72.970512      76.153624
18      73.930097      73.855385
19      80.993469      75.964693
20      84.814444      79.912670
21      89.775352      85.194422