Advertisement
Guest User

ProfitSniper

a guest
Jun 23rd, 2022
190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 24.06 KB | None | 0 0
  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 ProfitSniper : Indicator
  28.     {
  29.         /// <summary>
  30.         /// ProfitSniper - By PM
  31.         /// (upcoming)  - Select Your Custom Hotkeys with the Drop Down Menu
  32.         ///             - Multiple Orders Function
  33.         ///             - Orders Rejections Function
  34.         ///             - Auto-Populate QuantitySelector Field
  35.         /// Set Your Custom ProfitTargetDistance and StopLossDistance
  36.         /// Enable Your UseProfitTarget and/or UseStopLoss for OCO Extra Orders Functionality
  37.         /// Set Your Custom ProfitTargetMove and StopLossMove To Define The Movement Increment
  38.         /// Special thanks to Frosty (TickHunter indicator) and NinjaTrader_ChelseaB/Jesse/Jim -
  39.         ///     for their scripts and ideas/inputs that inspired this version of the indicator.
  40.         /// </summary>
  41.          
  42.        
  43.         #region Class Level Variables
  44.        
  45.             private Position                    accountPosition;
  46.             private List<Order>                 changeOrdersArray, submitOrdersArray;
  47.             private double                      currentPtPrice, currentSlPrice;
  48.             private Order                       entryBuyMarketOrder, entrySellMarketOrder, profitTargetOrder, stopLossOrder;
  49.             private Account                     myAccount;
  50.        
  51.        
  52.             #region QuantitySelector
  53.            
  54.                 // QS Define a Chart object to refer to the chart on which the indicator resides
  55.                 private Chart chartWindow;
  56.  
  57.                 /*QS for Hotkeys Keys And For Order Quantity (Anonymous Functions)
  58.                  * State == State.DataLoaded) -> ChartControl.Dispatcher.InvokeAsync((Action)(() -> quantitySelector
  59.                  * private void Add_Controls_To_Toolbar() -> ChartControl.Dispatcher.InvokeAsync((Action) -> quantitySelector
  60.                    // https://ninjatrader.com/support/forum/forum/ninjatrader-8/indicator-development/101315-set-quantityupdown-value-from-keyboard?p=1191246#post1191246
  61.                  * protected void ChartControl_PreviewKeyDown -> TriggerCustomEvent(o => & TriggerCustomEvent(o => -> quantitySelector.Value
  62.                 */
  63.                 NinjaTrader.Gui.Tools.QuantityUpDown quantitySelector = null;
  64.  
  65.                 // QS
  66.                 private bool Is_ToolBar_Controls_Added;
  67.                
  68.                 // QS https://stackoverflow.com/a/9301272/10789707
  69.                 private int qs;
  70.            
  71.             #endregion
  72.        
  73.         #endregion
  74.  
  75.        
  76.         #region OnStateChange
  77.        
  78.             protected override void OnStateChange()
  79.             {
  80.                 NinjaTrader.Gui.Tools.QuantityUpDown quantitySelector;
  81.                
  82.                 if (State == State.SetDefaults)
  83.                 {
  84.                 Description                                     = @"Orders Hotkeys Manager.";
  85.                     Name                                        = "ProfitSniper";
  86.                     Calculate                                   = Calculate.OnEachTick;
  87.                     IsOverlay                                   = true;
  88.                     DisplayInDataBox                            = false;
  89.  
  90.                     PrintDetails                                = false;
  91.                     ProfitTargetDistance                        = 10;
  92.                     StopLossDistance                            = 10;
  93.                     UseProfitTarget                             = true;
  94.                     UseStopLoss                                 = true;
  95.                     ProfitTargetMove                            = 1;
  96.                     StopLossMove                                = 1;
  97.                    
  98.                     AccountName                                 = "Sim101";
  99.                 }
  100.                 else if (State == State.Configure)
  101.                 {
  102.                 }
  103.                 else if (State == State.Historical)
  104.                 {
  105.                     //Call the custom addButtonToToolbar method in State.Historical to ensure it is only done when applied to a chart
  106.                     // -- not when loaded in the Indicators window
  107.                     if (!Is_ToolBar_Controls_Added) Add_Controls_To_Toolbar();
  108.                 }
  109.                 else if (State == State.DataLoaded)
  110.                 {
  111.                     // Find our account
  112.                     lock (Account.All)
  113.                         myAccount = Account.All.FirstOrDefault(a => a.Name == AccountName);
  114.                    
  115.                     if (myAccount != null)
  116.                         myAccount.OrderUpdate   += Account_OrderUpdate;
  117.                    
  118.                     if (ChartControl != null)
  119.                     {  
  120.                         ChartControl.PreviewKeyDown += ChartControl_PreviewKeyDown;
  121.                        
  122.                        
  123.                         ChartControl.Dispatcher.InvokeAsync((Action)(() =>
  124.                         {
  125.                             quantitySelector = (Window.GetWindow(ChartControl.Parent).FindFirst("ChartTraderControlQuantitySelector") as NinjaTrader.Gui.Tools.QuantityUpDown);
  126.                         }));           
  127.                     }
  128.                 }
  129.                 else if (State == State.Terminated)
  130.                 {
  131.                     if (myAccount != null)
  132.                         myAccount.OrderUpdate   -= Account_OrderUpdate;
  133.                    
  134.                     if (ChartControl != null)
  135.                     {  
  136.                         ChartControl.PreviewKeyDown -= ChartControl_PreviewKeyDown;        
  137.                     }
  138.                    
  139.                     //Call a custom method to dispose of any leftover objects in State.Terminated
  140.                     DisposeCleanUp();
  141.                 }
  142.             }
  143.            
  144.         #endregion
  145.        
  146.            
  147.         #region Hotkeys Snippets
  148.        
  149.             protected void ChartControl_PreviewKeyDown(object sender, KeyEventArgs e)
  150.             {
  151.                 // LONG Orders
  152.                 TriggerCustomEvent(o =>
  153.                 {
  154. //                  Order entryBuyMarketOrder = null; // Do Not enable or else the Order is always set to null
  155.  
  156.                     if (Keyboard.IsKeyDown(Key.NumPad1))
  157.                     {
  158.                         entryBuyMarketOrder  = myAccount.CreateOrder(
  159.                                 Instrument,
  160.                                 OrderAction.Buy,
  161.                                 OrderType.Market,
  162.                                 OrderEntry.Manual,
  163.                                 TimeInForce.Day,
  164.                                 quantitySelector.Value,
  165.                                 0,
  166.                                 0,
  167.                                 string.Empty,
  168.                                 "Entry",
  169.                                 Core.Globals.MaxDate,
  170.                                 null);
  171.                     }
  172.                    
  173.                     myAccount.Submit(new[] { entryBuyMarketOrder });
  174.  
  175.                 }, null);
  176.                 e.Handled = true;
  177.                
  178.                
  179.                 // SHORT Orders
  180.                 TriggerCustomEvent(p =>
  181.                 {
  182.     //              Order entrySellMarketOrder = null; // Do Not enable or else the Order is always set to null
  183.  
  184.                     if (Keyboard.IsKeyDown(Key.NumPad2))
  185.                     {
  186.                         entrySellMarketOrder  = myAccount.CreateOrder(
  187.                                 Instrument,
  188.                                 OrderAction.Sell,
  189.                                 OrderType.Market,
  190.                                 OrderEntry.Manual,
  191.                                 TimeInForce.Day,
  192.                                 quantitySelector.Value,
  193.                                 0,
  194.                                 0,
  195.                                 string.Empty,
  196.                                 "Entry",
  197.                                 Core.Globals.MaxDate,
  198.                                 null);
  199.                     }
  200.                    
  201.                     myAccount.Submit(new[] { entrySellMarketOrder });
  202.  
  203.                 }, null);
  204.                 e.Handled = true;
  205.                
  206.                 // Move Up Target OCO Order
  207.                 TriggerCustomEvent(qa =>
  208.                 {
  209.                     if (entryBuyMarketOrder != null)
  210.                     {
  211.                         if (Keyboard.IsKeyDown(Key.NumPad7) && !(Keyboard.IsKeyDown(Key.LeftCtrl) || Keyboard.IsKeyDown(Key.RightCtrl)))
  212.                         {
  213.                             changeOrdersArray = new List<Order>();
  214.                        
  215.                             currentPtPrice = profitTargetOrder.LimitPrice + (ProfitTargetMove * TickSize);
  216.                        
  217.                             profitTargetOrder.LimitPriceChanged = currentPtPrice;
  218.                        
  219.                             changeOrdersArray.Add(profitTargetOrder);
  220.                        
  221.                             myAccount.Change(changeOrdersArray);
  222.                         }
  223.                     }
  224.                     else if (entrySellMarketOrder != null)
  225.                     {
  226.                        
  227.                         if (Keyboard.IsKeyDown(Key.NumPad7) && !(Keyboard.IsKeyDown(Key.LeftCtrl) || Keyboard.IsKeyDown(Key.RightCtrl)))
  228.                         {
  229.                             changeOrdersArray = new List<Order>();
  230.                        
  231.                             currentPtPrice = profitTargetOrder.LimitPrice - (ProfitTargetMove * TickSize);
  232.                        
  233.                             profitTargetOrder.LimitPriceChanged = currentPtPrice;
  234.                        
  235.                             changeOrdersArray.Add(profitTargetOrder);
  236.                        
  237.                             myAccount.Change(changeOrdersArray);
  238.                         }
  239.                     }
  240.                 }, null);
  241.                 e.Handled = true;
  242.                
  243.                
  244.                 // Move Down Target OCO Order
  245.                 TriggerCustomEvent(qb =>
  246.                 {
  247.                     if (entryBuyMarketOrder != null)
  248.                     {
  249.                         if (Keyboard.IsKeyDown(Key.NumPad7) && (Keyboard.IsKeyDown(Key.LeftCtrl) || Keyboard.IsKeyDown(Key.RightCtrl)))
  250.                         {
  251.                             changeOrdersArray = new List<Order>();
  252.                        
  253.                             currentPtPrice = profitTargetOrder.LimitPrice - (ProfitTargetMove * TickSize);
  254.                        
  255.                             profitTargetOrder.LimitPriceChanged = currentPtPrice;
  256.                        
  257.                             changeOrdersArray.Add(profitTargetOrder);
  258.                        
  259.                             myAccount.Change(changeOrdersArray);
  260.                         }
  261.                     }
  262.                     else if (entrySellMarketOrder != null)
  263.                     {
  264.                         if (Keyboard.IsKeyDown(Key.NumPad7) && (Keyboard.IsKeyDown(Key.LeftCtrl) || Keyboard.IsKeyDown(Key.RightCtrl)))
  265.                         {
  266.                             changeOrdersArray = new List<Order>();
  267.                        
  268.                             currentPtPrice = profitTargetOrder.LimitPrice + (ProfitTargetMove * TickSize);
  269.                        
  270.                             profitTargetOrder.LimitPriceChanged = currentPtPrice;
  271.                        
  272.                             changeOrdersArray.Add(profitTargetOrder);
  273.                        
  274.                             myAccount.Change(changeOrdersArray);
  275.                         }
  276.                     }
  277.                 }, null);
  278.                 e.Handled = true;
  279.                
  280.                
  281.                 // Move Up StopLoss OCO Order
  282.                 TriggerCustomEvent(ra =>
  283.                 {
  284.                     if (entryBuyMarketOrder != null)
  285.                     {
  286.                         if (Keyboard.IsKeyDown(Key.NumPad4) && !(Keyboard.IsKeyDown(Key.LeftCtrl) || Keyboard.IsKeyDown(Key.RightCtrl)))
  287.                         {
  288.                             changeOrdersArray = new List<Order>();
  289.                        
  290.                             currentPtPrice = stopLossOrder.StopPrice + (StopLossMove * TickSize);
  291.                        
  292.                             stopLossOrder.StopPriceChanged = currentPtPrice;
  293.                        
  294.                             changeOrdersArray.Add(stopLossOrder);
  295.                        
  296.                             myAccount.Change(changeOrdersArray);
  297.                         }
  298.                     }
  299.                     else if (entrySellMarketOrder != null)
  300.                     {
  301.                         if (Keyboard.IsKeyDown(Key.NumPad4) && !(Keyboard.IsKeyDown(Key.LeftCtrl) || Keyboard.IsKeyDown(Key.RightCtrl)))
  302.                         {
  303.                             changeOrdersArray = new List<Order>();
  304.                        
  305.                             currentPtPrice = stopLossOrder.StopPrice - (StopLossMove * TickSize);
  306.                        
  307.                             stopLossOrder.StopPriceChanged = currentPtPrice;
  308.                        
  309.                             changeOrdersArray.Add(stopLossOrder);
  310.                        
  311.                             myAccount.Change(changeOrdersArray);
  312.                         }
  313.                     }
  314.                 }, null);
  315.                 e.Handled = true;
  316.                
  317.                
  318.                 // Move Down StopLoss OCO Order
  319.                 TriggerCustomEvent(rb =>
  320.                 {
  321.                     if (entryBuyMarketOrder != null)
  322.                     {
  323.                         if (Keyboard.IsKeyDown(Key.NumPad4) && (Keyboard.IsKeyDown(Key.LeftCtrl) || Keyboard.IsKeyDown(Key.RightCtrl)))
  324.                         {
  325.                             changeOrdersArray = new List<Order>();
  326.                        
  327.                             currentPtPrice = stopLossOrder.StopPrice - (StopLossMove * TickSize);
  328.                        
  329.                             stopLossOrder.StopPriceChanged = currentPtPrice;
  330.                        
  331.                             changeOrdersArray.Add(stopLossOrder);
  332.                        
  333.                             myAccount.Change(changeOrdersArray);
  334.                         }
  335.                     }
  336.                     else if (entrySellMarketOrder != null)
  337.                     {
  338.                         if (Keyboard.IsKeyDown(Key.NumPad4) && (Keyboard.IsKeyDown(Key.LeftCtrl) || Keyboard.IsKeyDown(Key.RightCtrl)))
  339.                         {
  340.                             changeOrdersArray = new List<Order>();
  341.                        
  342.                             currentPtPrice = stopLossOrder.StopPrice + (StopLossMove * TickSize);
  343.                        
  344.                             stopLossOrder.StopPriceChanged = currentPtPrice;
  345.                        
  346.                             changeOrdersArray.Add(stopLossOrder);
  347.                        
  348.                             myAccount.Change(changeOrdersArray);
  349.                         }
  350.                     }
  351.                    
  352.                 }, null);
  353.                 e.Handled = true;
  354.             }
  355.  
  356.         #endregion
  357.        
  358.            
  359.         #region Initial Target & StopLoss Snippets
  360.            
  361.             private void Account_OrderUpdate(object sender, OrderEventArgs orderUpdateArgs)
  362.             {
  363.                 Print("2A. Account_OrderUpdate() with KEYDOWN " + Time[0]);
  364.                 Print("entryBuyMarketOrder != null : " + (entryBuyMarketOrder != null));
  365.                 Print("entryBuyMarketOrder == orderUpdateArgs.Order : " + (entryBuyMarketOrder == orderUpdateArgs.Order));
  366.                 Print("orderUpdateArgs.Order.OrderState == OrderState.Filled : " + (orderUpdateArgs.Order.OrderState == OrderState.Filled));
  367.                 Print("orderUpdateArgs.Order.OrderState == OrderState.Working : " + (orderUpdateArgs.Order.OrderState == OrderState.Working));
  368.                 Print(" ");
  369.                 Print("accountPosition == null : " + (accountPosition == null));
  370.                 Print("profitTargetOrder == nul : " + (profitTargetOrder == null));
  371.                 Print("stopLossOrder == nul : " + (stopLossOrder == null));
  372.                 Print(" ");
  373.  
  374.                 // LONG Order Target and StopLoss OCO orders
  375.                 if (entryBuyMarketOrder != null && entryBuyMarketOrder == orderUpdateArgs.Order && orderUpdateArgs.Order.OrderState == OrderState.Filled)
  376.                 {
  377.                     string oco          = Guid.NewGuid().ToString("N");
  378.                     submitOrdersArray   = new List<Order>();
  379.  
  380.                     if (UseProfitTarget)
  381.                     {
  382.                         currentPtPrice = orderUpdateArgs.AverageFillPrice + ProfitTargetDistance * TickSize;
  383.  
  384.                         if (PrintDetails)
  385.                             Print(string.Format("{0} | Account_OrderUpdate | placing profit target | currentPtPrice: {1}", orderUpdateArgs.Time, currentPtPrice));
  386.  
  387.                         profitTargetOrder = myAccount.CreateOrder(orderUpdateArgs.Order.Instrument, OrderAction.Sell, OrderType.Limit, OrderEntry.Automated, TimeInForce.Day, orderUpdateArgs.Quantity, currentPtPrice, 0, oco, "Profit Target", Core.Globals.MaxDate, null);
  388.                         submitOrdersArray.Add(profitTargetOrder);
  389.                     }
  390.  
  391.                     if (UseStopLoss)
  392.                     {
  393.                         currentSlPrice = orderUpdateArgs.AverageFillPrice - StopLossDistance * TickSize;
  394.  
  395.                         if (PrintDetails)
  396.                             Print(string.Format("{0} | Account_OrderUpdate | placing stop loss | currentSlPrice: {1}", orderUpdateArgs.Time, currentSlPrice));
  397.                        
  398.                         stopLossOrder = myAccount.CreateOrder(orderUpdateArgs.Order.Instrument, OrderAction.Sell, OrderType.StopMarket, OrderEntry.Automated, TimeInForce.Day, orderUpdateArgs.Quantity, 0, currentSlPrice, oco, "Stop Loss", Core.Globals.MaxDate, null);
  399.                         submitOrdersArray.Add(stopLossOrder);
  400.                     }
  401.  
  402.                     myAccount.Submit(submitOrdersArray);
  403.                 }
  404.  
  405.                
  406.                
  407.                 // SHORT Order Target and StopLoss OCO orders
  408.                 if (entrySellMarketOrder != null && entrySellMarketOrder == orderUpdateArgs.Order && orderUpdateArgs.Order.OrderState == OrderState.Filled)
  409.                 {
  410.                     string oco          = Guid.NewGuid().ToString("N");
  411.                     submitOrdersArray   = new List<Order>();
  412.  
  413.                     if (UseProfitTarget)
  414.                     {
  415.                         currentPtPrice = orderUpdateArgs.AverageFillPrice - ProfitTargetDistance * TickSize;
  416.  
  417.                         if (PrintDetails)
  418.                             Print(string.Format("{0} | Account_OrderUpdate | placing profit target | currentPtPrice: {1}", orderUpdateArgs.Time, currentPtPrice));
  419.  
  420.                         profitTargetOrder = myAccount.CreateOrder(orderUpdateArgs.Order.Instrument, OrderAction.Buy, OrderType.Limit, OrderEntry.Automated, TimeInForce.Day, orderUpdateArgs.Quantity, currentPtPrice, 0, oco, "Profit Target", Core.Globals.MaxDate, null);
  421.                         submitOrdersArray.Add(profitTargetOrder);
  422.                     }
  423.  
  424.                     if (UseStopLoss)
  425.                     {
  426.                         currentSlPrice = orderUpdateArgs.AverageFillPrice + StopLossDistance * TickSize;
  427.  
  428.                         if (PrintDetails)
  429.                             Print(string.Format("{0} | Account_OrderUpdate | placing stop loss | currentSlPrice: {1}", orderUpdateArgs.Time, currentSlPrice));
  430.                        
  431.                         stopLossOrder = myAccount.CreateOrder(orderUpdateArgs.Order.Instrument, OrderAction.Buy, OrderType.StopMarket, OrderEntry.Automated, TimeInForce.Day, orderUpdateArgs.Quantity, 0, currentSlPrice, oco, "Stop Loss", Core.Globals.MaxDate, null);
  432.                         submitOrdersArray.Add(stopLossOrder);
  433.                     }
  434.  
  435.                     myAccount.Submit(submitOrdersArray);
  436.                 }
  437.  
  438.                 // once the exit orders are closed, reset for a new entry.
  439.                 else if ((profitTargetOrder != null && (profitTargetOrder.OrderState == OrderState.Filled
  440.                                                         || profitTargetOrder.OrderState == OrderState.Rejected
  441.                                                         || profitTargetOrder.OrderState == OrderState.Cancelled))
  442.                       || (stopLossOrder != null && (stopLossOrder.OrderState == OrderState.Filled
  443.                                                         || stopLossOrder.OrderState == OrderState.Rejected
  444.                                                         || stopLossOrder.OrderState == OrderState.Cancelled)))
  445.                 {
  446.                     entryBuyMarketOrder     = null;
  447.                     profitTargetOrder       = null;
  448.                     stopLossOrder           = null;
  449.                 }
  450.             }
  451.        
  452.         #endregion
  453.        
  454.        
  455.         #region OnBarUpdate Prints Snippets
  456.            
  457.             protected override void OnBarUpdate()
  458.             {
  459.                 if (myAccount != null && myAccount.Positions.Where(o => o.Instrument == Instrument).Count() > 0)
  460.                     accountPosition = myAccount.Positions.Where(o => o.Instrument == Instrument).Last();
  461.                 else
  462.                     accountPosition = null;
  463.                
  464.                 // Test with Prints if orders get executed or not if position detected
  465.                 if (accountPosition == null)
  466.                 {
  467.                     Print("### 1A. OnBarUpdate() with !KEYDOWN " + Time[0]);
  468.                     Print("accountPosition == null : " + (accountPosition == null));
  469.                     Print("profitTargetOrder == nul : " + (profitTargetOrder == null));
  470.                     Print("sstopLossOrder == nul : " + (stopLossOrder == null));
  471.                     Print(" ");
  472.                 }
  473.                 else
  474.                 // Test with Prints if orders get executed or not if position not detected
  475.                 {
  476.                     Print("1B. OnBarUpdate() with KEYDOWN " + Time[0]);
  477.                     Print("accountPosition == null : " + (accountPosition == null));
  478.                     Print("profitTargetOrder == nul : " + (profitTargetOrder == null));
  479.                     Print("stopLossOrder == nul : " + (stopLossOrder == null));
  480.                     Print(" ");
  481.                    
  482.                 }
  483.             }
  484.        
  485.         #endregion
  486.        
  487.            
  488.         #region Add Controls To Toolbar
  489.        
  490.             private void Add_Controls_To_Toolbar()
  491.             {
  492.                 // Use this.Dispatcher to ensure code is executed on the proper thread
  493.                 ChartControl.Dispatcher.InvokeAsync((Action)(() =>
  494.                 {
  495.              
  496.                     //Obtain the Chart on which the indicator is configured
  497.                     chartWindow = Window.GetWindow(this.ChartControl.Parent) as Chart;
  498.                     if (chartWindow == null)
  499.                     {
  500.                         Print("chartWindow == null");
  501.                         return;
  502.                     }
  503.  
  504.                     quantitySelector = new NinjaTrader.Gui.Tools.QuantityUpDown();
  505.                     //quantitySelector.ValueChanged +=  On_quantitySelector_ValueChanged;
  506.                     quantitySelector.PreviewKeyDown      +=  On_quantitySelector_PreviewKeyDown;
  507.  
  508.                     chartWindow.MainMenu.Add(quantitySelector);
  509.      
  510.                     Is_ToolBar_Controls_Added = true;
  511.                 }));
  512.             }
  513.        
  514.         #endregion  
  515.          
  516.        
  517.         #region QuantitySelector Snippets
  518.            
  519.             private void On_quantitySelector_PreviewKeyDown(object sender, KeyEventArgs p)
  520.             {
  521.                 NinjaTrader.Gui.Tools.QuantityUpDown qs = sender as NinjaTrader.Gui.Tools.QuantityUpDown;
  522.                
  523.                 if (p.Key == Key.Delete || p.Key == Key.Back)
  524.                 {
  525.                     p.Handled = true;
  526.                     qs.Value = 0;
  527.                 }
  528.                
  529.                 if ((p.Key >= Key.D0 && p.Key <= Key.D9 || (p.Key >= Key.NumPad0 && p.Key <= Key.NumPad9)))
  530.                 {
  531.                     p.Handled = true;
  532.                    
  533.                     string number = p.Key.ToString();
  534.                     string newnumber = qs.Value.ToString();
  535.                    
  536.                     number =  number.Replace("NumPad", "");
  537.                     number = number.Replace("D", "");
  538.                     int num = int.Parse(newnumber + number);
  539.                    
  540.                     if (qs != null) qs.Value = num;
  541.                 }
  542.             }
  543.            
  544.         #endregion
  545.            
  546.        
  547.         #region DisposeCleanUp Snippet
  548.        
  549.             private void DisposeCleanUp()
  550.             {
  551.                 //ChartWindow Null Check
  552.                 if (chartWindow != null)
  553.                 {
  554.                     //Dispatcher used to Assure Executed on UI Thread
  555.                     ChartControl.Dispatcher.InvokeAsync((Action)(() =>
  556.                     {
  557.                         if( quantitySelector != null )  chartWindow.MainMenu.Remove(quantitySelector);
  558.                     }));
  559.                 }
  560.             }
  561.            
  562.         #endregion
  563.        
  564.  
  565.         #region Properties
  566.            
  567.             [TypeConverter(typeof(NinjaTrader.NinjaScript.AccountNameConverter))]
  568.             [Display(Name = "Account Name", Description = "Selects The Account (from Ones Available)", Order = 1, GroupName = "Account Selector")]
  569.             public string AccountName { get; set; }
  570.  
  571.             [NinjaScriptProperty]
  572.             [Range(1, int.MaxValue)]
  573.             [Display(Name = "Profit Target Distance", Description = "Sets The ProfitTarget OCO Order Distance (in ticks)", Order = 2, GroupName = "Indicator's Input Setings")]
  574.             public int ProfitTargetDistance
  575.             { get; set; }
  576.  
  577.             [NinjaScriptProperty]
  578.             [Range(1, int.MaxValue)]
  579.             [Display(Name = "Stop Loss Distance", Description = "Sets The StopLoss OCO Order Distance (in ticks)", Order = 3, GroupName = "Indicator's Input Setings")]
  580.             public int StopLossDistance
  581.             { get; set; }
  582.  
  583.             [NinjaScriptProperty]
  584.             [Display(Name = "Use Profit Target", Description = "Enables ProfitTarget OCO Order", Order = 4, GroupName = "Indicator's Input Setings")]
  585.             public bool UseProfitTarget
  586.             { get; set; }
  587.  
  588.             [NinjaScriptProperty]
  589.             [Display(Name = "Use Stop Loss", Description = "Enables StopLoss OCO Order", Order = 5, GroupName = "Indicator's Input Setings")]
  590.             public bool UseStopLoss
  591.             { get; set; }
  592.  
  593.             [NinjaScriptProperty]
  594.             [Range(1, int.MaxValue)]
  595.             [Display(Name = "Profit Target Move (<= Profit Target Distance)", Description = "Sets The ProfitTarget OCO Order move (in ticks)", Order = 6, GroupName = "Indicator's Input Setings")]
  596.             public int ProfitTargetMove
  597.             { get; set; }
  598.  
  599.             [NinjaScriptProperty]
  600.             [Range(1, int.MaxValue)]
  601.             [Display(Name = "Stop Loss Move (<= Stop Loss Distance)", Description = "Sets The StopLoss OCO Order move (in ticks)", Order = 7, GroupName = "Indicator's Input Setings")]
  602.             public int StopLossMove
  603.             { get; set; }
  604.  
  605.             [NinjaScriptProperty]
  606.             [Display(Name = "Print Details", Description = "Prints In Output Window (Debugging)", Order = 8, GroupName = "Debug Mode")]
  607.             public bool PrintDetails
  608.             { get; set; }
  609.            
  610.         #endregion
  611.  
  612.     }
  613. }
  614.  
  615. #region NinjaScript generated code. Neither change nor remove.
  616.  
  617. namespace NinjaTrader.NinjaScript.Indicators
  618. {
  619.     public partial class Indicator : NinjaTrader.Gui.NinjaScript.IndicatorRenderBase
  620.     {
  621.         private ProfitSniper[] cacheProfitSniper;
  622.         public ProfitSniper ProfitSniper(int profitTargetDistance, int stopLossDistance, bool useProfitTarget, bool useStopLoss, int profitTargetMove, int stopLossMove, bool printDetails)
  623.         {
  624.             return ProfitSniper(Input, profitTargetDistance, stopLossDistance, useProfitTarget, useStopLoss, profitTargetMove, stopLossMove, printDetails);
  625.         }
  626.  
  627.         public ProfitSniper ProfitSniper(ISeries<double> input, int profitTargetDistance, int stopLossDistance, bool useProfitTarget, bool useStopLoss, int profitTargetMove, int stopLossMove, bool printDetails)
  628.         {
  629.             if (cacheProfitSniper != null)
  630.                 for (int idx = 0; idx < cacheProfitSniper.Length; idx++)
  631.                     if (cacheProfitSniper[idx] != null && cacheProfitSniper[idx].ProfitTargetDistance == profitTargetDistance && cacheProfitSniper[idx].StopLossDistance == stopLossDistance && cacheProfitSniper[idx].UseProfitTarget == useProfitTarget && cacheProfitSniper[idx].UseStopLoss == useStopLoss && cacheProfitSniper[idx].ProfitTargetMove == profitTargetMove && cacheProfitSniper[idx].StopLossMove == stopLossMove && cacheProfitSniper[idx].PrintDetails == printDetails && cacheProfitSniper[idx].EqualsInput(input))
  632.                         return cacheProfitSniper[idx];
  633.             return CacheIndicator<ProfitSniper>(new ProfitSniper(){ ProfitTargetDistance = profitTargetDistance, StopLossDistance = stopLossDistance, UseProfitTarget = useProfitTarget, UseStopLoss = useStopLoss, ProfitTargetMove = profitTargetMove, StopLossMove = stopLossMove, PrintDetails = printDetails }, input, ref cacheProfitSniper);
  634.         }
  635.     }
  636. }
  637.  
  638. namespace NinjaTrader.NinjaScript.MarketAnalyzerColumns
  639. {
  640.     public partial class MarketAnalyzerColumn : MarketAnalyzerColumnBase
  641.     {
  642.         public Indicators.ProfitSniper ProfitSniper(int profitTargetDistance, int stopLossDistance, bool useProfitTarget, bool useStopLoss, int profitTargetMove, int stopLossMove, bool printDetails)
  643.         {
  644.             return indicator.ProfitSniper(Input, profitTargetDistance, stopLossDistance, useProfitTarget, useStopLoss, profitTargetMove, stopLossMove, printDetails);
  645.         }
  646.  
  647.         public Indicators.ProfitSniper ProfitSniper(ISeries<double> input , int profitTargetDistance, int stopLossDistance, bool useProfitTarget, bool useStopLoss, int profitTargetMove, int stopLossMove, bool printDetails)
  648.         {
  649.             return indicator.ProfitSniper(input, profitTargetDistance, stopLossDistance, useProfitTarget, useStopLoss, profitTargetMove, stopLossMove, printDetails);
  650.         }
  651.     }
  652. }
  653.  
  654. namespace NinjaTrader.NinjaScript.Strategies
  655. {
  656.     public partial class Strategy : NinjaTrader.Gui.NinjaScript.StrategyRenderBase
  657.     {
  658.         public Indicators.ProfitSniper ProfitSniper(int profitTargetDistance, int stopLossDistance, bool useProfitTarget, bool useStopLoss, int profitTargetMove, int stopLossMove, bool printDetails)
  659.         {
  660.             return indicator.ProfitSniper(Input, profitTargetDistance, stopLossDistance, useProfitTarget, useStopLoss, profitTargetMove, stopLossMove, printDetails);
  661.         }
  662.  
  663.         public Indicators.ProfitSniper ProfitSniper(ISeries<double> input , int profitTargetDistance, int stopLossDistance, bool useProfitTarget, bool useStopLoss, int profitTargetMove, int stopLossMove, bool printDetails)
  664.         {
  665.             return indicator.ProfitSniper(input, profitTargetDistance, stopLossDistance, useProfitTarget, useStopLoss, profitTargetMove, stopLossMove, printDetails);
  666.         }
  667.     }
  668. }
  669.  
  670. #endregion
  671.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement