Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

How to acces previous condition

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

    How to acces previous condition

    Let say I have cross over strategy with SMA5 and SMA20.

    Now the 5 crosses above 20, but how can I acces the previous cross, that datapoint?

    My goal is to compare the last cross with the cross before the last cross.
    So my strategy will remember the last cross point.

    What kind of command do I need to use?

    thanks..!

    #2
    Originally posted by no111 View Post
    Let say I have cross over strategy with SMA5 and SMA20.

    Now the 5 crosses above 20, but how can I acces the previous cross, that datapoint?

    My goal is to compare the last cross with the cross before the last cross.
    So my strategy will remember the last cross point.

    What kind of command do I need to use?

    thanks..!
    How do you mean: "compare the last cross with the cross before the last cross."?
    What kind of data are you going to use to make the comparison?

    Comment


      #3
      Originally posted by koganam View Post
      How do you mean: "compare the last cross with the cross before the last cross."?
      What kind of data are you going to use to make the comparison?
      The data of the SMA and the Close..

      Comment


        #4
        Originally posted by no111 View Post
        The data of the SMA and the Close..
        Easiest way would be to define a struct to hold the data you want to compare and the bar on which it occurs.

        When you get a cross, you copy the existing struct into a holder, load the current values into the struct, and you can compare them then.

        Of course, you could simply record the necessary data into primitive variables that you keep related for each cross.

        Comment


          #5
          So in my strategy I have this for example:

          if (SMA(5)[0] > SMA(200)[0]
          && SMA(5)[0] < SMA(200)[0] )
          {
          double adat = Close[0];
          }

          Would that store the last Close (when there was a cross) into 'adat'?

          Comment


            #6
            Originally posted by no111 View Post
            So in my strategy I have this for example:

            if (SMA(5)[0] > SMA(200)[0]
            && SMA(5)[0] < SMA(200)[0] )
            {
            double adat = Close[0];
            }

            Would that store the last Close (when there was a cross) into 'adat'?
            1. Your if condition will never trigger.
            2. Assuming you corrected that, yes, the last Close will be stored in adat. However, as adat is declared as a local variable, it will not be accessible outside the block in which it was declared, nor held as a persistent value. You would want to declare it as a class variable in the Variables region, and then assign it as you have done.

            Comment


              #7
              Thanks again.
              I adjusted the if, a bit dumb , as you can see I'm not a advanced programmer.

              I decided that I want to create a dataseries so I can access previous crosses.
              My goal is that everytime the sma crosses, store Close[0] into myDataSeries.. but what do I wrong..?

              PHP Code:
                      #region Variables
              
                      private DataSeries myDataSeries;
                      #endregion
              
                      protected override void Initialize()
                      {
                          CalculateOnBarClose = true;
                      myDataSeries = new DataSeries(this);
                      }
              
              
              
              if (SMA(5)[0] > SMA(200)[0] 
              && SMA(5)[1] < SMA(200)[1] )
              {
              myDataSeries.Set(Close[0]);
              } 
              

              Comment


                #8
                Originally posted by no111 View Post
                Thanks again.
                I adjusted the if, a bit dumb , as you can see I'm not a advanced programmer.

                I decided that I want to create a dataseries so I can access previous crosses.
                My goal is that everytime the sma crosses, store Close[0] into myDataSeries.. but what do I wrong..?

                PHP Code:
                        #region Variables
                
                        private DataSeries myDataSeries;
                        #endregion
                
                        protected override void Initialize()
                        {
                            CalculateOnBarClose = true;
                        myDataSeries = new DataSeries(this);
                        }
                
                
                
                if (SMA(5)[0] > SMA(200)[0] 
                && SMA(5)[1] < SMA(200)[1] )
                {
                myDataSeries.Set(Close[0]);
                } 
                
                Not a very efficient method, but yes, you can use a DataSeries. You will still need to know the barNumber on which the cross occurred, so that you can reference it on the next cross, in order to retrieve the value that you stored in the DataSeries. That means that you still need to store that somewhere.

                Comment


                  #9
                  Ok thanks, well I changed it:

                  PHP Code:
                          #region Variables
                          private double datapoint = 0;
                          #endregion
                  protected override void OnBarUpdate(){
                  if (SMA(5)[0] > SMA(200)[0] 
                  && SMA(5)[0] < SMA(200)[0] )
                  {
                  datapoint = Close[0];
                  }    
                  
                  } 
                  
                  But how can I compare the last datapoint with the datapoint before?
                  Can't use [] because it's a double..
                  Last edited by no111; 07-21-2011, 07:09 AM.

                  Comment


                    #10
                    Originally posted by no111 View Post
                    Ok thanks, well I changed it:


                    Code:
                            #region Variables
                    		private double datapoint = 0;
                            #endregion
                    protected override void OnBarUpdate(){
                    if (SMA(5)[0] > SMA(200)[0] 
                    && SMA(5)[0] < SMA(200)[0] )
                    {
                    [COLOR="Red"]/*As at this point datapoint is the last value that you input at the last 
                    crossover. Do whatever comparison that you want here, then update the datapoint, with
                    your next statement below.*/[/COLOR]
                    datapoint = Close[0];
                    }	
                    
                    }
                    But how can I compare the last datapoint with the datapoint before?
                    Can't use [] because it's a double..
                    See red text above, inside your original code.

                    datapoint is a double, so you reference it the same way that you assign it; directly by its name

                    Comment


                      #11
                      Originally posted by koganam View Post
                      See red text above, inside your original code.

                      datapoint is a double, so you reference it the same way that you assign it; directly by its name
                      I understand but how can I access the previous datapoint..
                      Datapoint is only the last cross over, am I right?

                      Comment


                        #12
                        Originally posted by no111 View Post
                        I understand but how can I access the previous datapoint..
                        Datapoint is only the last cross over, am I right?
                        It is the value of the Close at the last crossover, because that is what you coded. I do not understand your question about accessing the previous datapoint. It is a value. You access it by its name.

                        Comment

                        Latest Posts

                        Collapse

                        Topics Statistics Last Post
                        Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                        0 responses
                        647 views
                        0 likes
                        Last Post Geovanny Suaza  
                        Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                        0 responses
                        369 views
                        1 like
                        Last Post Geovanny Suaza  
                        Started by Mindset, 02-09-2026, 11:44 AM
                        0 responses
                        108 views
                        0 likes
                        Last Post Mindset
                        by Mindset
                         
                        Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                        0 responses
                        572 views
                        1 like
                        Last Post Geovanny Suaza  
                        Started by RFrosty, 01-28-2026, 06:49 PM
                        0 responses
                        573 views
                        1 like
                        Last Post RFrosty
                        by RFrosty
                         
                        Working...
                        X