Python Forum
While Loop Variable Freezing?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
While Loop Variable Freezing?
#11
Ok, you know how reqPnL works, it is constantly getting updated with new pnl ticks.

def pnl(self, reqId, dailyPnL, unrealizedPnL, realizedPnL):
        super().pnl(reqId, dailyPnL, unrealizedPnL, realizedPnL)
        self.pnl_summary = {"ReqId":reqId, "DailyPnL": dailyPnL, "UnrealizedPnL": unrealizedPnL, "RealizedPnL": realizedPnL}
        print("unrealized PnL = ", self.pnl_summary["UnrealizedPnL"])
        app.trigger = False
        if app.pnl_summary["UnrealizedPnL"] < 200 and app.trigger == False:
            app.trigger = True
            print("Testing")
            square_off(app)
If my unrealizedPnL drops below 200, I want square_off(app) function to only run once, not every single time the unrealizedpnl ticks back again (every second) below 200.

The above code does not work, it constantly loops square_off(app) every time the unrealizedPnL ticks again, under 200.

Can you adjust the code for me, so that the square_off function only runs once. Thank you Heart
Reply
#12
Move app.trigger = False to the __init__ method, so it's set only once, when your program starts. Then never set it to False again, and square_off(app) won't be called more than once.
stylingpat likes this post
Reply
#13
(Feb-24-2021, 06:55 PM)nilamo Wrote: Move app.trigger = False to the __init__ method, so it's set only once, when your program starts. Then never set it to False again, and square_off(app) won't be called more than once.

Thank you so much! Seems to be working fine now in the post market. I'll keep testing this week! Big Grin
nilamo likes this post
Reply
#14
you needs static variable to know the value new target



Exampel :
class Exampel:
    newvalue, staticvalue = None , None
    
    def MULTIPLY(self, parm):
        self.newvalue = parm
        # here write your code
        
if __name__ == '__main__':
    import random 
    exm = Exampel()
    num = random.randint(1,100 )
    while num:
        num = random.randrange(1,100)
        exm.MULTIPLY(num)
        exm.staticvalue = 50
        if(exm.newvalue != exm.staticvalue ) and  ( not exm.newvalue < exm.staticvalue):
            print(exm.newvalue)
        elif exm.newvalue < exm.staticvalue:
            print('exit')
            break
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Variable definitions inside loop / could be better? gugarciap 2 432 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,685 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,572 Aug-17-2022, 10:17 PM
Last Post: deanhystad
  loop (create variable where name is dependent on another variable) brianhclo 1 1,136 Aug-05-2022, 07:46 AM
Last Post: bowlofred
  Multiple Loop Statements in a Variable Dexty 1 1,203 May-23-2022, 08:53 AM
Last Post: bowlofred
Big Grin Variable flag vs code outside of for loop?(Disregard) cubangt 2 1,168 Mar-16-2022, 08:54 PM
Last Post: cubangt
  How to save specific variable in for loop in to the database? ilknurg 1 1,145 Mar-09-2022, 10:32 PM
Last Post: cubangt
  How to add for loop values in variable paulo79 1 1,442 Mar-09-2022, 07:20 PM
Last Post: deanhystad
  Using Excel Cell As A Variable In A Loop knight2000 7 4,105 Aug-25-2021, 12:43 PM
Last Post: snippsat
  Using Excel Cell As A Variable In A Loop knight2000 7 5,000 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