├── Indicators ├── @CurrentDayOHL.cs ├── @EMA.cs ├── @MACD.cs ├── @SMA.cs ├── BBI.cs ├── TimeLimitedEMA.cs └── timeLimitedMACD.cs ├── README.md ├── Strategies ├── EMAOpenPrice.cs ├── NineThirty.cs ├── OpenPriceLong.cs ├── ThreeEMA.cs └── TimeTwoMACD.cs └── ninjascripts.zip /Indicators/@CurrentDayOHL.cs: -------------------------------------------------------------------------------- 1 | // 2 | // Copyright (C) 2019, NinjaTrader LLC . 3 | // NinjaTrader reserves the right to modify or overwrite this NinjaScript component with each release. 4 | // 5 | #region Using declarations 6 | using System; 7 | using System.Collections.Generic; 8 | using System.ComponentModel; 9 | using System.ComponentModel.DataAnnotations; 10 | using System.Linq; 11 | using System.Text; 12 | using System.Threading.Tasks; 13 | using System.Windows; 14 | using System.Windows.Input; 15 | using System.Windows.Media; 16 | using System.Xml.Serialization; 17 | using NinjaTrader.Cbi; 18 | using NinjaTrader.Gui; 19 | using NinjaTrader.Gui.Chart; 20 | using NinjaTrader.Gui.SuperDom; 21 | using NinjaTrader.Data; 22 | using NinjaTrader.NinjaScript; 23 | using NinjaTrader.Core.FloatingPoint; 24 | using NinjaTrader.NinjaScript.DrawingTools; 25 | #endregion 26 | 27 | // This namespace holds indicators in this folder and is required. Do not change it. 28 | namespace NinjaTrader.NinjaScript.Indicators 29 | { 30 | /// 31 | /// Plots the open, high, and low values from the session starting on the current day. 32 | /// 33 | public class CurrentDayOHL : Indicator 34 | { 35 | private DateTime currentDate = Core.Globals.MinDate; 36 | private double currentOpen = double.MinValue; 37 | private double currentHigh = double.MinValue; 38 | private double currentLow = double.MaxValue; 39 | private DateTime lastDate = Core.Globals.MinDate; 40 | private Data.SessionIterator sessionIterator; 41 | 42 | protected override void OnStateChange() 43 | { 44 | if (State == State.SetDefaults) 45 | { 46 | Description = NinjaTrader.Custom.Resource.NinjaScriptIndicatorDescriptionCurrentDayOHL; 47 | Name = NinjaTrader.Custom.Resource.NinjaScriptIndicatorNameCurrentDayOHL; 48 | IsAutoScale = false; 49 | DrawOnPricePanel = false; 50 | IsOverlay = true; 51 | IsSuspendedWhileInactive = true; 52 | ShowLow = true; 53 | ShowHigh = true; 54 | ShowOpen = true; 55 | BarsRequiredToPlot = 0; 56 | 57 | AddPlot(new Stroke(Brushes.Goldenrod, DashStyleHelper.Dash, 2), PlotStyle.Square, NinjaTrader.Custom.Resource.CurrentDayOHLOpen); 58 | AddPlot(new Stroke(Brushes.SeaGreen, DashStyleHelper.Dash, 2), PlotStyle.Square, NinjaTrader.Custom.Resource.CurrentDayOHLHigh); 59 | AddPlot(new Stroke(Brushes.Red, DashStyleHelper.Dash, 2), PlotStyle.Square, NinjaTrader.Custom.Resource.CurrentDayOHLLow); 60 | } 61 | else if (State == State.Configure) 62 | { 63 | currentDate = Core.Globals.MinDate; 64 | currentOpen = double.MinValue; 65 | currentHigh = double.MinValue; 66 | currentLow = double.MaxValue; 67 | lastDate = Core.Globals.MinDate; 68 | } 69 | else if (State == State.DataLoaded) 70 | { 71 | sessionIterator = new SessionIterator(Bars); 72 | } 73 | else if (State == State.Historical) 74 | { 75 | if (!Bars.BarsType.IsIntraday) 76 | { 77 | Draw.TextFixed(this, "NinjaScriptInfo", Custom.Resource.CurrentDayOHLError, TextPosition.BottomRight); 78 | Log(Custom.Resource.CurrentDayOHLError, LogLevel.Error); 79 | } 80 | } 81 | } 82 | 83 | protected override void OnBarUpdate() 84 | { 85 | if (!Bars.BarsType.IsIntraday) return; 86 | 87 | lastDate = currentDate; 88 | currentDate = sessionIterator.GetTradingDay(Time[0]); 89 | 90 | if (lastDate != currentDate || currentOpen == double.MinValue) 91 | { 92 | currentOpen = Open[0]; 93 | currentHigh = High[0]; 94 | currentLow = Low[0]; 95 | } 96 | 97 | currentHigh = Math.Max(currentHigh, High[0]); 98 | currentLow = Math.Min(currentLow, Low[0]); 99 | 100 | if (ShowOpen) 101 | CurrentOpen[0] = currentOpen; 102 | 103 | if (ShowHigh) 104 | CurrentHigh[0] = currentHigh; 105 | 106 | if (ShowLow) 107 | CurrentLow[0] = currentLow; 108 | } 109 | 110 | #region Properties 111 | [Browsable(false)] // this line prevents the data series from being displayed in the indicator properties dialog, do not remove 112 | [XmlIgnore()] // this line ensures that the indicator can be saved/recovered as part of a chart template, do not remove 113 | public Series CurrentOpen 114 | { 115 | get { return Values[0]; } 116 | } 117 | 118 | [Browsable(false)] // this line prevents the data series from being displayed in the indicator properties dialog, do not remove 119 | [XmlIgnore()] // this line ensures that the indicator can be saved/recovered as part of a chart template, do not remove 120 | public Series CurrentHigh 121 | { 122 | get { return Values[1]; } 123 | } 124 | 125 | [Browsable(false)] // this line prevents the data series from being displayed in the indicator properties dialog, do not remove 126 | [XmlIgnore()] // this line ensures that the indicator can be saved/recovered as part of a chart template, do not remove 127 | public Series CurrentLow 128 | { 129 | get { return Values[2]; } 130 | } 131 | 132 | [Display(ResourceType = typeof(Custom.Resource), Name = "ShowHigh", GroupName = "NinjaScriptParameters", Order = 1)] 133 | public bool ShowHigh 134 | { get; set; } 135 | 136 | [Display(ResourceType = typeof(Custom.Resource), Name = "ShowLow", GroupName = "NinjaScriptParameters", Order = 2)] 137 | public bool ShowLow 138 | { get; set; } 139 | 140 | [Display(ResourceType = typeof(Custom.Resource), Name = "ShowOpen", GroupName = "NinjaScriptParameters", Order = 3)] 141 | public bool ShowOpen 142 | { get; set; } 143 | #endregion 144 | } 145 | } 146 | 147 | #region NinjaScript generated code. Neither change nor remove. 148 | 149 | namespace NinjaTrader.NinjaScript.Indicators 150 | { 151 | public partial class Indicator : NinjaTrader.Gui.NinjaScript.IndicatorRenderBase 152 | { 153 | private CurrentDayOHL[] cacheCurrentDayOHL; 154 | public CurrentDayOHL CurrentDayOHL() 155 | { 156 | return CurrentDayOHL(Input); 157 | } 158 | 159 | public CurrentDayOHL CurrentDayOHL(ISeries input) 160 | { 161 | if (cacheCurrentDayOHL != null) 162 | for (int idx = 0; idx < cacheCurrentDayOHL.Length; idx++) 163 | if (cacheCurrentDayOHL[idx] != null && cacheCurrentDayOHL[idx].EqualsInput(input)) 164 | return cacheCurrentDayOHL[idx]; 165 | return CacheIndicator(new CurrentDayOHL(), input, ref cacheCurrentDayOHL); 166 | } 167 | } 168 | } 169 | 170 | namespace NinjaTrader.NinjaScript.MarketAnalyzerColumns 171 | { 172 | public partial class MarketAnalyzerColumn : MarketAnalyzerColumnBase 173 | { 174 | public Indicators.CurrentDayOHL CurrentDayOHL() 175 | { 176 | return indicator.CurrentDayOHL(Input); 177 | } 178 | 179 | public Indicators.CurrentDayOHL CurrentDayOHL(ISeries input ) 180 | { 181 | return indicator.CurrentDayOHL(input); 182 | } 183 | } 184 | } 185 | 186 | namespace NinjaTrader.NinjaScript.Strategies 187 | { 188 | public partial class Strategy : NinjaTrader.Gui.NinjaScript.StrategyRenderBase 189 | { 190 | public Indicators.CurrentDayOHL CurrentDayOHL() 191 | { 192 | return indicator.CurrentDayOHL(Input); 193 | } 194 | 195 | public Indicators.CurrentDayOHL CurrentDayOHL(ISeries input ) 196 | { 197 | return indicator.CurrentDayOHL(input); 198 | } 199 | } 200 | } 201 | 202 | #endregion 203 | -------------------------------------------------------------------------------- /Indicators/@EMA.cs: -------------------------------------------------------------------------------- 1 | // 2 | // Copyright (C) 2019, NinjaTrader LLC . 3 | // NinjaTrader reserves the right to modify or overwrite this NinjaScript component with each release. 4 | // 5 | #region Using declarations 6 | using System; 7 | using System.Collections.Generic; 8 | using System.ComponentModel; 9 | using System.ComponentModel.DataAnnotations; 10 | using System.Linq; 11 | using System.Text; 12 | using System.Threading.Tasks; 13 | using System.Windows; 14 | using System.Windows.Input; 15 | using System.Windows.Media; 16 | using System.Xml.Serialization; 17 | using NinjaTrader.Cbi; 18 | using NinjaTrader.Gui; 19 | using NinjaTrader.Gui.Chart; 20 | using NinjaTrader.Gui.SuperDom; 21 | using NinjaTrader.Data; 22 | using NinjaTrader.NinjaScript; 23 | using NinjaTrader.Core.FloatingPoint; 24 | using NinjaTrader.NinjaScript.DrawingTools; 25 | #endregion 26 | 27 | // This namespace holds indicators in this folder and is required. Do not change it. 28 | namespace NinjaTrader.NinjaScript.Indicators 29 | { 30 | /// 31 | /// Exponential Moving Average. The Exponential Moving Average is an indicator that 32 | /// shows the average value of a security's price over a period of time. When calculating 33 | /// a moving average. The EMA applies more weight to recent prices than the SMA. 34 | /// 35 | public class EMA : Indicator 36 | { 37 | private double constant1; 38 | private double constant2; 39 | 40 | protected override void OnStateChange() 41 | { 42 | if (State == State.SetDefaults) 43 | { 44 | Description = NinjaTrader.Custom.Resource.NinjaScriptIndicatorDescriptionEMA; 45 | Name = NinjaTrader.Custom.Resource.NinjaScriptIndicatorNameEMA; 46 | IsOverlay = true; 47 | IsSuspendedWhileInactive = true; 48 | Period = 14; 49 | 50 | AddPlot(Brushes.Goldenrod, NinjaTrader.Custom.Resource.NinjaScriptIndicatorNameEMA); 51 | } 52 | else if (State == State.Configure) 53 | { 54 | constant1 = 2.0 / (1 + Period); 55 | constant2 = 1 - (2.0 / (1 + Period)); 56 | } 57 | } 58 | 59 | protected override void OnBarUpdate() 60 | { 61 | Value[0] = (CurrentBar == 0 ? Input[0] : Input[0] * constant1 + constant2 * Value[1]); 62 | } 63 | 64 | #region Properties 65 | [Range(1, int.MaxValue), NinjaScriptProperty] 66 | [Display(ResourceType = typeof(Custom.Resource), Name = "Period", GroupName = "NinjaScriptParameters", Order = 0)] 67 | public int Period 68 | { get; set; } 69 | #endregion 70 | } 71 | } 72 | 73 | #region NinjaScript generated code. Neither change nor remove. 74 | 75 | namespace NinjaTrader.NinjaScript.Indicators 76 | { 77 | public partial class Indicator : NinjaTrader.Gui.NinjaScript.IndicatorRenderBase 78 | { 79 | private EMA[] cacheEMA; 80 | public EMA EMA(int period) 81 | { 82 | return EMA(Input, period); 83 | } 84 | 85 | public EMA EMA(ISeries input, int period) 86 | { 87 | if (cacheEMA != null) 88 | for (int idx = 0; idx < cacheEMA.Length; idx++) 89 | if (cacheEMA[idx] != null && cacheEMA[idx].Period == period && cacheEMA[idx].EqualsInput(input)) 90 | return cacheEMA[idx]; 91 | return CacheIndicator(new EMA(){ Period = period }, input, ref cacheEMA); 92 | } 93 | } 94 | } 95 | 96 | namespace NinjaTrader.NinjaScript.MarketAnalyzerColumns 97 | { 98 | public partial class MarketAnalyzerColumn : MarketAnalyzerColumnBase 99 | { 100 | public Indicators.EMA EMA(int period) 101 | { 102 | return indicator.EMA(Input, period); 103 | } 104 | 105 | public Indicators.EMA EMA(ISeries input , int period) 106 | { 107 | return indicator.EMA(input, period); 108 | } 109 | } 110 | } 111 | 112 | namespace NinjaTrader.NinjaScript.Strategies 113 | { 114 | public partial class Strategy : NinjaTrader.Gui.NinjaScript.StrategyRenderBase 115 | { 116 | public Indicators.EMA EMA(int period) 117 | { 118 | return indicator.EMA(Input, period); 119 | } 120 | 121 | public Indicators.EMA EMA(ISeries input , int period) 122 | { 123 | return indicator.EMA(input, period); 124 | } 125 | } 126 | } 127 | 128 | #endregion 129 | -------------------------------------------------------------------------------- /Indicators/@MACD.cs: -------------------------------------------------------------------------------- 1 | // 2 | // Copyright (C) 2019, NinjaTrader LLC . 3 | // NinjaTrader reserves the right to modify or overwrite this NinjaScript component with each release. 4 | // 5 | #region Using declarations 6 | using System; 7 | using System.Collections.Generic; 8 | using System.ComponentModel; 9 | using System.ComponentModel.DataAnnotations; 10 | using System.Linq; 11 | using System.Text; 12 | using System.Threading.Tasks; 13 | using System.Windows; 14 | using System.Windows.Input; 15 | using System.Windows.Media; 16 | using System.Xml.Serialization; 17 | using NinjaTrader.Cbi; 18 | using NinjaTrader.Gui; 19 | using NinjaTrader.Gui.Chart; 20 | using NinjaTrader.Gui.SuperDom; 21 | using NinjaTrader.Data; 22 | using NinjaTrader.NinjaScript; 23 | using NinjaTrader.Core.FloatingPoint; 24 | using NinjaTrader.NinjaScript.DrawingTools; 25 | #endregion 26 | 27 | // This namespace holds indicators in this folder and is required. Do not change it. 28 | namespace NinjaTrader.NinjaScript.Indicators 29 | { 30 | /// 31 | /// The MACD (Moving Average Convergence/Divergence) is a trend following momentum indicator 32 | /// that shows the relationship between two moving averages of prices. 33 | /// 34 | public class MACD : Indicator 35 | { 36 | private Series fastEma; 37 | private Series slowEma; 38 | private double constant1; 39 | private double constant2; 40 | private double constant3; 41 | private double constant4; 42 | private double constant5; 43 | private double constant6; 44 | 45 | protected override void OnStateChange() 46 | { 47 | if (State == State.SetDefaults) 48 | { 49 | Description = NinjaTrader.Custom.Resource.NinjaScriptIndicatorDescriptionMACD; 50 | Name = NinjaTrader.Custom.Resource.NinjaScriptIndicatorNameMACD; 51 | Fast = 12; 52 | IsSuspendedWhileInactive = true; 53 | Slow = 26; 54 | Smooth = 9; 55 | 56 | AddPlot(Brushes.DarkCyan, NinjaTrader.Custom.Resource.NinjaScriptIndicatorNameMACD); 57 | AddPlot(Brushes.Crimson, NinjaTrader.Custom.Resource.NinjaScriptIndicatorAvg); 58 | AddPlot(new Stroke(Brushes.DodgerBlue, 2), PlotStyle.Bar, NinjaTrader.Custom.Resource.NinjaScriptIndicatorDiff); 59 | AddLine(Brushes.DarkGray, 0, NinjaTrader.Custom.Resource.NinjaScriptIndicatorZeroLine); 60 | } 61 | else if (State == State.Configure) 62 | { 63 | constant1 = 2.0 / (1 + Fast); 64 | constant2 = (1 - (2.0 / (1 + Fast))); 65 | constant3 = 2.0 / (1 + Slow); 66 | constant4 = (1 - (2.0 / (1 + Slow))); 67 | constant5 = 2.0 / (1 + Smooth); 68 | constant6 = (1 - (2.0 / (1 + Smooth))); 69 | } 70 | else if (State == State.DataLoaded) 71 | { 72 | fastEma = new Series(this); 73 | slowEma = new Series(this); 74 | } 75 | } 76 | 77 | protected override void OnBarUpdate() 78 | { 79 | double input0 = Input[0]; 80 | 81 | if (CurrentBar == 0) 82 | { 83 | fastEma[0] = input0; 84 | slowEma[0] = input0; 85 | Value[0] = 0; 86 | Avg[0] = 0; 87 | Diff[0] = 0; 88 | } 89 | else 90 | { 91 | double fastEma0 = constant1 * input0 + constant2 * fastEma[1]; 92 | double slowEma0 = constant3 * input0 + constant4 * slowEma[1]; 93 | double macd = fastEma0 - slowEma0; 94 | double macdAvg = constant5 * macd + constant6 * Avg[1]; 95 | 96 | fastEma[0] = fastEma0; 97 | slowEma[0] = slowEma0; 98 | Value[0] = macd; 99 | Avg[0] = macdAvg; 100 | Diff[0] = macd - macdAvg; 101 | } 102 | } 103 | 104 | #region Properties 105 | [Browsable(false)] 106 | [XmlIgnore] 107 | public Series Avg 108 | { 109 | get { return Values[1]; } 110 | } 111 | 112 | [Browsable(false)] 113 | [XmlIgnore] 114 | public Series Default 115 | { 116 | get { return Values[0]; } 117 | } 118 | 119 | [Browsable(false)] 120 | [XmlIgnore] 121 | public Series Diff 122 | { 123 | get { return Values[2]; } 124 | } 125 | 126 | [Range(1, int.MaxValue), NinjaScriptProperty] 127 | [Display(ResourceType = typeof(Custom.Resource), Name = "Fast", GroupName = "NinjaScriptParameters", Order = 0)] 128 | public int Fast 129 | { get; set; } 130 | 131 | [Range(1, int.MaxValue), NinjaScriptProperty] 132 | [Display(ResourceType = typeof(Custom.Resource), Name = "Slow", GroupName = "NinjaScriptParameters", Order = 1)] 133 | public int Slow 134 | { get; set; } 135 | 136 | [Range(1, int.MaxValue), NinjaScriptProperty] 137 | [Display(ResourceType = typeof(Custom.Resource), Name = "Smooth", GroupName = "NinjaScriptParameters", Order = 2)] 138 | public int Smooth 139 | { get; set; } 140 | #endregion 141 | } 142 | } 143 | 144 | #region NinjaScript generated code. Neither change nor remove. 145 | 146 | namespace NinjaTrader.NinjaScript.Indicators 147 | { 148 | public partial class Indicator : NinjaTrader.Gui.NinjaScript.IndicatorRenderBase 149 | { 150 | private MACD[] cacheMACD; 151 | public MACD MACD(int fast, int slow, int smooth) 152 | { 153 | return MACD(Input, fast, slow, smooth); 154 | } 155 | 156 | public MACD MACD(ISeries input, int fast, int slow, int smooth) 157 | { 158 | if (cacheMACD != null) 159 | for (int idx = 0; idx < cacheMACD.Length; idx++) 160 | if (cacheMACD[idx] != null && cacheMACD[idx].Fast == fast && cacheMACD[idx].Slow == slow && cacheMACD[idx].Smooth == smooth && cacheMACD[idx].EqualsInput(input)) 161 | return cacheMACD[idx]; 162 | return CacheIndicator(new MACD(){ Fast = fast, Slow = slow, Smooth = smooth }, input, ref cacheMACD); 163 | } 164 | } 165 | } 166 | 167 | namespace NinjaTrader.NinjaScript.MarketAnalyzerColumns 168 | { 169 | public partial class MarketAnalyzerColumn : MarketAnalyzerColumnBase 170 | { 171 | public Indicators.MACD MACD(int fast, int slow, int smooth) 172 | { 173 | return indicator.MACD(Input, fast, slow, smooth); 174 | } 175 | 176 | public Indicators.MACD MACD(ISeries input , int fast, int slow, int smooth) 177 | { 178 | return indicator.MACD(input, fast, slow, smooth); 179 | } 180 | } 181 | } 182 | 183 | namespace NinjaTrader.NinjaScript.Strategies 184 | { 185 | public partial class Strategy : NinjaTrader.Gui.NinjaScript.StrategyRenderBase 186 | { 187 | public Indicators.MACD MACD(int fast, int slow, int smooth) 188 | { 189 | return indicator.MACD(Input, fast, slow, smooth); 190 | } 191 | 192 | public Indicators.MACD MACD(ISeries input , int fast, int slow, int smooth) 193 | { 194 | return indicator.MACD(input, fast, slow, smooth); 195 | } 196 | } 197 | } 198 | 199 | #endregion 200 | -------------------------------------------------------------------------------- /Indicators/@SMA.cs: -------------------------------------------------------------------------------- 1 | // 2 | // Copyright (C) 2019, NinjaTrader LLC . 3 | // NinjaTrader reserves the right to modify or overwrite this NinjaScript component with each release. 4 | // 5 | #region Using declarations 6 | using System; 7 | using System.Collections.Generic; 8 | using System.ComponentModel; 9 | using System.ComponentModel.DataAnnotations; 10 | using System.Linq; 11 | using System.Text; 12 | using System.Threading.Tasks; 13 | using System.Windows; 14 | using System.Windows.Input; 15 | using System.Windows.Media; 16 | using System.Xml.Serialization; 17 | using NinjaTrader.Cbi; 18 | using NinjaTrader.Gui; 19 | using NinjaTrader.Gui.Chart; 20 | using NinjaTrader.Gui.SuperDom; 21 | using NinjaTrader.Data; 22 | using NinjaTrader.NinjaScript; 23 | using NinjaTrader.Core.FloatingPoint; 24 | using NinjaTrader.NinjaScript.DrawingTools; 25 | #endregion 26 | 27 | // This namespace holds indicators in this folder and is required. Do not change it. 28 | namespace NinjaTrader.NinjaScript.Indicators 29 | { 30 | /// 31 | /// The SMA (Simple Moving Average) is an indicator that shows the average value of a security's price over a period of time. 32 | /// 33 | public class SMA : Indicator 34 | { 35 | private double priorSum; 36 | private double sum; 37 | 38 | protected override void OnStateChange() 39 | { 40 | if (State == State.SetDefaults) 41 | { 42 | Description = NinjaTrader.Custom.Resource.NinjaScriptIndicatorDescriptionSMA; 43 | Name = NinjaTrader.Custom.Resource.NinjaScriptIndicatorNameSMA; 44 | IsOverlay = true; 45 | IsSuspendedWhileInactive = true; 46 | Period = 14; 47 | 48 | AddPlot(Brushes.Goldenrod, NinjaTrader.Custom.Resource.NinjaScriptIndicatorNameSMA); 49 | } 50 | else if (State == State.Configure) 51 | { 52 | priorSum = 0; 53 | sum = 0; 54 | } 55 | } 56 | 57 | protected override void OnBarUpdate() 58 | { 59 | if (BarsArray[0].BarsType.IsRemoveLastBarSupported) 60 | { 61 | if (CurrentBar == 0) 62 | Value[0] = Input[0]; 63 | else 64 | { 65 | double last = Value[1] * Math.Min(CurrentBar, Period); 66 | 67 | if (CurrentBar >= Period) 68 | Value[0] = (last + Input[0] - Input[Period]) / Math.Min(CurrentBar, Period); 69 | else 70 | Value[0] = ((last + Input[0]) / (Math.Min(CurrentBar, Period) + 1)); 71 | } 72 | } 73 | else 74 | { 75 | if (IsFirstTickOfBar) 76 | priorSum = sum; 77 | 78 | sum = priorSum + Input[0] - (CurrentBar >= Period ? Input[Period] : 0); 79 | Value[0] = sum / (CurrentBar < Period ? CurrentBar + 1 : Period); 80 | } 81 | } 82 | 83 | #region Properties 84 | [Range(1, int.MaxValue), NinjaScriptProperty] 85 | [Display(ResourceType = typeof(Custom.Resource), Name = "Period", GroupName = "NinjaScriptParameters", Order = 0)] 86 | public int Period 87 | { get; set; } 88 | #endregion 89 | } 90 | } 91 | 92 | #region NinjaScript generated code. Neither change nor remove. 93 | 94 | namespace NinjaTrader.NinjaScript.Indicators 95 | { 96 | public partial class Indicator : NinjaTrader.Gui.NinjaScript.IndicatorRenderBase 97 | { 98 | private SMA[] cacheSMA; 99 | public SMA SMA(int period) 100 | { 101 | return SMA(Input, period); 102 | } 103 | 104 | public SMA SMA(ISeries input, int period) 105 | { 106 | if (cacheSMA != null) 107 | for (int idx = 0; idx < cacheSMA.Length; idx++) 108 | if (cacheSMA[idx] != null && cacheSMA[idx].Period == period && cacheSMA[idx].EqualsInput(input)) 109 | return cacheSMA[idx]; 110 | return CacheIndicator(new SMA(){ Period = period }, input, ref cacheSMA); 111 | } 112 | } 113 | } 114 | 115 | namespace NinjaTrader.NinjaScript.MarketAnalyzerColumns 116 | { 117 | public partial class MarketAnalyzerColumn : MarketAnalyzerColumnBase 118 | { 119 | public Indicators.SMA SMA(int period) 120 | { 121 | return indicator.SMA(Input, period); 122 | } 123 | 124 | public Indicators.SMA SMA(ISeries input , int period) 125 | { 126 | return indicator.SMA(input, period); 127 | } 128 | } 129 | } 130 | 131 | namespace NinjaTrader.NinjaScript.Strategies 132 | { 133 | public partial class Strategy : NinjaTrader.Gui.NinjaScript.StrategyRenderBase 134 | { 135 | public Indicators.SMA SMA(int period) 136 | { 137 | return indicator.SMA(Input, period); 138 | } 139 | 140 | public Indicators.SMA SMA(ISeries input , int period) 141 | { 142 | return indicator.SMA(input, period); 143 | } 144 | } 145 | } 146 | 147 | #endregion 148 | -------------------------------------------------------------------------------- /Indicators/BBI.cs: -------------------------------------------------------------------------------- 1 | #region Using declarations 2 | using System; 3 | using System.Collections.Generic; 4 | using System.ComponentModel; 5 | using System.ComponentModel.DataAnnotations; 6 | using System.Linq; 7 | using System.Text; 8 | using System.Threading.Tasks; 9 | using System.Windows; 10 | using System.Windows.Input; 11 | using System.Windows.Media; 12 | using System.Xml.Serialization; 13 | using NinjaTrader.Cbi; 14 | using NinjaTrader.Gui; 15 | using NinjaTrader.Gui.Chart; 16 | using NinjaTrader.Gui.SuperDom; 17 | using NinjaTrader.Gui.Tools; 18 | using NinjaTrader.Data; 19 | using NinjaTrader.NinjaScript; 20 | using NinjaTrader.Core.FloatingPoint; 21 | using NinjaTrader.NinjaScript.DrawingTools; 22 | #endregion 23 | 24 | //This namespace holds Indicators in this folder and is required. Do not change it. 25 | namespace NinjaTrader.NinjaScript.Indicators 26 | { 27 | public class BBI : Indicator 28 | { 29 | private SMA sma1; 30 | private SMA sma2; 31 | private SMA sma3; 32 | private SMA sma4; 33 | 34 | protected override void OnStateChange() 35 | { 36 | if (State == State.SetDefaults) 37 | { 38 | Description = @"4 SMA / 4"; 39 | Name = "BBI"; 40 | Calculate = Calculate.OnEachTick; 41 | IsOverlay = true; 42 | DisplayInDataBox = true; 43 | DrawOnPricePanel = true; 44 | DrawHorizontalGridLines = true; 45 | DrawVerticalGridLines = true; 46 | PaintPriceMarkers = true; 47 | ScaleJustification = NinjaTrader.Gui.Chart.ScaleJustification.Right; 48 | //Disable this property if your indicator requires custom values that cumulate with each new market data event. 49 | //See Help Guide for additional information. 50 | IsSuspendedWhileInactive = true; 51 | MA1 = 3; 52 | MA2 = 6; 53 | MA3 = 12; 54 | MA4 = 24; 55 | AddPlot(Brushes.DeepSkyBlue, "BullBear"); 56 | } 57 | else if (State == State.Configure) 58 | { 59 | } 60 | 61 | else if (State == State.DataLoaded) 62 | { 63 | // initialize the SMA using the primary series and assign to sma1 64 | sma1 = SMA(Close, Convert.ToInt32(MA1)); 65 | 66 | sma2 = SMA(Close, Convert.ToInt32(MA2)); 67 | 68 | sma3 = SMA(Close, Convert.ToInt32(MA3)); 69 | 70 | sma4 = SMA(Close, Convert.ToInt32(MA4)); 71 | } 72 | } 73 | 74 | protected override void OnBarUpdate() 75 | { 76 | //Add your custom indicator logic here. 77 | BullBear[0] = (sma1[0] + sma2[0] + sma3[0] + sma4[0]) / 4; 78 | } 79 | 80 | #region Properties 81 | [NinjaScriptProperty] 82 | [Range(1, int.MaxValue)] 83 | [Display(Name="MA1", Order=1, GroupName="Parameters")] 84 | public int MA1 85 | { get; set; } 86 | 87 | [NinjaScriptProperty] 88 | [Range(1, int.MaxValue)] 89 | [Display(Name="MA2", Order=2, GroupName="Parameters")] 90 | public int MA2 91 | { get; set; } 92 | 93 | [NinjaScriptProperty] 94 | [Range(1, int.MaxValue)] 95 | [Display(Name="MA3", Order=3, GroupName="Parameters")] 96 | public int MA3 97 | { get; set; } 98 | 99 | [NinjaScriptProperty] 100 | [Range(1, int.MaxValue)] 101 | [Display(Name="MA4", Order=4, GroupName="Parameters")] 102 | public int MA4 103 | { get; set; } 104 | 105 | [Browsable(false)] 106 | [XmlIgnore] 107 | public Series BullBear 108 | { 109 | get { return Values[0]; } 110 | } 111 | #endregion 112 | 113 | } 114 | } 115 | 116 | #region NinjaScript generated code. Neither change nor remove. 117 | 118 | namespace NinjaTrader.NinjaScript.Indicators 119 | { 120 | public partial class Indicator : NinjaTrader.Gui.NinjaScript.IndicatorRenderBase 121 | { 122 | private BBI[] cacheBBI; 123 | public BBI BBI(int mA1, int mA2, int mA3, int mA4) 124 | { 125 | return BBI(Input, mA1, mA2, mA3, mA4); 126 | } 127 | 128 | public BBI BBI(ISeries input, int mA1, int mA2, int mA3, int mA4) 129 | { 130 | if (cacheBBI != null) 131 | for (int idx = 0; idx < cacheBBI.Length; idx++) 132 | if (cacheBBI[idx] != null && cacheBBI[idx].MA1 == mA1 && cacheBBI[idx].MA2 == mA2 && cacheBBI[idx].MA3 == mA3 && cacheBBI[idx].MA4 == mA4 && cacheBBI[idx].EqualsInput(input)) 133 | return cacheBBI[idx]; 134 | return CacheIndicator(new BBI(){ MA1 = mA1, MA2 = mA2, MA3 = mA3, MA4 = mA4 }, input, ref cacheBBI); 135 | } 136 | } 137 | } 138 | 139 | namespace NinjaTrader.NinjaScript.MarketAnalyzerColumns 140 | { 141 | public partial class MarketAnalyzerColumn : MarketAnalyzerColumnBase 142 | { 143 | public Indicators.BBI BBI(int mA1, int mA2, int mA3, int mA4) 144 | { 145 | return indicator.BBI(Input, mA1, mA2, mA3, mA4); 146 | } 147 | 148 | public Indicators.BBI BBI(ISeries input , int mA1, int mA2, int mA3, int mA4) 149 | { 150 | return indicator.BBI(input, mA1, mA2, mA3, mA4); 151 | } 152 | } 153 | } 154 | 155 | namespace NinjaTrader.NinjaScript.Strategies 156 | { 157 | public partial class Strategy : NinjaTrader.Gui.NinjaScript.StrategyRenderBase 158 | { 159 | public Indicators.BBI BBI(int mA1, int mA2, int mA3, int mA4) 160 | { 161 | return indicator.BBI(Input, mA1, mA2, mA3, mA4); 162 | } 163 | 164 | public Indicators.BBI BBI(ISeries input , int mA1, int mA2, int mA3, int mA4) 165 | { 166 | return indicator.BBI(input, mA1, mA2, mA3, mA4); 167 | } 168 | } 169 | } 170 | 171 | #endregion 172 | -------------------------------------------------------------------------------- /Indicators/TimeLimitedEMA.cs: -------------------------------------------------------------------------------- 1 | // 2 | // Copyright (C) 2019, NinjaTrader LLC . 3 | // NinjaTrader reserves the right to modify or overwrite this NinjaScript component with each release. 4 | // 5 | #region Using declarations 6 | using System; 7 | using System.Collections.Generic; 8 | using System.ComponentModel; 9 | using System.ComponentModel.DataAnnotations; 10 | using System.Linq; 11 | using System.Text; 12 | using System.Threading.Tasks; 13 | using System.Windows; 14 | using System.Windows.Input; 15 | using System.Windows.Media; 16 | using System.Xml.Serialization; 17 | using NinjaTrader.Cbi; 18 | using NinjaTrader.Gui; 19 | using NinjaTrader.Gui.Chart; 20 | using NinjaTrader.Gui.SuperDom; 21 | using NinjaTrader.Data; 22 | using NinjaTrader.NinjaScript; 23 | using NinjaTrader.Core.FloatingPoint; 24 | using NinjaTrader.NinjaScript.DrawingTools; 25 | #endregion 26 | 27 | // This namespace holds indicators in this folder and is required. Do not change it. 28 | namespace NinjaTrader.NinjaScript.Indicators 29 | { 30 | /// 31 | /// Exponential Moving Average. The Exponential Moving Average is an indicator that 32 | /// shows the average value of a security's price over a period of time. When calculating 33 | /// a moving average. The EMA applies more weight to recent prices than the SMA. 34 | /// 35 | public class TimeLimitedEMA : Indicator 36 | { 37 | private double constant1; 38 | private double constant2; 39 | 40 | protected override void OnStateChange() 41 | { 42 | if (State == State.SetDefaults) 43 | { 44 | Description = NinjaTrader.Custom.Resource.NinjaScriptIndicatorDescriptionEMA; 45 | Name = "TimeLimitedEMA"; 46 | IsOverlay = true; 47 | IsSuspendedWhileInactive = true; 48 | Period = 14; 49 | StartTime = "09:30"; 50 | EndTime = "10:00"; 51 | 52 | AddPlot(Brushes.Goldenrod, NinjaTrader.Custom.Resource.NinjaScriptIndicatorNameEMA); 53 | } 54 | else if (State == State.Configure) 55 | { 56 | constant1 = 2.0 / (1 + Period); 57 | constant2 = 1 - (2.0 / (1 + Period)); 58 | } 59 | } 60 | 61 | protected override void OnBarUpdate() 62 | { 63 | DateTime StartSession = DateTime.Parse(StartTime, System.Globalization.CultureInfo.InvariantCulture); 64 | DateTime EndSession = DateTime.Parse(EndTime, System.Globalization.CultureInfo.InvariantCulture); 65 | if ( Times[0][0].TimeOfDay < StartSession.TimeOfDay || Times[0][0].TimeOfDay > EndSession.TimeOfDay) 66 | { 67 | return; 68 | } 69 | Value[0] = (CurrentBar == 0 ? Input[0] : Input[0] * constant1 + constant2 * Value[1]); 70 | } 71 | 72 | #region Properties 73 | [Range(1, int.MaxValue), NinjaScriptProperty] 74 | [Display(ResourceType = typeof(Custom.Resource), Name = "Period", GroupName = "NinjaScriptParameters", Order = 0)] 75 | public int Period 76 | { get; set; } 77 | 78 | 79 | [NinjaScriptProperty] 80 | [Display(ResourceType = typeof(Custom.Resource), Name = "StartTime", GroupName = "NinjaScriptParameters", Order = 1)] 81 | public string StartTime 82 | { get; set; } 83 | 84 | [NinjaScriptProperty] 85 | [Display(ResourceType = typeof(Custom.Resource), Name = "EndTime", GroupName = "NinjaScriptParameters", Order = 2)] 86 | public string EndTime 87 | { get; set; } 88 | #endregion 89 | } 90 | } 91 | 92 | #region NinjaScript generated code. Neither change nor remove. 93 | 94 | namespace NinjaTrader.NinjaScript.Indicators 95 | { 96 | public partial class Indicator : NinjaTrader.Gui.NinjaScript.IndicatorRenderBase 97 | { 98 | private TimeLimitedEMA[] cacheTimeLimitedEMA; 99 | public TimeLimitedEMA TimeLimitedEMA(int period, string startTime, string endTime) 100 | { 101 | return TimeLimitedEMA(Input, period, startTime, endTime); 102 | } 103 | 104 | public TimeLimitedEMA TimeLimitedEMA(ISeries input, int period, string startTime, string endTime) 105 | { 106 | if (cacheTimeLimitedEMA != null) 107 | for (int idx = 0; idx < cacheTimeLimitedEMA.Length; idx++) 108 | if (cacheTimeLimitedEMA[idx] != null && cacheTimeLimitedEMA[idx].Period == period && cacheTimeLimitedEMA[idx].StartTime == startTime && cacheTimeLimitedEMA[idx].EndTime == endTime && cacheTimeLimitedEMA[idx].EqualsInput(input)) 109 | return cacheTimeLimitedEMA[idx]; 110 | return CacheIndicator(new TimeLimitedEMA(){ Period = period, StartTime = startTime, EndTime = endTime }, input, ref cacheTimeLimitedEMA); 111 | } 112 | } 113 | } 114 | 115 | namespace NinjaTrader.NinjaScript.MarketAnalyzerColumns 116 | { 117 | public partial class MarketAnalyzerColumn : MarketAnalyzerColumnBase 118 | { 119 | public Indicators.TimeLimitedEMA TimeLimitedEMA(int period, string startTime, string endTime) 120 | { 121 | return indicator.TimeLimitedEMA(Input, period, startTime, endTime); 122 | } 123 | 124 | public Indicators.TimeLimitedEMA TimeLimitedEMA(ISeries input , int period, string startTime, string endTime) 125 | { 126 | return indicator.TimeLimitedEMA(input, period, startTime, endTime); 127 | } 128 | } 129 | } 130 | 131 | namespace NinjaTrader.NinjaScript.Strategies 132 | { 133 | public partial class Strategy : NinjaTrader.Gui.NinjaScript.StrategyRenderBase 134 | { 135 | public Indicators.TimeLimitedEMA TimeLimitedEMA(int period, string startTime, string endTime) 136 | { 137 | return indicator.TimeLimitedEMA(Input, period, startTime, endTime); 138 | } 139 | 140 | public Indicators.TimeLimitedEMA TimeLimitedEMA(ISeries input , int period, string startTime, string endTime) 141 | { 142 | return indicator.TimeLimitedEMA(input, period, startTime, endTime); 143 | } 144 | } 145 | } 146 | 147 | #endregion 148 | -------------------------------------------------------------------------------- /Indicators/timeLimitedMACD.cs: -------------------------------------------------------------------------------- 1 | // 2 | // Copyright (C) 2019, NinjaTrader LLC . 3 | // NinjaTrader reserves the right to modify or overwrite this NinjaScript component with each release. 4 | // 5 | #region Using declarations 6 | using System; 7 | using System.Collections.Generic; 8 | using System.ComponentModel; 9 | using System.ComponentModel.DataAnnotations; 10 | using System.Linq; 11 | using System.Text; 12 | using System.Threading.Tasks; 13 | using System.Windows; 14 | using System.Windows.Input; 15 | using System.Windows.Media; 16 | using System.Xml.Serialization; 17 | using NinjaTrader.Cbi; 18 | using NinjaTrader.Gui; 19 | using NinjaTrader.Gui.Chart; 20 | using NinjaTrader.Gui.SuperDom; 21 | using NinjaTrader.Data; 22 | using NinjaTrader.NinjaScript; 23 | using NinjaTrader.Core.FloatingPoint; 24 | using NinjaTrader.NinjaScript.DrawingTools; 25 | #endregion 26 | 27 | // This namespace holds indicators in this folder and is required. Do not change it. 28 | namespace NinjaTrader.NinjaScript.Indicators 29 | { 30 | /// 31 | /// The TimeLimitedMACD (Moving Average Convergence/Divergence) is a trend following momentum indicator 32 | /// that shows the relationship between two moving averages of prices. 33 | /// 34 | public class TimeLimitedMACD : Indicator 35 | { 36 | private Series fastEma; 37 | private Series slowEma; 38 | private double constant1; 39 | private double constant2; 40 | private double constant3; 41 | private double constant4; 42 | private double constant5; 43 | private double constant6; 44 | 45 | protected override void OnStateChange() 46 | { 47 | if (State == State.SetDefaults) 48 | { 49 | Description = NinjaTrader.Custom.Resource.NinjaScriptIndicatorDescriptionMACD; 50 | Name = "TimeLimitedMACD"; 51 | Fast = 12; 52 | IsSuspendedWhileInactive = true; 53 | Slow = 26; 54 | Smooth = 9; 55 | StartTime = "09:30"; 56 | EndTime = "10:00"; 57 | 58 | 59 | 60 | AddPlot(Brushes.DarkCyan, NinjaTrader.Custom.Resource.NinjaScriptIndicatorNameMACD); 61 | AddPlot(Brushes.Crimson, NinjaTrader.Custom.Resource.NinjaScriptIndicatorAvg); 62 | AddPlot(new Stroke(Brushes.DodgerBlue, 2), PlotStyle.Bar, NinjaTrader.Custom.Resource.NinjaScriptIndicatorDiff); 63 | AddLine(Brushes.DarkGray, 0, NinjaTrader.Custom.Resource.NinjaScriptIndicatorZeroLine); 64 | 65 | 66 | 67 | } 68 | else if (State == State.Configure) 69 | { 70 | constant1 = 2.0 / (1 + Fast); 71 | constant2 = (1 - (2.0 / (1 + Fast))); 72 | constant3 = 2.0 / (1 + Slow); 73 | constant4 = (1 - (2.0 / (1 + Slow))); 74 | constant5 = 2.0 / (1 + Smooth); 75 | constant6 = (1 - (2.0 / (1 + Smooth))); 76 | 77 | 78 | } 79 | else if (State == State.DataLoaded) 80 | { 81 | fastEma = new Series(this); 82 | slowEma = new Series(this); 83 | } 84 | 85 | 86 | } 87 | 88 | protected override void OnBarUpdate() 89 | { 90 | double input0 = Input[0]; 91 | 92 | DateTime StartSession = DateTime.Parse(StartTime, System.Globalization.CultureInfo.InvariantCulture); 93 | DateTime EndSession = DateTime.Parse(EndTime, System.Globalization.CultureInfo.InvariantCulture); 94 | 95 | if ( Times[0][0].TimeOfDay < StartSession.TimeOfDay || Times[0][0].TimeOfDay > EndSession.TimeOfDay) 96 | { 97 | 98 | return; 99 | } 100 | 101 | if (CurrentBar == 0) 102 | { 103 | fastEma[0] = input0; 104 | slowEma[0] = input0; 105 | Value[0] = 0; 106 | Avg[0] = 0; 107 | Diff[0] = 0; 108 | } 109 | else 110 | { 111 | double fastEma0 = constant1 * input0 + constant2 * fastEma[1]; 112 | double slowEma0 = constant3 * input0 + constant4 * slowEma[1]; 113 | double macd = fastEma0 - slowEma0; 114 | double macdAvg = constant5 * macd + constant6 * Avg[1]; 115 | 116 | fastEma[0] = fastEma0; 117 | slowEma[0] = slowEma0; 118 | Value[0] = macd; 119 | Avg[0] = macdAvg; 120 | Diff[0] = macd - macdAvg; 121 | } 122 | } 123 | 124 | #region Properties 125 | [Browsable(false)] 126 | [XmlIgnore] 127 | public Series Avg 128 | { 129 | get { return Values[1]; } 130 | } 131 | 132 | [Browsable(false)] 133 | [XmlIgnore] 134 | public Series Default 135 | { 136 | get { return Values[0]; } 137 | } 138 | 139 | [Browsable(false)] 140 | [XmlIgnore] 141 | public Series Diff 142 | { 143 | get { return Values[2]; } 144 | } 145 | 146 | [Range(1, int.MaxValue), NinjaScriptProperty] 147 | [Display(ResourceType = typeof(Custom.Resource), Name = "Fast", GroupName = "NinjaScriptParameters", Order = 0)] 148 | public int Fast 149 | { get; set; } 150 | 151 | [Range(1, int.MaxValue), NinjaScriptProperty] 152 | [Display(ResourceType = typeof(Custom.Resource), Name = "Slow", GroupName = "NinjaScriptParameters", Order = 1)] 153 | public int Slow 154 | { get; set; } 155 | 156 | [Range(1, int.MaxValue), NinjaScriptProperty] 157 | [Display(ResourceType = typeof(Custom.Resource), Name = "Smooth", GroupName = "NinjaScriptParameters", Order = 2)] 158 | public int Smooth 159 | { get; set; } 160 | 161 | [NinjaScriptProperty] 162 | [Display(ResourceType = typeof(Custom.Resource), Name = "StartTime", GroupName = "NinjaScriptParameters", Order = 3)] 163 | public string StartTime 164 | { get; set; } 165 | 166 | [NinjaScriptProperty] 167 | [Display(ResourceType = typeof(Custom.Resource), Name = "EndTime", GroupName = "NinjaScriptParameters", Order = 4)] 168 | public string EndTime 169 | { get; set; } 170 | 171 | 172 | #endregion 173 | } 174 | } 175 | 176 | #region NinjaScript generated code. Neither change nor remove. 177 | 178 | namespace NinjaTrader.NinjaScript.Indicators 179 | { 180 | public partial class Indicator : NinjaTrader.Gui.NinjaScript.IndicatorRenderBase 181 | { 182 | private TimeLimitedMACD[] cacheTimeLimitedMACD; 183 | public TimeLimitedMACD TimeLimitedMACD(int fast, int slow, int smooth, string startTime, string endTime) 184 | { 185 | return TimeLimitedMACD(Input, fast, slow, smooth, startTime, endTime); 186 | } 187 | 188 | public TimeLimitedMACD TimeLimitedMACD(ISeries input, int fast, int slow, int smooth, string startTime, string endTime) 189 | { 190 | if (cacheTimeLimitedMACD != null) 191 | for (int idx = 0; idx < cacheTimeLimitedMACD.Length; idx++) 192 | if (cacheTimeLimitedMACD[idx] != null && cacheTimeLimitedMACD[idx].Fast == fast && cacheTimeLimitedMACD[idx].Slow == slow && cacheTimeLimitedMACD[idx].Smooth == smooth && cacheTimeLimitedMACD[idx].StartTime == startTime && cacheTimeLimitedMACD[idx].EndTime == endTime && cacheTimeLimitedMACD[idx].EqualsInput(input)) 193 | return cacheTimeLimitedMACD[idx]; 194 | return CacheIndicator(new TimeLimitedMACD(){ Fast = fast, Slow = slow, Smooth = smooth, StartTime = startTime, EndTime = endTime }, input, ref cacheTimeLimitedMACD); 195 | } 196 | } 197 | } 198 | 199 | namespace NinjaTrader.NinjaScript.MarketAnalyzerColumns 200 | { 201 | public partial class MarketAnalyzerColumn : MarketAnalyzerColumnBase 202 | { 203 | public Indicators.TimeLimitedMACD TimeLimitedMACD(int fast, int slow, int smooth, string startTime, string endTime) 204 | { 205 | return indicator.TimeLimitedMACD(Input, fast, slow, smooth, startTime, endTime); 206 | } 207 | 208 | public Indicators.TimeLimitedMACD TimeLimitedMACD(ISeries input , int fast, int slow, int smooth, string startTime, string endTime) 209 | { 210 | return indicator.TimeLimitedMACD(input, fast, slow, smooth, startTime, endTime); 211 | } 212 | } 213 | } 214 | 215 | namespace NinjaTrader.NinjaScript.Strategies 216 | { 217 | public partial class Strategy : NinjaTrader.Gui.NinjaScript.StrategyRenderBase 218 | { 219 | public Indicators.TimeLimitedMACD TimeLimitedMACD(int fast, int slow, int smooth, string startTime, string endTime) 220 | { 221 | return indicator.TimeLimitedMACD(Input, fast, slow, smooth, startTime, endTime); 222 | } 223 | 224 | public Indicators.TimeLimitedMACD TimeLimitedMACD(ISeries input , int fast, int slow, int smooth, string startTime, string endTime) 225 | { 226 | return indicator.TimeLimitedMACD(input, fast, slow, smooth, startTime, endTime); 227 | } 228 | } 229 | } 230 | 231 | #endregion 232 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # craigyu 2 | NinjaTrader 8.0 indicators and strategies 3 | -------------------------------------------------------------------------------- /Strategies/EMAOpenPrice.cs: -------------------------------------------------------------------------------- 1 | #region Using declarations 2 | using System; 3 | using System.Collections.Generic; 4 | using System.ComponentModel; 5 | using System.ComponentModel.DataAnnotations; 6 | using System.Linq; 7 | using System.Text; 8 | using System.Threading.Tasks; 9 | using System.Windows; 10 | using System.Windows.Input; 11 | using System.Windows.Media; 12 | using System.Xml.Serialization; 13 | using NinjaTrader.Cbi; 14 | using NinjaTrader.Gui; 15 | using NinjaTrader.Gui.Chart; 16 | using NinjaTrader.Gui.SuperDom; 17 | using NinjaTrader.Gui.Tools; 18 | using NinjaTrader.Data; 19 | using NinjaTrader.NinjaScript; 20 | using NinjaTrader.Core.FloatingPoint; 21 | using NinjaTrader.NinjaScript.Indicators; 22 | using NinjaTrader.NinjaScript.DrawingTools; 23 | #endregion 24 | 25 | //This namespace holds Strategies in this folder and is required. Do not change it. 26 | namespace NinjaTrader.NinjaScript.Strategies 27 | { 28 | public class EMAOpenPrice : Strategy 29 | { 30 | private int OrderNum; 31 | //private bool WaitTillNext; 32 | private bool HasCrossedBelow; 33 | private int CurrentStage; 34 | private bool ProfitOneListed; 35 | private bool ProfitTwoListed; 36 | private bool ProfitThreeListed; 37 | private CurrentDayOHL CurrentDayOHL1; 38 | private EMA EMA1; 39 | private MACD MACD1; 40 | private bool BoughtWithOpen; 41 | private bool BoughtWithOpen2; 42 | private bool BoughtWithOpen3; 43 | 44 | protected override void OnStateChange() 45 | { 46 | if (State == State.SetDefaults) 47 | { 48 | Description = @"Enter the description for your new custom Strategy here."; 49 | Name = "EMAOpenPrice"; 50 | Calculate = Calculate.OnEachTick; 51 | EntriesPerDirection = 1; 52 | EntryHandling = EntryHandling.AllEntries; 53 | IsExitOnSessionCloseStrategy = true; 54 | ExitOnSessionCloseSeconds = 30; 55 | IsFillLimitOnTouch = false; 56 | MaximumBarsLookBack = MaximumBarsLookBack.TwoHundredFiftySix; 57 | OrderFillResolution = OrderFillResolution.Standard; 58 | Slippage = 0; 59 | StartBehavior = StartBehavior.WaitUntilFlat; 60 | TimeInForce = TimeInForce.Gtc; 61 | TraceOrders = false; 62 | RealtimeErrorHandling = RealtimeErrorHandling.StopCancelClose; 63 | StopTargetHandling = StopTargetHandling.PerEntryExecution; 64 | BarsRequiredToTrade = 20; 65 | // Disable this property for performance gains in Strategy Analyzer optimizations 66 | // See the Help Guide for additional information 67 | IsInstantiatedOnEachOptimizationIteration = true; 68 | ProfitTarget_1 = 3; 69 | ProfitTarget_2 = 100; 70 | ProfitTarget_3 = 12; 71 | ProfitOneListed = true; 72 | ProfitTwoListed = true; 73 | ProfitThreeListed = true; 74 | EMAPeriod = 2; 75 | OrderNum = 0; 76 | CurrentStage = 0; 77 | //WaitTillNext = false; 78 | HasCrossedBelow = true; 79 | BoughtWithOpen = false; 80 | OneFast = 2; 81 | OneSlow = 10; 82 | OneSmooth = 10; 83 | OpenPriceOffset = 7; 84 | OpenPriceOffset2 = 4; 85 | BoughtWithOpen2 = false; 86 | BoughtWithOpen3 = false; 87 | } 88 | else if (State == State.Configure) 89 | { 90 | } 91 | else if (State == State.DataLoaded) 92 | { 93 | CurrentDayOHL1 = CurrentDayOHL(Close); 94 | EMA1 = EMA(Close, Convert.ToInt32(EMAPeriod)); 95 | MACD1 = MACD(Close, Convert.ToInt32(OneFast), Convert.ToInt32(OneSlow), Convert.ToInt32(OneSmooth)); 96 | 97 | } 98 | } 99 | 100 | protected override void OnBarUpdate() 101 | { 102 | if (BarsInProgress != 0) 103 | return; 104 | 105 | if (CurrentBars[0] < 1) 106 | return; 107 | 108 | // Set 1 109 | if ((EMA1[0] > CurrentDayOHL1.CurrentOpen[0] 110 | || EMA1[0] > CurrentDayOHL1.CurrentOpen[0] + OpenPriceOffset 111 | || EMA1[0] > CurrentDayOHL1.CurrentOpen[0] + OpenPriceOffset2 112 | ) 113 | && (MACD1.Default[0] > MACD1.Avg[0])) 114 | { 115 | if((Position.Quantity == 0) && HasCrossedBelow){ 116 | EnterLong(Convert.ToInt32(3), ""); 117 | ProfitOneListed = false; 118 | ProfitTwoListed = false; 119 | ProfitThreeListed = false; 120 | 121 | if(EMA1[0] < CurrentDayOHL1.CurrentOpen[0] + OpenPriceOffset){ 122 | BoughtWithOpen = true; 123 | } 124 | else BoughtWithOpen = false; 125 | 126 | if(EMA1[0] >= CurrentDayOHL1.CurrentOpen[0] + OpenPriceOffset && EMA1[0] < CurrentDayOHL1.CurrentOpen[0] + OpenPriceOffset2){ 127 | BoughtWithOpen2 = true; 128 | } 129 | else BoughtWithOpen2 = false; 130 | 131 | if(EMA1[0] >= CurrentDayOHL1.CurrentOpen[0] + OpenPriceOffset && EMA1[0] < CurrentDayOHL1.CurrentOpen[0] + OpenPriceOffset2){ 132 | BoughtWithOpen3 = true; 133 | } 134 | else BoughtWithOpen3 = false; 135 | 136 | } 137 | 138 | else if((Position.Quantity == 3) && (Close[0] >= Position.AveragePrice + ProfitTarget_1) && !ProfitOneListed){ 139 | ProfitOneListed = true; 140 | ExitLong(Convert.ToInt32(1), "", ""); 141 | } 142 | else if((Position.Quantity == 2) && (Close[0] >= Position.AveragePrice + ProfitTarget_2) && !ProfitTwoListed){ 143 | ProfitTwoListed = true; 144 | ExitLong(Convert.ToInt32(1), "", ""); 145 | } 146 | else if((Position.Quantity == 1) && (Close[0] >= Position.AveragePrice + ProfitTarget_3) && !ProfitThreeListed){ 147 | ProfitThreeListed = true; 148 | ExitLong(Convert.ToInt32(1), "", ""); 149 | HasCrossedBelow = false; 150 | } 151 | } 152 | 153 | 154 | if(BoughtWithOpen && EMA1[0] >= CurrentDayOHL1.CurrentOpen[0] + OpenPriceOffset){ 155 | BoughtWithOpen = false; 156 | } 157 | 158 | if(BoughtWithOpen2 && EMA1[0] >= CurrentDayOHL1.CurrentOpen[0] + OpenPriceOffset2){ 159 | BoughtWithOpen2 = false; 160 | } 161 | 162 | 163 | // Set 6 164 | if ((EMA1[0] < CurrentDayOHL1.CurrentOpen[0]) && (MACD1.Default[0] < MACD1.Avg[0])) /*&& (CurrentStage == 4)*/ 165 | { 166 | if(Position.Quantity > 0){ 167 | Print("Bid Price < Open Price, Exit Long by Position size"); 168 | ExitLong(Convert.ToInt32(Position.Quantity), "", ""); 169 | Print("Remaining Position Size: " + Position.Quantity); 170 | } 171 | HasCrossedBelow = true; 172 | } 173 | 174 | if(EMA1[0] < CurrentDayOHL1.CurrentOpen[0] + OpenPriceOffset && (MACD1.Default[0] < MACD1.Avg[0]) && !BoughtWithOpen){ 175 | if(Position.Quantity > 0){ 176 | Print("Bid Price < Open Price, Exit Long by Position size"); 177 | ExitLong(Convert.ToInt32(Position.Quantity), "", ""); 178 | Print("Remaining Position Size: " + Position.Quantity); 179 | } 180 | HasCrossedBelow = true; 181 | } 182 | 183 | if(EMA1[0] < CurrentDayOHL1.CurrentOpen[0] + OpenPriceOffset2 && (MACD1.Default[0] < MACD1.Avg[0]) && !BoughtWithOpen2){ 184 | if(Position.Quantity > 0){ 185 | Print("Bid Price < Open Price, Exit Long by Position size"); 186 | ExitLong(Convert.ToInt32(Position.Quantity), "", ""); 187 | Print("Remaining Position Size: " + Position.Quantity); 188 | } 189 | HasCrossedBelow = true; 190 | } 191 | 192 | // if(EMA1[0] < CurrentDayOHL1.CurrentOpen[0] + OpenPriceOffset3 && (MACD1.Default[0] < MACD1.Avg[0]) && !BoughtWithOpen3){ 193 | // if(Position.Quantity > 0){ 194 | // Print("Bid Price < Open Price, Exit Long by Position size"); 195 | // ExitLong(Convert.ToInt32(Position.Quantity), "", ""); 196 | // Print("Remaining Position Size: " + Position.Quantity); 197 | // } 198 | // HasCrossedBelow = true; 199 | // } 200 | 201 | } 202 | #region Properties 203 | [NinjaScriptProperty] 204 | [Range(1, int.MaxValue)] 205 | [Display(Name="ProfitTarget_1", Order=1, GroupName="Parameters")] 206 | public int ProfitTarget_1 207 | { get; set; } 208 | 209 | [NinjaScriptProperty] 210 | [Range(1, int.MaxValue)] 211 | [Display(Name="ProfitTarget_2", Order=2, GroupName="Parameters")] 212 | public int ProfitTarget_2 213 | { get; set; } 214 | 215 | [NinjaScriptProperty] 216 | [Range(1, int.MaxValue)] 217 | [Display(Name="ProfitTarget_3", Order=3, GroupName="Parameters")] 218 | public int ProfitTarget_3 219 | { get; set; } 220 | 221 | [NinjaScriptProperty] 222 | [Range(1, int.MaxValue)] 223 | [Display(Name="EMAPeriod", Order=4, GroupName="Parameters")] 224 | public int EMAPeriod 225 | { get; set; } 226 | 227 | [NinjaScriptProperty] 228 | [Range(1, int.MaxValue)] 229 | [Display(Name="OneFast", Description="MACD One's Fast", Order=5, GroupName="Parameters")] 230 | public int OneFast 231 | { get; set; } 232 | 233 | [NinjaScriptProperty] 234 | [Range(1, int.MaxValue)] 235 | [Display(Name="OneSlow", Description="MACD One's Slow", Order=6, GroupName="Parameters")] 236 | public int OneSlow 237 | { get; set; } 238 | 239 | [NinjaScriptProperty] 240 | [Range(1, int.MaxValue)] 241 | [Display(Name="OneSmooth", Description="MACD One's Smooth", Order=7, GroupName="Parameters")] 242 | public int OneSmooth 243 | { get; set; } 244 | 245 | [NinjaScriptProperty] 246 | [Range(1, int.MaxValue)] 247 | [Display(Name="OpenPriceOffset", Description="MACD One's Slow", Order=9, GroupName="Parameters")] 248 | public int OpenPriceOffset 249 | { get; set; } 250 | 251 | [NinjaScriptProperty] 252 | [Range(1, int.MaxValue)] 253 | [Display(Name="OpenPriceOffset", Description="MACD One's Slow", Order=9, GroupName="Parameters")] 254 | public int OpenPriceOffset2 255 | { get; set; } 256 | #endregion 257 | } 258 | } 259 | -------------------------------------------------------------------------------- /Strategies/NineThirty.cs: -------------------------------------------------------------------------------- 1 | #region Using declarations 2 | using System; 3 | using System.Collections.Generic; 4 | using System.ComponentModel; 5 | using System.ComponentModel.DataAnnotations; 6 | using System.Linq; 7 | using System.Text; 8 | using System.Threading.Tasks; 9 | using System.Windows; 10 | using System.Windows.Input; 11 | using System.Windows.Media; 12 | using System.Xml.Serialization; 13 | using NinjaTrader.Cbi; 14 | using NinjaTrader.Gui; 15 | using NinjaTrader.Gui.Chart; 16 | using NinjaTrader.Gui.SuperDom; 17 | using NinjaTrader.Gui.Tools; 18 | using NinjaTrader.Data; 19 | using NinjaTrader.NinjaScript; 20 | using NinjaTrader.Core.FloatingPoint; 21 | using NinjaTrader.NinjaScript.Indicators; 22 | using NinjaTrader.NinjaScript.DrawingTools; 23 | #endregion 24 | 25 | //This namespace holds Strategies in this folder and is required. Do not change it. 26 | namespace NinjaTrader.NinjaScript.Strategies 27 | { 28 | public class NineThirty : Strategy 29 | { 30 | private int OrderNum; 31 | //private bool WaitTillNext; 32 | private bool HasCrossedBelow; 33 | private int CurrentStage; 34 | private bool ProfitOneListed; 35 | private bool ProfitTwoListed; 36 | private bool ProfitThreeListed; 37 | private CurrentDayOHL CurrentDayOHL1; 38 | private EMA EMA1; 39 | private MACD MACD1; 40 | private bool BoughtWithOpen; 41 | private bool BoughtWithOpen2; 42 | private bool BoughtWithOpen3; 43 | 44 | protected override void OnStateChange() 45 | { 46 | if (State == State.SetDefaults) 47 | { 48 | Description = @"Enter the description for your new custom Strategy here."; 49 | Name = "NineThirty"; 50 | Calculate = Calculate.OnEachTick; 51 | EntriesPerDirection = 1; 52 | EntryHandling = EntryHandling.AllEntries; 53 | IsExitOnSessionCloseStrategy = true; 54 | ExitOnSessionCloseSeconds = 30; 55 | IsFillLimitOnTouch = false; 56 | MaximumBarsLookBack = MaximumBarsLookBack.Infinite; 57 | OrderFillResolution = OrderFillResolution.Standard; 58 | Slippage = 0; 59 | StartBehavior = StartBehavior.WaitUntilFlat; 60 | TimeInForce = TimeInForce.Gtc; 61 | TraceOrders = false; 62 | RealtimeErrorHandling = RealtimeErrorHandling.StopCancelClose; 63 | StopTargetHandling = StopTargetHandling.PerEntryExecution; 64 | BarsRequiredToTrade = 20; 65 | // Disable this property for performance gains in Strategy Analyzer optimizations 66 | // See the Help Guide for additional information 67 | IsInstantiatedOnEachOptimizationIteration = true; 68 | ProfitTarget_1 = 3; 69 | ProfitTarget_2 = 100; 70 | ProfitTarget_3 = 12; 71 | ProfitOneListed = true; 72 | ProfitTwoListed = true; 73 | ProfitThreeListed = true; 74 | EMAPeriod = 2; 75 | OrderNum = 0; 76 | CurrentStage = 0; 77 | //WaitTillNext = false; 78 | HasCrossedBelow = true; 79 | BoughtWithOpen = false; 80 | OneFast = 2; 81 | OneSlow = 10; 82 | OneSmooth = 10; 83 | OpenPriceOffset = 7; 84 | OpenPriceOffset2 = 4; 85 | BoughtWithOpen2 = false; 86 | BoughtWithOpen3 = false; 87 | startTime = 63000; 88 | endTime = 93000; 89 | } 90 | else if (State == State.Configure) 91 | { 92 | } 93 | else if (State == State.DataLoaded) 94 | { 95 | CurrentDayOHL1 = CurrentDayOHL(Close); 96 | EMA1 = EMA(Close, Convert.ToInt32(EMAPeriod)); 97 | MACD1 = MACD(Close, Convert.ToInt32(OneFast), Convert.ToInt32(OneSlow), Convert.ToInt32(OneSmooth)); 98 | 99 | } 100 | } 101 | 102 | protected override void OnBarUpdate() 103 | { 104 | if (BarsInProgress != 0) 105 | return; 106 | 107 | if (CurrentBars[0] < 1) 108 | return; 109 | 110 | 111 | 112 | 113 | if (ToTime(Time[0]) >= startTime && ToTime(Time[0]) <= endTime){ 114 | // Set 1 115 | if ((EMA1[0] > CurrentDayOHL1.CurrentOpen[0] 116 | || EMA1[0] > CurrentDayOHL1.CurrentOpen[0] + OpenPriceOffset 117 | || EMA1[0] > CurrentDayOHL1.CurrentOpen[0] + OpenPriceOffset2 118 | ) 119 | && (MACD1.Default[0] > MACD1.Avg[0])) 120 | { 121 | if((Position.Quantity == 0) && HasCrossedBelow){ 122 | EnterLong(Convert.ToInt32(3), ""); 123 | ProfitOneListed = false; 124 | ProfitTwoListed = false; 125 | ProfitThreeListed = false; 126 | 127 | if(EMA1[0] < CurrentDayOHL1.CurrentOpen[0] + OpenPriceOffset){ 128 | BoughtWithOpen = true; 129 | } 130 | else BoughtWithOpen = false; 131 | 132 | if(EMA1[0] >= CurrentDayOHL1.CurrentOpen[0] + OpenPriceOffset && EMA1[0] < CurrentDayOHL1.CurrentOpen[0] + OpenPriceOffset2){ 133 | BoughtWithOpen2 = true; 134 | } 135 | else BoughtWithOpen2 = false; 136 | 137 | if(EMA1[0] >= CurrentDayOHL1.CurrentOpen[0] + OpenPriceOffset && EMA1[0] < CurrentDayOHL1.CurrentOpen[0] + OpenPriceOffset2){ 138 | BoughtWithOpen3 = true; 139 | } 140 | else BoughtWithOpen3 = false; 141 | 142 | } 143 | 144 | else if((Position.Quantity == 3) && (Close[0] >= Position.AveragePrice + ProfitTarget_1) && !ProfitOneListed){ 145 | ProfitOneListed = true; 146 | ExitLong(Convert.ToInt32(1), "", ""); 147 | } 148 | else if((Position.Quantity == 2) && (Close[0] >= Position.AveragePrice + ProfitTarget_2) && !ProfitTwoListed){ 149 | ProfitTwoListed = true; 150 | ExitLong(Convert.ToInt32(1), "", ""); 151 | } 152 | else if((Position.Quantity == 1) && (Close[0] >= Position.AveragePrice + ProfitTarget_3) && !ProfitThreeListed){ 153 | ProfitThreeListed = true; 154 | ExitLong(Convert.ToInt32(1), "", ""); 155 | HasCrossedBelow = false; 156 | } 157 | } 158 | 159 | 160 | if(BoughtWithOpen && EMA1[0] >= CurrentDayOHL1.CurrentOpen[0] + OpenPriceOffset){ 161 | BoughtWithOpen = false; 162 | } 163 | 164 | if(BoughtWithOpen2 && EMA1[0] >= CurrentDayOHL1.CurrentOpen[0] + OpenPriceOffset2){ 165 | BoughtWithOpen2 = false; 166 | } 167 | 168 | 169 | // Set 6 170 | if ((EMA1[0] < CurrentDayOHL1.CurrentOpen[0]) && (MACD1.Default[0] < MACD1.Avg[0])) /*&& (CurrentStage == 4)*/ 171 | { 172 | if(Position.Quantity > 0){ 173 | Print("Bid Price < Open Price, Exit Long by Position size"); 174 | ExitLong(Convert.ToInt32(Position.Quantity), "", ""); 175 | Print("Remaining Position Size: " + Position.Quantity); 176 | } 177 | HasCrossedBelow = true; 178 | } 179 | 180 | if(EMA1[0] < CurrentDayOHL1.CurrentOpen[0] + OpenPriceOffset && (MACD1.Default[0] < MACD1.Avg[0]) && !BoughtWithOpen){ 181 | if(Position.Quantity > 0){ 182 | Print("Bid Price < Open Price, Exit Long by Position size"); 183 | ExitLong(Convert.ToInt32(Position.Quantity), "", ""); 184 | Print("Remaining Position Size: " + Position.Quantity); 185 | } 186 | HasCrossedBelow = true; 187 | } 188 | 189 | if(EMA1[0] < CurrentDayOHL1.CurrentOpen[0] + OpenPriceOffset2 && (MACD1.Default[0] < MACD1.Avg[0]) && !BoughtWithOpen2){ 190 | if(Position.Quantity > 0){ 191 | Print("Bid Price < Open Price, Exit Long by Position size"); 192 | ExitLong(Convert.ToInt32(Position.Quantity), "", ""); 193 | Print("Remaining Position Size: " + Position.Quantity); 194 | } 195 | HasCrossedBelow = true; 196 | } 197 | } 198 | 199 | // if(EMA1[0] < CurrentDayOHL1.CurrentOpen[0] + OpenPriceOffset3 && (MACD1.Default[0] < MACD1.Avg[0]) && !BoughtWithOpen3){ 200 | // if(Position.Quantity > 0){ 201 | // Print("Bid Price < Open Price, Exit Long by Position size"); 202 | // ExitLong(Convert.ToInt32(Position.Quantity), "", ""); 203 | // Print("Remaining Position Size: " + Position.Quantity); 204 | // } 205 | // HasCrossedBelow = true; 206 | // } 207 | 208 | } 209 | 210 | #region Properties 211 | [NinjaScriptProperty] 212 | [Range(1, int.MaxValue)] 213 | [Display(Name="ProfitTarget_1", Order=1, GroupName="Parameters")] 214 | public int ProfitTarget_1 215 | { get; set; } 216 | 217 | [NinjaScriptProperty] 218 | [Range(1, int.MaxValue)] 219 | [Display(Name="ProfitTarget_2", Order=2, GroupName="Parameters")] 220 | public int ProfitTarget_2 221 | { get; set; } 222 | 223 | [NinjaScriptProperty] 224 | [Range(1, int.MaxValue)] 225 | [Display(Name="ProfitTarget_3", Order=3, GroupName="Parameters")] 226 | public int ProfitTarget_3 227 | { get; set; } 228 | 229 | [NinjaScriptProperty] 230 | [Range(1, int.MaxValue)] 231 | [Display(Name="EMAPeriod", Order=4, GroupName="Parameters")] 232 | public int EMAPeriod 233 | { get; set; } 234 | 235 | [NinjaScriptProperty] 236 | [Range(1, int.MaxValue)] 237 | [Display(Name="OneFast", Description="MACD One's Fast", Order=5, GroupName="Parameters")] 238 | public int OneFast 239 | { get; set; } 240 | 241 | [NinjaScriptProperty] 242 | [Range(1, int.MaxValue)] 243 | [Display(Name="OneSlow", Description="MACD One's Slow", Order=6, GroupName="Parameters")] 244 | public int OneSlow 245 | { get; set; } 246 | 247 | [NinjaScriptProperty] 248 | [Range(1, int.MaxValue)] 249 | [Display(Name="OneSmooth", Description="MACD One's Smooth", Order=7, GroupName="Parameters")] 250 | public int OneSmooth 251 | { get; set; } 252 | 253 | [NinjaScriptProperty] 254 | [Range(1, int.MaxValue)] 255 | [Display(Name="OpenPriceOffset", Description="MACD One's Slow", Order=9, GroupName="Parameters")] 256 | public int OpenPriceOffset 257 | { get; set; } 258 | 259 | [NinjaScriptProperty] 260 | [Range(1, int.MaxValue)] 261 | [Display(Name="OpenPriceOffset", Description="MACD One's Slow", Order=10, GroupName="Parameters")] 262 | public int OpenPriceOffset2 263 | { get; set; } 264 | 265 | [NinjaScriptProperty] 266 | [Range(1, double.MaxValue)] 267 | [Display(Name="startTime", Description="start time of trading", Order=11, GroupName="Parameters")] 268 | public double startTime 269 | { get; set; } 270 | 271 | [NinjaScriptProperty] 272 | [Range(1, double.MaxValue)] 273 | [Display(Name="endTime", Description="end time of trading", Order=12, GroupName="Parameters")] 274 | public double endTime 275 | { get; set; } 276 | #endregion 277 | } 278 | } 279 | -------------------------------------------------------------------------------- /Strategies/OpenPriceLong.cs: -------------------------------------------------------------------------------- 1 | #region Using declarations 2 | using System; 3 | using System.Collections.Generic; 4 | using System.ComponentModel; 5 | using System.ComponentModel.DataAnnotations; 6 | using System.Linq; 7 | using System.Text; 8 | using System.Threading.Tasks; 9 | using System.Windows; 10 | using System.Windows.Input; 11 | using System.Windows.Media; 12 | using System.Xml.Serialization; 13 | using NinjaTrader.Cbi; 14 | using NinjaTrader.Gui; 15 | using NinjaTrader.Gui.Chart; 16 | using NinjaTrader.Gui.SuperDom; 17 | using NinjaTrader.Gui.Tools; 18 | using NinjaTrader.Data; 19 | using NinjaTrader.NinjaScript; 20 | using NinjaTrader.Core.FloatingPoint; 21 | using NinjaTrader.NinjaScript.Indicators; 22 | using NinjaTrader.NinjaScript.DrawingTools; 23 | #endregion 24 | 25 | //This namespace holds Strategies in this folder and is required. Do not change it. 26 | namespace NinjaTrader.NinjaScript.Strategies 27 | { 28 | public class OpenPriceLong : Strategy 29 | { 30 | private int OrderNum; 31 | //private bool WaitTillNext; 32 | private bool HasCrossedBelow; 33 | private int CurrentStage; 34 | private bool ProfitOneListed; 35 | private bool ProfitTwoListed; 36 | private bool ProfitThreeListed; 37 | 38 | private CurrentDayOHL CurrentDayOHL1; 39 | 40 | protected override void OnStateChange() 41 | { 42 | if (State == State.SetDefaults) 43 | { 44 | Description = @"Use open price as an indicator to long or short"; 45 | Name = "OpenPriceLong"; 46 | Calculate = Calculate.OnEachTick; 47 | EntriesPerDirection = 1; 48 | EntryHandling = EntryHandling.AllEntries; 49 | IsExitOnSessionCloseStrategy = true; 50 | ExitOnSessionCloseSeconds = 30; 51 | IsFillLimitOnTouch = false; 52 | MaximumBarsLookBack = MaximumBarsLookBack.TwoHundredFiftySix; 53 | OrderFillResolution = OrderFillResolution.Standard; 54 | Slippage = 0; 55 | StartBehavior = StartBehavior.WaitUntilFlat; 56 | TimeInForce = TimeInForce.Gtc; 57 | TraceOrders = false; 58 | RealtimeErrorHandling = RealtimeErrorHandling.StopCancelClose; 59 | StopTargetHandling = StopTargetHandling.PerEntryExecution; 60 | BarsRequiredToTrade = 20; 61 | // Disable this property for performance gains in Strategy Analyzer optimizations 62 | // See the Help Guide for additional information 63 | IsInstantiatedOnEachOptimizationIteration = true; 64 | ProfitTarget_1 = 2; 65 | ProfitTarget_2 = 5; 66 | ProfitTarget_3 = 12; 67 | ProfitOneListed = true; 68 | ProfitTwoListed = true; 69 | ProfitThreeListed = true; 70 | OrderNum = 0; 71 | CurrentStage = 0; 72 | //WaitTillNext = false; 73 | HasCrossedBelow = true; 74 | } 75 | else if (State == State.Configure) 76 | { 77 | } 78 | else if (State == State.DataLoaded) 79 | { 80 | CurrentDayOHL1 = CurrentDayOHL(Close); 81 | } 82 | } 83 | 84 | protected override void OnBarUpdate() 85 | { 86 | if (BarsInProgress != 0) 87 | return; 88 | 89 | if (CurrentBars[0] < 1) 90 | return; 91 | 92 | int counter = 0; 93 | 94 | // Set 1 95 | if (Close[0] > CurrentDayOHL1.CurrentOpen[0]) 96 | { 97 | if((Position.Quantity == 0) && HasCrossedBelow){ 98 | EnterLong(Convert.ToInt32(3), ""); 99 | Draw.ArrowUp(this, @"OpenPriceLong Arrow up_1", false, 0, 0, Brushes.Red); 100 | ProfitOneListed = false; 101 | ProfitTwoListed = false; 102 | ProfitThreeListed = false; 103 | counter = 0; 104 | } 105 | 106 | else if((Position.Quantity == 3) && (Close[0] >= CurrentDayOHL1.CurrentOpen[0] + ProfitTarget_1) && !ProfitOneListed){ 107 | ProfitOneListed = true; 108 | ExitLong(Convert.ToInt32(1), "", ""); 109 | } 110 | else if((Position.Quantity == 2) && (Close[0] >= CurrentDayOHL1.CurrentOpen[0] + ProfitTarget_2) && !ProfitTwoListed){ 111 | ProfitTwoListed = true; 112 | ExitLong(Convert.ToInt32(1), "", ""); 113 | } 114 | else if((Position.Quantity == 1) && (Close[0] >= CurrentDayOHL1.CurrentOpen[0] + ProfitTarget_3) && !ProfitThreeListed){ 115 | ProfitThreeListed = true; 116 | ExitLong(Convert.ToInt32(1), "", ""); 117 | HasCrossedBelow = false; 118 | } 119 | } 120 | 121 | 122 | 123 | // Set 6 124 | if (Close[0] < CurrentDayOHL1.CurrentOpen[0]) /*&& (CurrentStage == 4)*/ 125 | { 126 | if(Position.Quantity > 0){ 127 | Print("Bid Price < Open Price, Exit Long by Position size"); 128 | ExitLong(Convert.ToInt32(Position.Quantity), "", ""); 129 | Print("Remaining Position Size: " + Position.Quantity); 130 | } 131 | HasCrossedBelow = true; 132 | } 133 | 134 | } 135 | 136 | #region Properties 137 | [NinjaScriptProperty] 138 | [Range(1, int.MaxValue)] 139 | [Display(Name="ProfitTarget_1", Order=1, GroupName="Parameters")] 140 | public int ProfitTarget_1 141 | { get; set; } 142 | 143 | [NinjaScriptProperty] 144 | [Range(1, int.MaxValue)] 145 | [Display(Name="ProfitTarget_2", Order=2, GroupName="Parameters")] 146 | public int ProfitTarget_2 147 | { get; set; } 148 | 149 | [NinjaScriptProperty] 150 | [Range(1, int.MaxValue)] 151 | [Display(Name="ProfitTarget_3", Order=3, GroupName="Parameters")] 152 | public int ProfitTarget_3 153 | { get; set; } 154 | #endregion 155 | 156 | } 157 | } 158 | -------------------------------------------------------------------------------- /Strategies/ThreeEMA.cs: -------------------------------------------------------------------------------- 1 | #region Using declarations 2 | using System; 3 | using System.Collections.Generic; 4 | using System.ComponentModel; 5 | using System.ComponentModel.DataAnnotations; 6 | using System.Linq; 7 | using System.Text; 8 | using System.Threading.Tasks; 9 | using System.Windows; 10 | using System.Windows.Input; 11 | using System.Windows.Media; 12 | using System.Xml.Serialization; 13 | using NinjaTrader.Cbi; 14 | using NinjaTrader.Gui; 15 | using NinjaTrader.Gui.Chart; 16 | using NinjaTrader.Gui.SuperDom; 17 | using NinjaTrader.Gui.Tools; 18 | using NinjaTrader.Data; 19 | using NinjaTrader.NinjaScript; 20 | using NinjaTrader.Core.FloatingPoint; 21 | using NinjaTrader.NinjaScript.Indicators; 22 | using NinjaTrader.NinjaScript.DrawingTools; 23 | #endregion 24 | 25 | //This namespace holds Strategies in this folder and is required. Do not change it. 26 | namespace NinjaTrader.NinjaScript.Strategies 27 | { 28 | public class ThreeEMA : Strategy 29 | { 30 | private EMA EMA1; 31 | private CurrentDayOHL CurrentDayOHL1; 32 | private EMA EMA2; 33 | private EMA EMA3; 34 | 35 | protected override void OnStateChange() 36 | { 37 | if (State == State.SetDefaults) 38 | { 39 | Description = @"Enter the description for your new custom Strategy here."; 40 | Name = "ThreeEMA"; 41 | Calculate = Calculate.OnEachTick; 42 | EntriesPerDirection = 1; 43 | EntryHandling = EntryHandling.AllEntries; 44 | IsExitOnSessionCloseStrategy = true; 45 | ExitOnSessionCloseSeconds = 30; 46 | IsFillLimitOnTouch = false; 47 | MaximumBarsLookBack = MaximumBarsLookBack.TwoHundredFiftySix; 48 | OrderFillResolution = OrderFillResolution.Standard; 49 | Slippage = 0; 50 | StartBehavior = StartBehavior.WaitUntilFlat; 51 | TimeInForce = TimeInForce.Gtc; 52 | TraceOrders = false; 53 | RealtimeErrorHandling = RealtimeErrorHandling.StopCancelClose; 54 | StopTargetHandling = StopTargetHandling.PerEntryExecution; 55 | BarsRequiredToTrade = 20; 56 | // Disable this property for performance gains in Strategy Analyzer optimizations 57 | // See the Help Guide for additional information 58 | IsInstantiatedOnEachOptimizationIteration = true; 59 | EMA_Period1 = 5; 60 | EMA_Period2 = 30; 61 | EMA_Period3 = 60; 62 | Profit_Target = 10; 63 | } 64 | else if (State == State.Configure) 65 | { 66 | } 67 | else if (State == State.DataLoaded) 68 | { 69 | EMA1 = EMA(Close, Convert.ToInt32(EMA_Period3)); 70 | CurrentDayOHL1 = CurrentDayOHL(Close); 71 | EMA2 = EMA(Close, Convert.ToInt32(EMA_Period1)); 72 | EMA3 = EMA(Close, Convert.ToInt32(EMA_Period2)); 73 | } 74 | } 75 | 76 | protected override void OnBarUpdate() 77 | { 78 | if (BarsInProgress != 0) 79 | return; 80 | 81 | if (CurrentBars[0] < 1) 82 | return; 83 | 84 | // Set 1 85 | if (CrossAbove(EMA1, CurrentDayOHL1.CurrentOpen, 1) && Position.Quantity == 0) 86 | { 87 | EnterLong(Convert.ToInt32(DefaultQuantity), ""); 88 | } 89 | 90 | // Set 2 91 | if ((CrossBelow(EMA2, CurrentDayOHL1.CurrentOpen, 1) 92 | || (CrossBelow(EMA3, CurrentDayOHL1.CurrentOpen, 1))) && Position.Quantity != 0) 93 | { 94 | ExitLong(Convert.ToInt32(DefaultQuantity), "", ""); 95 | } 96 | 97 | if(Position.Quantity != 0 && Close[0] >= Position.AveragePrice + Profit_Target){ 98 | ExitLong(Convert.ToInt32(DefaultQuantity), "", ""); 99 | } 100 | 101 | } 102 | 103 | #region Properties 104 | [NinjaScriptProperty] 105 | [Range(1, int.MaxValue)] 106 | [Display(Name="EMA_Period1", Order=1, GroupName="Parameters")] 107 | public int EMA_Period1 108 | { get; set; } 109 | 110 | [NinjaScriptProperty] 111 | [Range(1, int.MaxValue)] 112 | [Display(Name="EMA_Period2", Order=2, GroupName="Parameters")] 113 | public int EMA_Period2 114 | { get; set; } 115 | 116 | [NinjaScriptProperty] 117 | [Range(1, int.MaxValue)] 118 | [Display(Name="EMA_Period3", Order=3, GroupName="Parameters")] 119 | public int EMA_Period3 120 | { get; set; } 121 | 122 | [NinjaScriptProperty] 123 | [Range(1, int.MaxValue)] 124 | [Display(Name="Profit_Target", Order=4, GroupName="Parameters")] 125 | public int Profit_Target 126 | { get; set; } 127 | #endregion 128 | 129 | } 130 | } 131 | -------------------------------------------------------------------------------- /Strategies/TimeTwoMACD.cs: -------------------------------------------------------------------------------- 1 | #region Using declarations 2 | using System; 3 | using System.Collections.Generic; 4 | using System.ComponentModel; 5 | using System.ComponentModel.DataAnnotations; 6 | using System.Linq; 7 | using System.Text; 8 | using System.Threading.Tasks; 9 | using System.Windows; 10 | using System.Windows.Input; 11 | using System.Windows.Media; 12 | using System.Xml.Serialization; 13 | using NinjaTrader.Cbi; 14 | using NinjaTrader.Gui; 15 | using NinjaTrader.Gui.Chart; 16 | using NinjaTrader.Gui.SuperDom; 17 | using NinjaTrader.Gui.Tools; 18 | using NinjaTrader.Data; 19 | using NinjaTrader.NinjaScript; 20 | using NinjaTrader.Core.FloatingPoint; 21 | using NinjaTrader.NinjaScript.Indicators; 22 | using NinjaTrader.NinjaScript.DrawingTools; 23 | #endregion 24 | 25 | //This namespace holds Strategies in this folder and is required. Do not change it. 26 | namespace NinjaTrader.NinjaScript.Strategies 27 | { 28 | public class TimeTwoMACD : Strategy 29 | { 30 | private bool HasOrder; 31 | 32 | private TimeLimitedMACD MACD1; 33 | private TimeLimitedMACD MACD2; 34 | 35 | protected override void OnStateChange() 36 | { 37 | if (State == State.SetDefaults) 38 | { 39 | Description = @"2 MACD cross over long"; 40 | Name = "TimeTwoMACD"; 41 | Calculate = Calculate.OnEachTick; 42 | EntriesPerDirection = 1; 43 | EntryHandling = EntryHandling.UniqueEntries; 44 | IsExitOnSessionCloseStrategy = true; 45 | ExitOnSessionCloseSeconds = 30; 46 | IsFillLimitOnTouch = false; 47 | MaximumBarsLookBack = MaximumBarsLookBack.TwoHundredFiftySix; 48 | OrderFillResolution = OrderFillResolution.High; 49 | OrderFillResolutionType = BarsPeriodType.Minute; 50 | OrderFillResolutionValue = 1; 51 | Slippage = 0; 52 | StartBehavior = StartBehavior.WaitUntilFlat; 53 | TimeInForce = TimeInForce.Gtc; 54 | TraceOrders = true; 55 | RealtimeErrorHandling = RealtimeErrorHandling.StopCancelClose; 56 | StopTargetHandling = StopTargetHandling.PerEntryExecution; 57 | BarsRequiredToTrade = 20; 58 | // Disable this property for performance gains in Strategy Analyzer optimizations 59 | // See the Help Guide for additional information 60 | IsInstantiatedOnEachOptimizationIteration = true; 61 | OneFast = 12; 62 | OneSlow = 26; 63 | OneSmooth = 9; 64 | TwoFast = 12; 65 | TwoSlow = 26; 66 | TwoSmooth = 9; 67 | ProfitTarget = 20; 68 | StartTime = 93000; 69 | EndTime = 100000; 70 | StartTimeString = "09:30"; 71 | EndTimeString = "10:00"; 72 | HasOrder = false; 73 | } 74 | else if (State == State.Configure) 75 | { 76 | } 77 | else if (State == State.DataLoaded) 78 | { 79 | 80 | 81 | 82 | 83 | MACD1 = TimeLimitedMACD(Close, Convert.ToInt32(OneFast), Convert.ToInt32(OneSlow), Convert.ToInt32(OneSmooth) 84 | , StartTimeString, EndTimeString); 85 | MACD2 = TimeLimitedMACD(Close, Convert.ToInt32(TwoFast), Convert.ToInt32(TwoSlow), Convert.ToInt32(TwoSmooth), 86 | StartTimeString, EndTimeString); 87 | } 88 | } 89 | 90 | protected override void OnBarUpdate() 91 | { 92 | if (BarsInProgress != 0) 93 | return; 94 | 95 | if (CurrentBars[0] < 1) 96 | return; 97 | 98 | 99 | if(Position.Quantity == 0){ 100 | 101 | if ((CrossAbove(MACD2.Default, MACD2.Avg, 1)) 102 | || CrossAbove(MACD1.Default, MACD1.Avg, 1) 103 | ) 104 | { 105 | EnterLong(Convert.ToInt32(DefaultQuantity), ""); 106 | Draw.ArrowUp(this, @"BUY_IN Arrow up_MACD2", false, 0, 0, Brushes.Purple); 107 | 108 | } 109 | } 110 | else{ 111 | 112 | if(Position.GetUnrealizedProfitLoss(PerformanceUnit.Points, Close[0]) >= ProfitTarget){ 113 | ExitLong(Convert.ToInt32(DefaultQuantity), "", ""); 114 | Print("PROFIT SOLD"); 115 | return; 116 | } 117 | 118 | if((CrossBelow(MACD1.Default, MACD1.Avg, 1)) || (CrossBelow(MACD2.Default, MACD2.Avg, 1))){ 119 | ExitLong(Convert.ToInt32(DefaultQuantity), "", ""); 120 | Print("BELOW SOLD"); 121 | return; 122 | } 123 | 124 | if(ToTime(Time[0]) > EndTime){ 125 | ExitLong(Convert.ToInt32(DefaultQuantity), "", ""); 126 | Print("TIME OUT SOLD"); 127 | return; 128 | } 129 | 130 | } 131 | 132 | 133 | 134 | 135 | 136 | // // Set 1 137 | // if ((CrossAbove(MACD1.Default, MACD1.Avg, 1)) 138 | // && (HasOrder == false)) 139 | // { 140 | // EnterLong(Convert.ToInt32(DefaultQuantity), ""); 141 | // HasOrder = true; 142 | // Draw.ArrowUp(this, @"BUY_IN Arrow up_MACD1", false, 0, 0, Brushes.DodgerBlue); 143 | // } 144 | 145 | // // Set 2 146 | // if ((CrossAbove(MACD2.Default, MACD2.Avg, 1)) 147 | // && (HasOrder == false)) 148 | // { 149 | // EnterLong(Convert.ToInt32(DefaultQuantity), ""); 150 | // HasOrder = true; 151 | // Draw.ArrowUp(this, @"BUY_IN Arrow up_MACD2", false, 0, 0, Brushes.Purple); 152 | // } 153 | 154 | // // Set 3 155 | // if ((HasOrder == true) 156 | // // CrossBelow 157 | // && ((CrossBelow(MACD1.Default, MACD1.Avg, 1)) 158 | // || (CrossBelow(MACD2.Default, MACD2.Avg, 1)))) 159 | // { 160 | // ExitLong(Convert.ToInt32(DefaultQuantity), "", ""); 161 | // HasOrder = false; 162 | // Draw.ArrowDown(this, @"FLAT Arrow down_1", false, 0, 0, Brushes.Red); 163 | // } 164 | 165 | } 166 | 167 | #region Properties 168 | [NinjaScriptProperty] 169 | [Range(1, int.MaxValue)] 170 | [Display(Name="OneFast", Description="MACD One's Fast", Order=1, GroupName="Parameters")] 171 | public int OneFast 172 | { get; set; } 173 | 174 | [NinjaScriptProperty] 175 | [Range(1, int.MaxValue)] 176 | [Display(Name="OneSlow", Description="MACD One's Slow", Order=2, GroupName="Parameters")] 177 | public int OneSlow 178 | { get; set; } 179 | 180 | [NinjaScriptProperty] 181 | [Range(1, int.MaxValue)] 182 | [Display(Name="OneSmooth", Description="MACD One's Smooth", Order=3, GroupName="Parameters")] 183 | public int OneSmooth 184 | { get; set; } 185 | 186 | [NinjaScriptProperty] 187 | [Range(1, int.MaxValue)] 188 | [Display(Name="TwoFast", Description="MACD Two's Fast", Order=4, GroupName="Parameters")] 189 | public int TwoFast 190 | { get; set; } 191 | 192 | [NinjaScriptProperty] 193 | [Range(1, int.MaxValue)] 194 | [Display(Name="TwoSlow", Description="MACD Two's Slow", Order=5, GroupName="Parameters")] 195 | public int TwoSlow 196 | { get; set; } 197 | 198 | [NinjaScriptProperty] 199 | [Range(1, int.MaxValue)] 200 | [Display(Name="TwoSmooth", Description="MACD Two's Smooth", Order=6, GroupName="Parameters")] 201 | public int TwoSmooth 202 | { get; set; } 203 | 204 | [NinjaScriptProperty] 205 | [Range(1, int.MaxValue)] 206 | [Display(Name="ProfitTarget", Order=7, GroupName="Parameters")] 207 | public int ProfitTarget 208 | { get; set; } 209 | 210 | [NinjaScriptProperty] 211 | [Range(1, int.MaxValue)] 212 | [Display(Name="EndTime", Order=8, GroupName="Parameters")] 213 | public int EndTime 214 | { get; set; } 215 | 216 | [NinjaScriptProperty] 217 | [Range(1, int.MaxValue)] 218 | [Display(Name="StartTime", Order=9, GroupName="Parameters")] 219 | public int StartTime 220 | { get; set; } 221 | 222 | 223 | [NinjaScriptProperty] 224 | [Display(ResourceType = typeof(Custom.Resource), Name = "StartTime", GroupName = "NinjaScriptParameters", Order = 10)] 225 | public string StartTimeString 226 | { get; set; } 227 | 228 | [NinjaScriptProperty] 229 | [Display(ResourceType = typeof(Custom.Resource), Name = "EndTime", GroupName = "NinjaScriptParameters", Order = 11)] 230 | public string EndTimeString 231 | { get; set; } 232 | #endregion 233 | 234 | } 235 | } 236 | -------------------------------------------------------------------------------- /ninjascripts.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/craigyu/NinjaTrader-Scripts/d8fb38d23c5ba1527d29ab0651f09e523fa7174f/ninjascripts.zip --------------------------------------------------------------------------------