Hello all, very new to all this.
My question is as follows. I've been messing around with a strategy in Excel with historical data, and I'm ready to code it up and actually backtest it. Here's the strategy, and I'll highlight what problems I'm having.
1. Buy into the stock at the beginning of each month if out (I'm running with XIV, for a few reasons, but it works with anything volatile really).
This is very easy to code. I think I have this down.
2. Set a stoploss (or, more complicated, a trailing high-water mark stoploss) for 0.95 of entry price, at which I will sell out
I've read up on this, and I've tried a couple models I've gotten to run with this working.
3. Here's the kicker that I don't know how to code. Every time the stoploss is triggered and I sell out, I want to put in a buy order at the stoploss (or just above if noise will be too much of a factor). The purpose of this is if the stock goes down 5% right away, but then bounces back and has a fantastic month, I don't want to miss those gains. However, I want the stop to remain where it was (either .95 of beginning month price, or .95 of high-water mark if trailing is used) so that it limits my monthly downside to 5% (theoretically, I know slippage, fees etc exist).
I've attached a basic version, which basically doesn't have the reentry behavior I specify in 3, it simply stays out for the month if the stop is triggered and tries again next month. I've commented out the backin function, because, as I've said, it isn't behaving the way I want it to.
Thanks for the help!