Python Forum
While Loop Variable Freezing?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
While Loop Variable Freezing?
#1
Hey guys, I'm a newb. I'm trying to get this software to run on my computer but I seem to keep getting stuck in my while loop.

This is for trading, I am returning a set of information (variable?) to unrealizedPnL in my function pnl.

It updates every second.

def pnl(self, reqId, dailyPnL, unrealizedPnL, realizedPnL):
        print("UnrealizedPnL:", round(unrealizedPnL))
        if unrealizedPnL < -100:
            print("Step 1", unrealizedPnL)
            while unrealizedPnL < -50:
                time.sleep(1)
                print("Step 1.5", unrealizedPnL)
            else:
                print("Step 2, Worked", unrealizedPnL)
The numbers in step 1 (line 3), -100 and the -50 in step 2 (line 5) are just because its almost impossible to make a profit for the test every time. So I just open a trading position, let it lose money then adjust the numbers to run the test. I'm just trying to get the steps to work.

Once my account goes past -100, we goto step 1.5, the while loop. And it just loops infinitly (I know it does that) BUT.. if I close out my positions and my unrealizedPnL goes back to 0, or even moves around to any number, the while loop stays LOCKED on what ever number initiated it. It doesnt keep updating.

I've got time.sleep(1) in (line 6) while I try and figure this out and I'm not 100000x spammed by the while loop. Atleast I'm 5000x spammed hahah Big Grin

The end goal logically is, if I make over $100, we trigger a new set of conditions, if we stay above $50, all good. If we drop under $50, execute a command.

Let me know what you guys think!! Where am I messing up with this logical flow?

BTW Everything is working as intended.

There is a call
app.reqPnL(1000, "account", "")
that pulls the pnl into the pnl function so that I can manipulate it. It works as intended. But once the while loop triggers, thats when it freezes.

Heart
Reply


Messages In This Thread
While Loop Variable Freezing? - by stylingpat - Feb-19-2021, 11:48 PM
RE: While Loop Variable Freezing? - by bowlofred - Feb-20-2021, 12:01 AM
RE: While Loop Variable Freezing? - by stylingpat - Feb-20-2021, 12:04 AM
RE: While Loop Variable Freezing? - by bowlofred - Feb-20-2021, 12:44 AM
RE: While Loop Variable Freezing? - by stylingpat - Feb-20-2021, 12:10 AM
RE: While Loop Variable Freezing? - by stylingpat - Feb-20-2021, 03:40 AM
RE: While Loop Variable Freezing? - by nilamo - Feb-23-2021, 10:57 PM
RE: While Loop Variable Freezing? - by stylingpat - Feb-24-2021, 12:35 AM
RE: While Loop Variable Freezing? - by bowlofred - Feb-24-2021, 12:39 AM
RE: While Loop Variable Freezing? - by nilamo - Feb-24-2021, 04:32 PM
RE: While Loop Variable Freezing? - by stylingpat - Feb-24-2021, 06:10 PM
RE: While Loop Variable Freezing? - by nilamo - Feb-24-2021, 06:55 PM
RE: While Loop Variable Freezing? - by stylingpat - Feb-24-2021, 10:51 PM
RE: While Loop Variable Freezing? - by Abdullah - Feb-25-2021, 10:42 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Variable definitions inside loop / could be better? gugarciap 2 474 Jan-09-2024, 11:11 PM
Last Post: deanhystad
  How to create a variable only for use inside the scope of a while loop? Radical 10 1,845 Nov-07-2023, 09:49 AM
Last Post: buran
  Nested for loops - help with iterating a variable outside of the main loop dm222 4 1,629 Aug-17-2022, 10:17 PM
Last Post: deanhystad
  loop (create variable where name is dependent on another variable) brianhclo 1 1,162 Aug-05-2022, 07:46 AM
Last Post: bowlofred
  Multiple Loop Statements in a Variable Dexty 1 1,220 May-23-2022, 08:53 AM
Last Post: bowlofred
Big Grin Variable flag vs code outside of for loop?(Disregard) cubangt 2 1,198 Mar-16-2022, 08:54 PM
Last Post: cubangt
  How to save specific variable in for loop in to the database? ilknurg 1 1,167 Mar-09-2022, 10:32 PM
Last Post: cubangt
  How to add for loop values in variable paulo79 1 1,475 Mar-09-2022, 07:20 PM
Last Post: deanhystad
  Using Excel Cell As A Variable In A Loop knight2000 7 4,198 Aug-25-2021, 12:43 PM
Last Post: snippsat
  Using Excel Cell As A Variable In A Loop knight2000 7 5,075 Jul-18-2021, 10:52 AM
Last Post: knight2000

Forum Jump:

User Panel Messages

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