Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Using variable integer multiple times in an iSeries

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

    Using variable integer multiple times in an iSeries

    Rather than explain it I'll show you an example of what I want to accomplish:

    xPrice[int] = yPrice - (CurrentBar - int)*slope;

    I want the "int" value input into for "xPrice" to be carried over the the right side of the equation. If I specify "xPrice[10]." I want the calculation to be
    "yPrice - (CurrentBar - 10)*slope;" So more specifically I'm looking for a Ninjascript way to say " If the integer input for xPrice[int] is whatever then the integer input on the right is the same."
    Correct syntax eludes me.

    #2
    Hello Doctor JR,

    To clarify here, are you asking how to write a multi purpose method where the variable int is a number you specify to get a result from that equation?

    If you have an int input or variable you could use it just like you have shown in your equation, it could be used as a BarsAgo with the series or as a number in the equation.

    Code:
    int myInt = 10; 
    
    xPrice[myInt] = yPrice - (CurrentBar - myInt)*slope;

    Comment


      #3
      I figured it out. Just needed to be more methodical, which is my existential coding challenge.

      Comment


        #4
        Actually I wasn't quite there but your example was helpful.
        What if, using your example, I want to create a Series <double> xPrice and the same "myInt" bars back integer used for xPrice is carried over to the right side of the equation?
        Is that even doable? So far my attempts to make it work have not proven so.

        Comment


          #5
          Hello Doctor JR,

          I am not certain I understand your use of carry over to the right. Are you asking to use the variable myInt again on the right side of the equation? If so that is the code I had provided.

          Comment


            #6
            I need to accomplish something related to your simple code example. However, unlike the example, the BarsAgo "myInt" figure needs to indefinite. I'll run the resulting series through MAX/MIN functions.
            I've got two problems: When the BarsAgo "myInt" figure is indefinite it does not get passed along to be used as a multiplier per your example. I would like to find a way to do that. In addition, i would very much like to pull the latest figure from a double and basically divorce it from how it was calculated and any prior values. It would be an isolated fixed number connected to nothing else though it will have originated as the most recent of bar-to-bar double values. The specific number depends on the latest price movement.
            I'm about to give up on this. Wasted to much time digging for solutions and experimenting. Easy to describe. Hard to code. Any thoughts?

            Comment


              #7
              Hello Doctor JR,

              I need to accomplish something related to your simple code example. However, unlike the example, the BarsAgo "myInt" figure needs to indefinite. I'll run the resulting series through MAX/MIN functions.
              I've got two problems: When the BarsAgo "myInt" figure is indefinite it does not get passed along to be used as a multiplier per your example.
              Unfortunately this description did not help. Perhaps if we just use actual values that could help to explain what you want. In the previous example I provided the 10 would be passed along to get used with the multiple:

              Code:
              int myInt = 10;
              xPrice[myInt] = yPrice - (CurrentBar - myInt)*slope;
              would equate to:
              Code:
              xPrice[10] = yPrice - (CurrentBar - 10)*slope;
              Are you asking to do something else? If so please provide an example like above which includes the hard coded value so I can better understand what task you want to accomplish.


              In addition, i would very much like to pull the latest figure from a double and basically divorce it from how it was calculated and any prior values. It would be an isolated fixed number connected to nothing else though it will have originated as the most recent of bar-to-bar double values. The specific number depends on the latest price movement.
              It sounds like you mean a variable here. A variable can contain a result from a calculation and then you can use it later, that variable holds no information about the original calculation its just a result. Depending on where the variable is defined it would contain a result for a single bar or for the length of the script. For example:

              Code:
              double c = a + b;
              c only contains the result, a and b were the original values used in the calculation of c.

              An example of this concept using bar data would be:

              Code:
              double diff = Close[0] - Open[0]; 
              
              Print(diff);
              This would return the value for each bar and would only retain the value for a single bar.






              Comment


                #8
                Thanks for getting back to me. Sorry I didn't describe it better. My code is rather convoluted and I don't want you to have to dig through it so I tried to describe things in the most general terms which I suppose were so general as to lose meaning. I'm going to ponder these issues a bit more an I'll get back to you.

                Comment


                  #9
                  I'm drawing trend channels with parallel upper and lower lines. For purposes of this conversation I'll use terminology pertaining to only upward sloping lines (Actually rays as in Draw.Ray). When a bar goes over the upper line I adjust the slope (The line price pricing correspondingly changes) so that the upper line contains the price movement. It works fine.

                  However, as the slope of the parallel lines increase, previous bars that had been inside the lower sloping channel are now to the right of the the lower channel line. I want to adjust that lower line (while keeping it parallel to the upper line) so as to contain those bars. It's what I do when manually drawing trend channels.

                  To make this adjustment I need to reference a revised price of the lower line at the "violating" bars. That's where I run into problems because the trend line price at those previous bars was calculated at a previous (lower) slope value. To do this I need to retroactively re-price that prior section of the trend line as if the slope had been the same as the current bar slope.

                  It's not so simple to "override" those older prices recording by Ninjatrader.

                  Maybe there's a better solution. But if I were able to do this retroactive re-pricing my code might look something like this (though this of course doesn't accomplish the goal).

                  public class MyStrategy : Strategy
                  {
                  private double MostRecentPrice;
                  private Series <double> MostRecentPriceSeries;
                  private double MostRecentSlope;
                  private double: ViolationAmt;
                  private int StartBar // where the Line/Ray started
                  private int BarsBetween; // Difference between CurrentBar and StartBar
                  private int: myInt;
                  }

                  // Other standard stuff //

                  else if (State == State.DataLoaded)
                  {
                  MostRecentPriceSeries = new Series<double>(this, MaximumBarsLookBack.TwoHundredFiftySix);
                  }

                  protected override void OnBarUpdate()
                  {
                  // the usual CurrentBar stuff //

                  BarsBetween = CurrentBar - StartBar;

                  MostRecentPriceSeries[myInt] = (MostRecentPrice - (MostRecentSlope*myInt)) - Low[myInt];

                  ViolationAmt = MAX(MostRecentPriceSeries, BarsBetween)[1];

                  ///////////////////////////////////////////////////////////

                  To make this work I need "MostRecentPrice" to be constant throughout the evaluation of the previous bars where the trend line price had actually been different (due to a prior lower slope) when the prior bar at issue closed. I havent' been able to work that out. In addition, using the unspecified "myInt" integer works when used for "BarsAgo" but it's not recognized as a multiplier.

                  As for keeping "MostRecentPrice" fixed throughout the series, I've tried all kinds of tricks, including converting it to a string then back to a double. Nothing has worked for me. Similar results when using the unspecified "myInt" integer as a multiplier.

                  Any thoughts? Any questions? This isn't the most critical element of my strategy but it would be nice to avoid manually drawing lines and focus on other things.

                  Comment


                    #10
                    Hello Doctor JR,


                    It's not so simple to "override" those older prices recording by Ninjatrader.
                    If you mean to reset the values in the series that you made then you can use more advanced syntax to physically reset past bar values in that series. As I am not sure that is what you mean I won't provide a sample but here is a link to the reset method: https://ninjatrader.com/support/help...lightsub=reset



                    I havent' been able to work that out. In addition, using the unspecified "myInt" integer works when used for "BarsAgo" but it's not recognized as a multiplier.
                    This code specifically is not valid because of the colon:

                    Code:
                    private int: myInt;
                    it would be

                    Code:
                    private int myInt = 10;
                    To make this work I need "MostRecentPrice" to be constant throughout the evaluation of the previous bars where the trend line price had actually been different (due to a prior lower slope) when the prior bar at issue closed. I havent' been able to work that out.

                    As for keeping "MostRecentPrice" fixed throughout the series, I've tried all kinds of tricks, including converting it to a string then back to a double. Nothing has worked for me. Similar results when using the unspecified "myInt" integer as a multiplier.
                    I am not sure what you mean by fixed through the series, if you mean a constant value for each OnBarUpdate that is what you have now. A class level variable holds a value until you reset it to something else. I don't see where you set that variable in the code you provided, if that needed to be a constant value you could set it like this:

                    Code:
                    private double MostRecentPrice = 100; //any constant number you needed
                    If you set that somewhere in OnBarUpdate its going to be reset its time OnBarUpdate is called. If you needed to set it once you can also use OnStateChange State.DataLoaded for that purpose.


                    Comment


                      #11
                      Thanks for the effort you've put into this little thread.
                      One might phrase what I've been attempting as backward pricing and I figured it out just prior to checking my email and seeing that you'd responded to my latest post I really do appreciate the work that you and your associates do.

                      With reference to the last code I posted, I was hung up on the BarsAgo or "myInt" figure used a multiplier of "MostRecentSlope" used in determining a backward price. My solution was to convert CurrentBar into a double, then create a corresponding double series which I could then plug into a MAX function. So "MAXcurrentBar - CurrentBar became my mulitplier and it works.

                      Thanks again for you time.

                      Comment

                      Latest Posts

                      Collapse

                      Topics Statistics Last Post
                      Started by NullPointStrategies, Today, 05:17 AM
                      0 responses
                      38 views
                      0 likes
                      Last Post NullPointStrategies  
                      Started by argusthome, 03-08-2026, 10:06 AM
                      0 responses
                      124 views
                      0 likes
                      Last Post argusthome  
                      Started by NabilKhattabi, 03-06-2026, 11:18 AM
                      0 responses
                      64 views
                      0 likes
                      Last Post NabilKhattabi  
                      Started by Deep42, 03-06-2026, 12:28 AM
                      0 responses
                      41 views
                      0 likes
                      Last Post Deep42
                      by Deep42
                       
                      Started by TheRealMorford, 03-05-2026, 06:15 PM
                      0 responses
                      46 views
                      0 likes
                      Last Post TheRealMorford  
                      Working...
                      X