0

I have a nested if statement within a for loop but the first if statement seems not to be progressing onto the rest of the for loop.

for i, x in enumerate(S[start:-1], start):
 if i > max(sma_period1, sma_period2, sma_period3):
 j = i-start
 
 if ma1[i] == x:
 w[j+1] = w[j]
 cash[j+1] = cash[j]
 if ma1[i] < x: 
 w[j+1] = cash[j]/x + w[j]
 cash[j+1] = 0
 if ma1[i] > x:
 cash[j+1] = w[j]*x + cash[j]
 w[j+1] = 0
tf_strategy_ma1 = [a*b for a,b in zip(w,S[start:])]+ cash

Sorry if this is a very basic question, I am new to coding and completely stuck. Thanks for your help.

asked Feb 27, 2021 at 16:26
2
  • if its not getting in you need to get an idea of what the test is seeing. the max() statement as currently show in in your post does not vary each time through the loop and can be calculated and assigned a variable before entering the loop. I would do that, print that value then print the I value on each iteration. that will give you an idea how the loop is behaving. Commented Feb 27, 2021 at 16:37
  • Why have used S[start:-1] in enumerate. Never seen something like that? Commented Feb 27, 2021 at 16:40

1 Answer 1

1

Some debug pointers for a situation like this:

  1. you need to get an idea of what the test is seeing.
  2. the max() statement as currently show in in your post does not vary each time through the loop and can be calculated and assigned a variable before entering the loop.
  3. I would do that, print that value then print the I value on each iteration. that will give you an idea how the loop is behaving.
answered Feb 27, 2021 at 16:39
Sign up to request clarification or add additional context in comments.

1 Comment

Sorry for the very late reply. Thank you very much. I did in fact follow your advice.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.