If a stock is sold with a stop loss and I'm running a dip buying strategy, I want to avoid buying it back on the next bar. I can add a check of upward momentum to constrain the buys, so I guess that will help avoid this problem; however, it may work better to just blacklist a stock for a certain period of time after a stop loss sell. Is there a way of doing this in WL?
Rename
This would be my approach, the example checks to see if the last trade sold at a stop loss, and if so prevents another buy for 5 bars. You can adjust the number as desired.
CODE:
using WealthLab.Backtest; using WealthLab.Core; namespace WealthScript2 { public class MyStrategy : UserStrategyBase { //Initialize public override void Initialize(BarHistory bars) { } //Execute public override void Execute(BarHistory bars, int idx) { if (OpenPositions.Count == 0) { //determine if the last position was closed due to a stop loss, if so, don't buy again for another 5 bars Position pos = LastPosition; if (pos != null) { if (pos.ExitSignalName == "Stop") { if (idx - pos.ExitBar < 5) return; } } //buy at 1% below the low PlaceTrade(bars, TransactionType.Buy, OrderType.Limit, bars.Low[idx] * 0.99); } else { //set at a stop loss, 2% below the close PlaceTrade(bars, TransactionType.Sell, OrderType.Stop, bars.Close[idx] * 0.98, "Stop"); //or sell at 2% above the high PlaceTrade(bars, TransactionType.Sell, OrderType.Limit, bars.High[idx] * 1.02, "Limit"); } } } }
Thank you
UserStrategyBase has a BarsSinceLastExit method to force a cooling period. Since it's exit signal name agnostic, let's introduce a filter for exit signal name in B28.
Your Response
Post
Edit Post
Login is required