1

I have a forever loop that rebalance my portfolio. I call an exchange API which basically does not cost anything as long as I stay within their rate limit (1-2 call per sec), so I loop them forever with minimal sleep time (10 sec).

Then I want to add an API call from Coinmarketcap as a small loop that run once every 10 mins. This is done to avoid their 'free' API limit (333 call/day, and I use 2 calls per loop).

so My forever loop is simply:

while True:
    try:
       countCMC = 0

       for loop
          
          if countCMC == 0 or countCMC % 60 == 0:
             get Coinmarketcap api......
          
          countCMC = countCMC + 1

          run bunches of rebalance algo......
          time.sleep(10)
    except:
       ....

I want to call Coinmarketcap and parse their data to variables, and then use them in my rebalance algo. May be a small loop inside "try" with sleep time?

Update: I have tried to add count and then use modulo, so it runs at the beginning and every 60x10 = 600 sec thereafter. Wonder if there is a better way?

Alphatio
  • 47
  • 5
  • So, you'll have to count. You'll do your Coinmarketcap every 60 times through your loop. – Tim Roberts Apr 12 '22 at 06:36
  • I use count as you suggested and modulo. Wonder if there is a better or more efficient way – Alphatio Apr 12 '22 at 06:51
  • hello, guess this will help: https://stackoverflow.com/questions/474528/what-is-the-best-way-to-repeatedly-execute-a-function-every-x-seconds – Ren Apr 12 '22 at 07:05
  • by the way "if countCMC == 0 or countCMC % 60 == 0:" can be simplified to "if countCMC % 60 == 0:" because 0%60 = 0 – Ren Apr 12 '22 at 07:06
  • You can take help of Threads here. Run both in different threads and collect the results as you need. – Kris Apr 12 '22 at 07:08
  • You are sleeping for 10 seconds between every action you take. Efficiency is simply not a factor here. Don't waste time optimizing this. – Tim Roberts Apr 12 '22 at 17:37

0 Answers0