Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Profit target depending on last bar

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

    Profit target depending on last bar

    Hi there,

    Got a little problem while programming my strategy, concerning the Profit targets. I want to make them dependent of the last bar. So, let's say, the profit target of a position should always be on the Open of the last bar. I have really no idea, how I should get this.
    Can anyone help me?

    Thanks in advance

    Sepp

    #2
    Sepp, you would need to adjust your Target value on each bar then, this sample should help - http://www.ninjatrader-support2.com/...ead.php?t=3222

    Comment


      #3
      Thanks for the link, but it didn't help me as much as I hoped... My problem is, that for example I go long, when the bar was red (and vice versa). Now, I want to take the profit at the Open of the bar before. I tried it with SetProfitTarget(CalculationMode.Price, Open[1]) but it doesn't work
      Can you help me again?

      Thanks in advance

      Sepp

      Comment


        #4
        Sepp,

        Please provide the complete code snippet you are using. Thank you.
        Josh P.NinjaTrader Customer Service

        Comment


          #5
          Here it is:


          protected
          overridevoid Initialize()
          {
          SetProfitTarget(
          "", CalculationMode.Price, Open[1]);
          CalculateOnBarClose =
          false;
          }
          ///<summary>
          /// Called on each bar update event (incoming tick)
          ///</summary>
          protectedoverridevoid OnBarUpdate()
          {
          // Condition set 1
          if (Close[0] > Open[0])
          {
          EnterShort(DefaultQuantity,
          "");
          }
          // Condition set 2
          if (Close[0] < Open[0])
          {
          EnterLong(DefaultQuantity,
          "");
          }
          // Condition set 3
          if (BarsSinceEntry() > 0)
          {
          ExitLong(
          "", "");
          ExitShort(
          "", "");
          }
          }
          Just saw that the problem is in my opinion, that it exits only on the open of the next bar and not as soon as the profit is reached.

          Regards,

          Sepp

          Comment


            #6
            Sepp,

            You can't put it in Initialize(). Open[1] is not recognized there. You need to put it in OnBarUpdate().
            Josh P.NinjaTrader Customer Service

            Comment


              #7
              Thank you very much, now it works better but still not as I want. The main problem now is, that once the Profit target is triggered, he sells only on the next bar.
              I've put the SetProfitTarget now in a if (Market.Position == Long/Short) {SetProfitTarget(...)}, because he didn't do anything when I just wrote it into the code alone. Is there another way to set the target as soon as the position is opened?

              Regards,

              Sepp

              Comment


                #8
                Sepp,

                Not following you. When profit target is reached it will execute immediately. If it is not reached then nothing will happen.

                You should be calling your Set() method before you call your Enter() method.
                Josh P.NinjaTrader Customer Service

                Comment


                  #9
                  I still don't get it... Tried yesterday the whole evening... Here my code again:

                  Again my strategy: if a bar closes green, I want to sell short on the open of the next bar. The profit target for this example is the open of the last bar (the green bar e.g.) + 2% of the Close of the same bar. If the target shoulnd't be reached until the current bar closes, the position shall be closed.
                  Can you help me?

                  Thanks in advance again

                  Sepp

                  Comment


                    #10
                    Here's the code, cause he was too long for the other post:

                    protectedoverridevoid OnBarUpdate()
                    {
                    if (Position.MarketPosition == MarketPosition.Long)
                    {
                    SetProfitTarget(
                    "", CalculationMode.Price, Open[1]+0.02*Close[1]);
                    }
                    if (Position.MarketPosition == MarketPosition.Short)
                    {
                    SetProfitTarget(
                    "", CalculationMode.Price, Open[1]-0.02*Close[1]);
                    }

                    if (Close[0] > Open[0])
                    {
                    EnterShort(DefaultQuantity,
                    "");
                    }

                    if (Close[0] < Open[0])
                    {
                    EnterLong(DefaultQuantity,
                    "");
                    }

                    if (BarsSinceEntry() > 0)
                    {
                    ExitLong(
                    "", "");
                    ExitShort(
                    "", "");
                    }

                    Comment


                      #11
                      MasterSepp, please try printing the values you set as targets, so they actually return what you expect. You also want to reset those values when you're flat, as mentioned in this sample - http://www.ninjatrader-support2.com/...ead.php?t=3222

                      Comment


                        #12
                        First, I don't really understand, what you mean with "printing the values ... so they return what you expect"?
                        Second, I tried like in the PriceModification-Sample, but when I enter anything other than a number in the variables (for example Open[1]), he can't compile it correctly.
                        Can you help me again please?

                        Thanks in advance

                        Sepp

                        Comment


                          #13
                          Sepp, I refererred to printing the value for the stop loss to loss to the output window with the Print command - http://www.ninjatrader-support2.com/...ead.php?t=3418

                          This way you can easily check the calculated values on each bar and if they turn out to be what you expect.

                          What compile error do you receive in the Price modification sample?

                          Comment


                            #14
                            I don't think, that I have to use the print command, because I think to know what the system does My 2 main problems are just that the position is closed when it's hold longer than one bar. Second problem: the "complicated" profit target. My NT is in German, so I can tell you the exact problem, but the Codes are CS0120 and CS0266. Here the excerpt of the code:

                            #region Variables
                            privateint profittargetprice = Open[1];
                            #endregion
                            ///<summary>
                            /// This method is used to configure the strategy and is called once before any strategy method is called.
                            ///</summary>
                            protectedoverridevoid Initialize()
                            {
                            SetProfitTarget(CalculationMode.Price,profittarget price);

                            CalculateOnBarClose =
                            true;
                            }

                            Comment


                              #15
                              On the compile error, please try SetProfitTarget
                              Code:
                              (CalculationMode.Price,profittargetprice);
                              For a long trade in a rising market, this would get filled immediately I think, as you place a marketable limit order with this as the last price is most likely higher than the open then.

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                              0 responses
                              597 views
                              0 likes
                              Last Post Geovanny Suaza  
                              Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                              0 responses
                              343 views
                              1 like
                              Last Post Geovanny Suaza  
                              Started by Mindset, 02-09-2026, 11:44 AM
                              0 responses
                              103 views
                              0 likes
                              Last Post Mindset
                              by Mindset
                               
                              Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                              0 responses
                              556 views
                              1 like
                              Last Post Geovanny Suaza  
                              Started by RFrosty, 01-28-2026, 06:49 PM
                              0 responses
                              555 views
                              1 like
                              Last Post RFrosty
                              by RFrosty
                               
                              Working...
                              X