I'm working on a momentum strategy that needs a regime filter such as only buy the strategy if the SPY (Regime) is above the 50 day SMA.
I haven't found how to do that. Can anyone help?
I haven't found how to do that. Can anyone help?
Rename
I don't know how to do this with Blocks, although I'm sure it can be easily done. But using C# code, this is how you can Buy a stock when SPY is above its 50-day SMA. You can also use a crossover trigger (instead of an inequality as shown). Study the IF statements below.
This strategy doesn't make any money because none of the Buy/Sell conditions in the IF statements are a function of what the stock itself is doing. You can add that.
This strategy doesn't make any money because none of the Buy/Sell conditions in the IF statements are a function of what the stock itself is doing. You can add that.
CODE:If you are new to WealthLab, I would start with one of the sample strategies that work, then play with it until you get what you want. But build from an existing strategy so you get going immediately.
using WealthLab.Backtest; using WealthLab.Core; using WealthLab.Indicators; namespace WealthScript3 { public class MyStrategy : UserStrategyBase { BarHistory sp500; IndicatorBase sma50; public override void Initialize(BarHistory bars) { sp500 = GetHistory(bars, "SPY"); sma50 = SMA.Series(sp500.Close, 50); PlotTimeSeriesCloud(sp500.Close, sma50, "SPY and its SMA50", "SPY", WLColor.Orange, WLColor.YellowGreen); } public override void Execute(BarHistory bars, int idx) { if (HasOpenPosition(bars, PositionType.Long)) { //sell conditions below if (sp500[idx] < sma50[idx]) PlaceTrade(bars, TransactionType.Sell, OrderType.Market,0.0, "<sma50 Sell"); } else { //buy conditions below if (sp500[idx] > sma50[idx]) PlaceTrade(bars, TransactionType.Buy, OrderType.Market,0.0, ">sma50 Buy"); } } } }
Your Response
Post
Edit Post
Login is required