Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

Partner 728x90

Collapse

Buying at High[1] + TickSize

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Buying at High[1] + TickSize

    I'm trying to create a button that, once clicked, will buy with a stop limit at the High of the previous bar + 1 tick; however, it seems to keep executing the trade at the same price (and much higher than the H[1] + ticksize). Here is my order entry code.

    Order StopLimitOrder = account.CreateOrder(Instrument, OrderAction.Buy, OrderType.StopLimit, OrderEntry.Manual, TimeInForce.Day, 1, High[1] + TickSize, High[1] + TickSize, "Scalp 1_5" + CurrentBar, string.Empty, new DateTime(), null);
    account.Submit(new Order[] { StopLimitOrder} );

    NinjaTrader.NinjaScript.AtmStrategy.StartAtmStrate gy("Scalp 1_3", StopLimitOrder);
    Last edited by DooberXL; 05-20-2022, 05:58 AM.

    #2
    Hello DooberXL,

    From the given details it would be impossible to tell what may be happening. As a first step I would suggest to print the High[1] + TickSize at that time to see if that matches the order information. If you are comparing against a chart then a print would be a better approach to verify what the script is seeing when you call that logic. This could also relate to when/where that logic is being called in your script.

    If you are not able to determine where the problem is I would suggest to extract that part of the code and make a new empty script that displays just that as an example. Ensure it submits the order and still shows the problem. You can then attach that to give more context for the question.




    JesseNinjaTrader Customer Service

    Comment


      #3
      Thank you for the suggestions. Please see my code below. I am using a button to execute a buy stop limit order at H[1] + TIckSize


      Code:
      namespace NinjaTrader.NinjaScript.Indicators
      {
      public class ChartOrderButtonsIndicatorExample : Indicator
      {
      private Account account;
      private System.Windows.Controls.Grid buttonsGrid;
      private System.Windows.Controls.Button longStopLimitButton;
      
      
      
      
      protected override void OnStateChange()
      {
      if (State == State.SetDefaults)
      {
      Description = @"Example to demonstrate how to submit orders from an indicator's button click event using the Addon approach";
      Name = "ChartOrderButtonsIndicatorExample";
      Calculate = Calculate.OnEachTick;
      IsOverlay = true;
      
      
      
      
      }
      else if (State == State.DataLoaded)
      {
      account = Account.All.FirstOrDefault(a => a.Name == "Sim101");
      
      
      
      if (ChartControl != null)
      CreateWPFControls();
      
      
      }
      else if (State == State.Terminated)
      {
      
      
      if (ChartControl != null)
      RemoveWPFControls();
      }
      }
      
      
      
      private void CreateWPFControls()
      {
      // if the script has already added the controls, do not add a second time.
      if (UserControlCollection.Contains(buttonsGrid))
      return;
      
      // when making WPF changes to the UI, run the code on the UI thread of the chart
      ChartControl.Dispatcher.InvokeAsync((() =>
      {
      // this buttonGrid will contain the buttons
      buttonsGrid = new System.Windows.Controls.Grid
      {
      Background = Brushes.Red,
      Name = "ButtonsGrid",
      HorizontalAlignment = HorizontalAlignment.Right,
      VerticalAlignment = VerticalAlignment.Top
      };
      
      // add 3 columns to the grid
      for (int i = 0; i < 3; i++)
      buttonsGrid.ColumnDefinitions.Add(new System.Windows.Controls.ColumnDefinition());
      
      #region Use case 1, immediately submitting a stop limit order H[1] + 0.25
      
      longStopLimitButton = new System.Windows.Controls.Button
      {
      Name = "LongStopLimitButton",
      Content = "BUY H[1] + 0.25",
      Foreground = Brushes.White,
      Background = Brushes.Green
      };
      
      longStopLimitButton.Click += OnButtonClick;
      buttonsGrid.Children.Add(longStopLimitButton);
      System.Windows.Controls.Grid.SetColumn(longStopLim itButton, 0);
      #endregion
      
      
      
      // add our button grid to the main UserControlCollection over the chart
      UserControlCollection.Add(buttonsGrid);
      }));
      }
      
      
      
      private void OnButtonClick(object sender, RoutedEventArgs rea)
      {
      System.Windows.Controls.Button button = sender as System.Windows.Controls.Button;
      
      #region Use case 1, immediately submitting a stop limit order
      
      if (button == longStopLimitButton)
      {
      
      Order StopLimitOrder = account.CreateOrder(Instrument, OrderAction.Buy, OrderType.StopLimit, OrderEntry.Manual, TimeInForce.Day, 1, High[1] + TickSize, High[1] + TickSize, "Scalp 1_5" + DateTime.Now, string.Empty, new DateTime(), null);
      account.Submit(new Order[] { StopLimitOrder} );
      
      NinjaTrader.NinjaScript.AtmStrategy.StartAtmStrate gy("Scalp 1_3", StopLimitOrder);
      string high = High[1].ToString();
      Draw.TextFixed(this, "infobox", high, TextPosition.BottomLeft, Brushes.Green, new Gui.Tools.SimpleFont("Arial", 25), Brushes.Transparent, Brushes.Transparent, 100);
      // refresh the chart so that the text box will appear on the next render pass even if there is no incoming data
      ForceRefresh();
      
      //Order StopLossOrder = account.CreateOrder(Instrument, OrderAction.BuyToCover, OrderType.StopMarket, OrderEntry.Manual, TimeInForce.Day, 1, 0, High[1] - 11 * TickSize, "Scalp 1_5" + CurrentBar, string.Empty, new DateTime(), null);
      //account.Submit(new Order[] { StopLossOrder} );
      //Order ProfitOrder = account.CreateOrder(Instrument, OrderAction.BuyToCover, OrderType.Limit, OrderEntry.Manual, TimeInForce.Day, 1, 0, High[1] + 1.25, "Scalp 1_5" + CurrentBar, string.Empty, new DateTime(), null);
      //account.Submit(new Order[] { ProfitOrder} );
      
      }
      #endregion
      
      
      }
      
      private void RemoveWPFControls()
      {
      // when disabling the script, remove the button click handler methods from the click events
      // set the buttons to null so the garbage collector knows to clean them up and free memory
      ChartControl.Dispatcher.InvokeAsync((() =>
      {
      if (buttonsGrid != null)
      {
      
      
      if (longStopLimitButton != null)
      {
      longStopLimitButton.Click -= OnButtonClick;
      longStopLimitButton = null;
      }
      
      UserControlCollection.Remove(buttonsGrid);
      }
      }));
      }
      }
      }

      Click image for larger version

Name:	Example.PNG
Views:	430
Size:	56.8 KB
ID:	1202308


      On the bottom left is printed the "H[1]" - which is incorrect as the H[1] is about 3897.50.

      Thank you for your time.

      Comment


        #4
        Hello DooberXL,

        One problem here is that you are using a Button event to get price data, price data is related to bar processing so that has no context in the button event. You are likely getting a invalid price in the code you provided.

        The code inside the button event needs a TriggerCustomEvent:

        Code:
        private void OnButtonClick(object sender, RoutedEventArgs rea)
        {
             TriggerCustomEvent(o =>
            {
                 // your code here that uses [BarsAgo]
        
            }, null);
        }
        JesseNinjaTrader Customer Service

        Comment


          #5
          That worked perfectly! Thank you so much!

          Comment


            #6
            Utilizing the same code, I was able to enter orders immediately above the high of the prior bar. Would you have any guidance if I wanted the order to be entered above the high of the forming bar upon close? Essentially, I would click the order button, but the system will wait for the close of the bar, then immediately enter the order on or right before the first tick of the next bar.

            Comment


              #7
              Hello DooberXL,

              Please see the following sample on the user appshare, this uses OnBarUpdate to place trades based on buttons being pressed: https://ninjatraderecosystem.com/use...bar-buttons-2/

              The NinjaTrader Ecosystem website is for educational and informational purposes only and should not be considered a solicitation to buy or sell a futures contract or make any other type of investment decision. The add-ons listed on this website are not to be considered a recommendation and it is the reader's responsibility to evaluate any product, service, or company. NinjaTrader Ecosystem LLC is not responsible for the accuracy or content of any product, service or company linked to on this website.
              JesseNinjaTrader Customer Service

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by helpwanted, Today, 03:06 AM
              2 responses
              21 views
              0 likes
              Last Post NinjaTrader_LuisH  
              Started by DayTradingDEMON, Today, 09:28 AM
              0 responses
              1 view
              0 likes
              Last Post DayTradingDEMON  
              Started by navyguy06, Today, 09:28 AM
              0 responses
              2 views
              0 likes
              Last Post navyguy06  
              Started by rjbtrade1, 11-30-2023, 04:38 PM
              2 responses
              77 views
              0 likes
              Last Post DavidHP
              by DavidHP
               
              Started by cmtjoancolmenero, Yesterday, 03:58 PM
              7 responses
              31 views
              0 likes
              Last Post cmtjoancolmenero  
              Working...
              X