Python Forum
I want to run this loop but it skips all the condition
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
I want to run this loop but it skips all the condition
#1
I want to run this loop but it skips all the conditions and always runs n==1.
At the start of the code, I want it to run n==0 and get the data from the exchange. Then match with the order_id for data and set n==2 and count +1.

if data is not found I want it to run n == 1: code. Every time the count value is higher than loop_speed it must get new data from the exchange.
I know there is some problem with the loop help me solve it.
global data
global count
count = 0
global n
n = 1
loop_speed=10
def order_info(order_id):
  # print (loop_speed)
  # print (count)
   global count
   global n
   global data
   if count == 0:
        try:
            #order_id = 121553199503
            data = exchange.fetchOpenOrders(symbol)
            for d in data:
                if float(d['id']) == order_id:
                    info = d['info']
                    count = count + 1
                    print ("new")
                    print (info)
                    print (count)
                    n = 2
        except Exception as e:
            print (type(e).__name__, str(e))
            time.sleep(1)
   elif count <= loop_speed:

        # order_id = 121553198710
        #order_id = 121840387739
        print ('yess')
        for d in data:
            if float(d['id']) == order_id:
                info = d['info']
                print ("next")
                print (info)
                count = count + 1
                n = 2
                break
            else:
                n = 1
   elif count >= loop_speed:
        print (count)
        count = 0
        #order_id = 121840387739
        print ('y')
        for d in data:
            if float(d['id']) == order_id:
                info = d['info']
                print ("max")
                print (info)
                n = 2
                break
            else:
                n = 1
   elif n == 1:
        try:
            print ('true')
            info = exchange.fetchOrder(order_id)['info']
            count = count + 1
            print (info)
            print (count)
        except Exception as e:
            print (type(e).__name__, str(e))
            time.sleep(1)
            n == False
   return (info)
Reply
#2
This will never run:
   elif n == 1:
        try:
            print ('true')
            info = exchange.fetchOrder(order_id)['info']
            count = count + 1
            print (info)
            print (count)
        except Exception as e:
            print (type(e).__name__, str(e))
            time.sleep(1)
            n == False
Because one of these will always be True:
   elif count <= loop_speed:
    ...
   elif count >= loop_speed:
You need to write a description of what you want to do with enough clarity and detail that somebody could convert the description to code. This is not adequate.
Quote:I want to run this loop but it skips all the conditions and always runs n==1.
At the start of the code, I want it to run n==0 and get the data from the exchange. Then match with the order_id for data and set n==2 and count +1.

if data is not found I want it to run n == 1: code. Every time the count value is higher than loop_speed it must get new data from the exchange.
Reply
#3
(Feb-17-2022, 03:58 AM)deanhystad Wrote: This will never run:
   elif n == 1:
        try:
            print ('true')
            info = exchange.fetchOrder(order_id)['info']
            count = count + 1
            print (info)
            print (count)
        except Exception as e:
            print (type(e).__name__, str(e))
            time.sleep(1)
            n == False
Because one of these will always be True:
   elif count <= loop_speed:
    ...
   elif count >= loop_speed:
You need to write a description of what you want to do with enough clarity and detail that somebody could convert the description to code. This is not adequate.
Quote:I want to run this loop but it skips all the conditions and always runs n==1.
At the start of the code, I want it to run n==0 and get the data from the exchange. Then match with the order_id for data and set n==2 and count +1.

if data is not found I want it to run n == 1: code. Every time the count value is higher than loop_speed it must get new data from the exchange.



thanks, I have the problem it is going into the else loop and setting the value as one.

but I want to set n=1 only when the data value is not found
count = 0
loop_speed = 5
n=2
data = [1,2,3]
order_id = 3
if count == 0:
 for d in data:
   if float(d) == order_id:
     print ("count==0")
     print (count)
     count = count + 1
     print (count)
     info = d
     print (info)
elif count <= loop_speed and  n != 1:
  for d in data:
   if float(d) == order_id:
    print ("count <= loop_speed")
    print (count)
    count = count + 1
    print (count)
    info = d
    print (info)
    break
   else:
     n = 1
else:  
  for d in data:
   if float(d) == order_id:
    print ("else")
    print (count)
    count = 0 
    print (count)
    info = d
    print (info)
    break
  else:
     n = 1
if n == 1:
    print ("n==1")
    print (count)
    count = count + 1
    print (count)
    n = 2
Reply
#4
What do you mean by "when the data value is not found"? Do you mean when float(d) == ordder_id for all values of d in data?

How about something like this:
count = data.count(str(order_id))
if count > 0:
    ...
else:
    ...
Reply
#5
(Feb-17-2022, 06:06 AM)deanhystad Wrote: What do you mean by "when the data value is not found"? Do you mean when float(d) == ordder_id for all values of d in data?

How about something like this:
count = data.count(str(order_id))
if count > 0:
    ...
else:
    ...

No when data = exchange.fetchOpenOrders(symbol) which it got from the exchange does not match or is not found with the order id. It must set n = 1 or it must return the info.

if I put the code outside the function it works but when I put in the function it is just running. count = 0 one time then it sets n=1 and the other loops is skipped.

https://colab.research.google.com/drive/1PRwWv71-_WPMrKnO3zW_Ptk3xNoq8N8U?usp=sharing
Reply
#6
In both examples shown here:
https://colab.research.google.com/drive/...sp=sharing

You did this:
if n == 1:
In the code you posted here you do this:
elif n == 1:
That is why the posted code never runs the n == 1 part.

I think there are a lot of problems with the code, but that is the one that is making it work differently than when it wasn't in a function. I think you should write a description of what the code does. Using the word "and" more than twice indicates the function is trying to do too many things and should be refactored. I see a "check_open_orders(symbol, order_id)" function that returns a list of orders with matching id's. Actually, that is all I see for functions, but I don't understand what the count and clock rate is doing.
ndc85430 likes this post
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  else condition not called when if condition is false Sandz1286 10 5,935 Jun-05-2020, 05:01 PM
Last Post: ebolisa
  [HELP] Nested conditional? double condition followed by another condition. penahuse 25 8,171 Jun-01-2020, 06:00 PM
Last Post: penahuse
  While loop with condition that a list of strings contain a substring SnowingSilently 4 3,453 Sep-04-2018, 06:11 PM
Last Post: SnowingSilently
  Loop Condition Question malonn 6 3,498 Aug-01-2018, 01:56 PM
Last Post: malonn

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020