using System; using System.Collections.Generic; using System.Text; using System.Drawing; using WealthLab; using WealthLab.Indicators; using TASCIndicators;namespace WealthLab.Strategies { public class EwanRegimeSwitchStrategy : WealthScript { private StrategyParameter paramPeriod; public EwanRegimeSwitchStrategy() { paramPeriod = CreateParameter("Period", 21, 2, 100, 1); } protected override void Execute() { int period = paramPeriod.ValueInt; VolatilitySwitch voltSwitch = VolatilitySwitch.Series( Close,period ); for(int bar = GetTradingLoopStartBar( period * 3 ); bar < Bars.Count; bar++) { bool regimeSwitchIsTrending = voltSwitch[bar] > 0.5 ? false : true; if (IsLastPositionActive) { Position p = LastPosition; if( regimeSwitchIsTrending ) { if( CrossOver( bar, Close, SMA.Series( Close,10 ) ) ) SellAtMarket( bar+1, p, "Trend exit" ); } else { if( CrossUnder( bar, RSI.Series( Close,7 ), 60 ) ) SellAtMarket( bar+1, p, "MR exit" ); } } else { if( regimeSwitchIsTrending ) { if( CrossOver( bar, Close, SMA.Series( Close,10 ) ) ) BuyAtMarket( bar+1, "Trend" ); } else { if( CrossOver( bar, RSI.Series( Close,7 ), 30 ) ) BuyAtMarket( bar+1, "Mean reversion" ); } } } ChartPane vrsPane = CreatePane( 30,true,true ); PlotSeriesOscillator( vrsPane, voltSwitch, 0.5, 0.499, Color.Coral, Color.DarkGreen, Color.Transparent, LineStyle.Solid, 1 ); HideVolume(); } } }