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

ProfitTarget - reset default

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

    ProfitTarget - reset default

    Hello,

    I have a question because of a strange behavior with my understanding.

    In the script strategy I have only in "if(Position.MarketPosition == Flat)
    {SetStopLoss...
    SetProfitTarget...
    }

    But when I´m in a trade and I move the profit-target in chart trade in another chart then it snaps back to profittarget as here above. But why when not flat? There is no more line with SetProfitTarget in the code (I removed all)

    Is it possible that the reason is because of using different dataseries? So as script is working COBCtrue and I have a 15 minute dataseries as well for this dataseries its still "flat". And therefore SetProfitTarget snaps back to default?

    Thank you!
    Tony

    #2
    Hello Tony,

    Thanks for your question.

    Set methods will update based on the CalculateOnBarClose setting and the data series that the strategy is applied against. If you would like to allow your profit target and stop loss to be changed via Chart Trader and to not have them reverted to their initial level with subsequent data updates, I suggest following an approach similar to the SampleOnOrderUpdate strategy.

    SampleOnOrderUpdate - https://ninjatrader.com/support/help...and_onexec.htm

    Please let me know if I can be of further assistance.
    JimNinjaTrader Customer Service

    Comment


      #3
      Hello,

      thank you for your reply. Following that logic I have only in OnExecution

      if(entryOrderaSa != null && entryOrderaSa == execution.Order)
      {
      if(execution.Order.OrderState == OrderState.Filled)
      {
      entryprice = entryOrderaSa.AvgFillPrice;
      SetStopLoss("aS2a", CalculationMode.Ticks, istop2c, false);
      SetStopLoss("aS2b", CalculationMode.Ticks, istop2c, false);
      SetProfitTarget("aS2a", CalculationMode.Ticks, 5);

      if (execution.Order.OrderState != OrderState.PartFilled)
      {
      entryOrderaSa = null;
      }
      }
      }


      There is no more code in the script for SetProfitTarget but it snaps back when I move it after the entry!

      ??

      Thank you!
      Tony

      Comment


        #4
        Hello Tony,

        We would suggest using Exit methods in OnExecution to place your profit target and stop loss as opposed to using Set methods if you would like them to be adjustable with Chart Trader.

        Please let us know if you have any questions.
        JimNinjaTrader Customer Service

        Comment


          #5
          Hello,

          thank you for your reply. They do not work OCO by default as I remember from another thread(?)

          Why should the above logic not work, its the only code in the script for stoploss and profittarget because when I comment it then there is no stop nor target with entry.

          Thank you!
          Tony

          Comment


            #6
            Hello Tony,

            The SampleOnOrderUpdate strategy provides logic to cancel the stop loss/profit target when one of the order fills. The strategy handles the functionality that would be handled by OCO. If you want to utilize OCO so the profit target/stop loss get cancelled if one of the orders fill and NinjaTrader is offline, the Unmanaged Approach can be used. I have attached an example strategy that uses the Unmanaged Approach and models after the SampleOnOrderUpdate strategy. This strategy uses OCO for the profit target and stop loss orders which are submitted in OnExecution.

            Set methods will update depending on Bar Updates and the CalculateOnBarClose mode that is used. They would not be used to place a profit target/stop loss that is freely moveable with Chart Trader.

            Unmanaged Approach - https://ninjatrader.com/support/help...d_approach.htm

            I look forward to being of further assistance.
            Attached Files
            JimNinjaTrader Customer Service

            Comment


              #7
              Hello,

              thank you for your reply. I was trying to follow basically the logic from this nt-reference sample "SamplePriceModification" and it seemed to work in the past where SetStopLoss and SetProfittarget is modifed in the code or outside the code when using a bool in the condition. So when adding here a bool that sets the logic to false eg 2 bars after the entry, then the orders can be modified 3 bars after entry by chart-trader and should not be reset/snapback. No?

              Thank you!
              Tony

              Comment


                #8
                Hello Tony,

                A test strategy can be set up to test this behavior.

                You can enable this test strategy against the Simulated Data Feed to observe how the Stop/Target submitted with a Set method will be updated based on the data series and the CalculateOnBarClose option.

                Please let me know if you have any questions.
                Attached Files
                JimNinjaTrader Customer Service

                Comment


                  #9
                  Hello,

                  I have done as the SamplePriceModificationSetTest and with this it also snapsback, moreover the stoploss is added only the next bar (as script is working cobctrue).

                  When I try the solution with Iorders as it is in sampleOnOrderUpdate I have a problem: how do I reset the stop when it is not closed by target or stop? I do not use setprofittarget because this works not oco with fxcm. So as it occurred already once connection lost I´m naked if one is triggered and then the other side. I exit with targets by condition. How should the stoploss then be reset please to work with the logic from sampleOnOrderUpdate.

                  Thank you!
                  Tony
                  Last edited by tonynt; 12-09-2019, 11:28 AM. Reason: typo

                  Comment


                    #10
                    Hello Tony,

                    Set methods update the target/stop based on the CalculateOnBarClose setting. The test script demonstrates this.

                    SampleOnOrderUpdate approach would not need to reset the stop loss. The stop order is submitted with the price specified with the ExitLongStop order method. NinjaTrader will need to be running in order to see that the position associated with "MyEntry" is closed and the other order should be cancelled. (This would be done from the internal rules of the Managed Approach to cancel an order when the associated position is closed.)

                    If you would like to have the order cancelled via OCO (in the case that you want the order cancelled outside of NinjaTrader if the other OCO'd order fills and there has been a connection loss) I would recommend using the Unmanaged Approach so you can submit your limit and stop market orders for Profit Target and Stop Loss with OCO ID's. This will allow the target or stop to be cancelled if NinjaTrader is disconnected and the other order with the same OCO ID fills.

                    Please let me know if I can be of further assistance.
                    JimNinjaTrader Customer Service

                    Comment


                      #11
                      Hello,

                      thank you for your reply.

                      A.) I dont know what you mean. In this sample "...test", even with the bool the stoploss is set to its basic value.

                      B.) Referring the sampleOnOrderUpdate there is in line 112 (and the following) the set stoporder = null;
                      So do you mean now this code in the sample is not necessary? This is what I´m referring to that I dont know how to set stoporder=null when I exit with condtion)

                      C.) No, I do not want to cancel it OCO as in the meantime I experienced internet problems and I recognized that the bracket will not work on forex broker servers.

                      The understanding of B.) would be important for me. Now I´m confused.

                      Thank you!
                      Tony

                      Comment


                        #12
                        Hello Tony,

                        When you use a Set method, NinjaTrader will move the stop loss to the level specified with the Set method following the CalculateOnBarClose setting, regardless of if the Set method is called again or not.

                        In the SampleOnOrderUpdate strategy, the stopOrder and targetOrder IOrder objects are set to null when the target/stop order are executed. Following the example, if NinjaTrader sees the target or stop fill, the IOrder object is set to null. This does not cancel the order but would be good practice for IOrder object management. (The other order cancels because it is tied to the same entry following Entry signals and the strategy position gets closed out.) When using IOrders, we can check if the IOrder is not null to see if an order is active. (This is where the strategy checks if entryOrder is null in OnBarUpdate before calling EnterLong.) When the order executes or cancels and becomes terminal, we will want to set the IOrder object to null.

                        If you would like to cancel the other order when once the target or stop is cancelled, you can check if that order has been cancelled in OnOrderUpdate. IOrder objects should then be set back to null since the order is terminal.

                        I have attached a modification to the SampleOnOrderUpdate strategy that will cancel Target/Stop if one is cancelled. This example checks for order cancellation in OnOrderUpdate, calls CancelOrder and then sets the IOrder object to null.

                        Please let me know if you have any additional questions.
                        Attached Files
                        JimNinjaTrader Customer Service

                        Comment


                          #13
                          Hello,

                          thank you for your reply. Maybe the misunderstandings are caused with my english, I dont know.

                          The updatesample does cancel the order. OK.
                          But the question was how the stoporder has to be set to null when position is closed by condition. In this case the position its not closed by stoporder=filled nor targetorder=filled.
                          How the stop is set to null then? This is what I asked.

                          To resolve this I allow to do 2 questions as suggestions: a.) is it necessary to set stoporder to null? Or might it cause a problem with a new entry when it is not?
                          b.) can a targetorder be tied to an exit with conditions and not only working like a "normal" profittarget can the code with targetorder=filled is also working in this case.

                          Thank you!
                          Tony
                          Last edited by tonynt; 12-10-2019, 03:23 AM. Reason: incorrect translation

                          Comment


                            #14
                            Hello Tony,

                            But the question was how the stoporder has to be set to null when position is closed by condition. In this case the position its not closed by stoporder=filled nor targetorder=filled.
                            How the stop is set to null then? This is what I asked.
                            It is best practice to set IOrder objects to null when the order is terminal. An IOrder that is not null would then represent an active order.

                            You can set the IOrder to null in OnExecution when the order is filled, and also can be done OnOrderUpdate to trap when the order is cancelled. The example in post #12 can demonstrate how the IOrder objects are set to null.

                            a.) is it necessary to set stoporder to null? Or might it cause a problem with a new entry when it is not?
                            This would be best practice. If you do not have a reason to track the order as an IOrder, it would not be necessary to use the IOrder object. I, however, would still recommend doing so for programming consistency.

                            b.) can a targetorder be tied to an exit with conditions and not only working like a "normal" profittarget can the code with targetorder=filled is also working in this case.
                            You can still tie exits to entries following EntrySiongals/FromEntrySingals. If you would like to check if the tragetOrder has executed, you can check if the executing order in OnExecution (execution.Order) matches targetOrder, similarly to how OnExecution checks if entryOrder has executed.

                            Please let me know if you have any additional questions.
                            JimNinjaTrader Customer Service

                            Comment


                              #15
                              Hello,

                              thank you for your reply. But we do not make progress. You reply that targetorder can be checked if executed. Yes, there is no doubt about, this is also in the samples.

                              My question I did was when not using a targetorder as shown in the sample how the stoporder can be set to null when the trade is not closed by a stop nor by a target as in the sample but with a condition to exit eg. if High[0]>SMA... then ExitLong(1,"Entry1"). How does this work as a targetorder so this can be checked in onexecution.

                              if(High[0]>SMA(50)[0]) TargetOrder1=.... ??

                              Thank you!
                              Tony

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by ageeholdings, Today, 07:43 AM
                              0 responses
                              2 views
                              0 likes
                              Last Post ageeholdings  
                              Started by pibrew, Today, 06:37 AM
                              0 responses
                              4 views
                              0 likes
                              Last Post pibrew
                              by pibrew
                               
                              Started by rbeckmann05, Yesterday, 06:48 PM
                              1 response
                              14 views
                              0 likes
                              Last Post bltdavid  
                              Started by llanqui, Today, 03:53 AM
                              0 responses
                              6 views
                              0 likes
                              Last Post llanqui
                              by llanqui
                               
                              Started by burtoninlondon, Today, 12:38 AM
                              0 responses
                              12 views
                              0 likes
                              Last Post burtoninlondon  
                              Working...
                              X