Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Profit target depending on last bar

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

    #16
    It is written in that way, just got converted because of the forum.
    The errors could be translated something like, that for the Open command in the variables, an "object link" is needed. The second error says, that the "double" type can't be converted in "int".
    Of course the Open[1] was just an easy example. A bit more complicated could be Open[1]+0.02*Close[1] for example.

    Sepp

    Comment


      #17
      Sepp,

      Please provide the exact error text. It will also reference which line is causing the error. Please provide the code for the lines it is running into errors on. Thank you.
      Josh P.NinjaTrader Customer Service

      Comment


        #18
        I just don't get it, what the difference between the sample and my system is The problem is, that I first have to even define, what the profit target is. And this doesn't work, because if I use variables in SetProfitTarget, the variable doesn't allow me to define it as Open[1] or anything like that. The German error message is:

        Für das nich statische Feld, die Methode oder die Eigenschaft NinjaTrader.Strategy.StrategyBase.Open.get ist ein Objektverweis erforderlich.

        The second error message:

        Der Typ double kann nicht implizit in int konvertiert werden. Es ist eine explizite Konvertierung vorhanden. (Fehlt eine Umwandlung?)

        I hope, this can help you.

        Regards,

        Sepp

        Comment


          #19
          Sepp,

          Did you actually name a variable "Open"? Open is a reserved word and you cannot name any variable with that name.

          For the double to int one, you need to check the object type of your variables. Doubles can only be assigned to doubles and ints can only be assigned to ints.
          Josh P.NinjaTrader Customer Service

          Comment


            #20
            My actual code snippet:


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

            CalculateOnBarClose =
            true;
            }

            Comment


              #21
              Sepp,

              That will not work. You cannot access Open[] from there. If you want to set a variable to Open[1] you need to do it inside OnBarUpdate().
              Josh P.NinjaTrader Customer Service

              Comment


                #22
                Yes, but if I do it inside OnBarUpdate(), I see only the place instead of the variable. And there it doesn't seem to work when it gets a bit more complicated (like already said Open[1]+0.02*Close[1]). Is there another possibility or am I just acting wrong?
                Last edited by MasterSepp; 07-23-2009, 01:15 PM.

                Comment


                  #23
                  Sepp,

                  Not following you. You cannot access any DataSeries values outside like that. They don't exist in that context.

                  Not sure why you feel something like Open + Close would not work.

                  someVar = Open[1] + 0.02 * Close[1];

                  Will for sure work in OnBarUpdate().
                  Josh P.NinjaTrader Customer Service

                  Comment


                    #24
                    Now I got what you meant. Just tried it out... and again an error message -.-
                    Now it looks like this:

                    protected override void OnBarUpdate()
                    {
                    profittarget = Open[1]

                    blabla

                    EnterShort(DefaultQuantity,"");
                    SetProfitTarget(CalculationMode.Price, profittarget

                    blabla
                    }

                    It says that the name profittarget isn't contained in the actual context. Even when I put profittarget = Open[1] in the protected override void Initialize () it tells the same thing.
                    Can you help me again?

                    Sepp

                    EDIT: GOT IT. Will check tomorrow if it works, too tired now Now there's only the problem with the selling after one bar. Am I right with trying with Barssinceentry > 0?
                    Last edited by MasterSepp; 07-23-2009, 02:50 PM.

                    Comment


                      #25
                      Sepp,

                      When backtesting you will always trade on the next bar after the signal bar. This has to be the case because when backtesting, you are processing only single moments in time. The moments you are generating the signals from is the moment when you have the full OHLC of a bar. With the close price of a bar already known to you, that bar has no more feasible trading locations and thus all trades go to the open of the next bar.
                      Josh P.NinjaTrader Customer Service

                      Comment


                        #26
                        So it will automatically sell on this bar when I test it in realtime, right?
                        Back to the old problem: when backtesting, it doesn't give any error message, but it doesn't do anything at all. I think, I found the problem. Actually, my variable is set as "private double plus", where plus = Open[1] in the OnBarUpdate() section.
                        When I change it to private int plus and change plus=5000 or whatever, it works all well. Can you tell me, what I'm doing wrong? Because I can't define plus = Open[1] as long as it is written as "private int plus".
                        Again my code snippet:

                        #region Variables
                        privatedouble plus;
                        #endregion

                        protectedoverridevoid OnBarUpdate()
                        {
                        plus = Open[
                        1];

                        // Condition set 1
                        if (ToTime(Time[0]) >= ToTime(16, 0, 0)
                        && Close[
                        0] > Open[0])
                        {
                        EnterShort(DefaultQuantity,
                        "");
                        SetProfitTarget(CalculationMode.Price,plus);
                        }
                        // Condition set 2
                        if (ToTime(Time[0]) >= ToTime(16, 0, 0)
                        && Close[
                        0] < Open[0])
                        {
                        EnterLong(DefaultQuantity,
                        "");
                        SetProfitTarget(CalculationMode.Price,plus);
                        }

                        Regards,

                        Sepp

                        Comment


                          #27
                          Hello,

                          In real time it will depend on whether you have CalculateOnBarClose set to true or false. If set to true then execution will happen on the next bar like the backtesting does. If set to false, it will place the order on the tick that generates the order as soon as it qualifies.

                          For your "plus" variable, you will need to set it to the number of ticks above price you want the target to be. Open[1] is probably not what you intended. This link will help:
                          DenNinjaTrader Customer Service

                          Comment


                            #28
                            No I really need to be able to insert anything like Open[1] in the SetProfitTarget(). I found in the help guide the section called "Functions and Methods Explained". I've tried now this:

                            #region Variables
                            privatedouble variable;
                            privatedouble plus(double variable)
                            {
                            return variable = Open[1];
                            }
                            #endregion (all of this before OnBarUpdate() or Initialize() )

                            setting SetProfitTarget(CalculationMode.Price,variable), but now it doesn't set a profit target at all.
                            Could you perhaps write me the code snippet I need to define any variable as anything like Open[1]? This would help me alot.

                            Thanks in advance,

                            Sepp

                            Comment


                              #29
                              Sepp, try this for example in the OnBarUpdate() of yuor strategy, works as expected for me here on the 1 min ES 09-09 chart -

                              Code:
                               
                              if (CrossAbove(SMA(10), SMA(20), 1))
                              {
                              SetProfitTarget(CalculationMode.Price, Open[1]);
                              EnterLong();
                              }

                              Comment


                                #30
                                Thanks, I finally got it!
                                But there is already another question: I tried to programm that only close position if the last two bars had different colors (e.g. red - green or green - red). It looks like this:

                                if (BarsSinceEntry() == 0
                                && Close[1] < Open[1]
                                && Close[
                                2] > Open[2])
                                {
                                ExitLong(
                                "", "");
                                ExitShort(
                                "", "");
                                }

                                for the one side. But when I go now to check with the Strategy Analyzer, he doesn't anything at all; he neither buys or sells. Why this? There wasn't any error and he still doesn't work. Can you help me?

                                Regards,

                                Sepp

                                Comment

                                Latest Posts

                                Collapse

                                Topics Statistics Last Post
                                Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                                0 responses
                                636 views
                                0 likes
                                Last Post Geovanny Suaza  
                                Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                                0 responses
                                366 views
                                1 like
                                Last Post Geovanny Suaza  
                                Started by Mindset, 02-09-2026, 11:44 AM
                                0 responses
                                107 views
                                0 likes
                                Last Post Mindset
                                by Mindset
                                 
                                Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                                0 responses
                                569 views
                                1 like
                                Last Post Geovanny Suaza  
                                Started by RFrosty, 01-28-2026, 06:49 PM
                                0 responses
                                571 views
                                1 like
                                Last Post RFrosty
                                by RFrosty
                                 
                                Working...
                                X