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

Help with use of Indicator that is applied to A different Series

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

    Help with use of Indicator that is applied to A different Series


    Hello:

    I am trying to create a strategy that uses the ZigZag indicator in a different data series. I did have succes creating a strat that works with no issues when having the indicator utilize the current data series, but when I try to code it so that the ZigZaig indicator uses a different series, I am getting an error. The issue is when the strat attempts to grab the indicators hi/lo bar value an error is generated indicating that there is no object referenced.

    Here is an example of the code. It contains the setup and I point out the line where the error occurs. Any help is greatly appreciated.


    Code:
    private ZigZag zzTest;  //Instantiate the variable zzTest
    private struct zzposclr { public int clr; public int bar; }   //Create a structure type
    
    if (State == State.SetDefaults){
      //BLAH
      //BLAH
    } else if (State == State.Configure) {
        AddDataSeries(BarsPeriodType.Minute, 5); //Add series
        ZigZag(BarsArray[1],DeviationType.Points, 0.5, false); //setup zigzag inputs and use it on series BarsArray[1]
    }
    
    protected override void OnBarUpdate() {
        if(State == State.Historical) return;
        if (CurrentBars[0] < 1) return;
    
        if(BarsInProgress!=1) return;    //Only interested in dealing with series1 (5 Min TF)        
    
        zzT(); //Call Function
    }
    
    
    private int zzT() {
    
        int cnt = 0; // setup counter to 0
    
       /Instantiate peakx[] structured
      //initial size doesn't matter, this will be resized in loop below
       zzposclr[] peakx = new zzposclr[1];  
    
        for (int i = 1; i < 13; i++) // 13 bars is more than enuf.
        {
            //Get the corresponding bar values for any HIbar/LowBar results from ZigZag
           // THE ERROR HAPPENS AT FOLLOWING LINE//
           // COMPILER INDICATE THAT THERE'S NO OBJECT REFERENCED
            int hZZ = zzTest.HighBar(0, i, 100);  //<<===ERROR HERE
            int lZZ = zzTest.LowBar(0, i, 100);
    
            //Populate structured peakx[] elements values.  
            //1st resize peakx[]
            //2ns set Hi/Lo Peak # of Bars to peakx[].bar
            //3rd set Color/Direction of bar  to peakx[].clr  Long/Short (OP_BUY/OP_SELL);                
            if (hZZ != -1) { Array.Resize(ref peakx, cnt+1); peakx[cnt].clr=OP_BUY; peakx[cnt].bar=hZZ; cnt++; }
            if (lZZ != -1) { Array.Resize(ref peakx, cnt+1); peakx[cnt].clr=OP_SELL; peakx[cnt].bar=lZZ; cnt++; }
        }
    
        //Sort the struct array so we get the correct order sequence
       //of bars that represent the LAST FOUR PEAKS/VALLEYS
        Array.Sort<zzposclr>(peakx, (a,b) => a.bar.CompareTo(b.bar));                    
    
        //The result is a struct array that contains the last 4 ZZ peaks/valley  
         //peakx[x].bar  <== the # of bar of peak/valley stored here
        //peakx[x].clr     <== corresponding color/directiom stored here.  OP_BUY=Green  OP_SELL=RED    
        //where x= 0 to 3.  The size of peakx[] is larger but we only need 4 items.          
    }            
    
    ​

    Jess
    Last edited by xmess777; 07-12-2023, 05:14 PM.

    #2
    NEVERMIND!!!!!! My error was not setting the variable zzTest to the ZigZag() indicator setup.

    so
    ZigZag(BarsArray[1],DeviationType.Points, 0.5, false);

    Needs to be:

    zzTest=ZigZag(BarsArray[1],DeviationType.Points, 0.5, false);


    Function now works to the T !!!.


    Hope other can benefit from this code.

    Take care all,
    Jess

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by ageeholdings, Today, 07:43 AM
    0 responses
    5 views
    0 likes
    Last Post ageeholdings  
    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
    12 views
    0 likes
    Last Post burtoninlondon  
    Working...
    X