Hello Quants,
I am still very new and have been working on a few algorithms simply to better understand the system. I'm stuck on a strange problem. The algorithm is a bulked up momentum script:
First, I define an (arbitrary) selection of stocks and the %s at which buy/sell actions should take place. The first time it runs (when the portfolio is empty), it will take half the cash available and distribute it evenly among the stocks.
Next, it sells all stocks that have gone up in value by a certain % (defined by returns()), and then buys all stocks (that I don't already hold) that have gone down by a certain % (also defined by returns()). Instead of splitting available cash evenly among these stocks, I create a "buy curve" which favors the stocks with the most "middle of the road" returns (but that still fall within the "buy" threshold) and buys less of those that barely make the cut or have dropped too much.
However, when I was running it daily, I was seeing things like this:
4/27
Buy EROC: 34 shares @ $2.55. Total: $86.70
Buy LLNW: 23 shares @ $3.76. Total: $86.48
4/28
Sell EROC: 34 shares @ $2.45. Total: ($83.30)
Sell LLNW: 23 shares @ $3.68. Total: ($84.64)
It's selling when the price has dropped.
returns(), according to the documentation, is the % gain/lost in the last day. However, the returns() for these securities are positive on 4/28, even though their prices were lower when the orders took place.
I want this to run once daily, but to troubleshoot, I ran it in minute mode (but then had to add line 56, which skips that minute if there are still open orders; this should in theory only run through sell and buy functions when all orders have settled, thus preventing duplicate sells and buys). Same problem.
What am I missing? I am simply trying to say, "if the price went up x% since yesterday, sell. If it went down y% since yesterday, buy." returns() doesn't seem to provide a reliable way to do this.
Thanks in advance. This community has been incredibly helpful throughout my first few weeks here and I hope to be able to return the favor soon.