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

Undocumented/Unsupported Ninja Tips and Tricks

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

    Export to the Output Window from the graph

    Good Morning,
    I would like to write code with Ninja that allows you to export the values ​​of the candle (Close-Open-High-Low) in this way:
    I open the graph and load the code, then I click with the mouse on the candle to export the values ​​on Output windows.
    You can do it?
    Thank you.
    Roberto

    Comment


      Reply to Post #152

      Originally posted by Trader_55 View Post
      Excellent! This is good enough. For my purposes however, it would also help if I could somehow detect if the indicator code itself was executing inside a workspace that was currently the visually displayed one.

      In other words, supposed there were two workspaces name "WS1" and "WS2". Supposed two different charts were loaded, one in WS1, and one in WS2. Suppose WS1 was currently being displayed on screen, and WS2 was hidden. Suppose the charts were running the exact same indicator code.

      I'd like to be able to do the following in the indicator code:

      if ( /* running in the currently visible workspace, whether it is WS1 or WS2 */ )
      {
      // execute indicator code
      }
      else
      {
      // return and do nothing
      }

      This way, I could save CPU cycles by making the indicator calculate only within the currently visible workspace.

      Is there something in the WorkspaceOptions class that would help me do this? I could already run a hack by comparing the currently displayed workspace name to the current instrument loaded and run one workspace per instrument (thereby detecting if the code is actually executing in the currently visible workspace), but a better way of doing it would also be helpful.
      Sorry I didn't see this earlier. Trader_55, you've probably already solved your problem by now and/or moved on to other things, but here's an answer to your question.

      You can already do what you want with the call to NinjaTrader.Gui.WorkspaceOptions.GetCurrentFilenam e(). Just set some variable like originalWorkspaceFileName in OnStartUp() to hold the file name of the workspace that houses the indicator/strategy. The workspace that houses the indicator/strategy will be the current workspace at this time/call. Then check for the current file name of the workspace in OnBarUpdate() to see which workspace is currently being displayed on screen when the OnBarUpdate() of the indicator/strategy is called. If the two values match, execute your indicator code. Else, do nothing.

      To make things explicit and very readable, you could code it something like this:

      Code:
      private string originalWorkspaceFileName, currentWorkspaceFileName; 
      
      protected override void OnStartUp()  {[INDENT]originalWorkspaceFileName = NinjaTrader.Gui.WorkspaceOptions.GetCurrentFilename();
      [/INDENT]}
      
      protected override void OnBarUpdate() {[INDENT]currentWorkspaceFileName = NinjaTrader.Gui.WorkspaceOptions.GetCurrentFilename();
      
      if (currentWorkspaceFileName == originalWorkspaceFileName) {[INDENT]// execute indicator code
      [/INDENT]}
      
      // else { return and do nothing}
      [/INDENT]}
      Last edited by flonkle; 08-13-2014, 10:57 AM.

      Comment


        Free tools for "peeking under the hood" of NT7

        Some free tools that you can use to "peek under the hood" of NT7 are a free code profiling tool called "Microsoft FX Cop" and any of the free Visual Studio Express IDE's for C#.

        All of these tools have an object browser that will show you NT7's underlying class/object hierarchy/tree. Using calls to the objects/methods/properties shown in these object browsers will go a long way towards allowing you to perform tasks that are officially "unsupported" by NT.

        Here's a thread with links to both Microsoft FX Cop and to the Visual Studio Express IDE's:



        Check out Post #7 of the above thread to learn about how to install and use Microsoft FX Cop. (Post #7 actually takes you to an external thread at bigmiketrading that explains this.)

        Check out Post #8 of the above thread for links to the free Visual Studio Express IDE's that you can also use to explore the underlying structure of NT7.
        Last edited by flonkle; 08-13-2014, 11:18 AM.

        Comment


          What is the best way to structuring custom classes

          NT Forum,

          My question would likely have been faced by most developers at some point. What is the best way to structure custom classes for quick writing, eased debugging, improved reliability & increased reusability?

          I was of the understanding creating a new project for Business Logic (BL) (as per the attached screenshot) was the approach to take... now I'm not so sure. It is effective for creating custom classes which can be instantiated by classes within NT.Custom.Indicators. However, custom classes can not access properties (e.g. High[0], SMA(20)[0]) and methods from the class Indicator. What is the best way to structure custom code to access these properties from a project outside of the project NT.Custom? Alternatively, is there an entirely different approach I should be taking?

          By way of example, the code (below and attached) compiles and runs. It demonstrates dependency inversion within the namespace NT.Indicator within the project NT.Custom. However, my problem presents where:
          - a new project "NT.Custom.BL" is added to the solution NT.Custom
          - a reference is added to the project "NT.Custom.BL" pointing to NT.Core
          - a reference is added to the project "NT.Custom pointing" to "NT.Custom.BL" (within VS2013 and via the NinjaScript window within NT)
          - the class SubClass (in the below code) is moved from the project NT.Custom to the project NT.Custom.BL.

          As the reference added to NT.Custom points to NT.Custom.BL, a reference can not be added to NT.Custom.BL pointing back to NT.Custom. Without such a reference, NT.Custom.BL can not resolve the symbol 'StandardIndicator' in the constructor of the class SubClass. How can I appropriately access High[0] from a class outside of NT.Custom?

          Code:
          namespace NinjaTrader.Indicator
          {
            [Description("Enter the description of your new custom indicator here")]
            public class StandardIndicator : Indicator
            {
              private SubClass _subClass;
          
              protected override void Initialize()
              {
                Overlay = false;
              }
          
              protected override void OnStartUp()
              {
                _subClass = new SubClass(this);
              }
          
              protected override void OnBarUpdate()
              {
                var stdClassHigh = High[0];
                var subClassHigh = _subClass.GetHighUsingDependancyInversion();
                if (Math.Abs(stdClassHigh - subClassHigh) < 0.01)
                  Print(stdClassHigh.ToString("n") + " = " + subClassHigh.ToString("n"));
              }
            }
          
            /* Unlike the class StandardIndicator, the class SubClass does not inherit from the class Indicator.  
             * As such, it can not access any methods or properties from the class Indicator or in turn IndicatorBase */ 
            public class SubClass
            {
              /* The GetHigh method does not compile.  
               * Can not resolve the symbol 'High' */
              public double GetHigh()
              {
                //return High[0];
                return 0.00;
              }
          	
              /* Using Dependency Inversion, methods and properties from the class Indicator can be accessed.
               * This includes any methods in the class UserDefinedMethods. */
              private StandardIndicator _ind;
          
              public SubClass(StandardIndicator indicator)
              {
                _ind = indicator;
              }
          
              public double GetHighUsingDependancyInversion()
              {
                return _ind.High[0];
              }
            }
          }
          This has also been posted in the BMT forum.

          Thanks & regards
          Shannon
          Attached Files
          Last edited by Shansen; 08-21-2014, 10:57 PM. Reason: Added BMT link

          Comment


            Get account orders for modification

            Originally posted by DaveE View Post
            I have found that placing enough orders from NT strategy to IB will result in IB Account getting out of sync with Strategy.
            For instance in Managed using a strategy that reverses position will cause overfill after enough trades.
            Using Unmanaged strategy, if connection goes down while a limit order created by the strategy is active (and executes while connection down) then the strategy position gets out of sync when connection returns. In this case NT on reconnection will mark the limit order as status 'unknown' and unfilled (even though it IS filled on the server).
            In order to be able to code a correct recovery we need to be able to differentiate the Account Orders that were created by our strategy from those created by something else (like TWS or another strategy). Here's a start at the code:

            Code:
            Cbi.OrderCollection aoc = Account.Orders;
                        
                        for (int iaoc = 0; iaoc < aoc.Count; iaoc++) 
                        {
                            Cbi.Order ao = aoc[iaoc];
                            if (ao.Instrument.MasterInstrument.Name == Instrument.MasterInstrument.Name)
                            {//limit to account orders matching the strategy's instrument
                                Print(string.Format("Instrument.MasterInstrument.Name={0}, OrderType={1}, Account.Name={2}, OrderState={3}, Quantity={4}, LimitPrice={5},StopPrice={6}, Filled={7}, Info={8}",ao.Instrument.MasterInstrument.Name, ao.OrderType, ao.Account.Name, ao.OrderState, ao.Quantity, ao.LimitPrice, ao.StopPrice, ao.Filled, ao.Info));
                                Print(string.Format("  Name={0}, Oco={1}, OverFill={2}, TerminatedState={3}, Token={4}, UserLink={5}, Gtd={6}, OrderId={7}, Provider={8}, Time={9}",ao.Name, ao.Oco, ao.OverFill, ao.TerminatedState, ao.Token, ao.UserLink, ao.Gtd, ao.OrderId, ao.Provider, ao.Time));
                                Print(ao.ToString());
                                                    Cbi.Order mso = this.Orders.FindByToken(ao.Token);//matching strategy order (null if not found)
                                    if (mso!=null) Print("   token indicates there's a matching strategy order");
                                Print("");
                            }
                        }
            So we can match account orders to our strategy orders.
            You might think this would allow you to recover correct order status after a reconnect.
            However in the case of IB: After a reconnect the way NT currently works is to change the status of orders, that were executed at IB during disconnect,to 'unknown'. This happens both to Account.Orders and to the strategies orders (this.Orders). This means that we don't seem to have a way to find, in code, out the true status of the orders (that were working at disconnection) after reconnection.
            TWS does have the correct status (filled), and the execution is even recorded in NT's executions.txt file (after the reconnection), but no OnExecution() event is raised.
            Does anyone know an undocumented method to query the real (TWS) status of an order?
            Hi Dave

            Thanks for this; it works well for finding orders on the account. However, methods that work with orders, like ChangeOrder(), require an IOrder object.

            Does anyone know if there is a way of get IOrder objects for existing account orders? Or is there a way of getting order methods to work with Order objects?

            I've tried a simple cast, but that doesn't work. I've tried assigning the Order.Token to IOrder.Token, but it seems IOrder properties are read-only.

            All of this is so a re-started strategy can manage a trailing stop-loss even if it was not originally placed by the strategy.

            I may be able to work this out myself, but I don't know how to see inside NinjaTrader.Cbi. Any pointers?

            Cheers
            Tony

            Comment


              Get performance for all my strategies

              Hi,

              I want to calculate my potential performance of all my strategies together.
              Like Performance.AllTrades.TradesPerformance.Currency.C umProfit for all my strategies or to my account.
              how to do so?

              Thank you very much!
              Anat

              Comment


                Hello,

                Thank you for the question.

                In the future please post new questions as new threads rather than adding to old ones.

                For your question,

                From NinjaScript this would not really be possible, each strategy is its own instances so there is not really a way to pull data from a different strategy for calculations.

                You could potentially write each strategies performance to a file for viewing, we have some examples of writing to file here:



                I look forward to being of further assistance.
                JesseNinjaTrader Customer Service

                Comment


                  anata,
                  This probably isn't something you want to do in NT or in C# for that matter. 1) You can do a custom report quite easily using a language such as R (see data frame, data.table, xts, blotter packages) or Python (pandas), 2) NT reports are very basic and limited.

                  Comment


                    Is there s way now to display calculated info and results as an overlay on the main chart window? More like a dashboard on the upper right corner?

                    Any pointers/tips will be appreciated.

                    Comment


                      Use DrawTextFixed.

                      Separate multiple lines with '\n'.

                      More info here and here.
                      Last edited by bltdavid; 04-23-2020, 11:03 PM.

                      Comment

                      Latest Posts

                      Collapse

                      Topics Statistics Last Post
                      Started by Haiasi, Today, 06:53 PM
                      1 response
                      3 views
                      0 likes
                      Last Post NinjaTrader_Manfred  
                      Started by ScottWalsh, Today, 06:52 PM
                      1 response
                      6 views
                      0 likes
                      Last Post NinjaTrader_Manfred  
                      Started by ScottW, Today, 06:09 PM
                      1 response
                      4 views
                      0 likes
                      Last Post NinjaTrader_Manfred  
                      Started by ftsc2022, 10-25-2022, 12:03 PM
                      5 responses
                      256 views
                      0 likes
                      Last Post KeyonMatthews  
                      Started by Board game geek, 10-29-2023, 12:00 PM
                      14 responses
                      244 views
                      0 likes
                      Last Post DJ888
                      by DJ888
                       
                      Working...
                      X