#region Using declarations using System; using System.ComponentModel; using System.ComponentModel.DataAnnotations; using System.Linq; using System.Windows; using System.Windows.Controls; using System.Windows.Media; using NinjaTrader.Cbi; using NinjaTrader.Core; using NinjaTrader.Custom; #endregion namespace NinjaTrader.NinjaScript.Indicators { public class SwingNoBackFill : Indicator { protected override void OnStateChange() { if (base.State == 1) { base.Description = "Enter the description for your new custom Indicator here."; base.Name = "mxPendingOrder"; base.Calculate = 0; base.IsOverlay = true; base.DisplayInDataBox = true; base.DrawOnPricePanel = true; base.DrawHorizontalGridLines = true; base.DrawVerticalGridLines = true; base.PaintPriceMarkers = true; base.ScaleJustification = 1; base.IsSuspendedWhileInactive = true; this.eOrderOpt = E_Order.Buy; this.LimitPrice = 5369.0; this.DistanceFromLimit = 5.0; return; } if (base.State == 2) { return; } if (base.State == 4) { lock (Account.All) { if (this.Account_name != null) { this._acc = Account.All.FirstOrDefault((Account a) => a.Name == this.Account_name); } } base.ChartControl.Dispatcher.InvokeAsync(delegate() { if (base.UserControlCollection.Contains(this.myGrid)) { return; } this.myGrid = new Grid { Name = "MyCustomGrid", VerticalAlignment = VerticalAlignment.Top }; this.myGrid.HorizontalAlignment = HorizontalAlignment.Right; this.myGrid.Margin = new Thickness(0.0, 20.0, 5.0, 0.0); ColumnDefinition value = new ColumnDefinition { Width = GridLength.Auto }; RowDefinition value2 = new RowDefinition { Height = GridLength.Auto }; this.myGrid.ColumnDefinitions.Add(value); this.myGrid.RowDefinitions.Add(value2); this.applyButton = new Button { Name = "apply", Content = "APPLY", Foreground = Brushes.White, Background = Brushes.Maroon }; this.applyButton.Click += this.OnApplyButtonClick; Grid.SetColumn(this.applyButton, 0); this.myGrid.Children.Add(this.applyButton); base.UserControlCollection.Add(this.myGrid); }); return; } if (base.State == 8) { base.ChartControl.Dispatcher.InvokeAsync(delegate() { if (this.myGrid != null && this.applyButton != null) { this.myGrid.Children.Remove(this.applyButton); this.applyButton = null; } }); } } private void OnApplyButtonClick(object sender, RoutedEventArgs rea) { if (this.eOrderOpt == E_Order.Buy) { this.EntryLimit(0, 1, this.LimitPrice); this.EntryStop(0, 1, this.LimitPrice + this.DistanceFromLimit * base.TickSize); return; } this.EntryLimit(2, 1, this.LimitPrice); this.EntryStop(2, 1, this.LimitPrice - this.DistanceFromLimit * base.TickSize); } protected override void OnBarUpdate() { State state = base.State; } private void EntryLimit(OrderAction orderAct, int lot, double limitPrice) { Order order = this._acc.CreateOrder(base.Instrument, orderAct, 0, 0, 0, lot, limitPrice, 0.0, "", "", Globals.MaxDate, null); this._acc.Submit(new Order[] { order }); base.Print(order.OrderId); } private void EntryStop(OrderAction orderAct, int lot, double stopPrice) { Order order = this._acc.CreateOrder(base.Instrument, orderAct, 4, 0, 0, lot, 0.0, stopPrice, "", "", Globals.MaxDate, null); this._acc.Submit(new Order[] { order }); base.Print(order.OrderId); } [TypeConverter(typeof(AccountNameConverter))] [NinjaScriptProperty] [Display(ResourceType = typeof(Resource), Name = "Account", GroupName = "NinjaScriptParameters", Order = 0)] public string Account_name { get; set; } [NinjaScriptProperty] [Display(ResourceType = typeof(Resource), Name = "Order Option", GroupName = "NinjaScriptParameters", Order = 1)] public E_Order eOrderOpt { get; set; } [NinjaScriptProperty] [Display(ResourceType = typeof(Resource), Name = "Limit Price", GroupName = "NinjaScriptParameters", Order = 2)] public double LimitPrice { get; set; } [Display(ResourceType = typeof(Resource), Name = "Distance(Ticks) From limit price", GroupName = "NinjaScriptParameters", Order = 3)] [NinjaScriptProperty] public double DistanceFromLimit { get; set; } private Grid myGrid; private Button applyButton; private Account _acc; } #endregion } #region NinjaScript generated code. Neither change nor remove. namespace NinjaTrader.NinjaScript.Indicators { public partial class Indicator : NinjaTrader.Gui.NinjaScript.IndicatorRenderBase { private SwingNoBackFill[] cacheSwingNoBackFill; public SwingNoBackFill SwingNoBackFill(string account_name, E_Order eOrderOpt, double limitPrice, double distanceFromLimit) { return SwingNoBackFill(Input, account_name, eOrderOpt, limitPrice, distanceFromLimit); } public SwingNoBackFill SwingNoBackFill(ISeries input, string account_name, E_Order eOrderOpt, double limitPrice, double distanceFromLimit) { if (cacheSwingNoBackFill != null) for (int idx = 0; idx < cacheSwingNoBackFill.Length; idx++) if (cacheSwingNoBackFill[idx] != null && cacheSwingNoBackFill[idx].Account_name == account_name && cacheSwingNoBackFill[idx].eOrderOpt == eOrderOpt && cacheSwingNoBackFill[idx].LimitPrice == limitPrice && cacheSwingNoBackFill[idx].DistanceFromLimit == distanceFromLimit && cacheSwingNoBackFill[idx].EqualsInput(input)) return cacheSwingNoBackFill[idx]; return CacheIndicator(new SwingNoBackFill(){ Account_name = account_name, eOrderOpt = eOrderOpt, LimitPrice = limitPrice, DistanceFromLimit = distanceFromLimit }, input, ref cacheSwingNoBackFill); } } } namespace NinjaTrader.NinjaScript.MarketAnalyzerColumns { public partial class MarketAnalyzerColumn : MarketAnalyzerColumnBase { public Indicators.SwingNoBackFill SwingNoBackFill(string account_name, E_Order eOrderOpt, double limitPrice, double distanceFromLimit) { return indicator.SwingNoBackFill(Input, account_name, eOrderOpt, limitPrice, distanceFromLimit); } public Indicators.SwingNoBackFill SwingNoBackFill(ISeries input , string account_name, E_Order eOrderOpt, double limitPrice, double distanceFromLimit) { return indicator.SwingNoBackFill(input, account_name, eOrderOpt, limitPrice, distanceFromLimit); } } } namespace NinjaTrader.NinjaScript.Strategies { public partial class Strategy : NinjaTrader.Gui.NinjaScript.StrategyRenderBase { public Indicators.SwingNoBackFill SwingNoBackFill(string account_name, E_Order eOrderOpt, double limitPrice, double distanceFromLimit) { return indicator.SwingNoBackFill(Input, account_name, eOrderOpt, limitPrice, distanceFromLimit); } public Indicators.SwingNoBackFill SwingNoBackFill(ISeries input , string account_name, E_Order eOrderOpt, double limitPrice, double distanceFromLimit) { return indicator.SwingNoBackFill(input, account_name, eOrderOpt, limitPrice, distanceFromLimit); } } } #endregion