using System; using System.Collections.Generic; using System.Text; using System.Drawing; using WealthLab; using WealthLab.Indicators;namespace WealthLab.Strategies { public class SARStrategy : WealthScript { protected override void Execute() { DataSeries maFast = SMA.Series(Close, 50); DataSeries maSlow = SMA.Series(Close, 200); PlotSeries(PricePane,maFast,Color.Red,LineStyle.Solid,2); PlotSeries(PricePane,maSlow,Color.Green,LineStyle.Solid,2); for(int bar = 201; bar < Bars.Count; bar++) { // Detect crossover/crossunder and store state in a variable bool maXo = CrossOver(bar, maFast, maSlow); bool maXu = CrossUnder(bar, maFast, maSlow); // The first trade if (Positions.Count == 0){ if ( maXo ) BuyAtMarket( bar + 1 ); else if( maXu ) ShortAtMarket( bar + 1 ); } // Subsequent trades else { Position p = LastPosition; if ( p.PositionType == PositionType.Long ) { if ( maXu ) { SellAtMarket( bar + 1, p ); ShortAtMarket( bar + 1 ); } } else if ( maXo ) { CoverAtMarket( bar + 1, p ); BuyAtMarket( bar + 1 ); } } } } } }
using System; using System.Collections.Generic; using System.Text; using System.Drawing; using WealthLab; using WealthLab.Indicators;namespace WealthLab.Strategies { public class SARStrategy : WealthScript { protected override void Execute() { DataSeries HH = Highest.Series(High, 100); DataSeries LL = Lowest.Series(Low, 100); PlotSeries(PricePane,HH,Color.Blue,LineStyle.Solid,1); PlotSeries(PricePane,LL,Color.Red,LineStyle.Solid,1); for(int bar = GetTradingLoopStartBar(100); bar < Bars.Count; bar++) { // The first trade if (Positions.Count == 0){ if ( BuyAtStop( bar + 1, HHbar ) == null ) ShortAtStop( bar + 1, LLbar ); } // Subsequent trades else { Position p = LastPosition; if ( p.PositionType == PositionType.Long ) { if ( SellAtStop( bar + 1, p, LLbar ) ) ShortAtStop( bar + 1, LLbar ); } else { if (CoverAtStop( bar + 1, p, HHbar ) ) BuyAtStop( bar + 1, HHbar ); } } } } } }