Description
Exiting after a certain number of profitable closes is a time-based technique designed to take profits when a market is moving in your direction. This long-only system combines simple trend and countertrend entry rules, each of which will be exited after different numbers of profitable closes. A weakness of this technique is that by factoring out price action, it does not adapt to changing market conditions.
Trend entry rule: Buy with a stop order on a breakout of the highest high of the past 20 days.
Countertrend entry rule: Buy with a limit order at the lowest low of the past 10 days.
Trend exit rule: Exit at the market on the next bar’s open when the number of profitable closes (excluding the entry bar) reaches 20.
Countertrend exit rule: Exit at the market on the next bar’s open when the number of profitable closes (excluding the entry bar) reaches eight.
Stop-loss: Protect each trade with a stoploss order 10-percent below the entry price.
Code
using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using WealthLab;
using WealthLab.Indicators;
namespace WealthLab.Strategies
{
public class ProfitableCloses : WealthScript
{
protected override void Execute()
{
int bars, ProfitableClosesTrend, ProfitableClosesCounter;
double stopLevel = 0;
bars = 0;
// Number of profitable closes, trend mode
ProfitableClosesTrend = 20;
// Number of profitable closes, counter-trend mode
ProfitableClosesCounter = 8;
for(int bar = 20; bar < Bars.Count; bar++)
{
if (IsLastPositionActive)
{
stopLevel = ( LastPosition.PositionType == PositionType.Long) ? LastPosition.EntryPrice*0.9 : LastPosition.EntryPrice*1.1;
ExitAtStop( bar+1, LastPosition, stopLevel, "Stop Loss" );
if( Bars.Close[bar] > LastPosition.EntryPrice )
bars++;
if( LastPosition.EntrySignal == "Channel Breakout" )
{
if( bars == ProfitableClosesTrend )
SellAtMarket( bar+1, LastPosition, "Profitable Closes.Trend" );
} else
if( LastPosition.EntrySignal == "Counter Trend" )
{
if( bars == ProfitableClosesCounter )
SellAtMarket( bar+1, LastPosition, "Profitable Closes.CounterTrend" );
}
}
else
{
if( ( BuyAtStop( bar+1, Highest.Series( Bars.High, 20 )[bar], "Channel Breakout" ) != null ) ||
( BuyAtLimit( bar+1, Lowest.Series( Bars.Low, 10 )[bar], "Counter Trend" ) != null ) )
bars = 0;
}
}
}
}
}