using System; using System.Collections.Generic; using System.Text; using System.Drawing; using WealthLab; using WealthLab.Indicators;namespace WealthLab.Strategies { // Premier Stochastic public class PremierStochastic : DataSeries { public PremierStochastic(Bars bars): base(bars, "Premier Stochastic") { this.FirstValidValue = 8*3; EMACalculation m = EMACalculation.Modern; StochK K = StochK.Series( bars, 8 ); EMA S = EMA.Series( EMA.Series( ( (K - 50) * 0.1 ), 5, m ), 5, m ); for (int bar = FirstValidValue; bar < bars.Count; bar++) { this[bar] = ( Math.Exp( S[bar] ) - 1 ) / ( Math.Exp( S[bar] ) + 1 ); } } public static PremierStochastic Series(Bars bars) { string description = string.Concat(new object[] { "PremierStochastic" }); if (bars.Cache.ContainsKey(description)) { return (PremierStochastic)bars.Cache[description]; } PremierStochastic _PremierStochastic = new PremierStochastic(bars); bars.Cache[description] = _PremierStochastic; return _PremierStochastic; } } public class PremierStochasticStrategy : WealthScript { private StrategyParameter paramPTShort; private StrategyParameter paramPTLong; private StrategyParameter paramSLShort; private StrategyParameter paramSLLong; public PremierStochasticStrategy() { paramPTLong = CreateParameter("Profit Target (L)", 1.08, 1.01, 1.30, 0.01); paramSLLong = CreateParameter("Stop Loss (L)", 0.96, 0.85, 0.99, 0.01); paramPTShort = CreateParameter("Profit Target (S)", 0.92, 0.70, 0.99, 0.01); paramSLShort = CreateParameter("Stop Loss (S)", 1.04, 1.01, 1.15, 0.01); } protected override void Execute() { PremierStochastic PS = PremierStochastic.Series( Bars ); bool xo1, xo2, xu1, xu2; double stop, profit; for(int bar = PS.FirstValidValue; bar < Bars.Count; bar++) { xo1 = CrossOver(bar, PS, -0.9); xo2 = CrossOver(bar, PS, -0.2); xu1 = CrossUnder(bar, PS, 0.9); xu2 = CrossUnder(bar, PS, 0.2); if (IsLastPositionActive) { Position p = LastPosition; if ( p.PositionType == PositionType.Long ) { stop = p.EntryPrice * paramSLLong.Value; profit = p.EntryPrice * paramPTLong.Value; } else { stop = p.EntryPrice * paramSLShort.Value; profit = p.EntryPrice * paramPTShort.Value; } if( !ExitAtStop( bar+1, p, stop, "Stop Loss" ) ) ExitAtLimit( bar+1, p, profit, "Profit Target" ); } else { if( xo1 || xo2 ) { if( ShortAtMarket( bar+1 ) != null ) LastPosition.Priority = -PS[bar]; } else if( xu1 || xu2 ) { if( BuyAtMarket( bar+1 ) != null ) LastPosition.Priority = PS[bar]; } } } HideVolume(); ChartPane psPane = CreatePane( 40, false, true ); DrawHorzLine( psPane, 0.90, Color.LightBlue, LineStyle.Solid, 2 ); DrawHorzLine( psPane, 0.20, Color.LightBlue, LineStyle.Solid, 2 ); DrawHorzLine( psPane, -0.20, Color.LightCoral, LineStyle.Solid, 2 ); DrawHorzLine( psPane, -0.90, Color.LightCoral, LineStyle.Solid, 2 ); PlotSeriesOscillator( psPane, PS, 0, 0, Color.FromArgb(60, Color.Blue), Color.FromArgb(60, Color.Red ), Color.Transparent, LineStyle.Solid, 1 ); PlotSeries( psPane, PS, Color.Black, LineStyle.Solid, 1 ); } } }