if( CrossUnder( bar, tpEma, haEma ) ) //if( tpEmabar < haEmabar ) ... if( CrossOver( bar, tpEma, haEma ) ) //if( tpEmabar > haEmabar )
using System; using System.Collections.Generic; using System.Text; using System.Drawing; using WealthLab; using WealthLab.Indicators; using TASCIndicators;namespace WealthLab.Strategies { public class SVEHATypCrossStrategy : WealthScript { private StrategyParameter paramHAPer; private StrategyParameter paramTypPer; private StrategyParameter paramBrkEven; private StrategyParameter paramSL; public SVEHATypCrossStrategy() { paramHAPer = CreateParameter("H-A Period", 8, 6, 9, 1); paramTypPer = CreateParameter("Typical period", 5, 4, 6, 1); paramBrkEven = CreateParameter("Breakeven stop %", 10, 2, 10, 2); paramSL = CreateParameter("Stop Loss %", 9, 2, 10, 1); } protected override void Execute() { // Parameters int perHA = paramHAPer.ValueInt, perTyp = paramTypPer.ValueInt; EMACalculation m = EMACalculation.Modern; // Create a Heikin-Ashi chart SVEHaTypCross sve = SVEHaTypCross.Series( Bars, perHA, perTyp ); Bars bars = new Bars( Bars.Symbol.ToString() + " (Heikin-Ashi)", Bars.Scale, Bars.BarInterval ); // Heikin-Ashi series DataSeries HO = Open + 0, HH = High + 0, HL = Low + 0; DataSeries HC = (Open + High + Low + Close) / 4; // Build the Bars object for (int bar = 1; bar < Bars.Count; bar++) { double o1 = HObar-1; double c1 = HCbar-1; HObar = ( o1 + c1 ) / 2; HHbar = Math.Max( HObar, Highbar ); HLbar = Math.Min( HObar, Lowbar ); bars.Add( Bars.Datebar, HObar, HHbar, HLbar, HCbar, Bars.Volumebar); } bars = Synchronize( bars ); // Build EMA of Heikin-Ashi close and EMA of Typical price EMA haEma = EMA.Series(bars.Close, perHA, m); EMA tpEma = EMA.Series(AveragePriceC.Series( Bars ), perTyp, m); // Plot the series ChartPane haPane = CreatePane(75, false, true); ChartPane svePane = CreatePane(10, false, true); PlotSymbol(haPane, bars, Color.DodgerBlue, Color.Red); PlotSeries(haPane, haEma, Color.Red, LineStyle.Solid, 1); PlotSeries(haPane, tpEma, Color.Green, LineStyle.Solid, 1); PlotSeries(svePane, sve, Color.Blue, LineStyle.Solid, 1); SetBarColors(Color.Silver, Color.Silver); HideVolume(); for(int bar = 1; bar < Bars.Count; bar++) { SetBarColor(bar, (svebar > 0) ? Color.LightGreen : Color.Black ); if (IsLastPositionActive) { Position p = LastPosition; if( Closebar < Openbar ) { double stopLoss = (1.0 - paramSL.Value / 100d); double stop = p.EntryPrice * stopLoss; double brkEven = p.EntryPrice * 1.01; if( CrossUnder( bar, tpEma, haEma ) ) //if( tpEmabar < haEmabar ) SellAtMarket( bar+1, p, "Regular" ); else if( !SellAtStop(bar + 1, p, stop, "SL")) if (p.MFEAsOfBarPercent(bar) > paramBrkEven.Value) SellAtStop(bar + 1, p, brkEven, "Breakeven"); } } else { if( Closebar > Openbar ) { if( CrossOver( bar, tpEma, haEma ) ) //if( tpEmabar > haEmabar ) BuyAtMarket( bar+1 ); } } } } } }