using System; using System.Collections.Generic; using System.Text; using System.Drawing; using WealthLab; using WealthLab.Indicators;namespace WealthLab.Strategies { public class ColorBased : WealthScript { private StrategyParameter paramLookback; private StrategyParameter paramSL; private StrategyParameter paramPT; public ColorBased() { paramLookback = CreateParameter("Lookback", 20, 2, 100, 1); paramSL = CreateParameter("Stop %", 4, 1, 10, 1); paramPT = CreateParameter("Target %", 4, 1, 20, 1); } private Color color( int bar, int lookback ) { Color c = Color.Transparent; if( Bars.Count > lookback ) { int b = bar-lookback; bool green = (Closebar > Closeb) && (Volumebar > Volumeb); bool blue = (Closebar > Closeb) && (Volumebar <= Volumeb); bool orange = (Closebar < Closeb) && (Volumebar < Volumeb); bool red = (Closebar < Closeb) && (Volumebar >= Volumeb); if( green ) c = Color.Green; if( blue ) c = Color.Blue; if( orange ) c = Color.Orange; if( red ) c = Color.Red; } return c; } protected override void Execute() { int lookback = paramLookback.ValueInt; double SL = paramSL.Value; double PT = paramPT.Value; for(int bar = lookback; bar < Bars.Count; bar++) { Color c = color(bar,lookback); SetSeriesBarColor( bar, Volume, c ); SetBarColor( bar, c ); if (IsLastPositionActive) { Position p = LastPosition; double Stop = p.EntryPrice * (1 - SL / 100.0d); double Target = p.EntryPrice * (1 + PT / 100.0d); if( c == Color.Red || c == Color.Orange ) SellAtMarket(bar+1, p, c.ToString() ); else if( !SellAtStop(bar + 1, p, Stop, "SL") ) SellAtLimit(bar + 1, p, Target, "TP"); } else { if( c == Color.Green || c == Color.Blue ) BuyAtMarket( bar+1, c.ToString() ); } } } } }