if( CrossUnder( bar, tpEma, haEma ) ) //if( tpEma[bar] < haEma[bar] ) ... if( CrossOver( bar, tpEma, haEma ) ) //if( tpEma[bar] > haEma[bar] )
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 = HO[bar-1]; double c1 = HC[bar-1]; HO[bar] = ( o1 + c1 ) / 2; HH[bar] = Math.Max( HO[bar], High[bar] ); HL[bar] = Math.Min( HO[bar], Low[bar] ); bars.Add( Bars.Date[bar], HO[bar], HH[bar], HL[bar], HC[bar], Bars.Volume[bar]); } 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, (sve[bar] > 0) ? Color.LightGreen : Color.Black ); if (IsLastPositionActive) { Position p = LastPosition; if( Close[bar] < Open[bar] ) { double stopLoss = (1.0 - paramSL.Value / 100d); double stop = p.EntryPrice * stopLoss; double brkEven = p.EntryPrice * 1.01; if( CrossUnder( bar, tpEma, haEma ) ) //if( tpEma[bar] < haEma[bar] ) 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( Close[bar] > Open[bar] ) { if( CrossOver( bar, tpEma, haEma ) ) //if( tpEma[bar] > haEma[bar] ) BuyAtMarket( bar+1 ); } } } } } }