/*------------------------------------------------------------------------------- Trading Strategy Code Population member: 40 Max bars back: 111 Created by: Adaptrade Builder version 1.7.0.0 Created: 5/27/2014 11:49:59 AM Scripting language: NinjaTrader Price file: xxxx Build dates: xxxx Project file: xxxx -------------------------------------------------------------------------------*/ #region Using declarations using System; using System.ComponentModel; using System.Diagnostics; using System.Drawing; using System.Drawing.Drawing2D; using System.Xml.Serialization; using NinjaTrader.Cbi; using NinjaTrader.Data; using NinjaTrader.Indicator; using NinjaTrader.Strategy; #endregion namespace NinjaTrader.Strategy { /// /// Adaptrade Builder strategy Builder_Ninja_Ex. /// [Description("Adaptrade Builder strategy Builder_Ninja_Ex.")] public class Builder_Ninja_Ex : Strategy { #region Variables // Strategy inputs private int nl1 = 88; private int nl2 = 50; private int ns1 = 6; private int ns2 = 15; private int ns3 = 61; private int ns4 = 41; private int ns5 = 52; private int ns6 = 2; private int ns7 = 1; private double xs1 = 0.8869; private int shifts1 = 9; private int shifts2 = 18; private int natrl = 45; private double targfrl = 1.4393; private double atrfrtraill = 2.4060; private double trailpctl = 31.0000; private int nbarens1 = 4; private int natrs = 38; private double entfrs = 1.8395; private double targfrs = 4.8614; private int nbarexs = 41; private double startequity = 100000.00; private double psparam = 5.00; private bool roundps = true; private int roundto = 1; private int minsize = 1; private int sizelimit = 100; // Variables for average true range for entry and exit orders DataSeries ATRL; DataSeries ATRS; // Variables for entry and exit prices double EntPrS = 0; double TargPrL; double TargPrS; double LStop = 0; double NewLStop = 0; bool LTrailOn = false; // Variables for entry and exit conditions DataSeries VarL1; DataSeries VarL2; DataSeries VarL3; DataSeries VarL4; DataSeries VarS1; DataSeries VarS2; DataSeries VarS3; DataSeries VarS4; DataSeries VarS5; DataSeries VarS6; DataSeries VarS7; DataSeries VarS8; DataSeries VarS9; DataSeries VarS10; bool CondS1 = false; bool CondS2 = false; bool CondS4 = false; bool CondS5 = false; bool EntCondL = false; bool EntCondS = false; bool ExCondL = false; bool ExCondS = false; // Variables for position sizing int NShares = 0; double Equity = 0; double TrRisk = 0; double MxLoss = 0; #endregion /// /// Configure the strategy; called once at start of strategy. /// protected override void Initialize() { // Evaluate strategy logic at close of each bar CalculateOnBarClose = true; // Plot indicators used in strategy logic DM(NL1).Plots[0].Pen.Color = Color.Blue; Add(DM(NL1)); DM(NL2).Plots[0].Pen.Color = Color.Red; Add(DM(NL2)); StdDev(Open, NS1).Plots[0].Pen.Color = Color.Orange; Add(StdDev(Open, NS1)); Stochastics(3, 3, NS2).Plots[0].Pen.Color = Color.Green; Add(Stochastics(3, 3, NS2)); StochasticsFast(3, NS3).Plots[0].Pen.Color = Color.Yellow; Add(StochasticsFast(3, NS3)); ATR(NS5).Plots[0].Pen.Color = Color.Gray; Add(ATR(NS5)); Momentum(High, NS6).Plots[0].Pen.Color = Color.Purple; Add(Momentum(High, NS6)); // Define array variables VarL1 = new DataSeries(this); VarL2 = new DataSeries(this); VarL3 = new DataSeries(this); VarL4 = new DataSeries(this); VarS1 = new DataSeries(this); VarS2 = new DataSeries(this); VarS3 = new DataSeries(this); VarS4 = new DataSeries(this); VarS5 = new DataSeries(this); VarS6 = new DataSeries(this); VarS7 = new DataSeries(this); VarS8 = new DataSeries(this); VarS9 = new DataSeries(this); VarS10 = new DataSeries(this); ATRL = new DataSeries(this); ATRS = new DataSeries(this); } /// /// Main strategy logic; called on the close of each bar. /// protected override void OnBarUpdate() { // Average true range ATRL.Set(ATR(NATRL)[0]); ATRS.Set(ATR(NATRS)[0]); // Entry price EntPrS = High[NBarEnS1] - EntFrS * ATRS[0]; // Entry and exit conditions VarL1.Set(ATR(1)[0]); VarL2.Set(VarL1[0] + Low[0]); VarL3.Set(DM(NL1)[0]); VarL4.Set(DM(NL2)[0]); VarS1.Set(StdDev(Open, NS1)[0]); VarS2.Set(ATR(1)[0]); VarS3.Set(VarS2[0] * XS1); VarS4.Set(Stochastics(3, 3, NS2)[0]); VarS5.Set(StochasticsFast(3, NS3).K[0]); VarS6.Set(MAX(VarS5, NS4)[ShiftS1]); VarS7.Set(ATR(NS5)[0]); VarS8.Set(Momentum(High, NS6)[ShiftS2]); VarS9.Set(Math.Abs(VarS8[0])); VarS10.Set((int)(Time[0].DayOfWeek)); CondS1 = CrossAbove(VarS1, VarS3, 1); CondS2 = CrossBelow(VarS4, VarS6, 1); CondS4 = VarS7[0] > VarS9[0]; CondS5 = VarS10[0] == NS7; EntCondL = VarL2[0] > Close[0]; EntCondS = CondS1 || CondS2; ExCondL = CrossAbove(VarL3, VarL4, 1); ExCondS = CondS4 || CondS5; // Position sizing calculations Equity = StartEquity + Performance.AllTrades.TradesPerformance.GrossProfit + Performance.AllTrades.TradesPerformance.GrossLoss; MxLoss = Performance.AllTrades.TradesPerformance.Currency.LargestLoser; TrRisk = 0; NShares = 0; if (Math.Abs(TrRisk) > 0) NShares = (int)((Equity * 0.01 * PSParam)/Math.Abs(TrRisk)); else if (Math.Abs(MxLoss) > 0) NShares = (int)((Equity * 0.01 * PSParam)/Math.Abs(MxLoss)); if (RoundPS && RoundTo > 0) NShares = (int)Math.Floor((double)NShares/RoundTo) * RoundTo; NShares = Math.Max(NShares, MinSize); NShares = Math.Min(NShares, SizeLimit); // Entry orders if (Position.MarketPosition == MarketPosition.Flat && EntCondL) { EnterLong(NShares, "EnMark-L"); } if (Position.MarketPosition == MarketPosition.Flat && EntCondS) { EnterShortStop(NShares, EntPrS, "EnStop-S"); } // Exit orders, long trades if (Position.MarketPosition == MarketPosition.Long) { if (BarsSinceEntry() == 0) { LStop = 0; LTrailOn = false; } if (Close[0] - Position.AvgPrice > ATRFrTrailL * ATRL[0]) LTrailOn = true; if (LTrailOn) { NewLStop = Position.AvgPrice + TrailPctL * (Close[0] - Position.AvgPrice)/100.0; LStop = Math.Max(LStop, NewLStop); } if (ExCondL) ExitLong("ExMark-L", "EnMark-L"); if (LTrailOn) ExitLongStop(LStop, "ExTrail-L", "EnMark-L"); TargPrL = Position.AvgPrice + TargFrL * ATRL[0]; ExitLongLimit(TargPrL, "ExTarg-L", "EnMark-L"); } // Exit orders, short trades if (Position.MarketPosition == MarketPosition.Short) { if (ExCondS) ExitShort("ExMark-S", "EnStop-S"); if (BarsSinceEntry() >= NBarExS) ExitShort("ExNBars-S", "EnStop-S"); TargPrS = Position.AvgPrice - TargFrS * ATRS[0]; ExitShortLimit(TargPrS, "ExTarg-S", "EnStop-S"); } } #region Properties [Description("Indicator look-back length (bars), long trades")] [GridCategory("Parameters")] public int NL1 { get { return nl1; } set { nl1 = value; } } [Description("Indicator look-back length (bars), long trades")] [GridCategory("Parameters")] public int NL2 { get { return nl2; } set { nl2 = value; } } [Description("Indicator look-back length (bars), short trades")] [GridCategory("Parameters")] public int NS1 { get { return ns1; } set { ns1 = value; } } [Description("Indicator look-back length (bars), short trades")] [GridCategory("Parameters")] public int NS2 { get { return ns2; } set { ns2 = value; } } [Description("Indicator look-back length (bars), short trades")] [GridCategory("Parameters")] public int NS3 { get { return ns3; } set { ns3 = value; } } [Description("Indicator look-back length (bars), short trades")] [GridCategory("Parameters")] public int NS4 { get { return ns4; } set { ns4 = value; } } [Description("Indicator look-back length (bars), short trades")] [GridCategory("Parameters")] public int NS5 { get { return ns5; } set { ns5 = value; } } [Description("Indicator look-back length (bars), short trades")] [GridCategory("Parameters")] public int NS6 { get { return ns6; } set { ns6 = value; } } [Description("Day of week (0 to 6 for Sunday to Saturday), short trades")] [GridCategory("Parameters")] public int NS7 { get { return ns7; } set { ns7 = value; } } [Description("Multiple of ATR, short trades")] [GridCategory("Parameters")] public double XS1 { get { return xs1; } set { xs1 = value; } } [Description("Indicator shift value (bars), short trades")] [GridCategory("Parameters")] public int ShiftS1 { get { return shifts1; } set { shifts1 = value; } } [Description("Indicator shift value (bars), short trades")] [GridCategory("Parameters")] public int ShiftS2 { get { return shifts2; } set { shifts2 = value; } } [Description("Indicator look-back length (bars), long trades")] [GridCategory("Parameters")] public int NATRL { get { return natrl; } set { natrl = value; } } [Description("Multiple of ATR, long trades")] [GridCategory("Parameters")] public double TargFrL { get { return targfrl; } set { targfrl = value; } } [Description("Multiple of ATR, long trades")] [GridCategory("Parameters")] public double ATRFrTrailL { get { return atrfrtraill; } set { atrfrtraill = value; } } [Description("Trailing stop percentage, long trades")] [GridCategory("Parameters")] public double TrailPctL { get { return trailpctl; } set { trailpctl = value; } } [Description(", short trades")] [GridCategory("Parameters")] public int NBarEnS1 { get { return nbarens1; } set { nbarens1 = value; } } [Description("Indicator look-back length (bars), short trades")] [GridCategory("Parameters")] public int NATRS { get { return natrs; } set { natrs = value; } } [Description("Multiple of ATR, short trades")] [GridCategory("Parameters")] public double EntFrS { get { return entfrs; } set { entfrs = value; } } [Description("Multiple of ATR, short trades")] [GridCategory("Parameters")] public double TargFrS { get { return targfrs; } set { targfrs = value; } } [Description("Indicator look-back length (bars), short trades")] [GridCategory("Parameters")] public int NBarExS { get { return nbarexs; } set { nbarexs = value; } } [Description("Starting account equity value")] [GridCategory("Parameters")] public double StartEquity { get { return startequity; } set { startequity = value; } } [Description("Position sizing parameter value")] [GridCategory("Parameters")] public double PSParam { get { return psparam; } set { psparam = value; } } [Description("Round-to-nearest (true/false)")] [GridCategory("Parameters")] public bool RoundPS { get { return roundps; } set { roundps = value; } } [Description("Round-to position size value")] [GridCategory("Parameters")] public int RoundTo { get { return roundto; } set { roundto = value; } } [Description("Minimum allowable position size")] [GridCategory("Parameters")] public int MinSize { get { return minsize; } set { minsize = value; } } [Description("Maximum allowable position size")] [GridCategory("Parameters")] public int SizeLimit { get { return sizelimit; } set { sizelimit = value; } } #endregion } }