Search code examples
pythonloopsbinance

Python looping over lists creating new variables


i'd like to loop a snippet of code over a list creating into this loop new variables labeled with the names in the list I loop to.

This is intended for get list of close prices from several crypto in binance.

Does anyone konw how to deal with it?

pool = [BTCUSDT, ETHUSDT, FARMUSDT, SOLUSDT, OPUSDT]
for i in pool:  
 
        'ì'_candlesticks = client.get_historical_klines("ì", Client.KLINE_INTERVAL_1MINUTE, "1 Jan, 2020", "2 Jan, 2020>
        'i'_close = []
 
        for candle in 'i'_candlesticks:
                'i'_close.append(candle[4])

Solution

  • Nawal's method was a great suggestion but it didn't work for me. I solved it like this, now it works.

    close_dict = {}
    candle_dict = {}
    pool = ["BTCUSDT", "ETHUSDT"]
    
    for i in pool:
        candle_dict[f"{i}"] = client.get_historical_klines(i,
                                                       Client.KLINE_INTERVAL_6HOUR,
                                                       "1 Jan, 2020", "2 Jan, 2020")
    
    for i in pool: 
        c=[]
        
        for candle in candle_dict[f"{i}"]:
            c.append(candle[4])
        
        close_dict[f"{i}"]=c
    
    print (candle_dict)
    print("")
    print("++++++++++++++++++++++++++++++")
    print("")
    print (close_dict)
    

    I had to write a second for loop on the i's because the first one gave me an indentation error that was inexplicable to me. If anyone has a suggestion regarding this I appreciate it