Log in to see Cloud of Tags

Log in to search

# Indicators | Determine when Indicator crosses over/under for second (third etc.) time a month

## Problem

You're trying to create a trade trigger for a security that crosses under and SMA twice in one month so you short it after the second cross under in one month period.

## Solution

Here's how to count crossunders by calendar month:

```using System; using System.Collections.Generic; using System.Text; using System.Drawing; using WealthLab; using WealthLab.Indicators;namespace WealthLab.Strategies { public class SecondCross : WealthScript { protected override void Execute() { int crossesThisMonth = 0; DataSeries crossNum = new DataSeries( Bars, "Crossunder count" ); SMA sma = SMA.Series( Close,50 ); PlotSeries( PricePane, sma, Color.Blue, LineStyle.Solid, 1 ); for(int bar = sma.FirstValidValue; bar < Bars.Count; bar++) { bool thisMonth = ( Datebar.Month == Datebar-1.Month ); if( thisMonth ) { if( CrossUnder( bar, Close, sma ) ) { SetBackgroundColor( bar, Color.FromArgb( 60, Color.Orange ) ); crossesThisMonth++; } } else crossesThisMonth = 0; crossNumbar = crossesThisMonth; } HideVolume(); ChartPane cPane = CreatePane( 30, true, true ); PlotSeries( cPane, crossNum, Color.Red, LineStyle.Histogram, 3 ); for(int bar = sma.FirstValidValue; bar < Bars.Count; bar++) { if (IsLastPositionActive) { //code your exit rules here CoverAtMarket( bar+1, LastPosition ); } else { if( ( crossNumbar == 2 ) && CrossUnder( bar, Close, sma ) ) ShortAtMarket( bar+1 ); } } } } } ```

And here's how to count over that last rolling month (approx. 21 trading days):

``` using System; using System.Collections.Generic; using System.Text; using System.Drawing; using WealthLab; using WealthLab.Indicators;namespace WealthLab.Strategies { public class MyStrategy : WealthScript { protected override void Execute() { int period = 20; DataSeries sma = SMA.Series(Close, period); DataSeries cross = new DataSeries(Bars, "Crossing=1"); for(int bar = sma.FirstValidValue; bar < Bars.Count; bar++) { if (CrossUnder(bar, Close, sma)) { crossbar = 1d; } } // Now here's the indicator that counts the crossings for the last rolling month (21 days of trading) DataSeries xingCount = Sum.Series(cross, 21); xingCount.Description = "CrossUnder count in last month"; // Plots the count and sma ChartPane cp = CreatePane(40, true, true); PlotSeries(cp, xingCount, Color.Blue, LineStyle.Histogram, 2); PlotSeries(PricePane, sma, Color.Green, LineStyle.Solid, 2); for(int bar = 50; bar < Bars.Count; bar++) { if (IsLastPositionActive) { /* Exit after N days */ Position p = LastPosition; if ( bar+1 - p.EntryBar >= 10 ) CoverAtMarket( bar+1, p, "Timed" ); } else { if( Closebar < SMA.Series( Close,50 )bar ) if( ( xingCountbar >= 2 ) && CrossUnder( bar, Close, sma ) ) ShortAtMarket( bar+1 ); } } } } } ```
 Important Disclaimer: The information provided by Wealth-Lab is strictly for informational purposes and is not to be construed as advice or solicitation to buy or sell any security.  The owner of Wealth-Lab.com assumes no liability resulting from the use of the material contained herein for investment purposes. By using this web site, you agree to the terms of this disclaimer and our Terms of Use.

ScrewTurn Wiki. Some of the icons created by FamFamFam.