Ask coding questions

← Back to all posts
I need help with a control flow tool
rogeryoung (0)

The idea of the following Repl (program) is to maintain a portfolio with an allocation in 4 ETF's to 25% within set bands.
If any of the bands are exceeded the entire portfolio needs to be rebalanced.
Lines 71 to 78 ARE WRONG as all of you know but they (I hope) illustrate what I am trying to do here. In other words, if all of the conditions are met there is no need to rebalance and the program should stop. If the conditions are not met and the bands (+.35 or -.15) then the program should continue on to LINE 83 on beyond where it will rebalance the portfolio.
The following Repl does run and shows what a portfolio looks like that needs to be rebalanced:
I am thanking any of you in advance for any time that you spend on this for me.

InvisibleOne (2687)

I believe it should be

if vtival > float(bucket + .35*bucket or vtival > float(bucket -.15*bucket)):
  # do something
elif (tltval < float(bucket+.35*bucket) or tltval > float(bucket-.15*bucket)):
  # do something else
elif # so on
rogeryoung (0)

Is that Python? I assumed that somewhere it was mentioned that this is written in Python. @InvisibleOne

rogeryoung (0)

This looks like I will have to write more than 8 lines of code rather than 4. I think in reading a book on Python I got the idea that this would be simpler than it is as in:
"The for loop executes a block of code repeatedly until the condition in the for statement is no longer valid." and:
"6.6 Break When working with loops, sometimes you may want to exit the loop when a certain condition is met. To do that, we use the break keyword." and:
"Another useful keyword for loops is the continue keyword. When we use continue, the rest of the loop after the keyword is skipped for that iteration."
Please forgive me for being so slow at learning these things. I will be working on trying to apply what you said in the coming days.


InvisibleOne (2687)

Yeah you should just use elif through till you have all the values satisfied and the result for each one. @rogeryoung