I really appreciate any help I can get with making this code work (I am kind of a newbie)
Basically, I have a portfolio of about 100 positions that I intend to hold for several years. But to take advantage of short term price drops, I want an algorithm that, for any of my positions that drop by 20% below the cost basis, the algorithm will add more shares (1 share at a time) until the average price for that stock is no longer 20% blow cost basis.
For example, if I have 10 stock of CAT that I bought at $100/share. If CAT drops to $79/share (21% down), the algorithm would initiate an order to buy 1 share. Now I would have 11 shares of CAT with cost basis of 98.09 {(10 *100 + 1*79)/11}. Since $79 is about 19.5% blow $98.09, no more orders will be initiated but if CAT price drops again to $78 which is 20.5% below $98.09, the algorithm would initiate another order for 1share.
When I deployed this algorithm to live trading with Robinhood I thought it would work, but it did not add any of the stocks for the positions that met the specified criteria. I do not understand why - this is the part I need help figuring out. I have two positions in my portfolio that meet the 20% down criteria and there is enough cash in the portfolio. I kindly request help figuring out why.
Please help me figure why the algorithm did not initiating buy trades for stocks below 20% of cost basis.