<?xml version="1.0"?>
<Strategy xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <Name>COT_Extremes</Name>
  <ID>02340c1f-54d7-44db-9e90-e1bdd0c05710</ID>
  <Code>using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using WealthLab;
using WealthLab.Indicators;

namespace WealthLab.Strategies
{
	public class COT_Extremes : WealthScript
	{
		public enum COTSeries { CommLongAll, CommShortAll, NonCommLongAll, NonCommShortAll, NonReptLongAll, NonReptShortAll };
		private StrategyParameter lookbackPeriod;

		public COT_Extremes()
		{
			lookbackPeriod = CreateParameter("Lookback Period", 100, 50, 200, 25 );      
		}
		
		private string MatchingSymbol(COTSeries series)
		{
			/* Routine for symbol matching */
			string sym = Bars.Symbol;
			string newSym = sym.Substring(0, 2);
			
			switch(series)
			{
				case COTSeries.CommLongAll:
					return newSym + "##Comm_Positions_Long_All";
				case COTSeries.CommShortAll:
					return newSym + "##Comm_Positions_Short_All";
				case COTSeries.NonCommLongAll:
					return newSym + "##NonComm_Positions_Long_All";
				case COTSeries.NonCommShortAll:
					return newSym + "##NonComm_Positions_Short_All";
				case COTSeries.NonReptLongAll:
					return newSym + "##NonRept_Positions_Long_All";
				case COTSeries.NonReptShortAll:
					return newSym + "##NonRept_Positions_Short_All";
			}
			return ""; //impossible???
		}

		protected override void Execute()
		{
			WealthLab.LineStyle solid = WealthLab.LineStyle.Solid;
			WealthLab.LineStyle dashed = WealthLab.LineStyle.Dashed;
			WealthLab.Indicators.EMACalculation modern = WealthLab.Indicators.EMACalculation.Modern;
			int lookback = lookbackPeriod.ValueInt;
		
			if( Bars.Scale != BarScale.Daily ) 
				return;
			
			//All COTCollector data appears as the Close series
			Bars cotCommLong			= GetExternalSymbol( MatchingSymbol(COTSeries.CommLongAll), true );
			Bars cotCommShort			= GetExternalSymbol( MatchingSymbol(COTSeries.CommShortAll), true );
			DataSeries NetCommercials 	= cotCommLong.Close - cotCommShort.Close;
			NetCommercials.Description 	= "Net Commercials";

			Font font = new Font( "Impact", 12, FontStyle.Regular );
			ChartPane cotPane = CreatePane( 30, false, false );
			DrawHorzLine( cotPane, 0, Color.Brown, solid, 2 );
			PlotSeries( cotPane, NetCommercials, Color.Blue, solid, 2 );
			HideVolume(); PlotStops();
				
			DataSeries _high 	= Highest.Series( NetCommercials, lookback );
			DataSeries _low 	= Lowest.Series( NetCommercials, lookback );
			_high.Description = lookback + "-day high"; _low.Description = lookback + "-day low";
			PlotSeriesFillBand( cotPane, _high, _low, Color.FromArgb(0, 128, 128), Color.Empty, dashed, 2 );
			
			DataSeries ema13 = EMA.Series( Close, 13, modern ); ema13.Description = "EMA(13)";
			DataSeries ema39 = EMA.Series( Close, 39, modern );	ema39.Description = "EMA(39)";
			PlotSeries( PricePane, ema13, Color.Red, dashed, 2 );
			PlotSeries( PricePane, ema39, Color.Blue, dashed, 2 );
			
			for(int bar = lookback; bar &lt; Bars.Count; bar++)
			{
				double adx = ADX.Series( Bars, 14 )[bar];

				if( NetCommercials[bar] &gt; 0 )
					SetBackgroundColor( bar, Color.FromArgb(255, 227, 231) ); else 	// Reddish
					SetBackgroundColor( bar, Color.FromArgb(231, 255, 231) );		// Greenish

				if (IsLastPositionActive)
				{
					Position p = LastPosition;
					double stop = ( p.PositionType == PositionType.Long ) ? p.EntryPrice*0.95 : p.EntryPrice*1.05;
					
					if( p.PositionType == PositionType.Long )
					{
						if( NetCommercials[bar] &lt;= _low[bar] )
						{
							if( SellAtMarket( bar+1, p, "Extreme" ) )
								AnnotateBar( "Cover", bar, true, Color.Red, Color.Empty, font ); 
						} else
							if( SellAtStop( bar+1, p, stop, "stop loss" ) )
							AnnotateBar( "Cover", bar, true, Color.Red, Color.Empty, font );
					} else
					{
						if( NetCommercials[bar] &gt;= _high[bar] )
						{
							if( CoverAtMarket( bar+1, p, "Extreme" ) )
								AnnotateBar( "Cover", bar, true, Color.DarkGreen, Color.Empty, font );
						}else
							if( CoverAtStop( bar+1, p, stop, "stop loss" ) )
							AnnotateBar( "Cover", bar, true, Color.DarkGreen, Color.Empty, font );
					}					
				}
				else
				{
					if( NetCommercials[bar] &lt; 0 )
					{
						if( ( CrossOver( bar, ema13, ema39 ) ) &amp; adx &gt;= 15 )
							if( BuyAtMarket( bar+1 ) != null )
								AnnotateBar( "Buy", bar, false, Color.Blue, Color.Empty, font );
					} 
					else if( NetCommercials[bar] &gt; 0 )
					{
						if( ( CrossUnder( bar, ema13, ema39 ) ) &amp; adx &gt;= 15 )
							if( ShortAtMarket( bar+1 ) != null )
								AnnotateBar( "Short", bar, false, Color.Brown, Color.Empty, font );
					}
				}
			}
		}		
	}
}</Code>
  <Description />
  <Author>David Aronow</Author>
  <CreationDate>2008-03-13T13:58:15.0625-04:00</CreationDate>
  <LastModified>2008-03-13T14:32:07.046875-04:00</LastModified>
  <StrategyType>Script</StrategyType>
  <ParameterValues />
  <DataSetName></DataSetName>
  <Symbol></Symbol>
  <DataScale>
    <Scale>Daily</Scale>
    <BarInterval>0</BarInterval>
  </DataScale>
  <PositionSize>
    <Mode>Share</Mode>
    <RawProfitDollarSize>5000</RawProfitDollarSize>
    <RawProfitShareSize>1</RawProfitShareSize>
    <StartingCapital>25000</StartingCapital>
    <DollarSize>7500</DollarSize>
    <ShareSize>1</ShareSize>
    <PctSize>0</PctSize>
    <RiskSize>3</RiskSize>
    <SimuScriptName />
    <MarginFactor>1</MarginFactor>
  </PositionSize>
  <DataRange>
    <Range>RecentYears</Range>
    <StartDate>1995-01-01T00:00:00</StartDate>
    <EndDate>2008-02-14T00:00:00</EndDate>
    <FixedBars>500</FixedBars>
    <RecentValue>13</RecentValue>
    <IsStreaming>false</IsStreaming>
  </DataRange>
  <Indicators>AAAAAAAAAAA=</Indicators>
  <Rules />
  <SinglePosition>true</SinglePosition>
</Strategy>