'I am getting 'index out of bound error' when reading from csv in pandas but not when I extract the data via api. What could be the reason?

So for my bot, I am first extracting data via api and storing it in csv. When I run my for loop on data via api, it gives no error and runs smoothly.

But when the csv file is read and run, it gives out of bound error.

This is my function to generate data:

full_list = pd.DataFrame(columns=("date","open","high","low","close","volume","ticker","RSI","ADX","20_sma","max_100"))
def stock_data(ticker):
    create_data = fetchOHLC(ticker,'minute',60)
    create_data["ticker"] = ticker
    create_data["RSI"] = round(rsi(create_data,25),2)
    create_data["ADX"] = round(adx(create_data,14),2)
    create_data["20_sma"] = round(create_data.close.rolling(10).mean().shift(),2)
    create_data["max_100"] = create_data.close.rolling(100).max().shift()
    create_data.dropna(inplace=True,axis=0)
    create_data.reset_index(inplace=True)
    return create_data
stocklist = open("stocklist.txt","r+")
tickers = stocklist.readlines()
for x in tickers:
    try:
        full_list = full_list.append(stock_data(x.strip()))
    except:
        print(f'{x.strip()} did not work')
full_list.to_csv("All_Data")
full_list

So when I run the same code below on dataframe created I got no error. But when I run the same code on the csv file, I get out of bound error.

list_tickers = full_list["ticker"].unique()
for y in list_tickers[:2]:
    main = full_list[full_list["ticker"]==y]
    pos = 0
    num = 0
    tick = y
    signal_time = 0
    signal_rsi = 0
    signal_adx = 0
    buy_time = 0
    buy_price = 0  
    sl = 0
    #to add trailing sl in this.
    for x in main.index:
        maxx = main.iloc[x]["max_100"]
        rsi = main.iloc[x]["RSI"]
        adx = main.iloc[x]["ADX"]
        sma = main.iloc[x]["20_sma"]
        close = main.iloc[x]["close"] 
        high = main.iloc[x]["high"]
        if rsi > 80 and adx > 35 and close > maxx:
            if pos == 0:                
                buy_price = main.iloc[x+1]["open"]
                buy_time = main.iloc[x+1]["date"]
                pos=1
                signal_time = main.iloc[x]["date"]
                signal_rsi = main.iloc[x]["RSI"]
                signal_adx = main.iloc[x]["ADX"]
        elif close < sma:
            if pos == 1:                
                sell_time = main.iloc[x]["date"]
                sell_price = sma*.998
                pos=0
                positions.loc[positions.shape[0]] = [y,signal_time,signal_rsi,signal_adx,buy_time,buy_price,sell_time,sell_price]

Any idea why?

Here is a cleanup and file call code:

full_list = pd.read_csv("All_data")
full_list.dropna(inplace=True,axis=0)
full_list.drop(labels="Unnamed: 0",axis=1) < index of previous dataframe
full_list.head(5)

Thanks



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source