Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Sample code for chart trader buttons / Addons

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

    Sample code for chart trader buttons / Addons

    I would like to augment the functionality of the chart trader buttons to include some database logging and a check of my prior trades before accepting the click event e.g. checking my recent trade history before actually submitting the order via the "Buy Market" or "Sell Market" buttons.

    Is there a way to do this without building my own AddOn?
    Can you point me towards some sample code or documentation for accomplishing this? I have a decent bit of experience building indicators and strategies but I don't know anything about addOns or augmenting the UI.

    Thanks,
    Nick

    #2
    Hello NickyD,

    This would require a custom coded script. It could be an indicator applied to a chart.

    To have logic that can choose if an order is submitted you will want to create your own buttons to submit the orders.

    Below is a link to a few examples of indicators that add buttons to charts.
    https://ninjatrader.com/support/foru...ions#post96376

    As well as a link to an example script that places orders on a button click, where you could add custom logic to decide if the order is actually submitted.
    https://ninjatraderecosystem.com/use...bar-buttons-2/

    Below are links to examples of addons that you have requested.
    http://ninjatrader.com/support/forum...059#post477059
    https://ninjatrader.com/support/foru...720#post820720


    The NinjaTrader Ecosystem website is for educational and informational purposes only and should not be considered a solicitation to buy or sell a futures contract or make any other type of investment decision. The add-ons listed on this website are not to be considered a recommendation and it is the reader's responsibility to evaluate any product, service, or company. NinjaTrader Ecosystem LLC is not responsible for the accuracy or content of any product, service or company linked to on this website.
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Yes...I think what I need is in there somewhere. Thanks, these are detailed examples.

      Comment


        #4
        Chelsea,

        I have been using "ChartCustomSidePanelExample" as a guide for what I am trying to accomplish.

        One task I have not been able to figure out is how to calculate the PnL of my previous trades. I can list the executions or the orders from the account but I do not see a way to link up the executions that are marked IsEntry with the ones markets IsExit. If there is a TradeCollection object, that would be great, but I don't see any available to me in the indicator.


        Thanks!

        Comment


          #5
          Hello NickyD,

          There is no supported trade collection, but you may be able to make your own.

          Below is a link to a forum thread on this.
          https://ninjatrader.com/support/foru...407#post844407
          Chelsea B.NinjaTrader Customer Service

          Comment


            #6
            Chelsea,

            Thanks for the ideas. The code at that link uses SystemPerformance.AllTrades which is not accessible from an indicator as far as I know. Since I am working in an indicator right now, I've had to go a different route.

            At a high level I am doing this:
            private Account currentAccount;
            currentAccount = accountSelector.SelectedAccount;
            var myPositions = SystemPerformance.Calculate( currentAccount.Executions )

            TradeCollection trades = new TradeCollection(true, true);
            foreach(Trade trade in myPositions.AllTrades ) {
            if (trade.Entry.Instrument == BarsArray[0].Instrument) {
            trades.Add(trade);
            Print( trade.TradeNumber + ": " + trade.ProfitCurrency + " " + trade.ToString() );
            }
            }





            This is works for me with one caveat. It seems that currentAccount.Executions only returns executions from the active session... Consequently there is no corresponding entry execution for an overnight position I happened to have. The lack of this entry execution throws off the results of the SystemPerformance.Calculate() call. If I remove that one execution, like the following, everything works fine:

            var myPositions = SystemPerformance.Calculate( currentAccount.Executions.Where( c => c.ExecutionId != "304215553792" ).ToList() );

            I have not found any extensible way to filter the executions to remove any orphaned executions. How can I do this?

            I was hoping to use the IsEntry/IsExit property of the Execution object but it doesn't appear to be set properly...(though the Executions tab in the Control Center is aware of the property). Please see the attached image vs the Print Debug statements below.

            Code:
            foreach(Execution cae in currentAccount.Executions ) {
            Print("executionID: " + cae.ExecutionId + " - IsEntry: " + ( cae.IsEntry == true ? "True" :"false" ) + " IsExit: " + ( cae.IsExit == true ? "True" :"false" ) );
            }




            Output:
            executionID: 304661146112 - IsEntry: True IsExit: True
            executionID: 304688847360 - IsEntry: True IsExit: True
            executionID: 304689466624 - IsEntry: True IsExit: True

            Click image for larger version  Name:	accountExecutions.png Views:	0 Size:	23.6 KB ID:	1091000
            Last edited by NickyD; 03-19-2020, 06:49 AM.

            Comment


              #7
              Hello NickyD,

              You are correct, the executions and orders for the account are for the current day.

              The older executions and orders are saved in the database and shown in the Trade Performance window. However, there is no supported access to this in NinjaScript.
              https://ninjatrader.com/support/help...erformance.htm

              The custom TradeCollection, SystemPerformance.Calculate(), and <execution>.IsEntry / .IsExit would be undocumented/unsupported so I wouldn't have a recommended approach.
              https://ninjatrader.com/support/help.../execution.htm
              https://ninjatrader.com/support/help...erformance.htm
              https://ninjatrader.com/support/help...8/position.htm


              But, you might try making custom trade objects from the <account>.Orders.
              https://ninjatrader.com/support/help...rs_account.htm


              This thread will remain open for any community members that would like to share unsupported code or tips.

              You can also contact a professional NinjaScript Consultant who would be eager to create or modify this script at your request or assist you with your script. The NinjaTrader Ecosystem has affiliate contacts who provide educational as well as consulting services. Please let me know if you would like our business development follow up with you with a list of affiliate consultants who would be happy to create this script or any others at your request.
              Chelsea B.NinjaTrader Customer Service

              Comment


                #8
                For anyone who comes across this, here's some more info.

                It seems that <execution>.IsEntry / .IsExit properties are set upon the initial execution however I think they don't persist through restarts of NinjaTrader. When you query them after a restart they will both be set to true and therefor not usable for filtering.

                SystemPerformance.Calculate() seems to be able to handle open positions ok, but it can't handle any orphaned exit executions, mine was from an overnight position where the entry was no longer returned by <Account>.Executions . To fix this, I had iterate over the list of executions sorted by date and keep track of my account position. Once you know the account is flat, you can then add the rest of the executions and pass that collection to SystemPerformance.Calculate

                This is mildly tricky as some orders don't have names and you have to handle scale-in and scale outs, but the pseudo code for this will likely end up looking something like this:

                currentAccount = accountSelector.SelectedAccount;
                List<Execution> executionsFromFlat = new List<Execution>();
                int AccountPosition = 0;
                foreach(Execution cae in currentAccount.Executions.OrderBy( c => c.Time )) {
                IF cae.Position && cae.Quantity aren't what you're expecting, this is a position that was initiated before the session started.
                IF it is what you're expecting add it to your curated list of executions
                executionsFromFlat.Add( cae)
                }


                var myTrades = SystemPerformance.Calculate( executionsFromFlat );

                TradeCollection trades = new TradeCollection(true, true);
                foreach(Trade trade in myTrades.AllTrades ) {
                Print( trade.TradeNumber + ": " + trade.ProfitCurrency + " " + trade.ToString() );
                if (trade.Entry.Instrument == BarsArray[0].Instrument)
                trades.Add(trade);
                }

                Comment


                  #9
                  Hello NickyD,

                  Thank you for providing your solution to the community.

                  There is an open feature request for a tradeperformance collection for the account tracked with ID# SFT-3088. I will add your vote to this.
                  Chelsea B.NinjaTrader Customer Service

                  Comment


                    #10
                    Originally posted by NinjaTrader_ChelseaB View Post
                    Hello NickyD,

                    Thank you for providing your solution to the community.

                    There is an open feature request for a tradeperformance collection for the account tracked with ID# SFT-3088. I will add your vote to this.
                    I would like to add a vote for an object that holds various trades entered onto a specific account also.

                    Comment


                      #11
                      Originally posted by NinjaTrader_ChelseaB View Post
                      Hello NickyD,

                      Thank you for providing your solution to the community.

                      There is an open feature request for a tradeperformance collection for the account tracked with ID# SFT-3088. I will add your vote to this.
                      NinjaTrader_ChelseaB , is there a way to be notified when a change like this is implemented on a newer update to NT?

                      Comment


                        #12
                        Hello forrestang,

                        I have added a vote on your behalf.

                        You may check the Release Notes page of the Help Guide to check for new features. If you would like to get notified when a new release is available, you may subscribe to the Announcements forum. The ID in Release Notes will be different than the SFT ID, but you may check the descriptions in the Release Notes page.

                        Release Notes - https://ninjatrader.com/support/help...ease_notes.htm

                        Announcements - https://ninjatrader.com/support/foru...nouncements-aa

                        We look forward to assisting.
                        JimNinjaTrader Customer Service

                        Comment


                          #13
                          Originally posted by NinjaTrader_ChelseaB View Post
                          ...feature request for a TradePerformance collection for the account tracked with ID# SFT-3088.
                          +1

                          Please add my vote.

                          Comment


                            #14
                            Hello bltdavid,

                            I've added your vote. Thanks for your voice.
                            Chelsea B.NinjaTrader Customer Service

                            Comment


                              #15
                              Originally posted by NinjaTrader_ChelseaB View Post
                              Hello NickyD,

                              You are correct, the executions and orders for the account are for the current day.

                              The older executions and orders are saved in the database and shown in the Trade Performance window. However, there is no supported access to this in NinjaScript.
                              It was class NinjaTrader.Gui.Chart.ChartExecution in Ninja Trader 7.
                              So Lucas used this class to create Trades Collection:
                              https://ninjatraderecosystem.com/use...visualization/

                              I believe we should have the same class in NT8, but it was most likely renamed. I couldn't find it.
                              My question is what collection does NT8 use to Plot Executions markers on the chart?

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by Bydwoeoqwe, Today, 09:34 PM
                              0 responses
                              1 view
                              0 likes
                              Last Post Bydwoeoqwe  
                              Started by nutmame1, Today, 08:26 PM
                              0 responses
                              5 views
                              0 likes
                              Last Post nutmame1  
                              Started by dr0832, Today, 07:02 PM
                              0 responses
                              11 views
                              0 likes
                              Last Post dr0832
                              by dr0832
                               
                              Started by mlurskint8, Today, 05:27 PM
                              3 responses
                              18 views
                              0 likes
                              Last Post NinjaTrader_ShawnB  
                              Started by SteveTheCPA, Today, 04:54 PM
                              0 responses
                              10 views
                              0 likes
                              Last Post SteveTheCPA  
                              Working...
                              X