Home > Net >  unable to iterate through loop in python
unable to iterate through loop in python

Time:04-07

i have a sql query which basically retrieves coin names and submits an order for each coin. However, it only submits an order on one coin and fails to loop through the rest, not sure why thats happening .

import sys

**
import pandas as pd


postgreSQL_select_Query = "SELECT base,quote FROM instrument_static where exchange='ftx'"

cursor.execute(postgreSQL_select_Query)
row=([y for y in cursor.fetchall()])

for i in row:
   base=i[0]
   quote=i[1]
   portfolioItems = [
       {
           'exchange': 'ftx',
           'base': base,
           'quote': quote,
           'amount': 0.01,
       },

   ]


   def init():

       username = us
       password = passwordVal
       initialise(clientId, clientSecret, us, password)


if __name__ == "__main__":
       init()
       result = construct_portfolio_with_params(us, portname, portfolioItems)
       print(result)

CodePudding user response:

You need to initialize portfolioItems prior to the loop, and then you can add to it. Try replacing this snippet of code:

...
row=([y for y in cursor.fetchall()])

portfolioItems = []

for i in row:
   base=i[0]
   quote=i[1]
   portfolioItems.append(
       {
           'exchange': 'ftx',
           'base': base,
           'quote': quote,
           'amount': 0.01,
       }

   )
...
  • Related