Python - how to get add a counter inside a IF condition to add track the number of times something has occured

Viewed 33

I am new to python and learning it in bits and peices from internet. I have been tring to get a volume monitor for binance volumes.

for x in range(len(name)):
    # Code to get the data into panda dataframes for each token in name[] 
    hrlyvol = res1["volume"].iloc[0]
    min_2vol = res1["volume"].iloc[-1]
    actvol = hrlyvol - min_2vol
    voldiff = hrlyvol/actvol
    volch = voldiff-1
    op_price = res1["open"].iloc[0]
    cl_price = res1["close"].iloc[-1]
    price_change = (cl_price-op_price)/op_price
    
    if volch > 0.05 or price_change > 0.02:
    
        print("Volume is up by {:.0%} and Price has changed by {:.0%} in {}".format(
            volch, price_change, name[x]))
        time.sleep(3)

i tried adding i = i+1 but it then adds for all the tokens that satisfy this rule. what i want is for each token to have a separate counter since not all tokens would satisfy the condition. lets say xyz and abc met the condition then token should be xyz = 1 abc = 1 then if in the next iteration only xyz met the condition then xyz = 2 abc = 1 and so on.

1 Answers

I am using the example of odd number or even number to showcase the condition

i=0
j=0
for n in range(100):
    i = i+1 if n%2 == 0 else i+0
    j = j+1 if n%2 == 1 else j+0
Related