Wealth-Lab Wiki

API ChartStyles Community Components Community Indicators IndexDefinitions Knowledge Base Misc Optimizers Pending Deletion PosSizers Providers Standard Indicators TASC Traders Tips TASCIndicators Tutorial Videos Visualizers
RSS

Navigation


Quick Search
»
Advanced Search »


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

RSS

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 = ( Date[bar].Month == Date[bar-1].Month ); if( thisMonth ) { if( CrossUnder( bar, Close, sma ) ) { SetBackgroundColor( bar, Color.FromArgb( 60, Color.Orange ) ); crossesThisMonth++; } } else crossesThisMonth = 0; crossNum[bar] = 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( ( crossNum[bar] == 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)) { cross[bar] = 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( Close[bar] < SMA.Series( Close,50 )[bar] ) if( ( xingCount[bar] >= 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.

Used under license from FMR Corp. Copyright 2008 FMR Corp. All rights reserved.


ScrewTurn Wiki. Some of the icons created by FamFamFam.