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

Access data Series and Position data from UI Button click Events

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

    Access data Series and Position data from UI Button click Events

    Hello all!

    In my strategy, I create several UI elements including buttons.
    If I understand correctly, then indicator data (e.g. atr), price data (OHLC), Volume and position status are not always available from the thread where the buttons are running.
    How can I get a guaranteed access to this data from UI Event or Timer Event?​

    #2
    Hello tanas.eduard,

    Thanks for your post.

    The TriggerCustomEvent() method should be used to use your own custom events (such as a Timer object) so that internal NinjaScript indexes and pointers are correctly set prior to processing user code triggered by your custom event. When calling this event, NinjaTrader will synchronize all internal pointers and then call your custom event handler where your user code is located.

    See this help guide page for more information about TriggerCustomEvent() and sample code: https://ninjatrader.com/support/help...ustomevent.htm

    Let me know if I may assist further.
    Brandon H.NinjaTrader Customer Service

    Comment


      #3
      Thanks! We wiil learn this method, i think this is all we need.

      Comment


        #4
        Hey tanas, I have had the same problem. I found running a AddDataSeries of 1-Second and just processing the buttons on close of bar that is quick enough and everything is addressable. Hope this helps.

        Comment


          #5
          Hello tanas.eduard,

          Thanks for your notes.

          You may view the SampleWPFModifications reference sample from the NinjaTrader 8 help guide for an example that adds event handler methods to buttons.

          SampleWPFModifications: https://ninjatrader.com/support/help...ui)-modifi.htm

          The ChartOrderButtonExamples script located on this help guide page might also be helpful to you view: https://forum.ninjatrader.com/forum/...18#post1126818

          Please let me know if I may assist further.
          Brandon H.NinjaTrader Customer Service

          Comment


            #6
            Originally posted by kenz987 View Post
            Hey tanas, I have had the same problem. I found running a AddDataSeries of 1-Second and just processing the buttons on close of bar that is quick enough and everything is addressable. Hope this helps.
            Nice Cheat!!!! Thanks!

            Comment


              #7
              Originally posted by NinjaTrader_BrandonH View Post
              Hello tanas.eduard,

              Thanks for your notes.

              You may view the SampleWPFModifications reference sample from the NinjaTrader 8 help guide for an example that adds event handler methods to buttons.

              SampleWPFModifications: https://ninjatrader.com/support/help...ui)-modifi.htm

              The ChartOrderButtonExamples script located on this help guide page might also be helpful to you view: https://forum.ninjatrader.com/forum/...18#post1126818

              Please let me know if I may assist further.
              Thanks for example! I think is very cool example in thread.

              Comment


                #8
                Originally posted by NinjaTrader_BrandonH View Post
                Hello tanas.eduard,

                Thanks for your notes.

                You may view the SampleWPFModifications reference sample from the NinjaTrader 8 help guide for an example that adds event handler methods to buttons.

                SampleWPFModifications: https://ninjatrader.com/support/help...ui)-modifi.htm

                The ChartOrderButtonExamples script located on this help guide page might also be helpful to you view: https://forum.ninjatrader.com/forum/...18#post1126818

                Please let me know if I may assist further.


                Hi Brandon.
                I try to use TriggerCustomEvent, but in some cases have error.
                So i have OnClickEvent and in this event:
                PHP Code:
                  TriggerCustomEvent(=> { PlaceExitMar****rder(); }, null); 
                The function PlaceExitMar****rder have some logic but when they try access Position.Quantity - most times is zero.
                Can you help me and what the problem?

                Code of PlaceExitMar****rder
                PHP Code:
                 private void PlaceExitMar****rder()
                        {
                            
                LogEvent("PlaceExitMar****rder""Entry");

                            
                CancelAllOrders();
                            
                ClearClosedOrders();

                            
                logDic.Add("Position.Quantity"Position.Quantity);

                            
                LogEvent("PlaceExitMar****rder""Work""Place exit order"logDic);

                            
                _trade.StopLoss.Quantity Position.Quantity;
                            
                _trade.StopOrderPlaced true;

                            if (
                IsLong)
                                
                _orderSL SubmitOrderUnmanaged(0OrderAction.SellOrderType.MarketPosition.Quantity00""_orderNameSL);

                            if (!
                IsLong)
                                
                _orderSL SubmitOrderUnmanaged(0OrderAction.BuyToCoverOrderType.MarketPosition.Quantity00""_orderNameSL);

                            
                LogEvent("PlaceExitMar****rder""Exit");
                        }&
                #8203; 

                Comment


                  #9
                  Hello tanas.eduard,

                  Thanks for your notes.

                  What you could consider doing is using OnOrderUpdate() to check if the order.Name is equal to your entry order's SignalName that was placed and check if the orderState of the order is OrderState.Filled. Then, you could save the quantity of the order to a variable to use later in the script. This will allow you to get the position quantity of the order once the order was placed.

                  OnOrderUpdate() is an event driven method which is called each time an order managed by a strategy changes state. An order will change state when a change in order quantity, price or state (working to filled) occurs.

                  See this help guide page for more information about OnOrderUpdate(): https://ninjatrader.com/support/help...rderupdate.htm

                  Let me know if I may assist further.
                  Brandon H.NinjaTrader Customer Service

                  Comment


                    #10
                    Originally posted by NinjaTrader_BrandonH View Post
                    Hello tanas.eduard,

                    Thanks for your notes.

                    What you could consider doing is using OnOrderUpdate() to check if the order.Name is equal to your entry order's SignalName that was placed and check if the orderState of the order is OrderState.Filled. Then, you could save the quantity of the order to a variable to use later in the script. This will allow you to get the position quantity of the order once the order was placed.

                    OnOrderUpdate() is an event driven method which is called each time an order managed by a strategy changes state. An order will change state when a change in order quantity, price or state (working to filled) occurs.

                    See this help guide page for more information about OnOrderUpdate(): https://ninjatrader.com/support/help...rderupdate.htm

                    Let me know if I may assist further.
                    Hello Brandon.
                    I know about this method by in my strategy i use IB connection and can't correct use OnOrderUpdate with stocks.
                    At this time i need use a TriggerCustomEvent for sync all data and access position from Button event.
                    Can i do this and if can - how?

                    Comment


                      #11
                      Hello tanas.eduard,

                      If the orders are being submitted through the strategy using native strategy methods, all orders will updated in OnOrderUpdate(), including Equity instruments (stocks).

                      Try printing the order object in OnOrderUpdate().

                      Code:
                      protected override void OnOrderUpdate(Order order, double limitPrice, double stopPrice, int quantity, int filled, double averageFillPrice, OrderState orderState, DateTime time, ErrorCode error, string comment)
                      {
                      Print(order.ToString());
                      }


                      The position is not a series, and does not need to be synchronized with TriggerCustomEvent(). This is necessary for using Series information such as the Close series or the Time series, or an indicator plot Value series.

                      Below is a link to a video that demonstrates.
                      Chelsea B.NinjaTrader Customer Service

                      Comment

                      Latest Posts

                      Collapse

                      Topics Statistics Last Post
                      Started by cre8able, Today, 03:20 PM
                      1 response
                      9 views
                      0 likes
                      Last Post cre8able  
                      Started by fiddich, Today, 05:25 PM
                      0 responses
                      3 views
                      0 likes
                      Last Post fiddich
                      by fiddich
                       
                      Started by gemify, 11-11-2022, 11:52 AM
                      6 responses
                      804 views
                      2 likes
                      Last Post ultls
                      by ultls
                       
                      Started by ScottWalsh, Today, 04:52 PM
                      0 responses
                      4 views
                      0 likes
                      Last Post ScottWalsh  
                      Started by ScottWalsh, Today, 04:29 PM
                      0 responses
                      9 views
                      0 likes
                      Last Post ScottWalsh  
                      Working...
                      X