TASC 2020-01 | An Interplanetary Marriage (Leavitt)

Modified on 2019/11/29 15:04 by Eugene — Categorized as: TASC Traders Tips

Traders' Tip text

We strived to find clear rules that build a trading system in this month’s article but it doesn’t appear to contain a complete strategy. Here’s our attempt at creating something:

Entry rules


Exit rules



Image

Figure 1. Trading rules applied to a chart of AXP (American Express) Data provided by Yahoo Finance.


Figure 1 illustrates the system’s mechanics on a sample trade in AXP. In July’19 the LeavittConvSlope indicator hits a 50-bar highest high, touching the blue dashed line. Shortly after the LcAcceleration indicator turns down and the system is out.

To get the Strategy's C# code, no need in copy/paste: simply download it right from Wealth-Lab's "Open Strategy" dialog.

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 Leavitt : WealthScript { private StrategyParameter paramLength; private StrategyParameter paramPeak; public Leavitt() { paramLength = CreateParameter("LcP Length", 50, 10, 50, 10); paramPeak = CreateParameter("LcSlope Peak", 50, 5, 50, 5); } protected override void Execute() { bool reachedPeak = false, changedSign = false; int peakBar = -1, signBar = -1; int peakBars = paramPeak.ValueInt;

int Length = paramLength.ValueInt; var LeavittProjection = LinearReg.Series(Close, Length); int LenConv = Convert.ToInt32(Math.Sqrt(Length)); // this is how EasyLanguage extracts the integer portion of a variable var LeavittConv = LinearReg.Series(LeavittProjection, LenConv); //inline function call // The slope follows the same pattern. However, step 2, which defines the line, is replaced by capturing the slope of that line. var LeavittConvSlope = LinearRegSlope.Series(LeavittProjection, LenConv); LeavittConvSlope.Description = "LeavittConvSlope"; // The acceleration is simply the difference between two consecutive values of the slope. var LcAcceleration = LeavittConvSlope - (LeavittConvSlope>>1); LcAcceleration.Description = "LcAcceleration"; var LeavittConvAcc = new DataSeries(Bars,"LeavittConvAcc"); LeavittConvAcc.Description = "LeavittConvAcc"; HideVolume(); ChartPane p3 = CreatePane(10,false,false); ChartPane p2 = CreatePane(30,true,false); ChartPane p1 = CreatePane(30,true,false); PlotSeries(p1, LcAcceleration, Color.Blue, LineStyle.Histogram, 5 ); PlotSeries(p2, LeavittConvSlope, Color.Blue, LineStyle.Histogram, 5 ); PlotSeries(p3, LeavittConvAcc, Color.Blue, LineStyle.Histogram, 5 ); PlotSeries( p2, Highest.Series( LeavittConvSlope, peakBars ), Color.Blue, LineStyle.Dashed, 1 ); for(int bar = 1; bar < Bars.Count; bar++) { SetSeriesBarColor( bar, LcAcceleration, LcAccelerationbar > 0 ? Color.DarkGreen: Color.Red ); SetSeriesBarColor( bar, LeavittConvSlope, LeavittConvSlopebar > LeavittConvSlopebar-1 ? Color.DarkGreen: Color.Red );

if( bar > 2 ) { if (Closebar - 2 - 2*Closebar - 1 + Closebar > 0) LeavittConvAccbar = 1; else LeavittConvAccbar = -1; } } for(int bar = GetTradingLoopStartBar(1); bar < Bars.Count; bar++) { if (IsLastPositionActive) { //This exit was taken on the 30-minute chart precisely when //LcSlope reached its peak and LcAcceleration changed sign. var hb = HighestBar.Series( LeavittConvSlope, peakBars )bar; if( !reachedPeak ) { if( bar == hb ) { SetBackgroundColor( bar, Color.FromArgb(50,Color.LightBlue)); reachedPeak = true; peakBar = bar; } } if( reachedPeak ) { if( !changedSign ) { if( LcAccelerationbar < 0 ) { SetBackgroundColor( bar, Color.FromArgb(50,Color.LightSalmon)); changedSign = true; signBar = bar; } if( changedSign ) { if( SellAtMarket(bar+1, LastPosition ) ) { reachedPeak = false; changedSign = false; } } } } } else { if( LeavittConvSlopebar > 0 && LcAccelerationbar > 0 ) BuyAtMarket(bar+1); } } } } }


Gene Geren (Eugene)
Wealth-Lab team