Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Using Structs with Indicator within Strategy

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

    Using Structs with Indicator within Strategy

    Hi all,

    I am trying to implement the following from within the variables section and it is throwing an error.

    private double[] test = SMA(fast)[0];
    private double testCurrentBar = test[1];

    Then in my code I want to do something like:

    if (testCurrentBar > 0)

    ---Note---
    I am not really interested in SMA, rather any indicator and getting the value of the indicator for the current bar, last bar, bars back, etc.

    I'm just tired and it's not clicking, if anyone could lend a hand it would be appreciated.


    Last edited by r2kTrader; 05-27-2009, 02:34 PM.

    #2
    r2kTrader,

    I really don't understand what you are trying to do. If you want the SMA's value of the current bar just go SMA(Fast)[0]. If you want the previous bar just go SMA(Fast)[1]. Not following your reason for needing double[].
    Josh P.NinjaTrader Customer Service

    Comment


      #3
      Originally posted by NinjaTrader_Josh View Post
      r2kTrader,

      I really don't understand what you are trying to do. If you want the SMA's value of the current bar just go SMA(Fast)[0]. If you want the previous bar just go SMA(Fast)[1]. Not following your reason for needing double[].
      I'm trying to setup a Array that can hold the values so I can then just access the variable. I don't want to have to type in SMA(Fast)[0] for every instance I compare, rather I just want to say currentBar or whatever.

      Would I need a DataSeries for this?

      Easier to do

      if(testCurrentBar - testLastBar > 0)

      {
      //do something
      }

      Than

      if(SMA(Fast)[0] - SMA(Fast)[1] > 0)

      so this is what I am trying to do. I want my code cleaner and more readable.

      Thanks

      Comment


        #4
        Doing such an operation will be inefficient for the code. If you only want to compare current and last just create two double variables to store those values. Otherwise you would need to make a whole DataSeries which is essentially doubling the resources of just calling SMA directly.
        Josh P.NinjaTrader Customer Service

        Comment


          #5
          Josh,

          That is what I am asking how to do

          Can you spit me a snippet?

          Comment


            #6
            i think what Josh is suggesting is
            currentVal=SMA(Fast)[0];
            prevValue=SMA(Fast)[1];

            bool result =currentVal>prevValue?true:false;

            .
            not wanting to hijack this thread but to extend it.
            would calling the dataseries everytime make it recalculate the data
            for eg, SMA(Fast)[0] calculate the values for alteast <fast> periods and get the current value

            SMA(Fast)[1] calculate the values for alteast <fast> periods and get the prev value

            Comment


              #7
              Junkone,

              Thanks for jumping in.

              This is what I did:
              double poCurrentBar = Oscillator(PoFast, PoSlow, PoSmooth)[1]; //not zero because we calc on bar close for this osc - FirstTickOfBar
              double poOneBarsAgo = Oscillator(PoFast, PoSlow, PoSmooth)[2];
              double poTwoBarsAgo= Oscillator(PoFast, PoSlow, PoSmooth)[3];
              double poThreeBarsAgo= Oscillator(PoFast, PoSlow, PoSmooth)[4];

              But is this efficient? Wouldn't it be better to call it once than 4 times?

              How come it won't accept

              double[] poCurrentBar = Oscillator(PoFast, PoSlow, PoSmooth)[1];

              It says can't convert type double to type double[]

              I'm a student of C#, so any help would be appreciated.

              PS,

              I like the idea of the bool, would be more efficient.
              Last edited by r2kTrader; 05-28-2009, 06:19 AM.

              Comment


                #8
                bump

                Josh,

                Could you look at my code and let me know if I am calling the Oscillator more than is necessary? I wanted to call it once per bar update and then just load the array and reference the array, but it won't let me do a double[] from a double.

                Comment


                  #9
                  junkone,

                  When called from within the same script there already is an instance of the indicator in memory so it doesn't recalculate twice.


                  r2kTrader,
                  double[] is an array while Oscillator(...)[1] is a double. That is why you get the cannot convert message.

                  Try this untested code. Unfortunately I cannot provide more assistance than this. You can try searching google for examples.
                  double[] someVar = new double[4];
                  someVar[1] = Oscillator(..)[1];

                  Calling Oscillator() four times in your code is no more or less efficient than calling it once since it is already in memory.
                  Josh P.NinjaTrader Customer Service

                  Comment


                    #10
                    Originally posted by NinjaTrader_Josh View Post
                    junkone,

                    When called from within the same script there already is an instance of the indicator in memory so it doesn't recalculate twice.


                    r2kTrader,
                    double[] is an array while Oscillator(...)[1] is a double. That is why you get the cannot convert message.

                    Try this untested code. Unfortunately I cannot provide more assistance than this. You can try searching google for examples.
                    double[] someVar = new double[4];
                    someVar[1] = Oscillator(..)[1];

                    Calling Oscillator() four times in your code is no more or less efficient than calling it once since it is already in memory.
                    Then an array would just be redundant. Thank you for this update, very helpful.

                    Comment

                    Latest Posts

                    Collapse

                    Topics Statistics Last Post
                    Started by Rogers101, Today, 11:30 AM
                    0 responses
                    8 views
                    0 likes
                    Last Post Rogers101  
                    Started by MGHORBEL, Today, 11:16 AM
                    0 responses
                    1 view
                    0 likes
                    Last Post MGHORBEL  
                    Started by michi08, Today, 08:51 AM
                    2 responses
                    12 views
                    0 likes
                    Last Post michi08
                    by michi08
                     
                    Started by flybuzz, Today, 10:33 AM
                    0 responses
                    3 views
                    0 likes
                    Last Post flybuzz
                    by flybuzz
                     
                    Started by algospoke, 02-19-2024, 03:25 PM
                    6 responses
                    42 views
                    0 likes
                    Last Post Abiodun
                    by Abiodun
                     
                    Working...
                    X