Traders' Tip text
The moving trend indicator presented by William Rafter in this issue can be implemented in Wealth-Lab through the built-in linear regression indicator. Here, we present a script that displays the 20-day moving trend and a 20-day moving average for comparison.
We've also included a simple trading system. The system looks for cases where prices have moved a considerable distance (at least 7%) below the moving trend value. We go long and bet that prices will correct. The system exits using a limit order, the limit price based on the current value of the moving trend.
You can tune this system by modifying the factor used for entry. By increasing the position sizing rule to 7%, you will wind up with fewer trades, but those trades will have a higher likelihood of being profitable. Decreasing the threshold will result in more trading opportunities. You can also add a trend detection mechanism and reverse the signal to go short in a downward trending market.
WealthScript Code (C#)
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 linreg = LinearReg.Series( Close, period );
SMA MovAvg = SMA.Series( Close, period );
PlotSeries( PricePane, linreg, Color.Navy, WealthLab.LineStyle.Solid, 3 );
PlotSeries( PricePane, MovAvg, Color.Gray, WealthLab.LineStyle.Solid, 1 );
for(int bar = 100; bar < Bars.Count; bar++)
{
for (int pos = ActivePositions.Count - 1; pos >= 0; pos--)
{
Position p = ActivePositions[pos];
if( p.PositionType == PositionType.Long )
{
if( !SellAtStop( bar+1, p, p.EntryPrice * 0.8, "Stop" ) )
SellAtLimit( bar + 1, p, linreg[bar], "Limit" );
}
else
{
if( !CoverAtStop( bar+1, p, p.EntryPrice * 1.2, "Stop" ) )
CoverAtLimit( bar + 1, p, linreg[bar], "Limit" );
}
}
if( Close[bar] > SMA.Series( Close,100)[bar] )
if( linreg[bar] / Close[bar] > 1.04 )
if( BuyAtMarket( bar + 1 ) != null )
LastPosition.Priority = -Close[bar];
if( Close[bar] < SMA.Series( Close,100)[bar] )
if( Close[bar] / linreg[bar] > 1.08 )
if( ShortAtMarket( bar + 1 ) != null )
LastPosition.Priority = Close[bar];
}
}
}
}