Python Forum

Full Version: While Loop Variable Freezing?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
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
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.
(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
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
Pages: 1 2