Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Long Short Tollbar Butttons

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

    Long Short Tollbar Butttons

    Hello,
    have downloaded from the NinjaTrader page: User App Share, the Long Short Toolbar Buttons.
    Unfortunately, it does not work as desired.
    I want to go long market or short market as it works in ChartTrader when I click Buy Mkt or Sell Mkt.
    What am I doing wrong or how do I change the code.

    Thanks.

    Code:

    ​#region Using declarations
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.ComponentModel.DataAnnotations;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Xml.Serialization;
    using NinjaTrader.Cbi;
    using NinjaTrader.Gui;
    using NinjaTrader.Gui.Chart;
    using NinjaTrader.Gui.SuperDom;
    using NinjaTrader.Data;
    using NinjaTrader.NinjaScript;
    using NinjaTrader.Core.FloatingPoint;
    using NinjaTrader.NinjaScript.Indicators;
    using NinjaTrader.NinjaScript.DrawingTools;
    #endregion

    //This namespace holds Strategies in this folder and is required. Do not change it.
    namespace NinjaTrader.NinjaScript.Strategies
    {
    public class LONGSHORTToolbarButtonsB9 : Strategy
    {
    private bool longButtonClicked;
    private bool shortButtonClicked;
    private System.Windows.Controls.Button longButton;
    private System.Windows.Controls.Button shortButton;
    private System.Windows.Controls.Grid myGrid;
    protected override void OnStateChange()
    {
    if (State == State.SetDefaults)
    {
    Description = @"LONGSHORTToolbarButtonsB9";
    Name = "LONGSHORTToolbarButtonsB9";
    Calculate = Calculate.OnEachTick;
    EntriesPerDirection = 1;
    EntryHandling = EntryHandling.AllEntries;
    IsExitOnSessionCloseStrategy = true;
    ExitOnSessionCloseSeconds = 30;
    IsFillLimitOnTouch = false;
    MaximumBarsLookBack = MaximumBarsLookBack.TwoHundredFiftySix;
    OrderFillResolution = OrderFillResolution.Standard;
    Slippage = 0;
    StartBehavior = StartBehavior.WaitUntilFlat;
    TimeInForce = TimeInForce.Gtc;
    TraceOrders = false;
    RealtimeErrorHandling = RealtimeErrorHandling.StopCancelClose;
    StopTargetHandling = StopTargetHandling.PerEntryExecution;
    BarsRequiredToTrade = 20;
    }
    else if (State == State.Configure)
    {
    }
    else if (State == State.Historical)
    {
    if (UserControlCollection.Contains(myGrid))
    return;

    Dispatcher.InvokeAsync((() =>
    {
    myGrid = new System.Windows.Controls.Grid
    {
    Name = "MyCustomGrid", HorizontalAlignment = HorizontalAlignment.Right, VerticalAlignment = VerticalAlignment.Top
    };

    System.Windows.Controls.ColumnDefinition column1 = new System.Windows.Controls.ColumnDefinition();
    System.Windows.Controls.ColumnDefinition column2 = new System.Windows.Controls.ColumnDefinition();

    myGrid.ColumnDefinitions.Add(column1);
    myGrid.ColumnDefinitions.Add(column2);

    longButton = new System.Windows.Controls.Button
    {
    Name = "LongButton", Content = "LONG", Foreground = Brushes.White, Background = Brushes.Green
    };

    shortButton = new System.Windows.Controls.Button
    {
    Name = "ShortButton", Content = "SHORT", Foreground = Brushes.Black, Background = Brushes.Red
    };

    longButton.Click += OnButtonClick;
    shortButton.Click += OnButtonClick;

    System.Windows.Controls.Grid.SetColumn(longButton, 0);
    System.Windows.Controls.Grid.SetColumn(shortButton , 1);

    myGrid.Children.Add(longButton);
    myGrid.Children.Add(shortButton);

    UserControlCollection.Add(myGrid);
    }));
    }
    else if (State == State.Terminated)
    {
    Dispatcher.InvokeAsync((() =>
    {
    if (myGrid != null)
    {
    if (longButton != null)
    {
    myGrid.Children.Remove(longButton);
    longButton.Click -= OnButtonClick;
    longButton = null;
    }
    if (shortButton != null)
    {
    myGrid.Children.Remove(shortButton);
    shortButton.Click -= OnButtonClick;
    shortButton = null;
    }
    }
    }));
    }
    }

    protected override void OnBarUpdate()
    {
    if (longButtonClicked
    && Close[1] >= MAX(High, 2)[2] + 0 * TickSize
    && High[1] < CurrentDayOHL().CurrentHigh[1]
    && High[0] > High[1])
    EnterLong();

    if (shortButtonClicked
    && Low[1] > CurrentDayOHL().CurrentLow[1]
    && Low[0] < Low[1]
    && Close[1] < MIN(Low, 2)[2] - 0 * TickSize)
    EnterShort();

    if (!longButtonClicked
    && Position.MarketPosition == MarketPosition.Long)
    ExitLong();

    if (!shortButtonClicked
    && Position.MarketPosition == MarketPosition.Short)
    ExitShort();
    }

    private void OnButtonClick(object sender, RoutedEventArgs rea)
    {
    System.Windows.Controls.Button button = sender as System.Windows.Controls.Button;
    if (button == longButton && button.Name == "LongButton" && button.Content == "LONG")
    {
    button.Content = "Exit L";
    button.Name = "ExitLongButton";
    longButtonClicked = true;
    return;
    }

    if (button == shortButton && button.Name == "ShortButton" && button.Content == "SHORT")
    {
    button.Content = "Exit S";
    button.Name = "ExitShortButton";
    shortButtonClicked = true;
    return;
    }

    if (button == longButton && button.Name == "ExitLongButton" && button.Content == "Exit L")
    {
    button.Content = "LONG";
    button.Name = "LongButton";
    longButtonClicked = false;
    return;
    }

    if (button == shortButton && button.Name == "ExitShortButton" && button.Content == "Exit S")
    {
    button.Content = "SHORT";
    button.Name = "ShortButton";
    shortButtonClicked = false;
    return;
    }
    }
    }
    }​
    Attached Files

    #2
    Hello daxtrader,

    The sample that you are using has buttons which toggle variables, those varaibles are used in OnBarUpdate to submit orders based on the conditions used in OnBarUpdate. The button simply enables the logic to begin working but won't immediately submit orders, the conditiosn need to become true before the orders are submitted. If you wanted to submit an order right away you would need to put the orders code inside the button event so it happens when the button is pressed. Because buttons are not NinjaScript events you would also need to surround the code in the button event with a TriggerCustomEvent if you need any price data for the order.

    Code:
    private void OnButtonClick(object sender, RoutedEventArgs rea)
    {​
        TriggerCustomEvent(o =>
       {
          // your code inside here
       }, null);​
    }
    https://ninjatrader.com/support/help...tsub=TriggerCu stomEvent

    Comment


      #3
      Hello Jesse,

      thanks for the answer.
      Got the code:

      TriggerCustomEvent(0 =>

      misspelled and inserted incorrectly because I get an error message.

      Thanks.​


      Code:


      private void OnButtonClick(object sender, RoutedEventArgs rea)

      TriggerCustomEvent(0 =>

      {
      System.Windows.Controls.Button button = sender as System.Windows.Controls.Button;
      if (button == longButton && button.Name == "LongButton" && button.Content == "LONG")
      {
      button.Content = "Exit L";
      button.Name = "ExitLongButton";
      longButtonClicked = true;
      return;
      }

      if (button == shortButton && button.Name == "ShortButton" && button.Content == "SHORT")
      {
      button.Content = "Exit S";
      button.Name = "ExitShortButton";
      shortButtonClicked = true;
      return;
      }

      if (button == longButton && button.Name == "ExitLongButton" && button.Content == "Exit L")
      {
      button.Content = "LONG";
      button.Name = "LongButton";
      longButtonClicked = false;
      return;
      }

      if (button == shortButton && button.Name == "ExitShortButton" && button.Content == "Exit S")
      {
      button.Content = "SHORT";
      button.Name = "ShortButton";
      shortButtonClicked = false;
      return;
      }
      }
      }
      }​

      Comment


        #4
        Hello daxtrader,

        What error message are you getting?

        The original code that is used in the button events to control the variables won't be needed if you are moving the order submission to the button event. You really just need the conditions to tell which of the buttons was clicked. You would need to add whatever order code you wanted into the button event for example EnterLong() if you wanted to place a long market order.

        Comment


          #5
          Have inserted it at this line.
          But that can't be right.
          get the error message:
          Type or namespace definition expected​

          Code:

          ​private void OnButtonClick(object sender, RoutedEventArgs rea)
          {
          TriggerCustomEvent(0 =>
          }
          System.Windows.Controls.Button button = sender as System.Windows.Controls.Button;

          if (button == longButton && button.Name == "LongButton" && button.Content == "LONG")
          {
          button.Content = "Exit L";
          button.Name = "ExitLongButton";
          longButtonClicked = true;
          return;
          }

          if (button == longButton && button.Name == "ExitLongButton" && button.Content == "Exit L")
          {
          button.Content = "LONG";
          button.Name = "LongButton";
          longButtonClicked = false;
          return;​

          Comment


            #6
            Hello daxtrader,

            The error is because this code is not valid. I would suggest to undo the changes you made and get back to a point where you can compile before doing anything else.

            The code for the button event would need to look exactly like what I had posted in post 2. I removed the other code from that so you can easily see the curly braces of the button event and also TriggerCustomEvent. Your code would go inside the TriggerCustomEvent where I had commented. You need to make sure that the curly braces { } which define the method body remain how they are shown in post 2, any extra code you add would go inside the curly braces

            Comment


              #7
              Hello Jesse,

              the code must look like this:

              {
              TriggerCustomEvent(0 =>
              }​

              Comment


                #8
                Hello daxtrader,

                No thats not correct. You are missing some of the code for TriggerCustomEvent. It needs to look exactly like this:

                Code:
                private void OnButtonClick(object sender, RoutedEventArgs rea)
                {​
                    TriggerCustomEvent(o =>
                   {
                         // your code inside here
                   }, null);​
                }​
                All of that is required to make the OnButtonClick valid and also TriggerCustomEvent valid. Any other code you want to add goes where I put // your code inside here

                Comment


                  #9
                  Hello Jesse,

                  is it true and have I inserted the code correctly.

                  Code:

                  private void OnButtonClick(object sender, RoutedEventArgs rea)
                  {
                  TriggerCustomEvent(0 =>
                  }
                  {
                  System.Windows.Controls.Button button = sender as System.Windows.Controls.Button;

                  if (button == longButton && button.Name == "ExitLongButton" && button.Content == "Exit L")
                  {
                  button.Content = "LONG";
                  button.Name = "LongButton";
                  longButtonClicked = false;
                  return;
                  }​

                  Comment


                    #10
                    Hello daxtrader,

                    No that is still not correct. You are not keeping the exact syntax that I had posted as an example. In NinjaScript we are using C# so the way the code is formatted is important.

                    The OnButtonClick method is what gets called when the button is clicked. A C# method requires an opening and closing curly brace:

                    Code:
                    private void OnButtonClick(object sender, RoutedEventArgs rea)
                    {​
                    
                    }
                    The TriggerCustomEvent is a NinjaScript method which takes a function as a parameter so that it can execute that code in the NinjaScript engine. That requires an opening and closing brace as well but it inside the methods parameters, that looks like the following:

                    Code:
                    TriggerCustomEvent(o =>
                    {
                    
                    }, null);​
                    The , null); at the end is one of the other parameters of TriggerCustomEvent so that part is necessary.

                    When you combine those two pieces of code it needs to look exactly like this:

                    Code:
                    private void OnButtonClick(object sender, RoutedEventArgs rea)
                    {​
                        TriggerCustomEvent(o =>
                        {
                    
                        }, null);​
                    }​

                    To check for one of the buttons it would look like the following:

                    Code:
                    private void OnButtonClick(object sender, RoutedEventArgs rea)
                    {​
                       System.Windows.Controls.Button button = sender as System.Windows.Controls.Button;
                    ​
                       TriggerCustomEvent(o =>
                       {
                          if (button == longButton)
                           {​
                                  //your order submission here
                            }
                       }, null);​
                    }​​​​
                    Last edited by NinjaTrader_Jesse; 11-18-2022, 09:12 AM.

                    Comment


                      #11
                      Hello Jesse,

                      thank you very much for your patience.
                      I have now added it to the code like this.
                      Is that correct.​

                      CODE:

                      private void OnButtonClick(object sender, RoutedEventArgs rea)
                      {
                      System.Windows.Controls.Button button = sender as System.Windows.Controls.Button;

                      {​
                      TriggerCustomEvent(o =>
                      {
                      if (button == longButton && button.Name == "LongButton" && button.Content == "BUY")
                      {
                      button.Content = "Exit L";
                      button.Name = "ExitLongButton";
                      longButtonClicked = true;
                      return;
                      }
                      }, null);​
                      }​

                      Comment


                        #12
                        Hello daxtrader,

                        You have extra curly braces in what you provided and you are adding the code that was previously used to control the variables which is not needed for what you are trying to do. I would suggest to just copy/paste the example that I had provided in the last post as a starting point, that's got the correct braces and is ready to be used as a condition for the long button.

                        Comment


                          #13
                          Hallo Jesse,

                          thanks,it works.

                          it works.
                          Wouldn't it be simpler and make more sense to program the code as an indicator, instead of a strategy​.

                          Comment


                            #14
                            Hello daxtrader,

                            No that would be more complex because you would need to use the addon framework to do that. The strategy would be less code used to submit orders and you already have a sample that adds the buttons and events.

                            Comment


                              #15
                              Hello Jesse,

                              the Buy_Sell button is now in the upper right corner.
                              If I move the button 1 inch further down and 1 inch to the left
                              how do I have to change the code.

                              This is how the code looks now:



                              ​Dispatcher.InvokeAsync((() =>
                              {
                              myGrid = new System.Windows.Controls.Grid
                              {
                              Name = "MyCustomGrid", HorizontalAlignment = HorizontalAlignment.Right, VerticalAlignment = VerticalAlignment.Top
                              };

                              System.Windows.Controls.ColumnDefinition column1 = new System.Windows.Controls.ColumnDefinition();
                              System.Windows.Controls.ColumnDefinition column2 = new System.Windows.Controls.ColumnDefinition();

                              myGrid.ColumnDefinitions.Add(column1);
                              myGrid.ColumnDefinitions.Add(column2);

                              longButton = new System.Windows.Controls.Button
                              {
                              Name = "LongButton", Content = "BUY", Foreground = Brushes.White, Background = Brushes.Green, //Width = 50
                              };

                              shortButton = new System.Windows.Controls.Button
                              {
                              Name = "ShortButton", Content = "SELL", Foreground = Brushes.Black, Background = Brushes.Red //, Width = 50, MaxWidth = 500
                              };​

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by NullPointStrategies, Today, 05:17 AM
                              0 responses
                              50 views
                              0 likes
                              Last Post NullPointStrategies  
                              Started by argusthome, 03-08-2026, 10:06 AM
                              0 responses
                              126 views
                              0 likes
                              Last Post argusthome  
                              Started by NabilKhattabi, 03-06-2026, 11:18 AM
                              0 responses
                              69 views
                              0 likes
                              Last Post NabilKhattabi  
                              Started by Deep42, 03-06-2026, 12:28 AM
                              0 responses
                              42 views
                              0 likes
                              Last Post Deep42
                              by Deep42
                               
                              Started by TheRealMorford, 03-05-2026, 06:15 PM
                              0 responses
                              46 views
                              0 likes
                              Last Post TheRealMorford  
                              Working...
                              X