Hello. I am fairly new to Python. I have spent some time trying to implement this and wanted to ask the community for some advice. I am receiving an index out of bounds error. I would like help addressing that as well as any advice on a way to write this code more efficiently because mine is very rough and I am not sure it is doing what I think it is doing...
FYI - This is run on minutely data.
Here's the way I think of this. I'd like to have a variable (LastSignal) switch/toggle between two values: OverSold and OverBought. If the RSI crosses 70, variable returns OverBought. The variable remains OverBought until RSI crosses below 30 (OverSold). The variable is OverSold until RSI crosses back over 70 and etc. you get the picture...
Here's what I am doing in my code (or at least I think I am doing):
I am defining a function 'rebalance'. The function is re balanced every day and stores an empty array into a variable with context.
Every minute I am appending the RSI value to an array, masking the array so as to only contain values greater than 70 and lower than 30, and storing the most recent value of the array to a variable, which I can later check to see if the variable was OverSold more recent than OverBought.
Questions:
1) IndexError: index -1 is out of bounds for axis 0 with size 0.
How do I fix the out of bounds error on line 37?
Is the error because I am trying to access a value in an empty array?
2) I don't want to append every RSI value and filtering if I don't need to. If I could just set a variable to 1 if RSI was OverBought more recently than OverSold and then switch the value to -1 if RSI crosses back above 70. Then later in a code I can use expression such as LastSignal > 0, LastSignal < 0. Would it make more sense to use a dictionary?
Thanks in advance. I know this is a peculiar question and I hope I made sense.