Capturing indicator values for open order

 

I have an indicator that has values of -1, 0 and 1. I am trying to programatically capture the transition from -1 to 1 which does not happen in a sequential manner.

Here is an example of a valid trigger that I would like to capture to signal an open trade flag

Let say we are looking back over the previous 10 bars closed. The indicator provides the following values

-1, -1, -1, 0, 0, 0, 0, 0, 0, 1

This would trigger a buy flag

The example below is an invalid open trade flag

-1, -1, -1, 0, -1, 1, 0, 1, 1, 1

Thee needs to be a transition from -1 to 0 to 1 for the flag to be valid.

I was thinking of using a var to capture previous low/high values but not sure how to code it

Any assistance would be appreciated

 

mbaker71:

-1, -1, -1, 0, 0, 0, 0, 0, 0, 1

This would trigger a buy flag

Why is that valid, it meets your criteria in 8 bars

mbaker71: The example below is an invalid open trade flag

-1, -1, -1, 0, -1, 1, 0, 1, 1, 1

Thee needs to be a transition from -1 to 0 to 1 for the flag to be valid.

Why invalid? There is a transition from -1 to 0 to +1 as highlighted in three bars.

Until you can state your requirements in concrete terms, it can not be coded.

 
mbaker71:

I have an indicator that has values of -1, 0 and 1. I am trying to programatically capture the transition from -1 to 1 which does not happen in a sequential manner.

Here is an example of a valid trigger that I would like to capture to signal an open trade flag

Let say we are looking back over the previous 10 bars closed. The indicator provides the following values

-1, -1, -1, 0, 0, 0, 0, 0, 0, 1

This would trigger a buy flag

The example below is an invalid open trade flag

-1, -1, -1, 0, -1, 1, 0, 1, 1, 1

Thee needs to be a transition from -1 to 0 to 1 for the flag to be valid.

I was thinking of using a var to capture previous low/high values but not sure how to code it

Any assistance would be appreciated

If I have understood correctly, this is one way to do it.

  double test_buffer[]= {-1, -1, -1, 0, 0, 0, 0, 0, 0, 1}; //valid
  //double test_buffer[]= {-1, -1, -1, 0, -1, 1, 0, 1, 1, 1}; //invalid
  ArraySetAsSeries(test_buffer,true);
  int size=ArraySize(test_buffer);
  bool valid=false;
  if(test_buffer[0]==1.0 && test_buffer[1]==0)
   {
    for(int x=2; x<size; x++)
     {
      if(test_buffer[x]==1.0)
        break;
      if(test_buffer[x]==-1.0)
       {
        valid=true;
        break;
       }
     }
   }
  if(valid)
    Print("Is valid signal");
  else
    Print("Is NOT valid signal");
 
William Roeder #:

Why is that valid, it meets your criteria in 8 bars

Why invalid? There is a transition from -1 to 0 to +1 as highlighted in three bars.

Until you can state your requirements in concrete terms, it can not be coded.

Hi William,

Thanks for your reply. You will notice the first number in the sequence that is in question for the invalid series goes from 1 to 0 to 1, not -1 to 0 to 1. Thats why this will be invalid

 
mbaker71 #: from 1 to 0 to 1, not -1 to 0 to 1. Thats why this will be invalid
  1. Got it.
  2. Keith's idea is the way to go.
  3.   bool isValid(const double& buffer[]){
         int size=ArraySize(buffer); if(size < 3) return false; // -1, 0, +1 not possible.
         if(buffer[0]!=1.0)                       return false; // Not …, +1.
         int x=1; for(; x<size; ++x) if(buffer[x] != 0) break;  // Skip all zeros.
         if(x==1 || x == size)                    return false// No zeros, or all zeros
         return buffer[x]==-1.0;                                // Found -1,  0, … , 0, 1.
       }

 
William Roeder #:
  1. Got it.
  2. Keith's idea is the way to go.

Brilliant. Thank you very much