Hello all - I'm trying to create an algorithm that rebalances a static set of securities on either a monthly or quarterly basis, and every month it purchases an SPY Put 10% OTM. I can sometimes get the rebalancing to work, but I can't seem to get the options chain to update consistently and place the orders. I'd also like to close the options position 5 minutes before closing the day before it expires to avoid exercising. 

 

Can I please get some help, not sure what is wrong with my code.