Common Signals

The Common Signal category contains methods that are commonly used to produce trading signals.

BuyAtMarket

CrossOver

bool CrossOver(int bar, DataSeries ds1, DataSeries ds2);
bool CrossOver(int bar, DataSeries ds1, double value);

Returns true if the specified DataSeries (ds1) crosses over either another DataSeries (ds2), or a specific value, on the specified bar.  Specifically, CrossOver returns true if the current value is above the target value at the specified bar, and the previous value was less than or equal to the target value at the previous bar.


Example

protected override void Execute(){
    DataSeries wma1 = WMA.Series( Close, 30 );
    DataSeries wma2 = WMA.Series( Close, 60 );
    PlotSeries( PricePane, wma1, Color.LightCoral, WealthLab.LineStyle.Solid, 1 ); 
    PlotSeries( PricePane, wma2, Color.LightBlue, WealthLab.LineStyle.Solid, 1 );

    // A simple Weighted Moving Average Crossover System
    
    for(int bar = wma2.FirstValidValue; bar < Bars.Count; bar++)
    {
        if (IsLastPositionActive)
        {
            Position p = LastPosition;    
            SellAtStop( bar+1, p, p.EntryPrice * 0.96, "4% Stop" );
            SellAtLimit( bar+1, p, p.EntryPrice * 1.06, "6% Target" );    
        }
        else
        {
            if ( CrossOver( bar, wma1, wma2 ) )
            BuyAtMarket( bar+1 );
        }
    }
}
BuyAtMarket

CrossUnder

bool CrossUnder(int bar, DataSeries ds1, DataSeries ds2);
bool CrossUnder(int bar, DataSeries ds1, double value);

Returns true if the specified DataSeries (ds1) crosses under either another DataSeries (ds2), or a specific value, on the specified bar.  Specifically, CorssUnder returns true if the current value is below the target value at the specified bar, and the previous value was greater than or equal to the target value at the previous bar.


Example

protected override void Execute(){
    ChartPane StochPane = CreatePane( 50, true, true );
    DataSeries D = StochD.Series( Bars, 3, 20 );
    DataSeries Signal = EMA.Series( D, 9, WealthLab.Indicators.EMACalculation.Modern );
    PlotSeries( StochPane, D, Color.Blue, WealthLab.LineStyle.Solid, 1 );
    PlotSeries( StochPane, Signal, Color.Gray, WealthLab.LineStyle.Solid, 1 );

    for(int bar = 30; bar < Bars.Count; bar++)
    {
            // It closes all positions when Stochastic 
            // crosses below the signal line from above 80.
            
            if ( ( ActivePositions.Count > 0 ) && 
                CrossUnder( bar, D, Signal ) && ( D[bar-1] > 80 ) )
            {
                // Let's work directly with the list of active positions, introduced in WL5
                for( int p = ActivePositions.Count - 1; p > -1 ; p-- )
                    SellAtMarket( bar+1, ActivePositions[p] );
            }
        
            // This system opens a new position whenever Stochastic 
            // crosses above its signal line from below 20. 

            if ( CrossOver( bar, D, Signal ) )
            if ( D[bar-1] < 20 )
                BuyAtMarket( bar+1, "Stoch" );
    }
}
BuyAtMarket

TurnDown

bool TurnDown(int bar, DataSeries series);

Returns true if the specified DataSeries has "turned down" as of the specified bar.  The series has turned down if the value at bar is less than the value at bar - 1, and the next most recent change in value in the series was an increase.


Example

protected override void Execute(){
    // Buy when Williams %R turns down and is above 80
    DataSeries PctR = WilliamsR.Series( Bars, 30 );
    ChartPane PctRPane = CreatePane( 25, true, true );
    PlotSeriesOscillator( PctRPane, PctR, 90, 10, Color.LightCoral, Color.LightBlue, Color.Gray, WealthLab.LineStyle.Solid, 1 );

    // Time-based exit
    int days = 20;
    
    // Start trading loop with the first "valid" value of %R
    for(int bar = PctR.FirstValidValue; bar < Bars.Count; bar++)
    {
        if (IsLastPositionActive)
        {
            if ( bar+1 - LastPosition.EntryBar >= days )
                SellAtClose( bar, LastPosition );
        }
        else
        {
            // Color turndowns
            if ( TurnDown( bar, PctR ) )
            {
                SetSeriesBarColor( bar, PctR, Color.Red );
                if ( PctR[bar] > 80 )
                {
                    BuyAtMarket( bar+1, "WR" );
                }
            }
        }
    }
}
BuyAtMarket

TurnUp

bool TurnUp(int bar, DataSeries series);

Returns true if the specified DataSeries has "turned up" as of the specified bar.  The series has turned up if the value at bar is greater than the value at bar - 1, and the next most recent change in value in the series was a decrease.


Example

protected override void Execute(){
    // Enter the market when the slow stochastic turns up from below 15
    DataSeries stoch = StochD.Series( Bars, 5, 60 );
    ChartPane StochPane = CreatePane( 30, true, true );
    PlotSeries( StochPane, stoch, Color.Blue, WealthLab.LineStyle.Solid, 2 );

    // Start trading loop with the first "valid" value of StochD
    for(int bar = stoch.FirstValidValue; bar < Bars.Count; bar++)
    {
        if (IsLastPositionActive)
        {
            if(  CrossOver( bar, stoch, 80 ) )
            SellAtMarket( bar+1, LastPosition, "StochD Crosses 80" );
        }
        else
        {                    
            if ( stoch[bar-1] < 15 )
            {
            // Color TurnUps
            if ( TurnUp( bar, stoch ) )
                {                    
                SetSeriesBarColor( bar, stoch, Color.Red );
                BuyAtMarket( bar+1, "StochasticD Turns Up" );
                }
            }
        }
    }
}