Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Reference a custom value

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

    Reference a custom value

    Hi I would like to know how I can reference the last value of a calculated variable?

    for example something like calculating if we have a new higher low:

    LoLo = Swing(10).SwingHigh[0]
    if LoLo > LoLo[1] then ....

    #2
    phase,

    you would have to use a DataSeries type variable.

    Code:
    private DataSeries sLoLo;
    
    Initialize()
    {
         sLoLo = new DataSeries(this);
    }
    
    OnBarUpdate()
    {
    
        sLoLo.Set(Swing(10).SwingHigh[0]);
        if(sLoLo > sLoLo[1])
          ...
    }
    Or, you could simply do this

    Code:
    if(Swing(10).SwingHigh[0] > Swing(10).SwingHigh[1])
      ...
    mrlogik
    NinjaTrader Ecosystem Vendor - Purelogik Trading

    Comment


      #3
      thanks for your fast response!

      the second solution would even be nicer for me.
      did not know this will reference the last swing low.
      I thought it is pointing to the swing low value of one bar ago, and this would possibly the same as from current bar.

      Comment


        #4
        oh, yes.

        you are correct phase. If using a swing hi / low, it will be the last bars value. I was thinking in terms of an MA.

        In that case, I would do something like this.

        Code:
        private DataSeries sLoLo;
        
        Initialize()
        {
             sLoLo = new DataSeries(this);
        }
        
        OnBarUpdate()
        {
            if(Swing(10).SwingHigh[0] != Swing(10).SwingHigh[1])
                sLoLo.Set(Swing(1).SwingHigh[0])
            else
                sLoLo.Set(sLoLo[1]);
            
            if(sLoLo > sLoLo[1])
              ...
        }
        my misunderstanding...
        mrlogik
        NinjaTrader Ecosystem Vendor - Purelogik Trading

        Comment


          #5
          ok thats what I tried before...

          I get an error that says I can't use operator like ">" on NinjaTrader.Data.DataSeries and double can't be converted

          maybe I should explain what I want to do, there might be an easier way? :-D

          I want to use a long profit target which should always be the distance of last swing low to the entry price. Below code works fine as long as there is no new swing low. As soon as there is a higher low, this will move the profit target.

          Code:
          {
          ExitLongLimit (2, Position.AvgPrice + (Position.AvgPrice-(Swing(10).SwingLow[0])), "", "");
          }

          Comment


            #6
            I'm writing this code without NT so its sometimes difficult to see my simple syntax mistakes.

            Code:
            if(sLoLo[COLOR=Red][B][0][/B][/COLOR] > sLoLo[1])
            I see what you're trying to do. Why not just use the targetprofit function that NT has.
            This way, you can call that function with exit price you want to use specifically.
            mrlogik
            NinjaTrader Ecosystem Vendor - Purelogik Trading

            Comment


              #7
              I also tried targetprofit but as far as I understood there cant be any calculated values used, just fixed numbers.

              Comment


                #8
                maybe someone else could help me?

                I added a jpeg to illustrate what I would like to do.

                The profit target should always be the same value like entryprice to swinglow(long) or swinghi(short).

                as mentioned the code
                Code:
                {
                ExitLongLimit (2, Position.AvgPrice + (Position.AvgPrice-(Swing(10).SwingLow[0])), "", "");
                }
                works fine as long as there is no higher swing low. higher swin lows modify the profit target and this should not happen.
                Attached Files

                Comment


                  #9
                  hey phase I'm still here.

                  If you have a target price you want to use you can do this.

                  Code:
                      SetProfitTarget(CalculationMode.Price, vSomePrice);
                  Note, you will have to call this function two times per entry, if the value changes.

                  The first time is when prior to submitting the entry order.
                  The second time is whenever the High / Low swing changes, so you can reference the code from a few posts ago.

                  does that clear things up for you?
                  mrlogik
                  NinjaTrader Ecosystem Vendor - Purelogik Trading

                  Comment


                    #10
                    he mrlogic cool you'r still here :-D

                    ok I tried it this way:

                    Code:
                            protected override void Initialize()
                            {
                                CalculateOnBarClose = true;
                    
                    
                                if (Position.MarketPosition == MarketPosition.Long
                                    && Position.Quantity == 4)
                                {
                                    diff=Position.AvgPrice + (Position.AvgPrice-(Swing(Swingbars).SwingLow[0]));
                                    SetProfitTarget(CalculationMode.Ticks, diff);
                                }
                        
                    
                                if     (Position.MarketPosition == MarketPosition.Short
                                    && Position.Quantity == 4)
                                {
                                    diff=Position.AvgPrice - ((Swing(Swingbars).SwingHigh[0])-Position.AvgPrice);
                                    SetProfitTarget(CalculationMode.Ticks, diff);
                                }
                    But somehow NT does not like to load the strategy.

                    Besides this I have the problem that SetProfitTarget will sell my full position, but I only want to sell half position at profit target. :-(

                    Comment


                      #11
                      What do you mean NT doesn't load the strategy? Does it compile?

                      Also, don't forget that the swing indicator paints. If you have swingbars = 4, it will be 4 bars later than you visually see.

                      Did you check the LOG for any errors?
                      If you want to exit half the position, then you need to break up your entry orders. You have to have an entry for each corresponding exit, ie if you want 2 targets, you need to have 2 entries.

                      I'm assuming this is only your exit code, correct?
                      mrlogik
                      NinjaTrader Ecosystem Vendor - Purelogik Trading

                      Comment


                        #12
                        yes it does compile.

                        oh I see the error log entry:

                        13.05.2009 14:24:29 Strategy Failed to call method 'Initialize' for strategy 'Top1': 'Position' property can not be accessed from within 'Initialize' method



                        "Also, don't forget that the swing indicator paints." hmm... actually it also works if it doesnt paint, but anyway will lat it paint.

                        "If you want to exit half the position, then you need to break up your entry orders" oh godness :-(

                        "I'm assuming this is only your exit code, correct?" YES
                        Last edited by Phaseshifter; 05-13-2009, 07:39 AM.

                        Comment


                          #13
                          Okay, so that code you have in the Initialize() procedure, should be in the OnBarUpdate() procedure...

                          As for the exit, I haven't yet coded partial exits, so I'm sorry I can't assist 100%; Maybe NT can assist?

                          However you're using the swing high / low, just remember that sometimes the plots on the chart you see may of may not be there when you think they are, thats all.

                          I hope this helps.
                          mrlogik
                          NinjaTrader Ecosystem Vendor - Purelogik Trading

                          Comment


                            #14
                            Phaseshifter,

                            You have to scale-in before you can scale-out partial exits. And also to reiterate, Swing indicator does indeed repaint itself in the future so it is very hard to visually test your strategy since the values you see on bar #10 will not be the same as when you look at it again after you receive bar #15.
                            Josh P.NinjaTrader Customer Service

                            Comment


                              #15
                              Originally posted by mrlogik View Post
                              Okay, so that code you have in the Initialize() procedure, should be in the OnBarUpdate() procedure...
                              hmmm... but then again it's calculated every new bar?
                              I thinbk this is bad? ok but I will try it.

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                              0 responses
                              637 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