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 Determine if Stop Loss or Profit Target is Hit

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

    How to Determine if Stop Loss or Profit Target is Hit

    How to Determine if Stop Loss or Profit Target is Hit first while in a position?

    Are there events in NT8 to do this?

    Thanks in advance.

    #2
    Hello geekodude, and thank you for your question.

    There are two events, OnOrderUpdate and OnExecutionUpdate, which will allow you to react to Stop Loss and Profit Target orders being triggered. I am including some code which demonstrates how to do so. Please let us know if there are any other ways we can help.
    Attached Files
    Jessica P.NinjaTrader Customer Service

    Comment


      #3
      Thank you, I will study your example.

      Comment


        #4
        I have this working excellently for individual trades. Is there a way to separate between two unique trades? I have my code shown:

        protected override void OnExecutionUpdate (
        Execution execution, string executionId, double price, int quantity,
        MarketPosition marketPosition, string orderId, DateTime time
        )
        {
        // This is the code I used to discover the Stop loss order's name
        Print("OOU: Trade 1: " + execution.Order);

        // Now that I know it is 'Stop loss', I can use this name here
        if (execution.Order.Name == "Stop loss")
        {
        excelSheet.Cells[rowCount,9] = "Fail";
        }

        Print("OOU: Trade 2: " + execution.Order);


        // Now that I know it is 'Stop loss', I can use this name here
        if (execution.Order.Name == "Stop loss")
        {
        excelSheet.Cells[rowCount,10] = "Fail";
        }
        }

        I have it set up to print to excel whenever the stop loss is hit, which it does. However, it will fill the order name for whichever hits the stop loss first. How can I set it to where the stop loss for trade 1 is stored separately from trade 2?

        Comment


          #5
          Hello tatir,

          In your conditions you have two of the same condition checking for the name Stop loss. To find the unique order you would either need to submit an exit order with a unique name so the name is something other than just Stop loss or you would need to check the FromEntrySignal name: https://ninjatrader.com/support/help...lightsub=Order

          I look forward to being of further assistance.
          JesseNinjaTrader Customer Service

          Comment


            #6
            So there isn't a way to set execution.Order.Name as a function of the individual trades?

            I'm also a little lost on how to properly invoke FromEntrySignal for this example.

            Comment


              #7
              Hello tatir,

              The orders name is set when you submit the order however if you used the Set methods like SetStopLoss those have fixed names like Stop loss. You would need to submit an exit type order and provide a name when submitting the order. Alternatively if you use SetStopLoss you would need to use the entries name to know which stop it is. If you had two entries they could have two unique names which you could then see as the FromEntrySignal of the stoploss order.


              I look forward to being of further assistance.
              JesseNinjaTrader Customer Service

              Comment


                #8
                Originally posted by NinjaTrader_JessicaP View Post
                Hello geekodude, and thank you for your question.

                There are two events, OnOrderUpdate and OnExecutionUpdate, which will allow you to react to Stop Loss and Profit Target orders being triggered. I am including some code which demonstrates how to do so. Please let us know if there are any other ways we can help.
                to access onExecutionUpdate or onPositionUpdate , we use

                account.ExecutionUpdate += OnExecutionUpdate;

                account.PositionUpdate += OnPositionUpdate;

                how do we invoke OnOrderUpdate ???

                Comment


                  #9
                  Hello abhay,

                  It would be the same as the overrides you just don't have "On" in the name, its account.OrderUpdate.

                  I look forward to being of further assistance.
                  JesseNinjaTrader Customer Service

                  Comment

                  Latest Posts

                  Collapse

                  Topics Statistics Last Post
                  Started by geddyisodin, Today, 05:20 AM
                  4 responses
                  28 views
                  0 likes
                  Last Post geddyisodin  
                  Started by geotrades1, Today, 10:02 AM
                  2 responses
                  5 views
                  0 likes
                  Last Post geotrades1  
                  Started by ender_wiggum, Today, 09:50 AM
                  1 response
                  5 views
                  0 likes
                  Last Post NinjaTrader_Gaby  
                  Started by rajendrasubedi2023, Today, 09:50 AM
                  1 response
                  12 views
                  0 likes
                  Last Post NinjaTrader_BrandonH  
                  Started by bmartz, Today, 09:30 AM
                  1 response
                  10 views
                  0 likes
                  Last Post NinjaTrader_Erick  
                  Working...
                  X