Home > Software design >  Keep highest value for variable based on if statements
Keep highest value for variable based on if statements

Time:02-21

Could you help with the following:

I’m planning to use this code for the stock market. What I want to achieve is that if the second statement is triggered, so if price > b to keep the sell_price value 15 even if due to price fluctuation the first statement becomes true. However is the last condition becomes valid I want to keep the new sell price 25. So basically the sell price should continue to be updated as the price is going up and keep the highest value even when the price goes down and the last statement is not valid anymore. I tried several way to achieve this, but once the price goes down the sell price is also update with a lower value and I need the sell price to remain at the highest value.

Hope it’s clear what I want to achieve, so that I could get some valuable feedback on this.

if price > a
    sell_price= 10 

if price > b 
    sell_price = 15

if price > c 
    sell_price = 20

if price > d 
    sell_price = 25

CodePudding user response:

How about

if price > a:
    sell_price = max(sell_price,10)
if price > b:
    sell_price = max(sell_price,15)
# ...

CodePudding user response:

Thanks for the feedback and yes, it did result in what I wanted to achieve. I guess I was trying a too complex solution.

  • Related