0

I have defined a function which can get the history price for the coin:

def get_price(pair):
    df=binance.fetch_ohlcv(pair,limit=258,timeframe="1d")
    df=pd.DataFrame(df).rename(columns={0:"date",1:"open",2:"high",3:"low",4:"close",5:"volume"})
    df["date"]=pd.to_datetime(df["date"],unit="ms")+pd.Timedelta(hours=8)
    df.set_index("date",inplace=True)
    return df  

Then i want to use zip function to create two lists which can correspond to each other,so i can easily apply the function to get history data for each of the coin in the name list:

name=["btc","eth"]
symbol=["BTC/USDT","ETH/USDT"] 
for name,pair in zip(name,symbol):
    name=get_price(pair)
eth

But when i type "eth",to get the dataframe of "ETH/USDT", it gave me the error of "NameError: name 'eth' is not defined". The reason for me to do this is if i have a list of more than 10 pairs of coins, i dont want to use get_price function for each of them one by one in order to get the history data for all of them. can anyone help me to fix this errors? Thanksenter image description here

Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
  • 2
    why are you typing `eth`? Your function is runs inside the `for` loop. Just typing `eth` does not makes sense. – moys Sep 17 '19 at 04:07
  • For reference if you were trying to create an arbitrary number of variables: https://stackoverflow.com/questions/1373164/how-do-i-create-a-variable-number-of-variables – ALollz Sep 17 '19 at 04:31

2 Answers2

1

You may have to do another function if you want to keep the things the way you have. The first part (where you get the df seems fine since you have checked it). The issue is with the second part, where you are trying to run that function. Change is as below may help.

Example Run this & it will print ETH/USDT as output.

def func(x):
    name=["btc","eth"]
    symbol=["BTC/USDT","ETH/USDT"] 
    for name,pair in zip(name,symbol):
        if name == x:
            print(pair)
func('eth')

Similarly, if you want to run the function of getting the df, try something like this.

def func(x):
    name=["btc","eth"]
    symbol=["BTC/USDT","ETH/USDT"] 
    for name,pair in zip(name,symbol):
        if name == x:
            get_price(pair)
func('eth')
moys
  • 7,747
  • 2
  • 11
  • 42
1

Use a dict to store the dataframes

Original Code:

name=["btc","eth"]
symbol=["BTC/USDT","ETH/USDT"] 
for name,pair in zip(name,symbol):
    name=get_price(pair)
  • The loop is trying to assign a pd.DataFrame object, to a string, which won't work.
    • I'm surprised that didn't cause a SyntaxError: can't assign to literal
    • Equivalently, name=get_price(pair)'eth' = pd.DataFrame()
  • From the notebook, I see can a list of names and symbols, for which you're trying to create individual dataframes.
  • The notebook shows, get_price returns a dataframe, when given a symbol.

Replacement Code:

  • The following code will create a dict of dataframes, where each string in name, is a key.
df_dict = dict()
for name, pair in zip(name, symbol):
    df_dict[name] = get_price(pair)

df_dict['eth']
Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158