As always, I'm trying to do things which aren't easy. Following is a simple program to buy on a new high and sell after 10 bars.
CODE:
Please log in to see this code.
There are a couple of problems with the program as written:
1. If the stock continues to make new highs, it will keep buying and selling the stock instead of just holding it. Not only does that generate unnecessary alerts and transaction costs, but the Strategy is out of the market one day in between each sell and the next buy.
2. If the stock didn't make a new high on day the position was sold, a new buy signal won't be generated, so the Strategy may be out of the market for addition days until a new buy signal is generated (which may or may not make the Strategy less profitable).
Therefore, what I'd like to do is to buy on a new high and keep the position open for 10 bars after the last new high was made.
I can take the statement setting the sellBar variable out of the if statement, which will extend the trade if another new high is made. That solves these problems, but creates a new priority problem. What if there is a higher priority trade available for another symbol when the trade is extended?
If I didn't extend the trade, and simply closed the position and re-opened it, the PosSizer would look at the priority of the next buy signal to determine whether or not to take the trade. However, by extending the trade, even if I change the priority of the position, the PosSizer isn't going to do anything, because the position is already active. Therefore, even if there were higher priority trades at the time of the extension, it wouldn't be opened by the PosSizer, because this position is still open.
The only way I can think of to get around this problem is to go through my Data Range day by day, look at each symbol in my DataSet, make a list of the highest priority positions and if this is one of the highest priority positions, extend the trade. That is very computationally expensive.
Is there a better way to program this Strategy?