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

Long/Short Button click through Code

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

    Long/Short Button click through Code

    Hello,

    First post here. Not sure if this is something I'm able to get help with but figured I'll try anyways.
    Using the Long/Short button found in the ecosystem: https://ninjatraderecosystem.com/use...bar-buttons-2/

    Was able to modify to make a Scalp/Price action entry order button. Everything is done and seems to be working. But there is one thing with the button that I've been having trouble figuring out.

    Right now the button, once clicked. Stays live until it is manually clicked again. I'm trying to figure out a way to automatically revert the button back to the "Non-Live" Position once my account is no longer flat/ in a trade(3rd Picture)

    I don't have much experience coding. Been trying to teach myself these last few months. Any help with this will be greatly appreciated!

    Thankyou!

    Click image for larger version

Name:	TSLA BeforeTrade.png
Views:	96
Size:	74.7 KB
ID:	1207432​​​​​​
    Click image for larger version

Name:	TSLA LIVE.png
Views:	90
Size:	89.9 KB
ID:	1207433
    Click image for larger version

Name:	TSLA inTrade.png
Views:	88
Size:	117.5 KB
ID:	1207434





    Attached Files

    #2
    Hello TradeSimple(Dre),

    Thanks for your post.

    You will want to change the Content property of the button to change the displayed text. You will need to use a dispatcher here, because the buttons will reside on the UI thread and your trigger will need to come from another NinjaScript event on a different thread.

    I would recommend using OnExecutionUpdate, and checking when Position.MarketPosition becomes MarketPosition.Flat to set the test to something that reflects that you are flat.

    Below is an example:

    Code:
    protected override void OnExecutionUpdate(Execution execution, string executionId, double price, int quantity, MarketPosition marketPosition, string orderId, DateTime time)
    {
        if (Position.MarketPosition == MarketPosition.Flat)
        {
            ChartControl.Dispatcher.InvokeAsync(new Action(() => {
                longButton.Content = "Test - Flat";
            }));
        }
    }
    We look forward to assisting.
    JimNinjaTrader Customer Service

    Comment


      #3
      Hi Jim,

      Thank you so much for the reply, your help is greatly appreciated!

      I was able to successfully change the name of the button and turn off the "Live" part of it using the example above.
      However, now I'm trying to work out how to make the button useable again once the position is Flat.

      What I'm trying to do is:

      1. User Pushes button - Button changes to "Live" until conditions to enter are met or button is pushed again.
      2. Conditions are met, Enters Long - Button becomes disabled ("Test - Flat")
      3. Once Position is Flat - Return button back so user can push it again. ("Long" position)

      I got the first 2 steps down. Just been having trouble figuring out how to make the last step happen. I switched it back so it says "LONG" but the button stops working once the initial position exits.

      Not sure if I'm on the right track here but this is what I got so far. Any suggestions on how to get the button to work again would be amazing.

      Thank you again for your help!

      Code:
      protected override void OnExecutionUpdate(Execution execution, string executionId, double price, int quantity, MarketPosition marketPosition, string orderId, DateTime time)
      {
      if (Position.MarketPosition != MarketPosition.Flat)
      {
      ChartControl.Dispatcher.InvokeAsync(new Action(() => {
      longButton.Content = "Test - Flat";
      longButtonClicked = false;
      }));
      }
      
      if (Position.MarketPosition == MarketPosition.Flat)
      {
      ChartControl.Dispatcher.InvokeAsync(new Action(() => {
      longButton.Content = "LONG";
      }));
      }
      }

      Comment


        #4
        Hello TradeSimple(Dre)

        Checking when Position.MarketPosition becomes MarketPosition.Flat in OnExecutionUpdate will be the first place you can check when the strategy sees it is in a flat position.

        Prints can be used to verify that you code has reached these sections and can also be used to check various conditions in your code.

        For example, a print could tell you that the code has reached X point where some actions take place, and if that print is not seen, check the conditions used to control that action - What are the variable values and what do they need to be in order for those conditions to become true and have the action take place?

        Debugging tips — https://ninjatrader.com/support/help...script_cod.htm
        JimNinjaTrader Customer Service

        Comment


          #5
          Hi NinjaTrader_Jim,

          Just wanted to thank you for your help with this. This helped tremendously and led me in the right direction!

          In case anyone finds themselves googling this specific question. What I ended up doing is using the OnExecutionUpdate above and using the dispatcher to bring back the same buttons.
          Not sure if this is the proper method to do this, but ended up working exactly how I needed it to.

          This is the code I used to solve the issue I was having.

          Code:
          protected override void OnExecutionUpdate(Execution execution, string executionId, double price, int quantity, MarketPosition marketPosition, string orderId, DateTime time)
          {
          if (Position.MarketPosition != MarketPosition.Flat)
          
          {
          
          ChartControl.Dispatcher.InvokeAsync(new Action(() =>
          {
          longButton.Content = "Disabled L";
          longButtonClicked = false;
          
          
          shortButton.Content = "Disabled S";
          shortButtonClicked = false;
          
          }));
          
          }
          
          if (Position.MarketPosition == MarketPosition.Flat)
          
          {
          
          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;
          }
          }
          }));
          }
          }
          If anyone is interested in the full source code for this order Entry button/trade management system. You can now download it from the ecosystem:
          https://ninjatraderecosystem.com/use...entry-buttons/

          Comment


            #6
            Hi TradeSimple(Dre),

            I also want to provide a better example that is available on the forums.

            https://ninjatrader.com/support/foru...18#post1126818
            Chelsea B.NinjaTrader Customer Service

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by crystalet, Yesterday, 12:19 PM
            3 responses
            33 views
            0 likes
            Last Post crystalet  
            Started by Orogpd, Today, 03:26 AM
            0 responses
            1 view
            0 likes
            Last Post Orogpd
            by Orogpd
             
            Started by cbdgrenhouseusa, Today, 03:17 AM
            0 responses
            1 view
            0 likes
            Last Post cbdgrenhouseusa  
            Started by cbdgrenhouseusa, Today, 03:16 AM
            0 responses
            1 view
            0 likes
            Last Post cbdgrenhouseusa  
            Started by ラリー, 09-05-2022, 02:02 PM
            62 responses
            513 views
            0 likes
            Last Post ラリー  
            Working...
            X