4

I am using the package from here. What I am trying to do is to calculate the Stochastic value for each ticker. I have the following code:

import lxml
from urllib.request import urlopen
from bs4 import BeautifulSoup
import pandas as pd
from pandas import datetime
from pandas import DataFrame as df
# import matplotlib
from pandas_datareader import data as web
import matplotlib.pyplot as plt
import datetime
import numpy as np
import ta


html = urlopen('https://en.wikipedia.org/wiki/NIFTY_50')
soup = BeautifulSoup(html,'lxml')
niftylist_raw = soup.find('table', {'class': 'wikitable sortable'})

nifty_symbol =[]

for row in niftylist_raw.findAll('tr')[1:]:
    nifty_symbols = row.findAll('td')[1].text
    nifty_symbol.append(nifty_symbols)
tickerss = nifty_symbol

df = web.DataReader(tickerss, 'yahoo')


stoch1 = ta.momentum.StochasticOscillator(high= df['High'], low = df['Low'], close = df['Close'], n=14, fillna=False)

The above code works, however when I try:

stoch1.stoch()

It produces the following error:

ValueError: The truth value of a DataFrame is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

What can I do to fix this error?

Bertrand Martel
  • 32,363
  • 15
  • 95
  • 118
Slartibartfast
  • 476
  • 5
  • 28

1 Answers1

3

It seems that the method StochasticOscillator expects time series, with single column. But, when you have built your dataframe from web.DataReader, it gave you as much column as tickers.

So you just have to iterate over the tickers, and append the output time series to a new dataframe :

import lxml
from urllib.request import urlopen
from bs4 import BeautifulSoup

import pandas as pd
from pandas_datareader import data as web
import ta

html = urlopen('https://en.wikipedia.org/wiki/NIFTY_50')
soup = BeautifulSoup(html,'lxml')
niftylist_raw = soup.find('table', {'class': 'wikitable sortable'})

tickers = []

for row in niftylist_raw.findAll('tr')[1:]:
    nifty_symbols = row.findAll('td')[1].text
    tickers.append(nifty_symbols)

print(tickers)

df = web.DataReader(tickers, 'yahoo')

stoch_list = pd.DataFrame(columns = tickers, index = df.index)

for t in tickers:
    stoch1 = ta.momentum.StochasticOscillator(
        high = df['High'][t], 
        low = df['Low'][t], 
        close = df['Close'][t], 
        n = 14, 
        fillna = True
    )
    stoch_list[t] = stoch1.stoch()

print(stoch_list)

Output :

            ADANIPORTS.NS  ASIANPAINT.NS  AXISBANK.NS  BAJAJ-AUTO.NS  BAJFINANCE.NS  BAJAJFINSV.NS  BHARTIARTL.NS  ...   TECHM.NS   TITAN.NS  ULTRACEMCO.NS     UPL.NS    VEDL.NS   WIPRO.NS    ZEEL.NS
Date                                                                                                               ...                                                                                 
2015-06-15      21.909922       9.347667    48.704666      91.231693      77.791892      44.191114      30.140809  ...  53.666585  13.368975      14.346124  11.691838  14.367821  56.043549  91.428571
2015-06-16      72.363725      45.265156    74.247676      91.487233      66.564368      40.100098      55.669462  ...  36.333415  64.983097      88.990210  40.529881  21.072792  52.328376  87.301587
2015-06-17      56.363636      66.856091    53.025895      75.153149      91.088110      50.700195      85.667732  ...  21.000163  91.388957      80.205145  86.905950   3.869011  66.513717  92.325610
2015-06-18      56.363636      86.940831    57.636766      89.124719      94.661550      60.670907      71.193579  ...   5.263327  92.608643      78.613103  60.171097   7.142838  84.943018  70.000000
2015-06-19      11.428615      90.558724    66.282281      89.443078      86.336954      60.956522      88.276724  ...  19.378201  87.341779      81.569343  47.826973  22.928220  64.704798  39.333360
...                   ...            ...          ...            ...            ...            ...            ...  ...        ...        ...            ...        ...        ...        ...        ...
2020-06-08      91.351874      57.958253    87.042001      82.309507      94.710350      94.247354      60.630541  ...  90.171597  89.217165      71.472360  88.654333  75.702800  98.275850  78.462774
2020-06-09      88.169607      59.439045    77.926708      79.988254      86.386837      85.820652      35.190610  ...  79.905597  77.139145      68.528865  83.201404  73.694768  80.155220  70.400641
2020-06-10      89.285678      55.534959    84.539778      62.649461      85.347188      80.135976      38.385475  ...  84.417652  65.700137      64.559014  73.540428  79.718864  76.537583  64.615390
2020-06-11      71.651770      29.268271    72.028606      62.966014      72.695166      75.927493      15.018624  ...  64.094315  53.441767      41.152918  62.931034  59.437760  65.384598  40.940166
2020-06-12      85.508938      35.945931    66.097784      77.878054      87.243356      84.211742      37.430114  ...  45.712787  62.894536      53.112690  54.729316  68.473904  53.365369  17.986311

[1228 rows x 50 columns]
Bertrand Martel
  • 32,363
  • 15
  • 95
  • 118
  • Could I ask you why `fillna = True` ? Thanks – Slartibartfast Jun 13 '20 at 18:59
  • 1
    @newcoder you can set it to False, you would have NaN value for the first 13 values of each series according to https://school.stockcharts.com/doku.php?id=technical_indicators:stochastic_oscillator_fast_slow_and_full. – Bertrand Martel Jun 13 '20 at 20:55
  • 1
    @newcoder also if you look at `stoch_list["HDFC.NS"]` when you set it to False you will notice that it has NaN value until `2015-12-24`. After looking at the initial value of `"HDFC.NS"` it seems it has the same value for High, Low and Close from `2015-06-15` to `2015-12-24` – Bertrand Martel Jun 13 '20 at 21:03