#region Using declarations using System; using System.Collections.Generic; using System.ComponentModel; using System.ComponentModel.DataAnnotations; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Input; using System.Windows.Media; using System.Xml.Serialization; using NinjaTrader.Cbi; using NinjaTrader.Gui; using NinjaTrader.Gui.Chart; using NinjaTrader.Gui.SuperDom; using NinjaTrader.Gui.Tools; using NinjaTrader.Data; using NinjaTrader.NinjaScript; using NinjaTrader.Core.FloatingPoint; using NinjaTrader.NinjaScript.DrawingTools; #endregion //This namespace holds Indicators in this folder and is required. Do not change it. namespace NinjaTrader.NinjaScript.Indicators { public class BlackflagFTS : Indicator { public enum trailT { modified, unmodified }; private Series h_l; private Series trueRange; private Series wildMa; private Series trendUp; private Series trendDown; private Series trend; private Series atr; protected override void OnStateChange() { if (State == State.SetDefaults) { Description = @"Enter the description for your new custom Indicator here."; Name = "BlackflagFTS"; Calculate = Calculate.OnEachTick; IsOverlay = true; DisplayInDataBox = true; DrawOnPricePanel = true; DrawHorizontalGridLines = true; DrawVerticalGridLines = true; PaintPriceMarkers = true; ScaleJustification = NinjaTrader.Gui.Chart.ScaleJustification.Right; //Disable this property if your indicator requires custom values that cumulate with each new market data event. //See Help Guide for additional information. TrailType=trailT.modified; IsSuspendedWhileInactive = true; ATRPeriod = 28; ATRFactor = 5; Show_fib_entries = true; AddPlot(Brushes.Orange, "Fib1"); AddPlot(Brushes.Orange, "Fib2"); AddPlot(Brushes.Orange, "Fib3"); AddPlot(Brushes.Orange, "Trail"); AddPlot(new Stroke(Brushes.Yellow, DashStyleHelper.Solid,3),PlotStyle.Dot, "Ex"); AddPlot(new Stroke(Brushes.Yellow, DashStyleHelper.Solid,3),PlotStyle.TriangleUp, "Entry1"); AddPlot(new Stroke(Brushes.Yellow, DashStyleHelper.Solid,3),PlotStyle.TriangleUp, "Entry2"); AddPlot(new Stroke(Brushes.Yellow, DashStyleHelper.Solid,3),PlotStyle.TriangleUp, "Entry3"); AddPlot(new Stroke(Brushes.Purple, DashStyleHelper.Solid,3),PlotStyle.TriangleDown, "Entry4"); AddPlot(new Stroke(Brushes.Purple, DashStyleHelper.Solid,3),PlotStyle.TriangleDown, "Entry5"); AddPlot(new Stroke(Brushes.Purple, DashStyleHelper.Solid,3),PlotStyle.TriangleDown, "Entry6"); } else if (State == State.Configure) { } else if (State == State.DataLoaded) { h_l = new Series(this); trueRange = new Series(this); wildMa = new Series(this); trendUp = new Series(this); trendDown = new Series(this); trend = new Series(this); atr = new Series(this); } } protected override void OnBarUpdate() { if(CurrentBar<2) { wildMa[0]=0; trend[0]=0; Ex[0]=0; return; } //Add your custom indicator logic here. h_l[0] = High[0]-Low[0]; double HiLo = Math.Min(High[0] - Low[0], 1.5 * SMA(h_l, ATRPeriod)[0]); double HRef = Low[0]<= High[1] ? High[0] - Close[1] : (High[0] - Close[1]) - 0.5 * (Low[0]- High[1]); double LRef = High[0] >= Low[1] ? Close[1] - Low[0]:(Close[1] - Low[0]) - 0.5 * (Low[1] - High[0]); if(TrailType == trailT.modified) { trueRange[0]=Math.Max(HiLo, Math.Max(HRef, LRef)); } else { trueRange[0]=Math.Max(h_l[0], Math.Max(Math.Abs(High[0] - Close[1]), Math.Abs(Low[0] - Close[1]))); } wildMa[0]=wildMa[1]+(trueRange[0]-wildMa[1])/ATRPeriod; double loss = ATRFactor * wildMa[0]; double up = Close[0] - loss; double dn = Close[0] + loss; trendUp[0] = Close[1] > trendUp[1] ? Math.Max(up, trendUp[1]): up; trendDown[0] = Close[1] < trendDown[1] ? Math.Min(dn, trendDown[1]) : dn; trend[0] = Close[0] > trendDown[1] ? 1 : Close[0] < trendUp[1]? -1 : trend[1]; Trail[0] = trend[0] == 1? trendUp[0] : trendDown[0]; if(trend[0]>0 && trend[1]<=0) { Ex[0]=High[0]; } else if(trend[0]<0 && trend[1]>=0) { Ex[0]=Low[0]; } else if(trend[0]==1) { Ex[0]=Math.Max(Ex[1],High[0]); } else if(trend[0]==-1) { Ex[0]=Math.Min(Ex[1],Low[0]); } else { Ex[0]=Ex[1]; } double fib1Level = 61.8; double fib2Level = 78.6; double fib3Level = 88.6; Fib1[0] = Ex[0] + (Trail[0] - Ex[0]) * fib1Level / 100; Fib2[0] = Ex[0] + (Trail[0] - Ex[0]) * fib2Level / 100; Fib3[0] = Ex[0] + (Trail[0] - Ex[0]) * fib3Level / 100; Entry1.Reset(0); Entry2.Reset(0); Entry3.Reset(0); Entry4.Reset(0); Entry5.Reset(0); Entry6.Reset(0); atr[0]=SMA(trueRange,14)[0]; if(Show_fib_entries) { if(trend[1]==1) { if(Close[0]=Fib1[2]) { Entry1[0]=Low[0]-atr[0]; } if(Close[0]=Fib2[2]) { Entry2[0]=Low[0]-1.5*atr[0]; } if(Close[0]=Fib3[2]) { Entry3[0]=Low[0]-2*atr[0]; } } else { if(Close[0]>Fib1[1] && Close[1]<=Fib1[2]) { Entry4[0]=High[0]+atr[0]; } if(Close[0]>Fib2[1] && Close[1]<=Fib2[2]) { Entry5[0]=High[0]+1.5*atr[0]; } if(Close[0]>Fib3[1] && Close[1]<=Fib3[2]) { Entry6[0]=High[0]+2*atr[0]; } } if(trend[0]==1) { PlotBrushes[0][0] = Brushes.Green; PlotBrushes[1][0] = Brushes.Green; PlotBrushes[2][0] = Brushes.Green; PlotBrushes[3][0] = Brushes.Green; PlotBrushes[4][0] = Brushes.Lime; } else { PlotBrushes[0][0] = Brushes.Red; PlotBrushes[1][0] = Brushes.Red; PlotBrushes[2][0] = Brushes.Red; PlotBrushes[3][0] = Brushes.Red; PlotBrushes[4][0] = Brushes.Fuchsia; } } } #region Properties [NinjaScriptProperty] [Display(Name="TrailType", Order=1, GroupName="Parameters")] public NinjaTrader.NinjaScript.Indicators.BlackflagFTS.trailT TrailType { get; set; } [NinjaScriptProperty] [Display(Name="ATRPeriod", Order=2, GroupName="Parameters")] public int ATRPeriod { get; set; } [NinjaScriptProperty] [Display(Name="ATRFactor", Order=3, GroupName="Parameters")] public double ATRFactor { get; set; } [NinjaScriptProperty] [Display(Name="Show_fib_entries", Order=4, GroupName="Parameters")] public bool Show_fib_entries { get; set; } [Browsable(false)] [XmlIgnore] public Series Fib1 { get { return Values[0]; } } [Browsable(false)] [XmlIgnore] public Series Fib2 { get { return Values[1]; } } [Browsable(false)] [XmlIgnore] public Series Fib3 { get { return Values[2]; } } [Browsable(false)] [XmlIgnore] public Series Trail { get { return Values[3]; } } [Browsable(false)] [XmlIgnore] public Series Ex { get { return Values[4]; } } [Browsable(false)] [XmlIgnore] public Series Entry1 { get { return Values[5]; } } [Browsable(false)] [XmlIgnore] public Series Entry2 { get { return Values[6]; } } [Browsable(false)] [XmlIgnore] public Series Entry3 { get { return Values[7]; } } [Browsable(false)] [XmlIgnore] public Series Entry4 { get { return Values[8]; } } [Browsable(false)] [XmlIgnore] public Series Entry5 { get { return Values[9]; } } [Browsable(false)] [XmlIgnore] public Series Entry6 { get { return Values[10]; } } #endregion } } #region NinjaScript generated code. Neither change nor remove. namespace NinjaTrader.NinjaScript.Indicators { public partial class Indicator : NinjaTrader.Gui.NinjaScript.IndicatorRenderBase { private BlackflagFTS[] cacheBlackflagFTS; public BlackflagFTS BlackflagFTS(NinjaTrader.NinjaScript.Indicators.BlackflagFTS.trailT trailType, int aTRPeriod, double aTRFactor, bool show_fib_entries) { return BlackflagFTS(Input, trailType, aTRPeriod, aTRFactor, show_fib_entries); } public BlackflagFTS BlackflagFTS(ISeries input, NinjaTrader.NinjaScript.Indicators.BlackflagFTS.trailT trailType, int aTRPeriod, double aTRFactor, bool show_fib_entries) { if (cacheBlackflagFTS != null) for (int idx = 0; idx < cacheBlackflagFTS.Length; idx++) if (cacheBlackflagFTS[idx] != null && cacheBlackflagFTS[idx].TrailType == trailType && cacheBlackflagFTS[idx].ATRPeriod == aTRPeriod && cacheBlackflagFTS[idx].ATRFactor == aTRFactor && cacheBlackflagFTS[idx].Show_fib_entries == show_fib_entries && cacheBlackflagFTS[idx].EqualsInput(input)) return cacheBlackflagFTS[idx]; return CacheIndicator(new BlackflagFTS(){ TrailType = trailType, ATRPeriod = aTRPeriod, ATRFactor = aTRFactor, Show_fib_entries = show_fib_entries }, input, ref cacheBlackflagFTS); } } } namespace NinjaTrader.NinjaScript.MarketAnalyzerColumns { public partial class MarketAnalyzerColumn : MarketAnalyzerColumnBase { public Indicators.BlackflagFTS BlackflagFTS(NinjaTrader.NinjaScript.Indicators.BlackflagFTS.trailT trailType, int aTRPeriod, double aTRFactor, bool show_fib_entries) { return indicator.BlackflagFTS(Input, trailType, aTRPeriod, aTRFactor, show_fib_entries); } public Indicators.BlackflagFTS BlackflagFTS(ISeries input , NinjaTrader.NinjaScript.Indicators.BlackflagFTS.trailT trailType, int aTRPeriod, double aTRFactor, bool show_fib_entries) { return indicator.BlackflagFTS(input, trailType, aTRPeriod, aTRFactor, show_fib_entries); } } } namespace NinjaTrader.NinjaScript.Strategies { public partial class Strategy : NinjaTrader.Gui.NinjaScript.StrategyRenderBase { public Indicators.BlackflagFTS BlackflagFTS(NinjaTrader.NinjaScript.Indicators.BlackflagFTS.trailT trailType, int aTRPeriod, double aTRFactor, bool show_fib_entries) { return indicator.BlackflagFTS(Input, trailType, aTRPeriod, aTRFactor, show_fib_entries); } public Indicators.BlackflagFTS BlackflagFTS(ISeries input , NinjaTrader.NinjaScript.Indicators.BlackflagFTS.trailT trailType, int aTRPeriod, double aTRFactor, bool show_fib_entries) { return indicator.BlackflagFTS(input, trailType, aTRPeriod, aTRFactor, show_fib_entries); } } } #endregion