- ago
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?
0
1,051
Solved
3 Replies

Reply

Bookmark

Sort
Glitch8
 ( 10.10% )
- ago
#1
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");          }       }    } }
1
Best Answer
- ago
#2
Thank you
0
- ago
#3
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.
1

Reply

Bookmark

Sort