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

Button to close manually trades

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

    Button to close manually trades

    Hi,

    I have created a button to close manually trades.

    Code:
    protected void Button1Click(object sender, RoutedEventArgs e)
    {
    if(PositionAccount.MarketPosition == MarketPosition.Long) {
    EnterShort(PositionAccount.Quantity, "Close");
    // ExitLong(PositionAccount.Quantity);
    } else if(PositionAccount.MarketPosition == MarketPosition.Short) {
    EnterLong(PositionAccount.Quantity, "Close");
    // ExitShort(PositionAccount.Quantity);
    }
    }

    It is not working, when I open a long trade manually for example and press the button, the long trade is closed but it opens a new short trade automatically.
    I also have tried ExitShort and ExitLong and it is not working.

    Is there a way to flat all positions?


    Thanks

    #2
    Hello lju45,

    Thanks for your post.

    To detect manual trades and manage them programmatically with a button, you can use the AddOn approach and create a NinjaScript that:
    1. Loops through the Account.Orders collection for the account to find active orders
    2. Subscribes to Account OrderUpdate events to look for any newly opened orders.
    3. Tracks these Orders in the script so they can be changed or cancelled with Account.Change or Account.Cancel
    I have included NinjaTrader 8 documentation on these items below if you wish to investigate further.

    Order object - https://ninjatrader.com/support/help...n-us/order.htm

    Account.Orders - https://ninjatrader.com/support/help...rs_account.htm

    Account.OrderUpdate - https://ninjatrader.com/support/help...rderupdate.htm

    Account.Change - https://ninjatrader.com/support/help...-us/change.htm

    Account.Cancel - https://ninjatrader.com/support/help...-us/cancel.htm

    Let us know if we may assist further.
    Brandon H.NinjaTrader Customer Service

    Comment


      #3
      I see that it seems more complicated than I thought, I go to the beginning of the problem and see if you can help me with another solution. I have created a strategy that every time a position is opened, it makes a small calculation with the Pnl and shows it in a corner of the chart. The problem is that when I close the position with the chart button "Close", the strategy stops so I have to restart it every time I close a position. Hence my solution to create a new button to close positions without stopping the strategy, do I have any other viable solution?

      By the way, I can't open any of the links that you have sent me before it seems that they are not correct.

      Thanks a lot
      Last edited by lju45; 08-12-2021, 09:47 AM.

      Comment


        #4
        Hello lju45,

        Thanks for your note.

        That is correct. Closing out of a position placed by a strategy using the Close button in Chart Trader will close the position and disable the strategy.

        Something you could do is custom buttons that enters and exits positions. Please see the LongShortToolbar script linked below from our NinjaTrader Ecosystem User App Share center for an example of entering and exiting orders using custom buttons.

        https://ninjatraderecosystem.com/use...bar-buttons-2/

        I am able to open the help guide links shared in my previous post. I am posting them again below for your to view.

        Order object - https://ninjatrader.com/support/helpGuides/nt8/?order.htm

        Account.Orders - https://ninjatrader.com/support/help...rs_account.htm

        Account.OrderUpdate - https://ninjatrader.com/support/helpGuides/nt8/?orderupdate.htm

        Account.Change - https://ninjatrader.com/support/helpGuides/nt8/?change.htm

        Account.Cancel - https://ninjatrader.com/support/help...t8/?cancel.htm

        Let us know if we may assist further.


        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.
        Brandon H.NinjaTrader Customer Service

        Comment


          #5
          Thank you,

          Is there a way to create a text in the chart with the Pnl without a strategy, maybe using indicators?

          I'm using now in the strategy the next:
          Code:
          PositionAccount.GetUnrealizedProfitLoss(PerformanceUnit.Currency, Close[0])
          But it is not working for indicators.

          Can you help me?

          Comment


            #6
            Hello lju45,

            Thanks for your note.

            Yes, it would be possible to get the Unrealized PnL of an account and draw the Unrealized PnL on a chart using an indicator. You would need to use the Account class to find the account, then use the following code to assign the Unrealized PnL to a double variable named something like unrealizedPNL.

            unrealizedPNL = myAccount.Get(AccountItem.UnrealizedProfitLoss, Currency.UsDollar);

            After assigning the Unrealized PnL to a variable, you could use Draw.TextFixed to print out the Unrealized PnL assigned to that variable.

            See the attached example script demonstrating this.

            Also, see the help guide documentation below for more information.
            Account: https://ninjatrader.com/support/help...ghtsub=Account
            AccountItem: https://ninjatrader.com/support/help...ccountitem.htm
            Get: https://ninjatrader.com/support/helpGuides/nt8/get.htm
            Draw.TextFixed: https://ninjatrader.com/support/help..._textfixed.htm

            Let us know if we may assist further.
            Attached Files
            Brandon H.NinjaTrader Customer Service

            Comment


              #7
              Thank you! It really help me.

              Last question, now If I want to check the market position, PositionAccount.MarketPosition is not longer working on indicator.
              Last edited by lju45; 08-12-2021, 12:23 PM.

              Comment


                #8
                Hello lju45,

                Thanks for your note.

                That is correct. PositionAccount.MarketPosition is for use with strategies and is not for use with indicators.

                You would need to use the AddOn approach and the Account class Positions collection to get the positions on an account.

                See this help guide page for more information about using the Account Positions collection: https://ninjatrader.com/support/help...ns_account.htm

                The help guide contains an example of looping through each position in the account and printing the MarketPosition and AveragePrice.

                Let us know if we may assist further.
                Brandon H.NinjaTrader Customer Service

                Comment


                  #9
                  Thank you,

                  I get it but I have seen that the unrealizedPNL calculated by:
                  Code:
                      unrealizedPNL = myAccount.Get(AccountItem.UnrealizedProfitLoss, Currency.UsDollar);
                  is delayed 1 tick from the real PNL


                  How can I make it show me the updated value?


                  Comment


                    #10
                    Hello lju45,

                    Thanks for your note.

                    The indicator is initially set to Calculate OnBarClose. This means that the indicator will update at the close of each bar.

                    If you change the Calculate mode to OnEachTick, the Unrealized PnL will update on the chart once the PnL updates in the Accounts tab of the Control Center.

                    Let us know if we may assist further.
                    Brandon H.NinjaTrader Customer Service

                    Comment


                      #11
                      Hi Brandon,

                      I did from the begining but the PNL is always one tick deleyed for any type of candle.

                      Thanks

                      Comment


                        #12
                        Hello lju45,

                        Thanks for your note.

                        You could have the drawing object update faster by subscribing to AccountItemUpdate events and drawing the text from the OnAccountItemUpdate method.

                        I have attached a modified version of the previously attached example script demonstrating this. Note that upon initially enabling the indicator, a drawing object doesn't appear. The drawing object will display upon the first update to the Unrealized PnL in the Accounts tab since we are drawing our object when the AccountItemUpdate event occurs.

                        Also, see this help guide page for more information: https://ninjatrader.com/support/help...itemupdate.htm

                        Let us know if we may assist further.
                        Attached Files
                        Brandon H.NinjaTrader Customer Service

                        Comment


                          #13
                          Thank you, it works perfectly.

                          Is it possible to create a variable to select the account? I add in the Properties region the next:
                          Code:
                          [ NinjaScriptProperty ]
                          [ Display (Name = "Account", GroupName = "Parameters", Order = 7)]
                          [TypeConverter(typeof(NinjaTrader.NinjaScript.Accou ntNameConverter))]
                          public string AccountName
                          { get ; set ; }
                          So in the SetDefault status I have changed the name Sim101 by AccountName, like this:

                          Code:
                          AccountName = "Sim101";
                          
                          lock (Account.All)
                          myAccount = Account.All.FirstOrDefault(a => a.Name == AccountName);
                          
                          if (myAccount != null)
                          // Subscribe to events. Remember to unsubscribe with -= when you are done
                          myAccount.AccountItemUpdate += OnAccountItemUpdate;
                          The problem is that if I change the AccountName parameter the indicator doesn't do it and always take Sim101 account and not the others, how can I change the name of the account in order to create an indicator that can adapt to each account without having to change each time the code?

                          Thanks

                          Comment


                            #14
                            Hello lju45,

                            Thanks for your post.

                            Yes, you could create a NinjaScriptProperty that allows you to select which account you would like an indicator to apply to. For example, if you place the attached modified example indicator on a chart, the drawing object in the corner of the chart will draw the Unrealized PnL of the account that you applied the indicator to. Say you have two accounts, Sim101 and Sim102. Say Sim101 has an Unrealized PnL of 25 and Sim102 has an Unrealized PnL of 100. The indicator will draw a value of 100 on the chart since that is that account that the indicator is applied to.

                            Please see the attached modified example script which demonstrates this.

                            Let us know if we may assist further.
                            Attached Files
                            Brandon H.NinjaTrader Customer Service

                            Comment

                            Latest Posts

                            Collapse

                            Topics Statistics Last Post
                            Started by Option Whisperer, Today, 09:55 AM
                            1 response
                            11 views
                            0 likes
                            Last Post bltdavid  
                            Started by port119, Today, 02:43 PM
                            0 responses
                            1 view
                            0 likes
                            Last Post port119
                            by port119
                             
                            Started by Philippe56140, Today, 02:35 PM
                            0 responses
                            3 views
                            0 likes
                            Last Post Philippe56140  
                            Started by 00nevest, Today, 02:27 PM
                            0 responses
                            2 views
                            0 likes
                            Last Post 00nevest  
                            Started by Jonafare, 12-06-2012, 03:48 PM
                            5 responses
                            3,987 views
                            0 likes
                            Last Post rene69851  
                            Working...
                            X