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

Fix Value of double for look back Calculations

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

    Fix Value of double for look back Calculations

    I've created custom trend lines in which the slope can increase in absolute value. The current price of the trend line reflects the most recent slope. That works fine for me.

    HOWEVER, I've got a situation in which I need to calculate a trend line price at previous bars reflecting the most recent (revised) slope value. I've tried multiple approaches to doing this with a fixed (at the most recent value) slope but the price of those previous bars always reflects the slope that was in effect at the time the previous bar was formed, and, indeed, those WERE the correct values at the time, but they don't correspond with the revised trend line I now see drawn on my chart. For the most part I'm fine with that as the current trend line price is the main thing, but I've got this ONE other "looking back thing" I want to do.

    More specifically this pertains to a MAX/MIN operation in which the slope value is a component of the iSeries<double> input. Is there a way for me to keep the slope value fixed at the most recent value for such a purpose?

    In a sense I'm trying to rewrite history, saying "For this purpose, don't use the value that was in effect at the time, use this other value instead."


    #2
    As soon as I posted the above it hit me that the answer may involve converting the most recent slope value into a string (which I can do) and then converting that string into a double that the MAX/MIN operation can use. I do not know the proper syntax for the last part. I have difficulty translating the applicable C# code (I believe it may be the "Double.TryParse Method") into the Ninjascript equivalent.

    Comment


      #3
      Current slope double is "SlopeA"

      I convert it to a sting value with:
      SlopeB = SlopeA.ToString ("0.00000000");

      That works and prints the correct value for "SlopeB."

      Now I just need to convert string value SlopeB into a double which is "SlopeC" and apparently it's more complicated than going from double to string.

      Comment


        #4
        Hello Doctor JR,

        Thanks for your post.

        It sounds like you want to use MAX/MIN on a custom Series of your calculated slopes, but you always want the current value on the chart to be included in the MAX calculation.

        If this is the case, I would suggest making a custom Series<double> that you would populate with the values you specifically want included with the MAX calculation.

        Series<double> - https://ninjatrader.com/support/help...ml?seriest.htm

        Using a Series<double> for custom input to an indicator - https://ninjatrader.com/support/help...taseries_o.htm

        Let us know if there is anything else we can do to help.
        JimNinjaTrader Customer Service

        Comment


          #5
          I believe I have a decent understanding of making a a custom Series<double> but as long as my double input into the MAX/MIN calculation has a variable component I'm not going to get what I want.

          When I actually write the current numerical slope value into "price" double going into the MAX/MIN function I get the desired result, but that's not very practical with something that updates every fraction of a second (Heh heh).

          If I can just translate the slope string value back into a double I believe I'll have the solution. Do you have a snippet of code for that? I know you guys are not in the business of writing code for customers, but this would serve as a generic example. It's simply about converting a string into a double. I suspect that has some general value in the broad Ninjatrader community of fledgling programmers/traders.

          Let's say "SlopeA" is the original double value that can change bar-by-bar.
          Let's say "SlopeB" is the string value of SlopeA's most recent value (I know how to do that)
          Let's say "SlopeC" is the double value corresponding with the SlopeB string value so I can use it in subsequent operations (I CANT FIGURE OUT how to do that)

          Maybe you didn't see my subsequent responses to the original post. You get a "Like" anyway.

          Comment


            #6
            Hello Doctor JR,

            There are several resources on parsing strings in C# to convert them to doubles. As it is a C# concept, and is not NinjaScript, it is not something we would have an example on.

            A publicly available resource on converting strings to doubles can be found here - https://docs.microsoft.com/en-us/dot...tframework-4.8

            You should not need to do this anyhow, since your main goal is to modify what you give MAX(). If you work with double variables, you should not need to use a string.

            MAX will take a Series<double> as input, so we can consider populating our Series<double> with our own values to include.

            For example, MAX(MySeries, 5)[0] would mean "get the last 5 MySeries values and find the maximum."

            Since it is a custom Series<double>, you can assign this any value you like.

            So...

            Code:
            MySeries[4] = SomeValue;
            MySeries[3] = SomeValue;
            MySeries[2] = SomeValue;
            MySeries[1] = SomeValue;
            MySeries[0] = SomeValue;
            Print(MAX(MySeries, 5)[0]);
            (where SomeValue is always different, and MySeries is a custom Series<double>)

            will always print the maximum of those 5 values that you assigned in your Series. (This can be applied to using other indicators to use them for a custom calculation.)

            You can also consider using Math.Max to find the maximum between limited number of doubles. Then you would just need to track the variables you want to include with the calculated maximum as private variables.
            JimNinjaTrader Customer Service

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            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
            11 views
            0 likes
            Last Post burtoninlondon  
            Started by AaronKoRn, Yesterday, 09:49 PM
            0 responses
            16 views
            0 likes
            Last Post AaronKoRn  
            Working...
            X