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

Exit all positions at Loss or Profit

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

    Exit all positions at Loss or Profit

    Does anybody have a single strategy that can be enabled in the Control Center daily, will monitor the account during the regular session, and will sell all positions in that account should the total of realized plus unrealized losses be less than a designated amount, or should the the total of realized plus unrealized gains be more than a designated amount?

    #2
    Hello MathWiz,

    Thank you for your post.

    You could create a strategy that subscribes to the account-based event AccountItemUpdate to monitor the total account PnL. You would then create a condition in your script that checks if the Unrealized account PnL plus the Realized account PnL is less than a certain price value and call Flatten() to flatten all positions. Another condition would be created in the script that checks if the Unrealized PnL plus the Realized PnL is greater than a certain price value and call Flatten().

    Public documentation for AccountItemUpdate can be referenced here - https://ninjatrader.com/support/help...itemupdate.htm

    AccountItems that can be used are documented here - https://ninjatrader.com/support/help...ccountitem.htm

    See this documentation for information about Flatten() — https://ninjatrader.com/support/help...ghtsub=flatten

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

    Comment


      #3
      Originally posted by NinjaTrader_BrandonH View Post
      Hello MathWiz,

      Thank you for your post.

      You could create a strategy that subscribes to the account-based event AccountItemUpdate to monitor the total account PnL. You would then create a condition in your script that checks if the Unrealized account PnL plus the Realized account PnL is less than a certain price value and call Flatten() to flatten all positions. Another condition would be created in the script that checks if the Unrealized PnL plus the Realized PnL is greater than a certain price value and call Flatten().

      Public documentation for AccountItemUpdate can be referenced here - https://ninjatrader.com/support/help...itemupdate.htm

      AccountItems that can be used are documented here - https://ninjatrader.com/support/help...ccountitem.htm

      See this documentation for information about Flatten() — https://ninjatrader.com/support/help...ghtsub=flatten

      Let us know if we may further assist.
      Can you please give some advises/links/examples for NT7 as well ?
      Thanks

      Comment


        #4
        Hello George1455,

        Thank you for your note.

        NinjaTrader 7 does not have the ability to subscribe to the account-based event AccountItemUpdate. Instead, you would need to create a condition that checks if GetAccountValue(AccountItem.RealizedProfitLoss) is less than or greater than a certain value followed by calling Exit methods such as ExitLong/ExitShort to close out of your positions.

        GetAccountValue() - https://ninjatrader.com/support/help...countvalue.htm
        Order methods - https://ninjatrader.com/support/help...d_approach.htm

        If you have further questions regarding this, please open a new forum thread in the NinjaTrader 7 Strategy Development section of the forums.

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

        Comment


          #5
          George,

          Are you saying that code like this:

          CurrentPNL = SystemPerformance.AllTrades.TradesPerformance.Curr ency.CumProfit;

          if (CurrentPNL + Position.GetUnrealizedProfitLoss(PerformanceUnit.C urrency, Close[0])) < myDailyStoplevel)
          {
          Flatten()
          }

          running as its own strategy will monitor my entire account and flatten all positions if it reaches its trigger?

          Comment


            #6

            Hello MathWiz,

            Thank you for your note.

            CurrentPNL = SystemPerformance.AllTrades.TradesPerformance.Curr ency.CumProfit; would save the cumulative profit of all trades to the variable CurrentPNL.

            CumProfit — https://ninjatrader.com/support/help.../cumprofit.htm

            Position.GetUnrealizedProfitLoss(PerformanceUnit.C urrency, Close[0]) will return the unrealized PnL for the strategy position. (Strategy positions are different from Account positions)

            Position.GetUnrealizedProfitLoss — https://ninjatrader.com/support/helpGuides/nt8/position_getunrealizedprofitloss.htm

            This means that the code you shared would check if the cumulative profit of all trades + the unrealized PnL of the strategy position is less than myDailyStoplevel and calls Flatten(). This would not be checking the unrealized PnL of your account position.

            To have the strategy monitor the unrealized PnL of your account, you would need to use PositionAccount.GetUnrealizedProfitLoss() instead of Position.GetUnrealizedProfitLoss().

            PositionAccount.GetUnrealizedProfitLoss — https://ninjatrader.com/support/help...profitloss.htm

            Please note that instead of Flatten() you would need to call Account.Flatten() as seen in the help guide example in post number 2.

            See the attached example script that demonstrates how this would be accomplished.

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

            Comment


              #7
              Hello,
              I am hoping you can help me with this script? I downloaded it and changed the account name to my "Playback101" so it matched and I updated the instrument to "ES 06-22" and changed the properties setting to allow a negative number to allow for max loss.
              When I start my strategy and this one on the playback101, they are both in "Sync" until it opens a trade and then the CumProfit script "Sync" errors and shows "False" and doesn't work??
              I did also try it on a live feed Sim101 account and it did the same?
              Please help, thanks.

              Comment


                #8
                Hello Neilpacc76,

                Thanks for your note.

                This script is meant to be an example of how to call the Account.Flatten() method when the cumulative profit of all trades + the unrealized PnL of the account position is less than MyDailyStoplevel. It is not meant to be a fully working tool.

                That said, since the script does not place trades and trades are placed manually, the Strategy Position and Account Position would go out of sync. Strategies are not able to detect trades placed manually or placed by other strategies. For example, if the Strategy Position is 0 and the Account Position is 0, then a trade is placed manually, the Account Position would be 1 and the Strategy Position would be 0 since the strategy did not place the trade. This would cause the Strategy Position and Account Position to be out of sync.

                See this help guide page for more information: https://ninjatrader.com/support/help..._account_p.htm

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

                Comment


                  #9
                  Originally posted by NinjaTrader_BrandonH View Post
                  Hello MathWiz,

                  Thank you for your note.

                  CurrentPNL = SystemPerformance.AllTrades.TradesPerformance.Curr ency.CumProfit; would save the cumulative profit of all trades to the variable CurrentPNL.

                  CumProfit — https://ninjatrader.com/support/help.../cumprofit.htm

                  Position.GetUnrealizedProfitLoss(PerformanceUnit.C urrency, Close[0]) will return the unrealized PnL for the strategy position. (Strategy positions are different from Account positions)

                  Position.GetUnrealizedProfitLoss — https://ninjatrader.com/support/helpGuides/nt8/position_getunrealizedprofitloss.htm

                  This means that the code you shared would check if the cumulative profit of all trades + the unrealized PnL of the strategy position is less than myDailyStoplevel and calls Flatten(). This would not be checking the unrealized PnL of your account position.

                  To have the strategy monitor the unrealized PnL of your account, you would need to use PositionAccount.GetUnrealizedProfitLoss() instead of Position.GetUnrealizedProfitLoss().

                  PositionAccount.GetUnrealizedProfitLoss — https://ninjatrader.com/support/help...profitloss.htm

                  Please note that instead of Flatten() you would need to call Account.Flatten() as seen in the help guide example in post number 2.

                  See the attached example script that demonstrates how this would be accomplished.

                  Let us know if we may assist further.
                  Brandon,

                  I stumbled across your strategy and tried to change it into an account daily profit target as well. I'm currently using 154 strategies over 6 accounts(no SIM). Flattening each account with a different daily target and stop is very important. I changed Currency to Ticks and added the Profit Target to go along with the stop you had there. When I tried it out, it would stop the strategy on SIM101 account but seemingly at very sporadic times. I ran it on 5 second chart just so it would update often and wouldn't have to wait until the the bars close on the multiple strategies I'm running. Here is the code, do you see anything obvious why it wouldn't work properly? Thanks
                  Attached Files

                  Comment


                    #10
                    Hi Steve4616, thanks for posting. The SystemPerformance class will only provide the performance metrics of the single strategy it is being called from, not the entire account. Each strategy needs to check its own PnL value and close its position based on that calculation. You can get Account based metrics by using the Get method:


                    Kind regards,
                    -ChrisL
                    Chris L.NinjaTrader Customer Service

                    Comment


                      #11
                      Originally posted by NinjaTrader_ChrisL View Post
                      Hi Steve4616, thanks for posting. The SystemPerformance class will only provide the performance metrics of the single strategy it is being called from, not the entire account. Each strategy needs to check its own PnL value and close its position based on that calculation. You can get Account based metrics by using the Get method:


                      Kind regards,
                      -ChrisL
                      Thanks Chris, I will look into this, might be the missing link, cheers

                      Comment


                        #12
                        Originally posted by NinjaTrader_ChrisL View Post
                        Hi Steve4616, thanks for posting. The SystemPerformance class will only provide the performance metrics of the single strategy it is being called from, not the entire account. Each strategy needs to check its own PnL value and close its position based on that calculation. You can get Account based metrics by using the Get method:


                        Kind regards,
                        -ChrisL
                        Hi Chris,

                        I tried it out and it compiles without errors, but immediately disables. Also, if i have any other strategies enabled on the account, it immediately flattens also. I had the profit target and stop set at 200 and -200 and the sim101 account was down $25 and no open positions and it still disabled immediately. Does anything jump out to you that would cause this? Thanks again

                        Comment


                          #13
                          Hi, thanks for the follow up. If a strategy is disabled immediately, check the Log tab of the Control Center for any errors. There is likely an exception being thrown.

                          Kind regards,
                          -ChrisL
                          Chris L.NinjaTrader Customer Service

                          Comment


                            #14
                            Originally posted by NinjaTrader_ChrisL View Post
                            Hi, thanks for the follow up. If a strategy is disabled immediately, check the Log tab of the Control Center for any errors. There is likely an exception being thrown.

                            Kind regards,
                            -ChrisL
                            Thanks Chris,
                            It only shows this, no errors. Also there were no trades open and not trades in the Sim101 account today so the PL is 0, thanks
                            Attached Files

                            Comment


                              #15
                              Hi Steve, Could you export and post the code you are using so I can test it?

                              Kind regards.
                              Chris L.NinjaTrader Customer Service

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by samish18, Today, 05:11 PM
                              0 responses
                              1 view
                              0 likes
                              Last Post samish18  
                              Started by akushyn, 12-10-2018, 02:23 PM
                              7 responses
                              437 views
                              0 likes
                              Last Post garth
                              by garth
                               
                              Started by Vikuno1, Today, 04:31 PM
                              0 responses
                              4 views
                              0 likes
                              Last Post Vikuno1
                              by Vikuno1
                               
                              Started by aligator, Today, 04:09 PM
                              0 responses
                              5 views
                              0 likes
                              Last Post aligator  
                              Started by icebergdelphi, Yesterday, 04:01 PM
                              4 responses
                              23 views
                              0 likes
                              Last Post icebergdelphi  
                              Working...
                              X