Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Error Message in OnBarUpdate Event

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

    #16
    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.Gui.Tools;
    using NinjaTrader.Data;
    using NinjaTrader.NinjaScript;
    using NinjaTrader.Core.FloatingPoint;
    using NinjaTrader.NinjaScript.DrawingTools;
    #endregion
    
    //This namespace holds Indicators in this folder and is required. Do not change it.
    namespace NinjaTrader.NinjaScript.Indicators.AlanIndicators
    {
        public class PrintChartTraderSelectedAccount : Indicator
        {
    
            NinjaTrader.Gui.Tools.AccountSelector xAlselector;
            
            protected override void OnStateChange()
            {
                if (State == State.SetDefaults)
                {
                    Description                                    = @"Enter the description for your new custom Indicator here.";
                    Name                                        = "PrintChartTraderSelectedAccount";
                    Calculate                                    = Calculate.OnBarClose;
                    IsOverlay                                    = false;
                    DisplayInDataBox                            = true;
                    DrawOnPricePanel                            = true;
                    DrawHorizontalGridLines                        = true;
                    DrawVerticalGridLines                        = true;
                    PaintPriceMarkers                            = true;
                    ScaleJustification                            = NinjaTrader.Gui.Chart.ScaleJustification.Right;
                    //Disable this property if your indicator requires custom values that cumulate with each new market data event.
                    //See Help Guide for additional information.
                    IsSuspendedWhileInactive                    = true;
                }
                else if (State == State.Configure)
                {
                }
                else if (State == State.DataLoaded)
                {
    
                }
            
                
            }
        
    
            protected override void OnBarUpdate()
            {
                ChartControl.Dispatcher.InvokeAsync((Action)(() =>
                {
                            //You have to put the stuff below within this ChartControl.Dispatcher.InvokeAsync((Action)(() =>, because you are trying to access something on a different thread.
                            xAlselector = Window.GetWindow(ChartControl.Parent).FindFirst("ChartTraderControlAccountSelector") as NinjaTrader.Gui.Tools.AccountSelector;
                            Print(xAlselector.SelectedAccount.ToString());
                }));
            
        
            }
        
        }
    }
    
    #region NinjaScript generated code. Neither change nor remove.
    
    namespace NinjaTrader.NinjaScript.Indicators
    {
        public partial class Indicator : NinjaTrader.Gui.NinjaScript.IndicatorRenderBase
        {
            private AlanIndicators.PrintChartTraderSelectedAccount[] cachePrintChartTraderSelectedAccount;
            public AlanIndicators.PrintChartTraderSelectedAccount PrintChartTraderSelectedAccount()
            {
                return PrintChartTraderSelectedAccount(Input);
            }
    
            public AlanIndicators.PrintChartTraderSelectedAccount PrintChartTraderSelectedAccount(ISeries<double> input)
            {
                if (cachePrintChartTraderSelectedAccount != null)
                    for (int idx = 0; idx < cachePrintChartTraderSelectedAccount.Length; idx++)
                        if (cachePrintChartTraderSelectedAccount[idx] != null &&  cachePrintChartTraderSelectedAccount[idx].EqualsInput(input))
                            return cachePrintChartTraderSelectedAccount[idx];
                return CacheIndicator<AlanIndicators.PrintChartTraderSelectedAccount>(new AlanIndicators.PrintChartTraderSelectedAccount(), input, ref cachePrintChartTraderSelectedAccount);
            }
        }
    }
    
    namespace NinjaTrader.NinjaScript.MarketAnalyzerColumns
    {
        public partial class MarketAnalyzerColumn : MarketAnalyzerColumnBase
        {
            public Indicators.AlanIndicators.PrintChartTraderSelectedAccount PrintChartTraderSelectedAccount()
            {
                return indicator.PrintChartTraderSelectedAccount(Input);
            }
    
            public Indicators.AlanIndicators.PrintChartTraderSelectedAccount PrintChartTraderSelectedAccount(ISeries<double> input )
            {
                return indicator.PrintChartTraderSelectedAccount(input);
            }
        }
    }
    
    namespace NinjaTrader.NinjaScript.Strategies
    {
        public partial class Strategy : NinjaTrader.Gui.NinjaScript.StrategyRenderBase
        {
            public Indicators.AlanIndicators.PrintChartTraderSelectedAccount PrintChartTraderSelectedAccount()
            {
                return indicator.PrintChartTraderSelectedAccount(Input);
            }
    
            public Indicators.AlanIndicators.PrintChartTraderSelectedAccount PrintChartTraderSelectedAccount(ISeries<double> input )
            {
                return indicator.PrintChartTraderSelectedAccount(input);
            }
        }
    }
    
    #endregion​
    I don't understand this code. I can understand first part but no others. I want to place orders and handle it in the selected account of ChartTrader. Should I add any code in the StrategyRenderBase Part? How can I integrate my strategy with this code? Please help me in this problem.

    Comment


      #17
      If I develop an Add-On, can use AccountSelector, but I can't implement my logic. My strategy works well. So is there any way to integrate my existing strategy and Add-On?

      Comment


        #18
        Also, when I make changes to an order, sometimes the order status is pending changes, and then I can't handle the order anymore. Please let me know how I can fix this issue.

        Comment


          #19
          Hello Rogers101,

          If you are having problems understanding code I would suggest to focus efforts on that first before trying to create a custom project.

          The code you copied is used to find the account selector in the chart from code. That is all that code does. A strategy cannot make use of that account, the account the strategy will use is the account you select when configuring the strategy.

          If you are seeing orders get stuck on the sim account that would be due to logic used. To clear the orders you can right click -> edit the sim account and reset it. To avoid that happening you would need to change your strategies logic so it does not do the conflicting action which leads to the stuck order.



          Comment


            #20
            Thanks for your reply. Can you review my code? I am sorry but this is my first NinjaScript so there are a lot of errors.


            And I will appreciate if you provide a suggestion how I can convert this strategy to use selected account like Add-On or Indicator.

            Thank you in advance.

            CustomStrategy.cs

            Comment


              #21
              Hello Rogers101,

              Our support does not provide code review services, we can answer specific questions that you have

              Comment


                #22
                Oh that sounds bad

                Comment


                  #23
                  Please help me how can I place and handle orders on the selected account of ChartTrader. I'm not sure if I need to develop an add-on, an indicator or a strategy, what's clear is that I can't do that in the strategy as I select the account when I import the strategy.

                  Comment


                    #24
                    Hello Rogers101,

                    An indicator using the addon framework can use the account from the account selector. You would use the account based methods shown in the help guide if you wanted to do actions on that account. There is a sample of working with an account here:


                    Comment

                    Latest Posts

                    Collapse

                    Topics Statistics Last Post
                    Started by NullPointStrategies, Yesterday, 05:17 AM
                    0 responses
                    65 views
                    0 likes
                    Last Post NullPointStrategies  
                    Started by argusthome, 03-08-2026, 10:06 AM
                    0 responses
                    139 views
                    0 likes
                    Last Post argusthome  
                    Started by NabilKhattabi, 03-06-2026, 11:18 AM
                    0 responses
                    75 views
                    0 likes
                    Last Post NabilKhattabi  
                    Started by Deep42, 03-06-2026, 12:28 AM
                    0 responses
                    45 views
                    0 likes
                    Last Post Deep42
                    by Deep42
                     
                    Started by TheRealMorford, 03-05-2026, 06:15 PM
                    0 responses
                    50 views
                    0 likes
                    Last Post TheRealMorford  
                    Working...
                    X