I'm a little bit of a newbie and would like to know how to program the following scenario. I will use TQQQ, SQQQ and QQQ here, but it could be use with any very liquid leveraged pair.

1. At a given interval (based on time or volume), purchase SQQQ and TQQQ with the exact same amount of money (let's say $30k each).  Because both move in opposite directions, I would be neutral in the market. (or close enough)

2. If the price of TQQQ moves up by 5% (or some number of my liking), sell it at market and purchase QQQ immediately to rebalance the portfolio.

3. Decide at some point before the end of the day the best time to exit all positions without losing money.

4. I don't want to keep Leveraged ETFs overnight because of the decay factor. I could see scenarios where I can keep QQQ longer. 

5. I have done this manually and it has given me some decent results. However, I would like to automate this process. I trade commission free.

Thanks for any code or hints from experts.