This is the source code for the trading system used in the tutorial "Finding A Profitable System With The Built-In Optimizer".
This code implements the 4 Percent Model, developed by Ned Davis and popularized by Martin Zweig in the book Winning on Wall Street. This is a very simple trend following model. It selects an initial direction when the price raises or falls by a specified percentage. Once an initial direction is set, it stays on the trade until the price retraces from the maximum or minimum by the specified percentage.
//// 4X Lab.NET © Copyright 2005-2006 ASCSE LLC// http://www.4xlab.net// email: 4xlab@4xlab.net//using System;namespace _4XLab.NET { // add bollinger bands and try to express the percentage of change // based on volatility
public override void ReceiveTick(sTick pTick) { Spread = pTick.Ask - pTick.Bid; Tick = pTick; DecisionFunction(); }
public override void ReceiveCandle(sCandle pCandle, int pPeriod, string pCBTitle) { } private void DecisionFunction() { if (FirstTick) { FirstTick = false; iMin = Tick.Bid; iMax = Tick.Bid; } else { if (Framework.Account.InTrade()==1) { if (Tick.Bid > iMax) { iMax = Tick.Bid; } if ((iMax-Tick.Bid) > iPercentage*iMax) { Framework.Account.ExitTrade(); Framework.Account.EnterTrade(-1); iMin = Tick.Bid; iMax = 0; } }
if (Framework.Account.InTrade()==-1) { if (Tick.Bid < iMin) { iMin = Tick.Bid; } if ((Tick.Bid-iMin) > iPercentage*iMin) { Framework.Account.ExitTrade(); Framework.Account.EnterTrade(1); iMax = Tick.Bid; iMin = 0; } }
if (Framework.Account.InTrade()==0) { if ((Tick.Bid-iMin) > iPercentage*iMin) { Framework.Account.EnterTrade(1); iMax = Tick.Bid; iMin = 0; }
double logInTrade = Framework.Account.InTrade(); double logMargin = Framework.Account.GetAccount().C; double MinTh = iMin + iMin*iPercentage; double MaxTh = iMax - iMax*iPercentage; Framework.WriteGraphLine(logInTrade+","+logMargin+","+Tick.Bid+","+iMin+","+MinTh+","+iMax+","+MaxTh); }
public override bool Finished() { bool returnv = true; return returnv; }
public override string Title() { return "% Trend Change"; }
}}