Python Forum
While Loop Variable Freezing? - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: While Loop Variable Freezing? (/thread-32591.html)

Pages: 1 2


RE: While Loop Variable Freezing? - stylingpat - Feb-24-2021

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


RE: While Loop Variable Freezing? - nilamo - Feb-24-2021

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.


RE: While Loop Variable Freezing? - stylingpat - Feb-24-2021

(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


RE: While Loop Variable Freezing? - Abdullah - Feb-25-2021

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