1

I am writing a program that requires a function to keep track of the simulated value of several different currency wallets across multiple exchanges. I wrote a class to declare, hold, and retrieve the simulated balance:

>>> class Balance:
    def __init__(self,exchange,currency):
        self.exchange = exchange
        self.currency = currency
    def update(self,val):
        self.val = val
    def get(self):
        qty = self.val
        if qty > 0:
            return qty
        else:
            return False

Each 'balance keeper' object is referenced by currency_exchange as below. When declaring the object manually, everything works fine:

>>> BTC_poloniex=Balance('poloniex','BTC')
>>> BTC_poloniex.update(5.5)
>>> BTC_poloniex.get()
5.5
>>> XMR_poloniex=Balance('poloniex,'XMR')
>>> XMR_poloniex.update(11)
>>> XMR_poloniex.get()
11

However, I would like to iterate through the following lists and dynamically declare the objects and assign the values as such:

>>> CURRENCIES=['BTC','ETH','XRP','XLM','NEO','DASH','XMR','LSK','ZEC','LTC','ETC', 'ADA', 'XMR']
>>> BALANCES = [("BTC", 5.0), ("DASH", 10.0), ("ETH", 10.0), ("XRP", 100.0), ("XLM", 1000.0), ("NEO", 100.0), ("XMR", 10.0), ("LSK", 1000.0), ("ZEC", 10.0), ("LTC", 20.0), ("ETC", 20.0), ("ADA", 1000.0)]
>>> EXCHANGES=["poloniex", "cex", "bittrex", "binance", "okex"]
>>> for ex in EXCHANGES:
    for cur in CURRENCIES:
        balance_keeper = cur+'_'+ex
        balance_keeper=Balance(ex,cur)
        for balance in BALANCES:
            if balance[0] == cur:
                val = balance[1]
                print(str(ex)+str('_')+str(cur)+str(' updated to : ')+str(+val))
                balance_keeper.update(val)
                balance_keeper.get()


poloniex_BTC updated to : 5.0
5.0
poloniex_ETH updated to : 10.0
10.0
poloniex_XRP updated to : 100.0
100.0
poloniex_XLM updated to : 1000.0
1000.0
poloniex_NEO updated to : 100.0
100.0
poloniex_DASH updated to : 10.0
10.0
poloniex_XMR updated to : 10.0
10.0
poloniex_LSK updated to : 1000.0
1000.0
poloniex_ZEC updated to : 10.0
10.0
poloniex_LTC updated to : 20.0
20.0
poloniex_ETC updated to : 20.0
20.0
poloniex_ADA updated to : 1000.0
1000.0
poloniex_XMR updated to : 10.0
10.0
cex_BTC updated to : 5.0
5.0
cex_ETH updated to : 10.0
10.0
cex_XRP updated to : 100.0
100.0
cex_XLM updated to : 1000.0
1000.0
cex_NEO updated to : 100.0
100.0
cex_DASH updated to : 10.0
10.0
cex_XMR updated to : 10.0
10.0
cex_LSK updated to : 1000.0
1000.0
cex_ZEC updated to : 10.0
10.0
cex_LTC updated to : 20.0
20.0
cex_ETC updated to : 20.0
20.0
cex_ADA updated to : 1000.0
1000.0
cex_XMR updated to : 10.0
10.0
bittrex_BTC updated to : 5.0
5.0
bittrex_ETH updated to : 10.0
10.0
bittrex_XRP updated to : 100.0
100.0
bittrex_XLM updated to : 1000.0
1000.0
bittrex_NEO updated to : 100.0
100.0
bittrex_DASH updated to : 10.0
10.0
bittrex_XMR updated to : 10.0
10.0
bittrex_LSK updated to : 1000.0
1000.0
bittrex_ZEC updated to : 10.0
10.0
bittrex_LTC updated to : 20.0
20.0
bittrex_ETC updated to : 20.0
20.0
bittrex_ADA updated to : 1000.0
1000.0
bittrex_XMR updated to : 10.0
10.0
binance_BTC updated to : 5.0
5.0
binance_ETH updated to : 10.0
10.0
binance_XRP updated to : 100.0
100.0
binance_XLM updated to : 1000.0
1000.0
binance_NEO updated to : 100.0
100.0
binance_DASH updated to : 10.0
10.0
binance_XMR updated to : 10.0
10.0
binance_LSK updated to : 1000.0
1000.0
binance_ZEC updated to : 10.0
10.0
binance_LTC updated to : 20.0
20.0
binance_ETC updated to : 20.0
20.0
binance_ADA updated to : 1000.0
1000.0
binance_XMR updated to : 10.0
10.0
okex_BTC updated to : 5.0
5.0
okex_ETH updated to : 10.0
10.0
okex_XRP updated to : 100.0
100.0
okex_XLM updated to : 1000.0
1000.0
okex_NEO updated to : 100.0
100.0
okex_DASH updated to : 10.0
10.0
okex_XMR updated to : 10.0
10.0
okex_LSK updated to : 1000.0
1000.0
okex_ZEC updated to : 10.0
10.0
okex_LTC updated to : 20.0
20.0
okex_ETC updated to : 20.0
20.0
okex_ADA updated to : 1000.0
1000.0
okex_XMR updated to : 10.0
10.0

The problem I am having is that the variable balance_keeper is not doing what I would expect. For example, look that the last two lines above.

Instead of initializing the object okex_ADA, and then subsequently initializing a new object, okex_XMR, the object is simply being overwritten with each iteration and the variable that balance_keeper is supposed to hold is not being initialized at all; rather the loop simply keeps assigning the object to the litteral variable name 'balance_keeper' instead of the variable (example okex_XMR) that it is supposed to hold:

>>> okex_XMR.get()
Traceback (most recent call last):
  File "<pyshell#73>", line 1, in <module>
    okex_XMR.get()
NameError: name 'okex_XMR' is not defined

As you can see, instead of creating a new object, it simply is overwriting the previous object as it iterates:

>>> balance_keeper.get()
10.0

What am I doing wrong? How can I iterate through these lists, and properly initialize each variable, as opposed to simply overwriting the placeholder variable?

Chev_603
  • 313
  • 3
  • 14
  • 2
    Possible duplicate of [How do you create different variable names while in a loop?](https://stackoverflow.com/questions/6181935/how-do-you-create-different-variable-names-while-in-a-loop) – a_guest Jan 04 '19 at 01:51
  • Your code has two lines of the form: `balance_keeper =`, one right after the other. So, the first line of that form sets `balance_keeper` to a `String`, then that is immediately over written with an instance of your `Balance` class. The first of those two lines is essentially useless and accomplishes nothing. – John Anderson Jan 04 '19 at 02:03

2 Answers2

1

Use a dictionary to store your objects by assigning them string names

datadict = {}
for ex in EXCHANGES:
    for cur in CURRENCIES:
        ex_cur_name = cur+'_'+ex
        datadict[ex_cur_name] = Balance(ex, cur)

You can then access the objects via:

>>>datadict[ex_cur_name]
ycx
  • 3,155
  • 3
  • 14
  • 26
1

I think you missed understand some syntax in python. Look at this code: balance_keeper = cur+'_'+ex. As I can see, what you expected is that the balance_keeper will become variables like okex_XMR. No, it does not work that way.

  1. The assignment above only assigns the balance_keeper to a string. Not a variable. Remember that cur+'_'+ex is an expression, and it returns string!

  2. The of balance_keeper will be overwritten right after that balance_keeper=Balance(ex,cur).

  3. The idea here is keeping multiple wallets, each of them is identified by a pair of currency and exchange rate. This leads to using a dictionary, with key is a tuple of (cur, ex).