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

How to stop strategy if realized PnL is above or less than a value?

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

    How to stop strategy if realized PnL is above or less than a value?

    I have a strategy I’m trying I made in Strategy Builder. But I’m really desperately trying to find how I can modify it to not trade anymore if I have already made $100.00 or more or have negative 100 or less for the day or while its enabled whatever makes most sense.

    #2
    Hello MatthewLesko,

    Thanks for your post.

    I understand that you would like to implement a Daily Profit Limit or Daily Loss Limit in your script so that the strategy stops trading once that profit/loss is reached.

    The Strategy Builder custom series can be used with offsets to do math and subtract the total PnL from yesterday's PnL to get the current PnL and my colleague has provided an example of how to set a Daily Loss Limit using the Strategy Builder.

    Please see the example script, DailyLossLimitBuilderExample, in the forum post linked below for how this would be accomplished.
    https://ninjatrader.com/support/foru...limit-examples

    Similar logic could be used to implement a Daily Profit Limit in your script like the Daily Loss Limit in this example script.

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

    Comment


      #3
      Thanks I'm trying it and seems to work however, if I have a losing trade and then a winning trade and am in the positive (typically will be around $45 green if I have a loss then a win), it will keep trading even though I'm saying set the daily profit to true if I'm greater than or equal to $20.00.
      It will still keep strategy running and not set to true

      Comment


        #4
        Hello MatthewLesko,

        Thanks for your note.

        Ultimately, debugging steps should be taken if your strategy is not behaving as you expect it to.

        To understand why the script is behaving as it is, such as placing orders or not placing orders or drawing objects when expected, it is necessary to add prints to the script that print the values used for the logic of the script to understand how the script is evaluating.

        In the strategy add prints that print the values of every variable used in every condition that places an order along with the time of that bar. Prints will appear in the NinjaScript Output window (New > NinjaScript Output window).

        Below is a link to a forum post that demonstrates how to use prints to understand behavior.
        https://ninjatrader.com/support/foru...121#post791121

        Let me know if I may assist further.
        Brandon H.NinjaTrader Customer Service

        Comment


          #5
          Hi Brandon I think maybe the issue is my strategy is on bar close but realized PnL I think has to be calculated per tick?
          So upon bar close it's executing another order perhaps because the variable is both true/false at the same time?
          That said is there maybe a way to have a different strategy set the value for a variable that I can refer to in another strategy?

          Comment


            #6
            Hello MatthewLesko,

            Thanks for your note.

            "..perhaps because the variable is both true/false at the same time?"

            Have you added debugging prints to your strategy to determine how it is processing values and placing orders?

            Debugging prints would allow you to determine if the variable is calculated as true or false. This could be compared to other values being used for conditions to place orders to determine if an order should have been placed.

            To understand why the script is behaving as it is, such as placing orders or not placing orders or drawing objects when expected, it is necessary to add prints to the script that print the values used for the logic of the script to understand how the script is evaluating.

            In the strategy add prints (outside of any conditions) that print the values of every variable used in every condition that places an order along with the time of that bar. Prints will appear in the NinjaScript Output window (New > NinjaScript Output window).

            Also, enable TraceOrders which will let us know if any orders are being ignored and not being submitted when the condition to place the orders is evaluating as true.

            Below is a link to a forum post that demonstrates how to use prints to understand behavior.
            https://ninjatrader.com/support/foru...121#post791121

            Let me know if I may assist further.
            Brandon H.NinjaTrader Customer Service

            Comment


              #7
              Ok so I think with what I already is working, it's just that if immediately upon bar close of 2nd trade that is a loser, if the next bar open matches conditions to make a trade, it will still fire off one more trade.
              however if there's a bar in between my 2nd loss and the next potential bar to execute a trade, it will not fire off since I've reached the loss limi

              Comment


                #8
                For now my only workaround is to add this which sucks because sometimes my winners are consecutive trades after previous bar close I just don't know what else to do:

                Code:
                && ((BarsSinceExitExecution(0, "", 0) >= 2)
                || (BarsSinceExitExecution(0, "", 0) == -1)))

                Comment


                  #9
                  Hello MatthewLesko,

                  Thanks for your note.

                  Have you added debugging prints to your strategy to determine how it behaves and calculate values?

                  If not then you would need to add prints to the script that print out every variable in the script along with the Time. These prints could be compared in a New > NinjaScript Output window to determine how the variables in your script are calculating.

                  For example, suppose you print out the DailyPNL value, bools, and your DailyProfitLimit variable. In that case, you could compare those calculations in the NinjaScript Output window to see if the DailyPNL is above or below your DailyProfitLimit variable and if the bools are evaluating as true or false.

                  Once you understand exactly how the variables in your script are calculating, you could modify the script accordingly.

                  Also, enable TraceOrders which will let us know if any orders are being ignored and not being submitted when the condition to place the orders is evaluating as true.

                  Below is a link to a forum post that demonstrates how to use prints to understand behavior.
                  https://ninjatrader.com/support/foru...121#post791121

                  Please note in the support department at NinjaTrader it is against our policy to create, debug, or modify, code or logic for our clients. Further, we do not provide C# programming education services in our support. This is so that we can maintain a high level of service for all of our clients as well as our partners.

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

                  Comment


                    #10
                    Good afternoon, I tried this setup and it still trades after the target loss has been met. I'm wondering does the realized pnl currency value recognize the whole number in the user input section as an equivalent value?

                    Comment


                      #11
                      Hello lbolton64,

                      Thanks for your notes.

                      Yes, the script should recognize a user-defined property that is set to a whole number.​

                      Have you added debugging prints to the script to understand exactly how the script is behaving and placing orders?

                      In the script, one line above the condition, print out all the variables used for the condition to place the order to confirm how the strategy logic is evaluating.

                      Below is a link to a forum post that demonstrates how to use prints to understand behavior.
                      https://ninjatrader.com/support/foru...121#post791121

                      Here are some example scripts that you could view in the forum post linked below for how to implement a daily loss limit in NinjaScript.
                      https://ninjatrader.com/support/foru...limit-examples
                      Brandon H.NinjaTrader Customer Service

                      Comment

                      Latest Posts

                      Collapse

                      Topics Statistics Last Post
                      Started by TraderIqbal, 10-20-2023, 10:12 AM
                      2 responses
                      118 views
                      0 likes
                      Last Post Funnywo
                      by Funnywo
                       
                      Started by Creamers, 04-27-2024, 05:32 AM
                      12 responses
                      72 views
                      0 likes
                      Last Post Creamers  
                      Started by fredericlebre, Today, 01:19 AM
                      0 responses
                      7 views
                      0 likes
                      Last Post fredericlebre  
                      Started by TraderJA, Today, 12:06 AM
                      0 responses
                      10 views
                      0 likes
                      Last Post TraderJA  
                      Started by SnailHorn, Yesterday, 10:49 PM
                      0 responses
                      9 views
                      0 likes
                      Last Post SnailHorn  
                      Working...
                      X