• If this is your first visit, you will have to register before you can post. To view messages, please scroll below and select the forum that you would like to visits. Questions? Be sure to check out the Forum FAQ.

Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

How to LIMIT loss and daily gain?

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

    How to LIMIT loss and daily gain?

    Good Morning,
    I am designing a strategy with the Strategy Buider of NinjaTrader 8.

    I need to program something very simple, however, I have been looking for information on the website of NinjaTrader and in the forum, support etc and I can not find it.

    I want to set a limit of daily loss and another of daily gain for my strategy (independent of other possible strategies in operation).

    That is to say,
    I want to:

    1. when the cumulative loss of the day is -200 $, the strategy or the robot disconnects or stops working until the next day or session
    and on the other hand,

    2. that when the accumulated profit is $ 400, the strategy or the robot is disconnected or stops working until the next day or session.

    It is very important to me your help please.
    Thank you very much and greetings
    NATALIA

    #2
    Hello danakede,

    Thanks for your post and welcome to the NinjaTrader forums!

    You can accomplish your goal using the strategy builder, however, you would need to start/stop your strategy on a daily basis in order to correctly access the strategies realized PNL. Otherwise, the strategy PNL will continue to accumulate as it is not reset.

    What you would need to do is to create a bool variable (initially set to true) that is set false when you are greater than or equal to 400 or are less than or equal to -200. The bool variable then can be a part of each set that has an entry condition. This means that if the realized PNL is >= 400 or is <= -200 the bool will be set to false and no further orders can occur because the bool is set to false and to trade you need the bool to be true.

    You would create the bool variable in the "Variables" section of the Inputs and variables screen. As a suggestion, you might use the name of "OkToTrade" and set it true by default. The name can be anything you wish but using OkToTrade makes the function of the bool clear when looking at the strategy builder.

    In a set, in the Conditions section, you would create a group to place two conditions. Please left click on the word "group" to open the "Condition Group Editor" window. It is important that the selection "if Any" is showing in the group drop down, as you will want either condition to set the bool to false. Now add the two PNL conditions. Click Add to open the conditions screen. On the left side select the strategy folder and inside locate and select "RealizedPnL", in the center select "Greater equal", on the right side select the "Misc" folder and inside find and select "Numeric value", in the field enter the value 400, click OK to add the condition. Repeat the same steps to add the next condition except in the center select "Less equal" and on the right, for the numeric value, enter -200, click OK. The Conditions group editor window should have both conditions and again should show "if Any". Click OK to complete the conditions section.
    Next in the actions section (below the conditions section), please left click "add" to add an action.In the "Actions" window, left click on to open the "Misc" folder, locate and select Set OkToTrade (if that was the name you used, or select whichever name you gave to your bool variable), once select the bottom 1/2 of the window will show the bool and a check box. It will show checked because it was set to true by default when created, at this point we want it to be set to false, so please click to "uncheck" the check box which means we are now setting it false. Click OK to close the actions window.

    So you have created a set that will set the bool OkToTrade to false if the realizedPnL is >=400 or is <= -200

    In your condition sets where you have your entry conditions, you can now add the condition to see if the bool OkToTrade is true. Please open one of your existing condition sets where you place a trade. In the conditions section click "add", in the conditions window, on the left side, left click on the folder "user variables", in there select "OkToTrade". In the center section select "Equals", on the right side select the "misc" folder and find and select "True", then click OK to add the condition. Repeat this for ever set where you have an entry condition.

    Here are strategy builder reference materials that may help your overall knowldege of the strategy builder:
    https://ninjatrader.com/support/help...gy_builder.htm
    https://www.youtube.com/watch?v=HCyt...ZmVnauWXkWe0Nf
    Paul H.NinjaTrader Customer Service

    Comment


      #3
      Wow. Over the past few years I've seen countless times users asked for help with a daily profit and loss for their strategy. I've been thinking about going back to Tradestation due to this limitation in Ninjatrader. This is the first time I recall seeing a solution using the Strategy Builder.

      Nice job Paul!

      Comment


        #4
        Hello bob77713,

        Thanks for your post.

        The limitation in the strategy builder is that you have to stop and start the strategy on a daily basis for the strategies realizedPnL to be reset as otherwise the realizedPNL accumulates the longer the strategy runs and at some point it will stop taking trades.

        If you create a strategy in Ninjascript (or convert the strategy builder into the coded version) you can create a situation where you can effectively reset the daily PNL. You would do this by storing the strategies realized pNL into a variable on the first bar of the session (or when ever you wish really), then instead of testing the strategies realized PNL you would test the difference between the strategies realized Pnl and the variable that holds the realized PNl from the start of the session. The difference then being that days realizedPnl.
        Paul H.NinjaTrader Customer Service

        Comment


          #5
          Good morning Paul,

          Thank you very much for your effort to help me with my problem. I have been studying your information carefully and I have some questions:

          1. In the "Actions" section of the "Misc" folder I can not find OkToTrade to "uncheck" the check box. I only find the Alert, Log, Play Sound, Send Mail, Print and Share options. Can you tell me how to do it?


          2. I had designed my strategy in the following way:

          If All:
          Conditions for entry in Largo
          Realized Pnl (currency) <= 400
          Realized Pnl> = -200
          Then:
          Enter Long Position

          If All:
          Conditions for entry in Short
          Realized Pnl (currency) <= 400
          Realized Pnl> = -200
          Then:
          Enter Short Position


          Is this strategy the same as yours? If not, what is the difference between the two?

          3. The option that you propose to me, daily reset the strategy, it does not work for me, that is a semi-automatic strategy. I want an automatic robot. Also I can not perform optimizations, nor can I backtest my strategy, so it's not useful.

          There is a way to get it, you say you can get programming the source code, I just need someone to provide the piece of software that does what you indicate, that stores the Pnl in a variable in the first bar of the session and that in the conditions it is compared with the difference between the pnl performed and the variable that contains the pnl made since the beginning of the session. Maybe it could even be done with the Strategy Builder, right?

          Please, help me and a large number of traders that we have the same problem.

          Thank you very much Paul
          NATALIA

          Comment


            #6
            Hello danakede,

            Thanks for your reply.

            1) "OkToTrade", I wrote, "You would create the bool variable in the "Variables" section of the Inputs and variables screen. As a suggestion, you might use the name of "OkToTrade" and set it true by default. The name can be anything you wish but using OkToTrade makes the function of the bool clear when looking at the strategy builder."

            2) What you have written is perfectly fine (within the same limitations of resetting the strategy each day). The differences are that I choose a different way to do the same thing so that you would only need to use one condition (OkToTrade = true) in each entry set.

            3) In the case of a continuous running strategy that needs the strategy PNL reset on every session, you would not be able to use the strategy builder. There are no means to perform the subtraction of the realizedPNl from the saved pnl in the variable. I will create a feature request to advise the need for a daily Pnl type variable and will update this thread when I have further information. A feature request does not mean that the feature will or will not be implemented but is a tool to let program management be aware of user interest/requests. To be clear this is not something that would happen quickly if it is implemented at all.

            If you want a continuous running strategy that needs the strategy PNL reset on every session, then it would have to be coded in Ninjascript.

            If you would like someone to code your strategy for you, if requested, we can provide references to 3rd party coders who can provide that service for you.
            Paul H.NinjaTrader Customer Service

            Comment


              #7
              Hello danakede,

              Just to follow-up, the feature request to provide a daily PNL variable has been assigned as SFT-2980.
              If the feature is implemented, it will be identified in the release notes of the NT8 version. The release note can be found here: https://ninjatrader.com/support/help...ease_notes.htm
              Paul H.NinjaTrader Customer Service

              Comment


                #8
                Here's some NT8 code that will limit daily PL and doesn't need to be reset each session. Link

                Comment


                  #9
                  Hello algotrading,

                  Thanks for your post.

                  We appreciate your Ninjascript contribution, however, please note that this thread is about using the strategy builder for this effort.
                  Paul H.NinjaTrader Customer Service

                  Comment


                    #10
                    Is it possible to automatically exit the trade when the daily profit/loss is reached?

                    Comment


                      #11
                      Hello TheMasterBlaster,

                      Thanks for your post.

                      Yes, you would check your unrealized PNL (perhaps added with your realizedPNL) and issue an ExitLong() or ExitShort() which are markets orders to close the position. You would likely need to also set a bool variable when you have reached your limit and use that bool as one of the entry condition to prevent further orders when you have reached your goal.
                      Paul H.NinjaTrader Customer Service

                      Comment


                        #12
                        Hello TheMasterBlaster,

                        You may want to check with your broker to see if your broker is able to impose a daily loss limit that will exit an open position.


                        Alternatively, you could write a custom coded Addon script to monitor the account and exit a position if the loss is to great.
                        Below are links to an example strategy with this kind of logic that may be adaptable to an Addon, and example addon that places orders, and a link to the help guide.
                        https://ninjatrader.com/support/foru...ples#post93881
                        https://ninjatrader.com/support/foru...082#post536082
                        https://ninjatrader.com/support/help...ount_class.htm
                        Chelsea B.NinjaTrader Customer Service

                        Comment

                        Latest Posts

                        Collapse

                        Topics Statistics Last Post
                        Started by dkbyond, Today, 06:43 PM
                        0 responses
                        3 views
                        0 likes
                        Last Post dkbyond
                        by dkbyond
                         
                        Started by i2w8am9ii2, Today, 06:05 PM
                        0 responses
                        1 view
                        0 likes
                        Last Post i2w8am9ii2  
                        Started by YevhenShynkarenko, Today, 05:46 PM
                        0 responses
                        4 views
                        0 likes
                        Last Post YevhenShynkarenko  
                        Started by Bionian, Today, 04:14 PM
                        0 responses
                        3 views
                        0 likes
                        Last Post Bionian
                        by Bionian
                         
                        Started by dkbyond, Today, 04:03 PM
                        0 responses
                        5 views
                        0 likes
                        Last Post dkbyond
                        by dkbyond
                         
                        Working...
                        X